数据增强通常是随机批量生产的,一般使用组合形式,即同时随机裁剪、翻转、旋转等。
import torchvision.transforms as transforms
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('cat.jpg')
img.show()
augment = transforms.Compose(
[
transforms.Resize((150)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomCrop(112),
transforms.ColorJitter(brightness=1, contrast=1, hue=0.5, saturation=0.5),
transforms.RandomRotation(15)
]
)
for i in range(1,10):
plt.subplot(3,3,i)
plt.imshow(augment(img))
plt.show()
输出为: