当前位置:网站首页>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()
边栏推荐
- redis分布式锁的原子保证
- 逆向分析实战2
- 解决编译安装gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.“ 问题
- After 5 years of Ali internship interview~
- Problems caused by List getting the difference
- 为什么突然间麒麟 9000 5G 版本,又有库存了?
- 传统项目转型
- Gateway 路由网关
- How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?
- spicy(二)unit hooks
猜你喜欢

How to solve the error "no such file or directory" when EasyCVR starts?

小程序毕设作品之微信二手交易小程序毕业设计成品(4)开题报告

星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!

Nacos集群分区

Pytorch框架学习记录2——TensorBoard的使用

spicy(一)基本定义

小程序毕设作品之微信二手交易小程序毕业设计成品(6)开题答辩PPT

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT

route filter

组织在线化:组织数字化变革的新趋势
随机推荐
After 5 years of Ali internship interview~
Pytorch框架学习记录2——TensorBoard的使用
Operational configuration: How to run multiple EasyCVR programs as a service in one server?
STM32 SPI+WM8978语音回环
Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product
spicy(二)unit hooks
EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?
ospf 导图
组织在线化:组织数字化变革的新趋势
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
Nacos命名空间
小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板
List获取差集产生的问题
spicy (two) unit hooks
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!
小程序毕设作品之微信二手交易小程序毕业设计成品(4)开题报告
forward与redirect的区别
OpenFeign realize load balance
小程序毕设作品之微信二手交易小程序毕业设计成品(6)开题答辩PPT