当前位置:网站首页>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
边栏推荐
- This sideline workload is small, 10-15k, free unlimited massage
- 2022危险化学品生产单位安全生产管理人员题库及答案
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- Day 52 - tree problem
- Grey correlation cases and codes
- LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
- Dataloader的使用
- LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
- Cmake selecting compilers and setting compiler options
- 技术分享| 融合调度中的广播功能设计
猜你喜欢

I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own

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

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target

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

C - detailed explanation of operators and summary of use cases

pytorch 卷积操作

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

JVM栈和堆简介

VIM easy to use tutorial

神经网络-非线性激活
随机推荐
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
VIM简易使用教程
Why is Internet thinking not suitable for AI products?
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
分布式事务-解决方案
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
技术分享| 融合调度中的广播功能设计
Common interview questions ①
Quelques outils dont les chiens scientifiques pourraient avoir besoin
解决qiankun中子应用外链文件无法获取
Odeint et GPU
STM32 extended key scan
This sideline workload is small, 10-15k, free unlimited massage
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
分布式全局唯一ID解决方案详解
分布式架构系统拆分原则、需求、微服务拆分步骤
Fitness without equipment
Difference between cookie and session
LeetCode_35(搜索插入位置)
软件研发的十大浪费:研发效能的另一面