当前位置:网站首页>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
边栏推荐
- R语言观察日志(part24)--初始化设置
- 用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
- Glitch free clock switching technology
- 3. Detailed explanation of Modbus communication protocol
- Ape anthropology topic 20 (the topic will be updated from time to time)
- Shell script case in and regular expressions
- 固定资产管理系统让企业动态掌握资产情况
- Mysql 优化
- Shell脚本-字符串
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
猜你喜欢

Daily practice of C language - day 80: currency change

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

【pytorch】2.4 卷积函数 nn.conv2d

How to solve the problem of fixed assets management and inventory?

如何做好固定资产管理?易点易动提供智能化方案

How to manage fixed assets well? Easy to point and move to provide intelligent solutions

Principle and application of single chip microcomputer timer, serial communication and interrupt system

Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

An overview of the design of royalties and service fees of mainstream NFT market platforms

I use flask to write the website "one"
随机推荐
Promise异步编程
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Daily office consumables management solution
Installing Oracle EE
Mysql8.0 learning record 17 -create table
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
Shell脚本-位置参数(命令行参数)
固定资产管理系统让企业动态掌握资产情况
毕业季,我想对你说
NiO zero copy
安装Oracle EE
DataBinding源码分析
nacos简易实现负载均衡
Why is the Ltd independent station a Web3.0 website!
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
Shell script case in and regular expressions
Shell脚本-echo命令 转义符
美团2022年机试
2.4 激活函数