当前位置:网站首页>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
边栏推荐
- nacos簡易實現負載均衡
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
- It technology ebook collection
- DataBinding源码分析
- Leetcode daily question brushing record --540 A single element in an ordered array
- Jeecg restart alarm 40001
- 2.4 激活函数
- Shell script echo command escape character
- pcl_ Viewer command
- It is designed with high bandwidth, which is almost processed into an open circuit?
猜你喜欢

Which method is good for the management of fixed assets of small and medium-sized enterprises?

Personal decoration notes

Principles of Microcomputer - Introduction

Pain points and solutions of fixed assets management of group companies

【电赛训练】红外光通信装置 2013年电赛真题

Football and basketball game score live broadcast platform source code /app development and construction project

Nacos - service discovery

Why is the Ltd independent station a Web3.0 website!

Imitation of Baidu search results top navigation bar effect

VSYNC+三重缓存机制+Choreographer
随机推荐
易点易动助力企业设备高效管理,提升设备利用率
【电赛训练】红外光通信装置 2013年电赛真题
记一次redis超时
How to effectively align team cognition
Flink面试题
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Installing Oracle EE
It technology ebook collection
Key points of NFT supervision and overseas policies
Serialization, listening, custom annotation
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
Personal decoration notes
Input标签的type设置为number,去掉上下箭头
How to solve the problem of fixed assets management and inventory?
Shell script case in statement
3. Detailed explanation of Modbus communication protocol
Shell script echo command escape character
It is designed with high bandwidth, which is almost processed into an open circuit?
【pytorch学习】torch.device
【pytorch】nn.AdaptiveMaxPool2d