提交 0b1da100 编写于 作者: T Tom Ren 提交者: Aston Zhang

Add functions for data augmentation (#310)

* add functions for images augmetation in image-augmentation.md

* add function img_aug to generate augmetation images in gluonbook/utils.py

* change a wrong word in neural-style.md
上级 ac2834b2
......@@ -106,6 +106,49 @@ augs = gdata.vision.transforms.Compose([
apply(img, augs)
```
#### 对数据集图片进行增广操作
如果希望对自己的数据进行曾广扩充,可以利用img_aug()函数来直接对某个文件夹下的图片进行随机变换和增强
```{.python .input}
# 先定义一组增广操作,其中的参数可以根据上述讲解来调整
augs = [gdata.vision.transforms.RandomFlipLeftRight(),
gdata.vision.transforms.RandomResizedCrop(
(200, 200), scale=(0.1, 1), ratio=(0.8, 1.25)),
gdata.vision.transforms.RandomBrightness(0.1),
gdata.vision.transforms.RandomHue(0.5),
gdata.vision.transforms.RandomColorJitter(
brightness=0.1, contrast=0.5, saturation=0.5, hue=0.5)
] """augmentation list example"""
```
```{.python .input}
#将增广操作应用于图像上,其中num_aug为每种操作进行的次数
def apply_aug(img, augs, path, name, num_aug=10):
"""Apply augmetaion manipulation on images"""
for aug in augs:
Y = [aug(img) for _ in range(num_aug)]
for i in range(len(Y)):
augged = Y[i].asnumpy()
id_name = name.split('.')[0]
new_name = path+'aug/'+id_name+'0'+str(i)+'.jpg'
cv2.imwrite(new_name,augged)
#定义文件夹增广函数,作用后可以在path/aug/下生成变化后的增广数据
def img_aug(path):
"""Generate augmentation images, your train img store in path/"""
if not os.path.exists(path+'aug/'):
os.mkdir(path+'aug/')
names = os.listdir(path)
for name in names:
if name.endswith('.jpg'):
aug_img = image.imread(path+name)
apply_aug(aug_img, augs,path,name)
```
```{.python .input}
img_aug('./../where/you/store/your/images/')
```
## 使用图片增广来训练
接下来我们来看一个将图片增广应用在实际训练中的例子,并比较其与不使用时的区别。这里我们使用CIFAR-10数据集,而不是之前我们一直使用的Fashion-MNIST。原因在于Fashion-MNIST中物体位置和尺寸都已经归一化了,而CIFAR-10中物体颜色和大小区别更加显著。下面我们展示CIFAR-10中的前32张训练图片。
......
......@@ -93,7 +93,7 @@ def extract_features(x, content_layers, style_layers):
return contents, styles
```
最后我们定义函数分别对内容图片和样式图片抽取对应的特征。因为在训练时我们不修改网络的权重,所以我们可以在训练开始之前提取所要的特征。
最后我们定义函数分别对内容图片和样式图片抽取对应的特征。因为在训练时我们不修改网络的权重,所以我们可以在训练开始之前提取所要的特征。
```{.python .input}
def get_contents(image_shape, ctx):
......
......@@ -10,6 +10,7 @@ import mxnet as mx
from mxnet import autograd, gluon, image, nd
from mxnet.gluon import nn, data as gdata, loss as gloss, utils as gutils
import numpy as np
import cv2
voc_classes = ['background', 'aeroplane', 'bicycle', 'bird', 'boat',
......@@ -565,4 +566,36 @@ class VOCSegDataset(gdata.Dataset):
def __len__(self):
return len(self.data)
def apply_aug(img, augs, path, name, num_aug=10):
"""Apply augmetaion manipulation on images"""
for aug in augs:
Y = [aug(img) for _ in range(num_aug)]
for i in range(len(Y)):
augged = Y[i].asnumpy()
id_name = name.split('.')[0]
new_name = path+'aug/'+id_name+'0'+str(i)+'.jpg'
cv2.imwrite(new_name,augged)
def img_aug(path):
"""Generate augmentation images, your train img store in path/"""
augs = [gdata.vision.transforms.RandomFlipLeftRight(),
gdata.vision.transforms.RandomResizedCrop(
(200, 200), scale=(0.1, 1), ratio=(0.8, 1.25)),
gdata.vision.transforms.RandomBrightness(0.1),
gdata.vision.transforms.RandomHue(0.5),
gdata.vision.transforms.RandomColorJitter(
brightness=0.1, contrast=0.5, saturation=0.5, hue=0.5)
] """augmentation list example"""
if not os.path.exists(path+'aug/'):
os.mkdir(path+'aug/')
names = os.listdir(path)
for name in names:
if name.endswith('.jpg'):
aug_img = image.imread(path+name)
apply_aug(aug_img, augs,path,name)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册