当前位置:网站首页>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
边栏推荐
- Redis——Lettuce连接redis集群
- C语言学生信息管理系统
- Shell script - array definition and getting array elements
- The meaning of yolov5 training visualization index
- 3D打印Arduino 四轴飞行器
- Log4j 日志框架
- Shell script case in statement
- Understand shallow replication and deep replication through code examples
- Dynamic proxy
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
猜你喜欢
随机推荐
It is designed with high bandwidth, which is almost processed into an open circuit?
如何解决固定资产管理和盘点的难题?
Serialization, listening, custom annotation
Nacos - Configuration Management
An overview of the design of royalties and service fees of mainstream NFT market platforms
猿人学第20题(题目会不定时更新)
足球篮球体育比赛比分直播平台源码/app开发建设项目
安装Oracle EE
Nacos - 配置管理
集团公司固定资产管理的痛点和解决方案
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
Shell脚本-read命令:读取从键盘输入的数据
【MFC开发(17)】高级列表控件List Control
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
动态代理
C语言学生信息管理系统
How can enterprises and developers take the lead in the outbreak of cloud native landing?
3. Detailed explanation of Modbus communication protocol
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error


![[MFC development (16)] tree control](/img/b9/1de4330c0bd186cfe062b02478c058.png)






