当前位置:网站首页>常用的Transforms中的方法
常用的Transforms中的方法
2022-07-01 04:35:00 【booze-J】
文章
这里用一些实际代码来举例,Transforms中常用方法的一些使用(ToTensor、Normalize、Resize、Compose、RandomCrop等等)
示例代码如下:
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
writer = SummaryWriter("logs")
img = Image.open(r"D:\pycharm_professional\PycharmProjects\learning_pytorch\images\baby.jpg")
# ToTensor
# 生成类对象
trans_totensor = transforms.ToTensor()
# 往类对象中传入参数并返回结果
img_tensor = trans_totensor(img)
# 将结果添加到事件当中
writer.add_image("ToTenser",img_tensor)
# Normalize
print(img_tensor[0][0][0])
# 为什么是传入[0.5,0.5,0.5],因为其有测试图片有三个通道
# 生成类对象
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
# 往类对象中传入参数并返回结果
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
# 将结果添加到事件当中
writer.add_image("Normalize",img_norm)
# Resize
print(img.size)
# 生成类对象
trans_resize = transforms.Resize((512,512))
# img PIL -> resize -> img_resize PIL
img_resize = trans_resize(img)
print(img_resize) # <PIL.Image.Image image mode=RGB size=512x512 at 0x13A7271BE50>
# resize之后的图像是PIL.Image的数据类型,想要把变化后的图片在tensorboard显示的话,还要将图像转换为tensor的数据类型
# img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize)
# Compose - resize -2
trans_resize_2 = transforms.Resize(512)
# PIL -> PIl -> tensor
trans_compose = transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize",img_resize_2,1)
# RandomCrop
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,trans_totensor])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop,i)
writer.close()
上述代码运行结果在tensorboard可视化:
tips
- 其实可以发现哈,ToTensor、Normalize、Resize、Compose的使用方法都是先生成一个对应的类对象,然后再向类对象中传入图片,返回一个结果。
# 示例:
# 生成类对象
trans_totensor = transforms.ToTensor()
# 往类对象中传入参数并返回结果
img_tensor = trans_totensor(img)
# 将结果添加到事件当中
writer.add_image("ToTenser",img_tensor)
- 需要注意的一点就是,在使用一个方法之前一定要弄清楚,这个方法要求的参数类型是什么!一定要注意一个方法的输入是什么类型,输出是什么类型!
# 示例:
# Resize
print(img.size)
# 生成类对象
trans_resize = transforms.Resize((512,512))
# img PIL -> resize -> img_resize PIL
img_resize = trans_resize(img)
print(img_resize) # <PIL.Image.Image image mode=RGB size=512x512 at 0x13A7271BE50>
# resize之后的图像是PIL.Image的数据类型,想要把变化后的图片在tensorboard显示的话,还要将图像转换为tensor的数据类型
# img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize)
像是Resize就是先将PIL进行尺寸变换,再对尺寸变换后的PIL图像进行ToTensor变换,再进行展示,而不能直接对tensor类型的图像数据进行Resize。
- 记住在pycharm中写代码的时候一定要熟练掌握
ctrl + alt和ctrl + p的使用,应为这两个快捷键可以减少很多的问题 - 还有就是在使用到一个不怎么常用的方法的时候,第一件事你可以想到的就是跳回到源码,看看官方的解释。
总结:
1.关注输入和输出类型
2.多看官方文档
3.不知道返回值的时候:
- print()
- print(type())
- debug
边栏推荐
猜你喜欢

Software testing needs more and more talents. Why do you still not want to take this path?

Knowledge supplement: basic usage of redis based on docker

LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver

Maixll-Dock 使用方法

2022 polymerization process test questions and simulation test

【LeetCode】100. Same tree

Section 27 remote access virtual private network workflow and experimental demonstration

slf4j 简单实现

Threejs opening

I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
随机推荐
JS rotation chart
Pytest automated testing - compare robotframework framework
Why is Internet thinking not suitable for AI products?
Difficulties in the development of knowledge map & the importance of building industry knowledge map
2022年化工自动化控制仪表操作证考试题库及答案
2022 polymerization process test questions and simulation test
Basic exercise of test questions hexadecimal to decimal
Learn Chapter 20 of vue3 (keep alive cache component)
Knowledge supplement: redis' basic data types and corresponding commands
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
2022年聚合工艺考试题及模拟考试
Shell analysis server log command collection
Pytorch(二) —— 激活函数、损失函数及其梯度
Basic usage, principle and details of session
Seven crimes of counting software R & D Efficiency
[recommended algorithm] C interview question of a small factory
JS image path conversion Base64 format
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
2022 gas examination question bank and online simulation examination
数据加载及预处理