当前位置:网站首页>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
边栏推荐
猜你喜欢

Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记

神经网络-非线性激活

CF1638E. Colorful operations Kodori tree + differential tree array

2022 hoisting machinery command registration examination and hoisting machinery command examination registration

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

数据加载及预处理

JVM栈和堆简介

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试

神经网络的基本骨架-nn.Moudle的使用

Applications and features of VR online exhibition
随机推荐
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
分布式数据库数据一致性的原理、与技术实现方案
分布式-总结列表
科研狗可能需要的一些工具
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
2022年T电梯修理题库及模拟考试
C -- array
神经网络-使用Sequential搭建神经网络
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
Leecode question brushing record 1310 subarray XOR query
Overview of the construction details of Meizhou veterinary laboratory
PgSQL failed to start after installation
STM32 extended key scan
C - detailed explanation of operators and summary of use cases
LeetCode_28(实现 strStr())
Leecode record 1351 negative numbers in statistical ordered matrix
Codeworks round 449 (Div. 1) C. Kodori tree template
分布式全局唯一ID解决方案详解
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
RuntimeError: “max_pool2d“ not implemented for ‘Long‘