当前位置:网站首页>Pytorch framework learning record 3 - the use of Transform
Pytorch framework learning record 3 - the use of Transform
2022-07-30 03:57:00 【Yuzu Roo】
Pytorch框架学习记录3——Transform的使用
1. 导入
from torchvision import transforms
2.ToTensor的用法
作用:将PIL图像或者numpy转换成tensor格式

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
img_path = "C:\\Users\\hp\\PycharmProjects\pythonProject\\Pytorch_Learning\\flower_data\\train\daisy\\5547758_eea9edfd54_n.jpg"
img = Image.open(img_path)
tensor = transforms.ToTensor()
tensor_img = tensor(img)
writer = SummaryWriter("logs")
writer.add_image("tensor_img", tensor_img, 1)
writer.close()

3. Normalize的用法
用均值和标准差归一化张量图像,参数:mean, std;The input image type is tensor,输出类型为tensor
o u t p u t [ c h a n n e l ] = ( i n p u t [ c h a n n e l ] − m e a n [ c h a n n e l ] ) / s t d [ c h a n n e l ] output[channel] = (input[channel] - mean[channel]) / std[channel] output[channel]=(input[channel]−mean[channel])/std[channel]
# Normalize
transform_normal = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
img_normal = transform_normal(img_tensor)
print(type(img_normal))
writer.add_image("Normalize", img_normal, 1)

4. RandomCrop 的用法
随机裁剪,参数sizeIndicates the size of the randomly cropped image,The input image type is PIL类型,The type of the output is alsoPIL类型.
The example below is a random crop of an image10次,The size of the image after each crop is 32*32
# RandomCrop
transform_randomCrop = transforms.RandomCrop((32, 32))
for i in range(0, 10):
img_randomCrop = transform_randomCrop(img_PIL)
# print(type(img_randomCrop))
img_randomCrop = transform_tensor(img_randomCrop)
writer.add_image("RandomCrop", img_randomCrop, i)

5. Resize的用法
将输入的PILCrop the image to the specified size,输入参数为size(int类型或者turple类型),输出为PIL图像
# Resize
transform_resize = transforms.Resize((128, 128))
img_resize = transform_resize(img_PIL)
img_resize = transform_tensor(img_resize)
writer.add_image("Resize", img_resize, 1)

6. Compose的用法
This method brings all the methods together,按照顺序依次执行,用法示例如下:
# Compose
transform_compose = transforms.Compose([transform_resize, transform_tensor])
img_resize_2 = transform_compose(img_PIL)
writer.add_image("Compose", img_resize_2, 1)
writer.close()
7. 所有代码
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
from torchvision.transforms import transforms
img_path = "C:\\Users\\hp\\PycharmProjects\pythonProject\\Pytorch_Learning\\flower_data\\train\daisy\\5547758_eea9edfd54_n.jpg"
writer = SummaryWriter("logs")
img_PIL = Image.open(img_path)
# ToTensor
transform_tensor = transforms.ToTensor()
img_tensor = transform_tensor(img_PIL)
# Normalize
transform_normal = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
img_normal = transform_normal(img_tensor)
print(type(img_normal))
writer.add_image("Normalize", img_normal, 1)
# RandomCrop
transform_randomCrop = transforms.RandomCrop((32, 32))
for i in range(0, 10):
img_randomCrop = transform_randomCrop(img_PIL)
# print(type(img_randomCrop))
img_randomCrop = transform_tensor(img_randomCrop)
writer.add_image("RandomCrop", img_randomCrop, i)
# Resize
transform_resize = transforms.Resize((128, 128))
img_resize = transform_resize(img_PIL)
img_resize = transform_tensor(img_resize)
writer.add_image("Resize", img_resize, 1)
# Compose
transform_compose = transforms.Compose([transform_resize, transform_tensor])
img_resize_2 = transform_compose(img_PIL)
writer.add_image("Compose", img_resize_2, 1)
writer.close()
边栏推荐
- Problems caused by List getting the difference
- Smart answer function, CRMEB knowledge payment system must have!
- 逆向分析实战2
- Anti-shake and throttling
- cv2.polylines
- (redistribute, special comprehensive experiment ospf area)
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
- MySQ死锁
- day10--在Linux上安装mysql
- EasyCVR启动时报错“no such file or directory”,该如何解决?
猜你喜欢

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report

Nacos命名空间

Summary of Rpc and gRpc Introduction

小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要

函数的底层机制

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions

逆向分析实战2

WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template

星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
发给你的好友,让 TA 请你吃炸鸡!
随机推荐
RRU、BBU、AAU
spicy(一)基本定义
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
OpenFeign实现负载均衡
OpenFeign realize load balance
小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
JIT vs AOT
sublime text 3 settings
Rpc 和 gRpc 简介汇总
小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
Nacos service registration and discovery
操作配置:如何在一台服务器中以服务方式运行多个EasyCVR程序?
How to solve the error "no such file or directory" when EasyCVR starts?
Nacos cluster partition
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?
Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product
Resampling a uniformly sampled signal
What is the difference between mission, vision and values?