当前位置:网站首页>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
边栏推荐
- Take a cold bath
- Common interview questions ①
- Summary of acl2021 information extraction related papers
- Maixll dock quick start
- 科研狗可能需要的一些工具
- LeetCode_53(最大子数组和)
- Pytorch(四) —— 可视化工具 Visdom
- 【FTP】FTP常用命令,持续更新中……
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- 细数软件研发效能的七宗罪
猜你喜欢

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

Pytorch(二) —— 激活函数、损失函数及其梯度

先有网络模型的使用及修改

【硬十宝典】——1.【基础知识】电源的分类

Dataloader的使用

TCP server communication flow

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

Simple implementation of slf4j

Cmake selecting compilers and setting compiler options
随机推荐
Applications and features of VR online exhibition
Why is Internet thinking not suitable for AI products?
STM32 extended key scan
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
Common UNIX Operation and maintenance commands of shell
Codeforces Round #771 (Div. 2) ABCD|E
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
Day 52 - tree problem
Pytorch(三) —— 函数优化
How to view the changes and opportunities in the construction of smart cities?
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
JS image path conversion Base64 format
Leecode question brushing record 1332 delete palindrome subsequence
神经网络的基本骨架-nn.Moudle的使用
Shell analysis server log command collection
手动实现一个简单的栈
2022 question bank and answers for safety production management personnel of hazardous chemical production units
Section 27 remote access virtual private network workflow and experimental demonstration
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
Overview of the construction details of Meizhou veterinary laboratory