当前位置:网站首页>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 :size
padding
: 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.Resize
Standardization :transforms.Normalize
To tensor, And normalized to [0-1]:transforms.ToTensor
fill :transforms.Pad
Change the brightness 、 Contrast and saturation :transforms.ColorJitter
Go to grayscale :transforms.Grayscale
linear transformation :transforms.LinearTransformation()
Affine transformation :transforms.RandomAffine
According to probability p Turn to grayscale :transforms.RandomGrayscale
Convert data to PILImage:transforms.ToPILImage
transforms.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 operate
transforms.RandomApply(transforms, p=0.5), Give me a transform Plus the probability , Operate according to probability
transforms.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
边栏推荐
- Shell脚本-if else语句
- 2.4 激活函数
- Embedded Engineer Interview Question 3 Hardware
- Personal decoration notes
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
- VSYNC+三重缓存机制+Choreographer
- 易点易动助力企业设备高效管理,提升设备利用率
- 又到年中,固定资产管理该何去何从?
- Jetson Nano 安装TensorFlow GPU及问题解决
- Nacos - 配置管理
猜你喜欢
【检测技术课案】简易数显电子秤的设计与制作
jeecg 重启报40001
Principle and application of single chip microcomputer timer, serial communication and interrupt system
中小企业固定资产管理办法哪种好?
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
足球篮球体育比赛比分直播平台源码/app开发建设项目
Nacos - Configuration Management
Phishing identification app
【pytorch】softmax函数
Which method is good for the management of fixed assets of small and medium-sized enterprises?
随机推荐
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
毕业季,我想对你说
Nacos - gestion de la configuration
Football and basketball game score live broadcast platform source code /app development and construction project
如何做好固定资产管理?易点易动提供智能化方案
C语言学生信息管理系统
Phishing identification app
nacos簡易實現負載均衡
Redis source code learning (29), compressed list learning, ziplist C (II)
Shell脚本-select in循环
pcl_ Viewer command
An overview of the design of royalties and service fees of mainstream NFT market platforms
Shell脚本-while循环详解
Pain points and solutions of fixed assets management of group companies
Shell script - definition, assignment and deletion of variables
【pytorch】softmax函数
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
VSYNC+三重缓存机制+Choreographer
Bird recognition app