作者:AbsentM
前言在深度学习中,为了避免出现过拟合(Overfitting),通常我们需要输入充足的数据量.为了得到更加充足的数据,我们通常需要对原有的图像数据进行几何变换,改变图像像素的位置并保证特征不变.
本页面主要记录下常用的数据增强(Data Augmentation)变换方法及其实现.

数据增强变换(Data Augmentation Transformation)
不同的任务背景下, 我们可以通过图像的几何变换, 使用以下一种或多种组合数据增强变换来增加输入数据的量. 这里具体的方法都来自数字图像处理的内容, 相关的知识点介绍, 网上都有, 就不一一介绍了.
旋转 | 反射变换(Rotation/reflection): 随机旋转图像一定角度; 改变图像内容的朝向; 翻转变换(flip): 沿着水平或者垂直方向翻转图像; 缩放变换(zoom): 按照一定的比例放大或者缩小图像; 平移变换(shift): 在图像平面上对图像以一定方式进行平移;可以采用随机或人为定义的方式指定平移范围和平移步长, 沿水平或竖直方向进行平移. 改变图像内容的位置; 尺度变换(scale): 对图像按照指定的尺度因子, 进行放大或缩小; 或者参照SIFT特征提取思想, 利用指定的尺度因子对图像滤波构造尺度空间. 改变图像内容的大小或模糊程度; 对比度变换(contrast): 在图像的HSV颜色空间,改变饱和度S和V亮度分量,保持色调H不变. 对每个像素的S和V分量进行指数运算(指数因子在0.25到4之间), 增加光照变化; 噪声扰动(noise): 对图像的每个像素RGB进行随机扰动, 常用的噪声模式是椒盐噪声和高斯噪声; 颜色变换(color): 在训练集像素值的RGB颜色空间进行PCA, 得到RGB空间的3个主方向向量,3个特征值, p1, p2, p3, λ1, λ2, λ3. 对每幅图像的每个像素$${I_x} y = {[I^R {xy}, I^G {xy}, I^B {xy}]}^T$$进行加上如下的变化: [ p 1 , p 2 , p 3 ] [ α 1 λ 1 , α 2 λ 2 , α 3 λ 3 ] T [p1,p2,p3][α1λ1,α2λ2,α3λ3]T
其 中 : α i 是 满 足 均 值 为 0 , 方 差 为 0.1 的 随 机 变 量 . 其中:αi是满足均值为0,方差为0.1的随机变量.
注意: 几何变换不改变像素值, 而是改变像素所在的位置. 通过Data Augmentation方法扩张了数据集的范围, 作为输入时, 以期待网络学习到更多的图像不变性特征.
代码实现作为实现部分, 这里介绍一下在python 环境下, 利用已有的开源代码库 Keras 作为实践:
Keras: Deep Learning library for Theano and TensorFlow
Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.
环境搭建<1> 实现环境: ubuntu 14.04, python;
<2> Keras 依赖库: 参照这里
python环境搭建: 参见博客地址 numpy, scipy: sudo pip install numpy 和 sudo pip install scipy pyyaml: sudo pip install pyyaml HDF5 and h5py(可选, 如果你希望将模型保存为hdf5格式):sudo apt-get install libhdf5-serial-dev sudo pip install h5py 或者 sudo apt-get install python-h5py cuDNN(可选, 如果你使用CNNS) Theano(作为后端, 从实现本文的主要内容看, 推荐安装这个): Theano安装方法请参考这里 TensorFlow(作为后端, 可以了解一下): TensorFlow安装方法请参考这里
<3> 安装 Keras
sudo pip install keras 编程实现<1> 先上代码:
#!/usr/bin/env python #-*- coding: utf-8 -*- # import packages fromkeras.preprocessing.imageimportImageDataGenerator, array_to_img, img_to_array, load_img datagen = ImageDataGenerator( rotation_range=0.2, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode=‘nearest’) img = load_img(‘~/Desktop/lena.jpg’)# this is a PIL image, please replace to your own file path x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150) x = x.reshape((1,) + x.shape)# this is a Numpy array with shape (1, 3, 150, 150) # the .flow() command below generates batches of randomly transformed images # and saves the results to the preview/directory i = 0 forbatchindatagen.flow(x, batch_size=1, save_to_dir=‘~/Desktop/preview’, save_prefix=‘lena’, save_format=‘jpg’): i += 1 ifi >20: break# otherwise the generator would loop indefinitely注意: 修改自己的存放图像的路径!
看看我的效果图:

<2> 参数讲解: 文档来自这里
主要函数: ImageDataGenerator 实现了大多数上文中提到的图像几何变换方法.
rotation_range: 旋转范围, 随机旋转(0-180)度; width_shift and height_shift: 随机沿着水平或者垂直方向,以图像的长宽小部分百分比为变化范围进行平移; rescale: 对图像按照指定的尺度因子, 进行放大或缩小, 设置值在0- 1之间,通常为1 / 255; shear_range: 水平或垂直投影变换, 参考这里 zoom_range: 按比例随机缩放图像尺寸; horizontal_flip: 水平翻转图像; fill_mode: 填充像素, 出现在旋转或平移之后.参考资料 参考资料 [1]. http://www.docin.com/p-751118909.html[2]. http://imbinwang.github.io/blog/data-augmentation-in-deep-learning
[3]. http://keras.io/preprocessing/image/
[4]. https://en.wikipedia.org/wiki/Shear_mapping
[5]. https://github.com/fchollet/keras
[6]. http://keras.io/
[7]. http://www.360doc.com/content/16/0314/19/31629080_542186577.shtml