当前位置:网站首页>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
边栏推荐
猜你喜欢

Ape anthropology topic 20 (the topic will be updated from time to time)

2.4 激活函数

Principles of Microcomputer - Introduction

【pytorch】softmax函数

jeecg 重启报40001

安装Oracle EE

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据

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

3. Detailed explanation of Modbus communication protocol

Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
随机推荐
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Jetson Nano 安装TensorFlow GPU及问题解决
I use flask to write the website "one"
MySQL optimization
[MFC development (17)] advanced list control list control
Embedded Engineer Interview frequently asked questions
Why is the Ltd independent station a Web3.0 website!
Football and basketball game score live broadcast platform source code /app development and construction project
Bimianhongfu queren()
TV size and viewing distance
Glitch free clock switching technology
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
C语言学生信息管理系统
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: ws2812 light control system based on Alibaba cloud, applet and Arduino
【电赛训练】红外光通信装置 2013年电赛真题
Shell script case in and regular expressions
Record a redis timeout
Set the type of the input tag to number, and remove the up and down arrows
Shell脚本-case in语句