当前位置:网站首页>2.2 【pytorch】torchvision.transforms
2.2 【pytorch】torchvision.transforms
2022-07-01 09:03:00 【Enzo 想砸电脑】
这里写目录标题
关于 torchvision 和 transforms
(1)torchvision有四个功能模块:model、dataset、transforms、utils
(2)transforms 模块 可对源数据进行预处理、增强
(3)transforms 提供了对PIL Image 对象和 Tensor 对象的常用操作。 可通过PIL直接读取图片, PIL.Image.open(path) ;
官方文档地址: https://pytorch.org/vision/stable/transforms.html
transforms
1、裁剪(Crop)
中心裁剪:transforms.CenterCrop
torchvision.transforms.CenterCrop(size)
参数:size: 所需裁剪的图片尺寸
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()

以上例子我们可知:
(1)如果切正方形,transforms.CenterCrop(100) 和 transforms.CenterCrop((100, 100)),两种写size的方法,效果一样
(2)如果设置的输出的图片尺寸大于原尺寸,会在边上补黑色
随机裁剪:transforms.RandomCrop
# 依据给定的size随机裁剪
torchvision.transforms.RandomCrop(size,
padding = None,
pad_if_needed = False,
fill=0,
padding_mode ='constant')
功能:
从图片中随机裁剪出尺寸为 size 的图片,如果有 padding,那么先进行 padding,再随机裁剪 size 大小的图片。
参数:sizepadding: 设置填充大小
– – 当为 a 时,上下左右均填充 a 个像素
– – 当为 (a, b) 时,左右填充 a 个像素,上下填充 b 个像素
– – 当为 (a, b, c, d) 时,左上右下分别填充 a,b,c,dpad_if_needed:当图片小于设置的 size,是否填padding_mode:
– – constant: 像素值由 fill 设定 (默认)
– – edge: 像素值由图像边缘像素设定
– – reflect: 镜像填充,最后一个像素不镜像。([1,2,3,4] -> [3,2,1,2,3,4,3,2])
– – symmetric: 镜像填充,最后一个像素也镜像。([1,2,3,4] -> [2,1,1,2,3,4,4,4,3])fill:当 padding_mode 为 constant 时,设置填充的像素值 (默认为0)
随机长宽比裁剪:transforms.RandomResizedCrop `
torchvision.transforms.RandomResizedCrop(size,
scale=(0.08, 1.0),
ratio=(0.75, 1.3333333333333333),
interpolation=2)
功能:
随机大小、随机宽高比裁剪图片。首先根据 scale 的比例裁剪原图,然后根据 ratio 的长宽比再裁剪,最后使用插值法把图片变换为 size 大小。
参数:size: 裁剪的图片尺寸scale: 随机缩放面积比例,默认随机选取 (0.08, 1) 之间的一个数ratio: 随机长宽比,默认随机选取 ( 3 4 \displaystyle\frac{3}{4} 43, 4 3 \displaystyle\frac{4}{3} 34 ) 之间的一个数。因为超过这个比例会有明显的失真interpolation: 当裁剪出来的图片小于 size 时,就要使用插值方法 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()

上下左右中心裁剪:transforms.FiveCrop
上下左右中心裁剪后翻转,transforms.TenCrop
2、翻转和旋转(Flip and Rotation)
依概率p水平翻转:transforms.RandomHorizontalFlip(p=0.5)
依概率p垂直翻转:transforms.RandomVerticalFlip(p=0.5)
随机旋转:transforms.RandomRotation
3、图像变换
transforms.Resize标准化:transforms.Normalize转为tensor,并归一化至[0-1]:transforms.ToTensor填充:transforms.Pad修改亮度、对比度和饱和度:transforms.ColorJitter转灰度图:transforms.Grayscale线性变换:transforms.LinearTransformation()仿射变换:transforms.RandomAffine依概率p转为灰度图:transforms.RandomGrayscale将数据转换为PILImage:transforms.ToPILImagetransforms.Lambda:Apply a user-defined lambda as a transform
4、transforms 方法操作
使数据增强更灵活 transforms.RandomChoice(transforms),从给定的一系列transforms中选一个进行操作transforms.RandomApply(transforms, p=0.5),给一个transform加上概率,依概率进行操作transforms.RandomOrder,将transforms中的操作随机打乱
reference:
https://www.cnblogs.com/zhangxiann/p/13570884.html
边栏推荐
- Serialization, listening, custom annotation
- NiO zero copy
- 记一次redis超时
- 【MFC开发(17)】高级列表控件List Control
- Insert mathematical formula in MD document and mathematical formula in typora
- Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
- FreeRTOS learning easy notes
- In the middle of the year, where should fixed asset management go?
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
- Ape anthropology topic 20 (the topic will be updated from time to time)
猜你喜欢

AVL树的理解和实现

MySQL optimization

【MFC开发(17)】高级列表控件List Control

Vsync+ triple cache mechanism +choreographer

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

Nacos - 服务发现

Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting

Advanced C language pointer (Part 2)

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

Nacos - 配置管理
随机推荐
中小企业固定资产管理办法哪种好?
AVL树的理解和实现
How to solve the problem of fixed assets management and inventory?
易点易动助力企业设备高效管理,提升设备利用率
Shell脚本-for循环和for int循环
Pain points and solutions of fixed assets management of group companies
Differences among tasks, threads and processes
Shell script - definition, assignment and deletion of variables
Software Engineer Interview Question brushing website and experience method
Common interview questions for embedded engineers 2-mcu_ STM32
Redis——Lettuce连接redis集群
LogBack
Databinding source code analysis
Shell script -select in loop
Advanced C language pointer (Part 2)
Bird recognition app
NiO zero copy
TV size and viewing distance
Phishing identification app
In the middle of the year, where should fixed asset management go?