当前位置:网站首页>2.2 【pytorch】torchvision. transforms
2.2 【pytorch】torchvision. transforms
2022-07-01 09:08:00 【Enzo tried to smash the computer】
Here's the catalog title
About torchvision and transforms
(1)torchvision There are four functional modules :model、dataset、transforms、utils
(2)transforms modular The source data can be preprocessed 、 enhance
(3)transforms Provide for the right to PIL Image Objects and Tensor Common operations of objects . It can be done by PIL Read pictures directly , PIL.Image.open(path) ;
Official document address : https://pytorch.org/vision/stable/transforms.html
transforms
1、 tailoring (Crop)
Center cut :transforms.CenterCrop
torchvision.transforms.CenterCrop(size)
Parameters :size: The size of the image to be cropped
from PIL import Image
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
img_src = Image.open('./bird.jpg')
img_1 = transforms.CenterCrop(200)(img_src)
img_2 = transforms.CenterCrop((200, 200))(img_src)
img_3 = transforms.CenterCrop((300, 200))(img_src)
img_4 = transforms.CenterCrop((500, 500))(img_src)
plt.subplot(231)
plt.imshow(img_src)
plt.subplot(232)
plt.imshow(img_1)
plt.subplot(233)
plt.imshow(img_2)
plt.subplot(234)
plt.imshow(img_3)
plt.subplot(235)
plt.imshow(img_4)
plt.show()

We can see from the above examples :
(1) If you cut a square ,transforms.CenterCrop(100) and transforms.CenterCrop((100, 100)), Two kinds of writing size Methods , The effect is the same
(2) If the set output picture size is larger than the original size , Will fill the edges with black
Random cutting :transforms.RandomCrop
# According to the given size Random cutting
torchvision.transforms.RandomCrop(size,
padding = None,
pad_if_needed = False,
fill=0,
padding_mode ='constant')
function :
Randomly cut out the size of... From the picture size Pictures of the , If there is padding, Then go ahead padding, Then cut randomly size Size picture .
Parameters :sizepadding: Set fill size
– – When it comes to a when , Fill up, down, left and right a Pixel
– – When it comes to (a, b) when , Fill left and right a Pixel , Fill it up and down b Pixel
– – When it comes to (a, b, c, d) when , Fill the upper left and the lower right respectively a,b,c,dpad_if_needed: When the picture is smaller than the set size, Whether to fill in padding_mode:
– – constant: The pixel value is determined by fill Set up ( Default )
– – edge: The pixel value is set by the image edge pixels
– – reflect: Mirror fill , The last pixel is not mirrored .([1,2,3,4] -> [3,2,1,2,3,4,3,2])
– – symmetric: Mirror fill , The last pixel is also mirrored .([1,2,3,4] -> [2,1,1,2,3,4,4,4,3])fill: When padding_mode by constant when , Sets the pixel value of the fill ( The default is 0)
Random aspect ratio tailoring :transforms.RandomResizedCrop `
torchvision.transforms.RandomResizedCrop(size,
scale=(0.08, 1.0),
ratio=(0.75, 1.3333333333333333),
interpolation=2)
function :
The random size 、 Crop pictures with random aspect ratio . First of all, according to the scale Cut the original drawing to the scale of , And then according to ratio And then cut the length to width ratio , Finally, the image is transformed into size size .
Parameters :size: Cropped image size scale: Randomly scale the area , Default random selection (0.08, 1) A number between ratio: Random aspect ratio , Default random selection ( 3 4 \displaystyle\frac{3}{4} 43, 4 3 \displaystyle\frac{4}{3} 34 ) A number between . Because beyond this ratio, there will be obvious distortion interpolation: When the cropped image is smaller than size when , We need to use interpolation method resize
– – PIL.Image.NEAREST
– – PIL.Image.BILINEAR
– – PIL.Image.BICUBIC
from PIL import Image
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
img_src = Image.open('./bird.jpg')
img_6 = transforms.RandomResizedCrop((200, 300), scale=(1, 1))(img_src)
img_7 = transforms.RandomResizedCrop((200, 300), scale=(0.5, 0.5))(img_src)
img_8 = transforms.RandomResizedCrop((200, 300), scale=(1, 1), ratio=(0.5, 0.5))(img_src)
img_9 = transforms.RandomResizedCrop((400, 600), scale=(1, 1), ratio=(0.5, 0.5))(img_src)
plt.subplot(231)
plt.imshow(img_src)
plt.subplot(232)
plt.imshow(img_6)
plt.subplot(233)
plt.imshow(img_7)
plt.subplot(234)
plt.imshow(img_8)
plt.subplot(235)
plt.imshow(img_9)
plt.show()

Top, bottom, left and right center cut :transforms.FiveCrop
Cut the top, bottom, left and right center, and then flip ,transforms.TenCrop
2、 Flip and rotate (Flip and Rotation)
According to probability p Flip horizontal :transforms.RandomHorizontalFlip(p=0.5)
According to probability p Flip vertically :transforms.RandomVerticalFlip(p=0.5)
Random rotation :transforms.RandomRotation
3、 Image transformation
transforms.ResizeStandardization :transforms.NormalizeTo tensor, And normalized to [0-1]:transforms.ToTensorfill :transforms.PadChange the brightness 、 Contrast and saturation :transforms.ColorJitterGo to grayscale :transforms.Grayscalelinear transformation :transforms.LinearTransformation()Affine transformation :transforms.RandomAffineAccording to probability p Turn to grayscale :transforms.RandomGrayscaleConvert data to PILImage:transforms.ToPILImagetransforms.Lambda:Apply a user-defined lambda as a transform
4、transforms Method of operation
Make data enhancement more flexible transforms.RandomChoice(transforms), From a given series of transforms Choose one of them to operatetransforms.RandomApply(transforms, p=0.5), Give me a transform Plus the probability , Operate according to probabilitytransforms.RandomOrder, take transforms The operations in are randomly disrupted
reference:
https://www.cnblogs.com/zhangxiann/p/13570884.html
Official document address : https://pytorch.org/vision/stable/transforms.html
边栏推荐
- JCL 和 SLF4J
- 【pytorch】2.4 卷积函数 nn.conv2d
- 动态代理
- Pain points and solutions of equipment management in large factories
- Dynamic proxy
- Jetson nano installs tensorflow GPU and problem solving
- Shell脚本-case in语句
- 【pytorch】nn.AdaptiveMaxPool2d
- R language observation log (part24) -- initialization settings
- Daily practice of C language - day 80: currency change
猜你喜欢

Jetson nano installs tensorflow GPU and problem solving

Principles of Microcomputer - Introduction

jeecg 重启报40001

Bimianhongfu queren()
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

Understanding and implementation of AVL tree
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

3D printing Arduino four axis aircraft

【pytorch】softmax函数

nacos簡易實現負載均衡
随机推荐
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
Understand shallow replication and deep replication through code examples
Shell script -read command: read data entered from the keyboard
R language observation log (part24) -- initialization settings
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
It is designed with high bandwidth, which is almost processed into an open circuit?
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
猿人学第20题(题目会不定时更新)
Nacos - 配置管理
【pytorch学习】torch.device
Shell脚本-echo命令 转义符
nacos簡易實現負載均衡
3D打印Arduino 四轴飞行器
Principle and application of single chip microcomputer timer, serial communication and interrupt system
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
美团2022年机试
Record a redis timeout
Embedded Engineer Interview Question 3 Hardware