赞
踩
固定形状?卷积神经网络通常是一个固定的输入。
作用好不好是不一定的。很多时候觉得它有用是因为觉得测试集里会有图片跟这个效果是差不多的。
所以如何选择数据增强?可以从后往前推,想一下部署和测试集里面的图片与训练集的图片会有什么变化,从而确定需要什么样的数据增强方式。
多样性更强,使得泛化性更好。增广的目的是想让训练集更像测试集,如果完全一样就更好了!:)
- import torch
- import torchvision
- from torch import nn
- from d2l import torch as d2l
- import matplotlib.pyplot as plt
-
-
- d2l.set_figsize()
- img = d2l.Image.open('./img/cat1.jpg')
- d2l.plt.imshow(img);
-
-
- # aug:图片增广的方式
- 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)
- plt.show()
-
-
- # 左右翻转图像
- # apply(img, torchvision.transforms.RandomHorizontalFlip())
-
- # 上下翻转图像
- # apply(img, torchvision.transforms.RandomVerticalFlip())
-
- # 随机裁剪
- shape_aug = torchvision.transforms.RandomResizedCrop(
- (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
-
- # 随机更改图像的亮度
- '''
- 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)
-
- # 结合多种图像增广方法
- augs = torchvision.transforms.Compose([
- torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])
- apply(img, augs)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。