【笔记】pytorch中transforms使用详解(一)

笔者学习pytorch时遇到transforms函数对数据进行预处理,参考官方文档和大佬的讲解,自己进行总结并标注以作记录。
觉得有用请点个赞哦哈哈哈哈

参考自《PyTorch 模型训练实用教程》,获取全文pdf请点击:https://github.com/tensor-yu/PyTorch_Tutorial

还是按照大佬的思路,将其分为4大类:
裁剪——Crop
中心裁剪:transforms.CenterCrop
随机裁剪:transforms.RandomCrop
随机长宽比裁剪:transforms.RandomResizedCrop
上下左右中心裁剪:transforms.FiveCrop
上下左右中心裁剪后翻转:transforms.TenCrop
翻转和旋转——Flip and Rotation
依概率p水平翻转:transforms.RandomHorizontalFlip(p=0.5) 依概率p垂直翻转:transforms.RandomVerticalFlip(p=0.5)
随机旋转:transforms.RandomRotation
图像变换
resize: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.

对transforms操作,使数据增强更灵活 transforms.RandomChoice(transforms):从给定的一系列transforms中选一个进行操作 transforms.RandomApply(transforms, p=0.5):给一个transform加上概率,依概率进行操作 transforms.RandomOrder:将transforms中的操作随机打乱

以下是详解:

一、 裁剪——Crop

1.随机裁剪:transforms.RandomCrop
transforms.RandomCrop(size,padding=None,pad_if_needed=False,fill=0,padding_mode=‘constant’)

参数:

  • size-(sequence or int):若sequence,则为(h,w);若为int,则(size,size) 。

  • padding-(sequence or int, optional):此参数是设置填充多少个pixel。当为int时,图像上下左右均填充int个,例如padding=4,则上下左右均填充4个pixel,若为3232,则会变成4040。当为sequence时,若有2个数,则第一个数表示左右扩充多少,第二个数表示上下的。当有4个数时,则为左,上,右,下。

  • fill- (int or tuple): 填充的值为常量,可以是int or
    tuple。int时,各通道均填充该值,当长度为3的tuple时,表示RGB通道需要填充的值

  • padding_mode:填充模式,有constant、edge、reflect、symmetric4种。

2.中心裁剪:transforms.CenterCrop
class torchvision.transforms.CenterCrop(size)
参数:

  • size- (sequence or int):若为sequence,则为(h,w),若为int,则(size,size)

3.随机长宽比裁剪 transforms.RandomResizedCrop
torchvision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75,1.3333333333333333),interpolation=2)
功能:随机大小,随机长宽比裁剪原始图片,最后将图片resize到设定好的size
参数

  • size- 输出的分辨率

  • scale- 随机crop的大小区间,如scale=(0.08, 1.0),表示随机crop出来的图片会在的0.08倍至1倍之间。

  • ratio- 随机长宽比设置 interpolation- 插值的方法,默认为双线性插值(PIL.Image.BILINEAR)

4.上下左右中心裁剪:transforms.FiveCrop
class torchvision.transforms.FiveCrop(size)
功能:对图片进行上下左右以及中心裁剪,获得5张图片,返回一个4D-tensor
参数:

  • size- (sequence or int),若为sequence,则为(h,w),若为int,则(size,size)

5.上下左右中心裁剪后翻转: transforms.TenCrop
class torchvision.transforms.TenCrop(size,vertical_flip=False)
功能:对图片进行上下左右以及中心裁剪,然后全部翻转(水平或者垂直),获得10张图片,返回一个4D-tensor。
参数:

  • size- (sequence or int),若为sequence,则为(h,w),若为int,则(size,size)
  • vertical_flip (bool) - 是否垂直翻转,默认为flase,即默认为水平翻转
二、翻转和旋转——Flip and Rotation

6.依概率p水平翻转transforms.RandomHorizontalFlip
class torchvision.transforms.RandomHorizontalFlip(p=0.5)
功能:依据概率p对PIL图片进行水平翻转
参数: p- 概率,默认值为0.5

7.依概率p垂直翻转transforms.RandomVerticalFlip
class torchvision.transforms.RandomVerticalFlip(p=0.5)
功能:依据概率p对PIL图片进行垂直翻转
参数: p- 概率,默认值为0.5

8.随机旋转:transforms.RandomRotation
class torchvision.transforms.RandomRotation(degrees, resample=False, expand=False, center=None)
功能:依degrees随机旋转一定角度
参数

  • degress- (sequence or float or int) ,若为单个数,如 30,则表示在(-30,+30)之间随机旋转若为sequence,如(30,60),则表示在30-60度之间随机旋转

  • resample- 重采样方法选择,可选 PIL.Image.NEAREST, PIL.Image.BILINEAR,
    PIL.Image.BICUBIC,默认为最近邻

  • expand-布尔值: If true, expands the output to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation(有点不懂,所以直接上原文,欢迎大佬留言解释)

  • center- 可选为中心旋转还是左上角旋转

转自:https://blog.csdn.net/u011995719/article/details/85107009

emmm写不动了,先这么多,剩下两个有时间再来记。之后会把链接放这里。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值