介绍
在不同场景下,图片很可能环境中其他因素干扰,比如噪声
如果在训练数据时引入噪声,模型往往有更好的鲁棒性
数据增强
- 增加一个已有数据集,使得有更多的多样性
- 在语言里面加入各种不同的背景噪音
- 改变图片的颜色和形状
翻转:


1 2 3 4 5 6 7 8
| import torchvision def apply(img, aug, num_rows=2, num_cols=4, scale=1.5): Y = [aug(img) for _ in range(num_rows * num_cols)] d2l.show_images(Y, num_rows, num_cols, scale=scale)
apply(img, torchvision.transforms.RandomVerticalFlip()) apply(img, torchvision.transforms.RandomHorizontalFlip())
|

切割:
- 从图片中切割一块,然后变形到固定形状
- 随机高宽比 (e.g.[3/4,4/3])
- 随机大小(e.g.[8%,100%]
- 随机位

1 2 3 4
| shape_aug = torchvision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
apply(img, shape_aug)
|

颜色
•改变色调,饱和度,明亮度(e.g.[0.5,1.5])

1 2 3 4 5 6 7 8 9 10
| apply(img, torchvision.transforms.ColorJitter(brightness=0.5, contrast=0, saturation=0, hue=0)) apply(img, torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0.5))
color_aug = torchvision.transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug)
|
还有许多其他的方法……
只使用最简单的随机左右翻转
1 2 3 4 5 6
| train_augs = torchvision.transforms.Compose([ torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()])
test_augs = torchvision.transforms.Compose([ torchvision.transforms.ToTensor()])
|
代码
https://courses.d2l.ai/zh-v2/assets/notebooks/chapter_computer-vision/image-augmentation.slides.html
总结
- 数据增广通过变形数据来获取多样性从而使得模型泛化性能更好
- 常见图片增广包括翻转、切割、变色