当前位置:网站首页>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 大小的图片。
参数:size
padding
: 设置填充大小
– – 当为 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.ToPILImage
transforms.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
边栏推荐
- Shell脚本-read命令:读取从键盘输入的数据
- Jetson nano installs tensorflow GPU and problem solving
- 个人装修笔记
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- Software Engineer Interview Question brushing website and experience method
- FAQ | FAQ for building applications for large screen devices
- Shell脚本-字符串
- Nacos - 服务发现
- Foundation: 3 Opencv getting started images and videos
- pcl_viewer命令
猜你喜欢
Only in China! Alicloud container service enters the Forrester leader quadrant
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
Principles of Microcomputer - internal and external structure of microprocessor
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
Nacos - 服务发现
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Nacos - 配置管理
Ape anthropology topic 20 (the topic will be updated from time to time)
Foundation: 2 The essence of image
Do you know how data is stored? (C integer and floating point)
随机推荐
Jetson Nano 安装TensorFlow GPU及问题解决
DataBinding源码分析
Redis源码学习(29),压缩列表学习,ziplist.c(二)
I use flask to write the website "one"
Promise异步编程
Databinding source code analysis
Glitch free clock switching technology
大型工厂设备管理痛点和解决方案
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Flink interview questions
Computer tips
Understanding and implementation of AVL tree
Foundation: 2 The essence of image
Personal decoration notes
Common interview questions for embedded engineers 2-mcu_ STM32
The meaning of yolov5 training visualization index
Advanced C language pointer (Part 2)
Yolov3, 4, 5 and 6 Summary of target detection
固定资产管理系统让企业动态掌握资产情况
足球篮球体育比赛比分直播平台源码/app开发建设项目