torchvision.transforms : 常用的图像预处理方法
torchvision.datasets : 常用数据集的dataset实现,MNIST,CIFAR-10,ImageNet等
torchvision.model : 常用的模型预训练,AlexNet,VGG, ResNet,GoogLeNet等
2、常用的图像预处理方法数据中心化、数据标准化、缩放、裁剪、旋转、翻转、填充、噪声添加、灰度变换、线性变换、仿射变换、亮度、饱和度及对比度变换。同时transforms所处的位置为(在函数getitem中):
功能:逐channel的对图像进行标准化、output = (input - mean) / std、 mean:各通道的均值、std:各通道的标准差、inplace:是否原地操作。图像标准化是将数据通过去均值实现中心化的处理,根据凸优化理论与数据概率分布相关知识,数据中心化符合数据分布规律,更容易取得训练之后的泛化效果, 数据标准化是数据预处理的常见方法之一。(这里的均值是像素均值)
transforms.Normalize(mean,std,inplace=False)
注:标准化的原理是我们默认自然图像是一类平稳的数据分布(即数据每一维的统计都服从相同分布),此时,在每个样本上减去数据的统计平均值可以移除共同的部分,凸显像素个体差异。 个人认为还可以去除图像的亮度信息,增加模型的泛化能力。
2、裁剪1)transforms.CenterCrop,功能:中心裁剪,size:所需尺寸大小
2)transforms.RandomCrop,功能:从图片中随机裁剪。
transforms.RandomCrop(size,padding=None,pad_if_needed=False,fill=0,padding_mode='constant')
size:所需裁剪图片尺寸
padding:设置填充大小,当为a时,上下左右均填充a个像素,当为(a, b)时,上下填充b个像素,左右填充a个像素,当为(a, b, c, d)时,左,上,右,下分别填充a, b, c, d。
pad_if_need:若图像小于设定size,则填充。
padding_mode:填充模式,有4种模式。a:constant:像素值由fill设定;edge:像素值由图像边缘像素决定;reflect:镜像填充,最后一个像素不镜像,eg:[1,2,3,4] → [3,2,1,2,3,4,3,2]
fill:constant时,设置填充的像素值
3)transforms.RandomCrop,功能:随机大小、长宽比裁剪图片
RandomResizedCrop(size,scale=(0.08, 1.0),ratio=(3/4, 4/3),interpolation)
size:所需裁剪图片尺寸
scale:随机裁剪面积比例, 默认(0.08, 1)
ratio:随机长宽比,默认(3/4, 4/3)
interpolation:插值方法:PIL.Image.NEAREST,PIL.Image.BILINEAR,PIL.Image.BICUBIC
4)FiveCrop,功能:在图像的上下左右以及中心裁剪出尺寸为size的5张图片
transforms.TenCrop(size,vertical_flip=False)
5)TenCrop,功能:功能:在图像的上下左右以及中心裁剪出尺寸为size的5张图片,在对对这5张图片进行水平或垂直镜像获得10张图片
transforms.TenCrop(size,vertical_flip=False)
size:所需裁剪图片尺寸
vertical_flip:是否垂直翻转
1)RandomHorizontalFlip,RandomVerticalFlip,功能:依概率水平(左右)或垂直(上下)
RandomHorizontalFlip(p=0.5) #p:翻转概率
RandomVerticalFlip(p=0.5)
2)RandomRotation,功能:随机旋转图片
RandomRotation(degrees,resample=False,expand=False,center=None)
degrees:旋转角度当为a时,在(-a,a)之间选择旋转角度,当为(a, b)时,在(a, b)之间选择旋转角度
resample:重采样方法
expand:是否扩大图片,以保持原图
center:旋转点设置,默认中心旋转
1) Pad,功能:对图片边缘进行填充。
transforms.Pad(padding,fill=0,padding_mode='constant')
padding:设置填充大小,当为a时,上下左右均填充a个像素,当为(a, b)时,上下填充b个像素,左右填充a个像素,当为(a, b, c, d)时,左,上,右,下分别填充a, b, c, d。
padding_mode:填充模式,有4种模式,constant、edge、reflect和symmetric。
fill:constant时,设置填充的像素值,(R, G, B) or (Gray)
2)ColorJitter,功能:调整亮度、对比度、饱和度和色相
transforms.ColorJitter(brightness=0,contrast=0,saturation=0,hue=0)
brightness:亮度调整因子,当为a时,从[max(0, 1-a), 1+a]中随机选择,当为(a, b)时,从[a, b]中
contrast:对比度参数,同brightness
saturation:饱和度参数,同brightness
hue:色相参数,当为a时,从[-a, a]中选择参数,注: 0<= a <= 0.5,当为(a, b)时,从[a, b]中选择参数,注:-0.5 <= a <= b <= 0.5
3)Grayscale,功能:将图片转换为灰度图
Grayscale(num_output_channels)
4)RandomGrayscale,功能:依概率将图片转换为灰度图
RandomGrayscale(num_output_channels,p=0.1)
num_ouput_channels:输出通道数,只能设1或3
p:概率值,图像被转换为灰度图的概率
5)RandomAffine,功能:进行仿射变换,仿射变换是二维的线性变换,由五种基本原子变换构成,有旋转、平移、缩放、错切和翻转
RandomAffine(degrees,translate=None,scale=None,shear=None,resample=False,fillcolor=0)
degrees:旋转角度设置
translate:平移区间设置,如(a, b), a设置宽(width),b设置高(height),图像在宽维度平移的区间为 -img_width * a < dx < img_width * a
scale:缩放比例(以面积为单位)
fill_color:填充颜色设置
6)RandomErasing,功能:对图像进行随机遮挡
p:概率值,执行该操作的概率
scale:遮挡区域的面积
ratio:遮挡区域长宽比
value:设置遮挡区域的像素值,(R, G, B) or (Gray)
7)transforms.Lambda,功能:用户自定义lambda方法
transforms.Lambda(lambd)
lambd:lambda匿名函数,ambda [arg1 [,arg2, … , argn]] : expression
transforms.TenCrop(200, vertical_flip=True),
transforms.Lambda(lambda crops: torch.stack([transforms.Totensor()(crop) for crop in crops]))
5、transforms的操作1)transforms.RandomChoice,功能:从一系列transforms方法中随机挑选一个
transforms.RandomChoice([transforms1, transforms2, transforms3])
2)transforms.RandomApply,功能:依据概率执行一组transforms操作
transforms.RandomApply([transforms1, transforms2, transforms3], p=0.5)
3)transforms.RandomOrder,功能:对一组transforms操作打乱顺序
transforms.RandomOrder([transforms1, transforms2, transforms3]) 6、自定义transforms
1)基本要素:仅接收一个参数,返回一个参数;注意上下游的输出与输入;通过类实现多参数传入
class Compose(object):
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img
class YourTransforms(object):
def __init__(self, ...):
...
def __call__(self, img):
...
return img
2)举例(自定义实现椒盐噪声)
class Compose(object):
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img
class AddPepperNoise(object):
def __init__(self, snr, p):
self.snr = snr
self.p = p
def __call__(self, img):
```
添加椒盐噪声具体实现过程
```
return img
1、tensor.sub_:在操作中带下划线的都是直接对原始数据进行更改的操作。
2、图像处理技术应用(注意的是训练集应与测试集保持一致)
空间位置:平移
色彩:灰度图,色彩抖动
形状:仿射变换
上下文场景:遮挡,填充
相关知识
使用PyTorch实现对花朵的分类
图像分类:AlexNet网络、五分类 flower 数据集、pytorch
基于pytorch搭建神经网络的花朵种类识别(深度学习)
pytorch深度学习框架——实现病虫害图像分类
基于pytorch搭建AlexNet神经网络用于花类识别
Pytorch神经网络【手写数字识别】
Pytorch实现102类鲜花分类(102 Category Flower Dataset)
AlexNet pytorch代码注释
【免费】基于pytorch的深度学习花朵种类识别项目完整教程(内涵完整文件和代码)
pytorch实现迁移训练
网址: pytorch使用——(五)transforms详解 https://m.huajiangbk.com/newsview1074159.html
上一篇: Android10 androi |
下一篇: 输出n个格子需要的麦粒数 |