transforms.RandomCrop(200, padding=16)
transforms.RandomResizedCrop(size=200, scale=(0.1, 0.9))
(4)FiveCrop裁剪,在图片的左上角、左下角、右上角、右下角和中心位置截取出五幅图片
transforms.FiveCrop(112),
transforms.Lambda(lambda crops: torch.stack([(transforms.ToTensor()(crop)) for crop in crops])),
(5)TenCrop裁剪,在FiveCrop的基础上,在水平或者垂直方向上翻转复制5幅图片
transforms.TenCrop(112, vertical_flip=True),
transforms.Lambda(lambda crops: torch.stack([(transforms.ToTensor()(crop)) for crop in crops])),
transforms.RandomHorizontalFlip(p=1) #以概率1水平翻转图片
transforms.RandomVerticalFlip(p=0.8)
transforms.RandomRotation(90, center=(0, 0)) #以(0, 0)为中心随机旋转(-90, 90)
transforms.ColorJitter(brightness=0.5)
transforms.Grayscale(num_output_channels=3)
class AddPepperNoise(object):
'''
功能:增加椒盐噪声
snr:信噪比Signal Noise Rate
p:概率值
'''
def __init__(self, snr, p=0.9):
# 断言用于判断一个表达式,在表达式条件为 false 的时候触发异常。
assert isinstance(snr, float) or (isinstance(p, float))
self.snr = snr
self.p = p
def __call__(self, img):
if random.uniform(0, 1) < self.p:
img_ = np.array(img).copy()
h, w, c = img_.shape
signal_pct = self.snr
noise_pct = (1 - self.snr) # 噪声信号的比例
mask = np.random.choice((0, 1, 2), size=(h, w, 1), p=[signal_pct, noise_pct*0.8, noise_pct*0.2])
mask = np.repeat(mask, c, axis=2)
img_[mask == 1] = 255 # 盐噪声
img_[mask == 2] = 0 # 椒噪声
return Image.fromarray(img_.astype('uint8')).convert('RGB')
else:
return img