当前位置:网站首页>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
边栏推荐
- Common interview questions for embedded engineers 2-mcu_ STM32
- 美团2022年机试
- 易点易动助力企业设备高效管理,提升设备利用率
- Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
- How to manage fixed assets efficiently in one stop?
- Shell script -select in loop
- Shell脚本-位置参数(命令行参数)
- Dynamic proxy
- 中小企业固定资产管理办法哪种好?
- Advanced C language pointer (Part 2)
猜你喜欢

MySQL optimization

Dynamic proxy

An overview of the design of royalties and service fees of mainstream NFT market platforms

Principles of Microcomputer - Introduction

Advanced level of C language pointer (Part 1)

Pain points and solutions of equipment management in large factories

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

Redis——Lettuce连接redis集群

Personal decoration notes

NiO zero copy
随机推荐
jeecg 重启报40001
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
类加载
I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
Graduation season, I want to tell you
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Principles of Microcomputer - Introduction
【MFC开发(17)】高级列表控件List Control
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
How to manage fixed assets efficiently in one stop?
Naoqi robot summary 28
Shell脚本-数组定义以及获取数组元素
Nacos - 配置管理
Glitch free clock switching technology
FreeRTOS learning easy notes
易点易动助力企业设备高效管理,提升设备利用率
Shell脚本-while循环详解
嵌入式工程师面试题3-硬件
软件工程师面试刷题网站、经验方法
Shell script case in statement