当前位置:网站首页>Common methods in transforms
Common methods in transforms
2022-07-01 04:44:00 【booze-J】
article
Here are some examples of actual code ,Transforms Some uses of common methods in (ToTensor、Normalize、Resize、Compose、RandomCrop wait )
The sample code is as follows :
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
# Generate class objects
trans_totensor = transforms.ToTensor()
# Pass parameters into the class object and return results
img_tensor = trans_totensor(img)
# Add results to events
writer.add_image("ToTenser",img_tensor)
# Normalize
print(img_tensor[0][0][0])
# Why is it incoming [0.5,0.5,0.5], Because it has three channels for test pictures
# Generate class objects
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
# Pass parameters into the class object and return results
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
# Add results to events
writer.add_image("Normalize",img_norm)
# Resize
print(img.size)
# Generate class objects
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 The next image is PIL.Image Data type of , Want to put the changed picture in tensorboard If it shows , Also convert the image to tensor Data type of
# 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()
The above code results in tensorboard visualization :
tips
- In fact, you can find that ,ToTensor、Normalize、Resize、Compose The method of using is to create a corresponding class object , Then pass the picture into the class object , Return a result .
# Example :
# Generate class objects
trans_totensor = transforms.ToTensor()
# Pass parameters into the class object and return results
img_tensor = trans_totensor(img)
# Add results to events
writer.add_image("ToTenser",img_tensor)
- One thing to note is that , Be sure to find out before using a method , What parameter types are required for this method ! It is important to note what type of input a method has , What type of output !
# Example :
# Resize
print(img.size)
# Generate class objects
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 The next image is PIL.Image Data type of , Want to put the changed picture in tensorboard If it shows , Also convert the image to tensor Data type of
# img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize)
Like Resize It's about putting PIL Carry out size transformation , After the size change PIL Image processing ToTensor Transformation , Show it again , Not directly to tensor Type of image data Resize.
- Remember that in pycharm When writing code in, you must master
ctrl + altandctrl + pUse , These two shortcuts can reduce a lot of problems - There is also the use of a less commonly used method , The first thing you can think of is to jump back to the source code , Look at the official explanation .
summary :
1. Focus on input and output types
2. Read more official documents
3. When you don't know the return value :
- print()
- print(type())
- debug
边栏推荐
- Difference between cookie and session
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- 2022年上海市安全员C证考试题模拟考试题库及答案
- Shell之分析服务器日志命令集锦
- Summary of acl2021 information extraction related papers
- Software testing needs more and more talents. Why do you still not want to take this path?
- Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
- 手动实现一个简单的栈
- pytorch 卷积操作
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
猜你喜欢

Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions

The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears

2022年上海市安全员C证考试题模拟考试题库及答案

The design points of voice dialogue system and the importance of multi round dialogue

Annual inventory review of Alibaba cloud's observable practices in 2021

Research on medical knowledge atlas question answering system (I)

VIM简易使用教程

2022危险化学品生产单位安全生产管理人员题库及答案

数据加载及预处理

C - detailed explanation of operators and summary of use cases
随机推荐
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
分布式架构系统拆分原则、需求、微服务拆分步骤
LeetCode_28(实现 strStr())
The index is invalid
Maixll dock quick start
Pytorch(二) —— 激活函数、损失函数及其梯度
分布式-总结列表
js解决浮点数相乘精度丢失问题
Pytorch(一) —— 基本语法
2022年上海市安全员C证考试题模拟考试题库及答案
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
About the transmission pipeline of stage in spark
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.2 multiple registration protocol (MRP)
无器械健身
STM32扩展板 温度传感器和温湿度传感器的使用
Simple implementation of slf4j
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
STM32 extended key scan