数据增强
数据增强又称为数据增广,数据扩增,它是对训练集进行变换,使训练集更丰富,从而让模型更具泛化能力。
如我们高考之前做的五年高考,三年模拟
一 、 transforms. -- crop
1. transforms.CenterCrop(size)
transforms.CenterCrop(196)
transforms.CenterCrop(512)
2. transforms.RandomCrop(size,padding=None,pad_if_needed=False,fill=0,padding_mode="constant")
transforms.RandomCrop(224, padding=16)
transforms.RandomCrop(224, padding=(16, 64))
transforms.RandomCrop(224, padding=16, fill=(255, 0, 0))
transforms.RandomCrop(512, pad_if_needed=True)
transforms.RandomCrop(224, padding=64, padding_mode='edge')
transforms.RandomCrop(224, padding=64, padding_mode='reflect')
transforms.RandomCrop(1024, padding=1024, padding_mode='symmetric')
3.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)
transforms.RandomResizedCrop(size=224, scale=(0.5, 0.5))
4. transforms.FiveCrop(size)
功能:在图像的左上角、右上角、左下角、右下角以及中心进行裁剪5张图片
5. transform.TenCrop(size,vertical_flip=False)
transforms.FiveCrop(112), #TypeError: pic should be PIL Image or ndarray. Got
transforms.Lambda(lambda crops: torch.stack([(transforms.ToTensor()(crop)) for crop in crops]))
transforms.TenCrop(112, vertical_flip=True),
transforms.Lambda(lambda crops: torch.stack([(transforms.ToTensor()(crop)) for crop in crops])),
翻转、旋转
1.RandomHorizontalFlip(p=0.5)
2.RandomVerticalFlip(p=0.5)
transforms.RandomHorizontalFlip(p=1)
transforms.RandomVerticalFlip(p=1)
3. RandomRotation(degrees,resample=False,expand=False,center=None)
transforms.RandomRotation(90)
transforms.RandomRotation((90), expand=True)
transforms.RandomRotation(30, center=(0, 0))
transforms.RandomRotation(30, center=(0, 0), expand=True)
图像变换
1.transforms.Pad(padding,fill=0,padding_mode="constant")
transforms.Pad(padding=32, fill=(255, 0, 0), padding_mode='constant')
transforms.Pad(padding=(8, 64), fill=(255, 0, 0), padding_mode='constant')
transforms.Pad(padding=(8, 16, 32, 64), fill=(255, 0, 0), padding_mode='constant')
transforms.Pad(padding=(8, 16, 32, 64), fill=(255, 0, 0), padding_mode='symmetric')
2. transforms.ColorJitter(brightness=0,contrast=0,saturation=0,hue=0)
transforms.ColorJitter(brightness=0.5)
transforms.ColorJitter(contrast=0.5)
transforms.ColorJitter(saturation=0.5)
transforms.ColorJitter(hue=0.3)
3. transforms.Grayscale(num_output_channels)
4.transforms.RandomGrayscale(num_output_channels,p=0.1)
transforms.Grayscale(num_output_channels=3)
5. transforms.RandomAffine(degrees,translate=None,scale=None,shear=None,resample=False,fillcolor=0)
transforms.RandomAffine(degrees=30)
transforms.RandomAffine(degrees=0, translate=(0.2, 0.2), fillcolor=(255, 0, 0)),
transforms.RandomAffine(degrees=0, scale=(0.7, 0.7))
transforms.RandomAffine(degrees=0, shear=(0, 0, 0, 45))
transforms.RandomAffine(degrees=0, shear=90, fillcolor=(255, 0, 0))
6. transforms.RandomErasing(p=0.5,scale=(0.02,0.33),ratio=(0.3,0.3),value=0,inplace=False)
transforms.ToTensor(),
transforms.RandomErasing(p=1, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=(254/255, 0, 0)),
transforms.ToTensor(),
# transforms.RandomErasing(p=1, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=(254/255, 0, 0)),
transforms.RandomErasing(p=1, scale=(0.02, 0.33), ratio=(0.3, 3.3), value='1234'),
7. transforms.Lambda(lambda)
transforms.Lambda(lambda crops:torch.stack([transforms.Totensor()(crop) for crop in crops]))
transforms.TenCrop(200,vertical_filp=True)
transfroms的选择操作
1.transforms.RandomChoice([transforms1,transforms2,transforms3])
功能:从一系列transforms方法中随机挑选一个
2. transforms.RandomApply([transforms1,transforms2,transforms3],p=0.5)
功能:依据概率执行一组transforms操作
3. transforms.RandomOrder([transforms1,transforms2,transforms3])
功能:对一组transforms操作打乱顺序
transforms.RandomChoice([transforms.RandomVerticalFlip(p=1), transforms.RandomHorizontalFlip(p=1)])
transforms.RandomApply([transforms.RandomAffine(degrees=0, shear=45, fillcolor=(255, 0, 0)),
transforms.Grayscale(num_output_channels=3)], p=0.5),
transforms.RandomOrder([transforms.RandomRotation(15),
transforms.Pad(padding=32),
transforms.RandomAffine(degrees=0, translate=(0.01, 0.1), scale=(0.9, 1.1))]),
自定义transforms方法
自定义transforms要素:
class Compose(object):
def __call__(self,img):
for t in self.transfroms:
img = t(img)
return img
通过类实现多参数输入(下面结构是自定义transfrom是基本结构):
class YourTransfroms(object):
def __init__(self,...):
...
def __call__(self,img):
...
return img
椒盐噪声
椒盐噪声又称为脉冲噪声,是一种随机出现的白点或者黑点,白点称为盐噪声,黑点称为椒噪声
信噪比(signal-noice rate,SNR)是衡量噪声的比例,图像中为图像像素的占比
class AddPepperNoice(object):
def __init__(self,snr,p):
self.snr = snr
self.p = p
def __call__(self,img):
"""
添加椒盐噪声具体实现过程
"""
return img
class Compose(object):
def __call__(self,img):
for t in self.transforms:
img = t(img)
return img
数据增强实战
原则:让训练集与测试更接近