设为首页 收藏本站
查看: 1291|回复: 0

[经验分享] 深度学习中的Data Augmentation方法(转)基于keras

[复制链接]

尚未签到

发表于 2017-6-22 06:27:08 | 显示全部楼层 |阅读模式
  在深度学习中,当数据量不够大时候,常常采用下面4中方法:




1. 人工增加训练集的大小. 通过平移, 翻转, 加噪声等方法从已有数据中创造出一批"新"的数据.也就是Data Augmentation
2. Regularization. 数据量比较小会导致模型过拟合, 使得训练误差很小而测试误差特别大. 通过在Loss Function 后面加上正则项可以抑制过拟合的产生. 缺点是引入了一个需要手动调整的hyper-parameter. 详见 https://www.wikiwand.com/en/Regularization_(mathematics)
3. Dropout. 这也是一种正则化手段. 不过跟以上不同的是它通过随机将部分神经元的输出置零来实现. 详见 http://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf
4. Unsupervised Pre-training. 用Auto-Encoder或者RBM的卷积形式一层一层地做无监督预训练, 最后加上分类层做有监督的Fine-Tuning. 参考 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.207.1102&rep=rep1&type=pdf



下面我们来讨论Data Augmentation:

  不同的任务背景下, 我们可以通过图像的几何变换, 使用以下一种或多种组合数据增强变换来增加输入数据的量. 这里具体的方法都来自数字图像处理的内容, 相关的知识点介绍, 网上都有, 就不一一介绍了.


  • 旋转 | 反射变换(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. 对每幅图像的每个像素Ixy=[IRxy,IGxy,IBxy]T进行加上如下的变化:
  [p1,p2,p3][α1λ1,α2λ2,α3λ3]T
  其中:αi是满足均值为0,方差为0.1的随机变量.

代码实现
  作为实现部分, 这里介绍一下在python 环境下, 利用已有的开源代码库Keras作为实践:



1 # -*- coding: utf-8 -*-
2 __author__ = 'Administrator'
3
4 # import packages
5 from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
6
7 datagen = ImageDataGenerator(
8         rotation_range=0.2,
9         width_shift_range=0.2,
10         height_shift_range=0.2,
11         shear_range=0.2,
12         zoom_range=0.2,
13         horizontal_flip=True,
14         fill_mode='nearest')
15
16 img = load_img('C:\Users\Administrator\Desktop\dataA\lena.jpg')  # this is a PIL image, please replace to your own file path
17 x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
18 x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
19
20 # the .flow() command below generates batches of randomly transformed images
21 # and saves the results to the `preview/` directory
22
23 i = 0
24 for batch in datagen.flow(x,
25                           batch_size=1,
26                           save_to_dir='C:\Users\Administrator\Desktop\dataA\pre',#生成后的图像保存路径
27                           save_prefix='lena',
28                           save_format='jpg'):
29     i += 1
30     if i > 20: #这个20指出要扩增多少个数据
31         break  # otherwise the generator would loop indefinitely
  主要函数:ImageDataGenerator 实现了大多数上文中提到的图像几何变换方法.


  • rotation_range: 旋转范围, 随机旋转(0-180)度;
  • width_shift and height_shift: 随机沿着水平或者垂直方向,以图像的长宽小部分百分比为变化范围进行平移;
  • rescale: 对图像按照指定的尺度因子, 进行放大或缩小, 设置值在0 - 1之间,通常为1 / 255;
  • shear_range: 水平或垂直投影变换, 参考这里 https://keras.io/preprocessing/image/
  • zoom_range: 按比例随机缩放图像尺寸;
  • horizontal_flip: 水平翻转图像;
  • fill_mode: 填充像素, 出现在旋转或平移之后.
  效果如下图所示:
DSC0000.jpg

  转载于:http://blog.csdn.net/mduanfire/article/details/51674098


DSC0001.gif DSC0002.gif


1 # -*- coding: utf-8 -*-
2 __author__ = 'Administrator'
3
4 # import packages
5 from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
6
7 datagen = ImageDataGenerator(
8         rotation_range=0.2,
9         width_shift_range=0.2,
10         height_shift_range=0.2,
11         shear_range=0.2,
12         zoom_range=0.2,
13         horizontal_flip=True,
14         fill_mode='nearest')
15
16 for k in range(33):
17     numstr = "{0:d}".format(k);
18     filename='C:\\Users\\Administrator\\Desktop\\bad\\'+numstr+'.jpg';
19     ufilename = unicode(filename , "utf8")
20     img = load_img(ufilename)  # this is a PIL image, please replace to your own file path
21     x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
22     x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
23
24     # the .flow() command below generates batches of randomly transformed images
25     # and saves the results to the `preview/` directory
26
27     i = 0
28
29     for batch in datagen.flow(x,
30                               batch_size=1,
31                               save_to_dir='C:\\Users\\Administrator\\Desktop\\dataA\\',#生成后的图像保存路径
32                               save_prefix=numstr,
33                               save_format='jpg'):
34         i += 1
35         if i > 20:
36             break  # otherwise the generator would loop indefinitely
37 end
View Code




1 # -*- coding: utf-8 -*-
2 __author__ = 'Administrator'
3
4 # import packages
5 from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
6
7 datagen = ImageDataGenerator(
8         rotation_range=10,
9         width_shift_range=0.2,
10         height_shift_range=0.2,
11         rescale=1./255,
12         shear_range=0.2,
13         zoom_range=0.2,
14         horizontal_flip=True,
15         fill_mode='nearest')
16 import os
17
18 import sys
19 reload(sys)
20 sys.setdefaultencoding('utf8')
21
22 ufilename = unicode("C:\\Users\\Administrator\\Desktop\\测试" , "utf8")
23
24 for filename in os.listdir(ufilename):              #listdir的参数是文件夹的路径
25     print ( filename)                                  #此时的filename是文件夹中文件的名称
26     pathname='C:\\Users\\Administrator\\Desktop\\测试\\'+filename;
27     #ufilename = unicode(pathname , "utf8")
28     img = load_img(pathname)  # this is a PIL image, please replace to your own file path
29     x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
30     x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
31     # the .flow() command below generates batches of randomly transformed images
32     # and saves the results to the `preview/` directory
33     i = 0
34     for batch in datagen.flow(x,
35                               batch_size=1,
36                               save_to_dir='C:\\Users\\Administrator\\Desktop\\result\\',#生成后的图像保存路径
37                               save_prefix=filename,
38                               save_format='jpg'):
39         i += 1
40         if i > 100:
41             break  # otherwise the generator would loop indefinitely
42
43
44 # datagen = ImageDataGenerator(
45 #         rotation_range=0.2,
46 #         width_shift_range=0.2,
47 #         height_shift_range=0.2,
48 #         rescale=1./255,
49 #         shear_range=0.1,
50 #         zoom_range=0.4,
51 #         horizontal_flip=True,
52 #         fill_mode='nearest')
53 #
54 # ufilename = unicode("C:\\Users\\Administrator\\Desktop\\训练" , "utf8")
55 # for filename in os.listdir(ufilename):              #listdir的参数是文件夹的路径
56 #     print ( filename)                                  #此时的filename是文件夹中文件的名称
57 #     pathname='C:\\Users\\Administrator\\Desktop\\训练\\'+filename;
58 #    # ufilename = unicode(pathname , "utf8")
59 #     img = load_img(pathname)  # this is a PIL image, please replace to your own file path
60 #     x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
61 #     x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
62 #
63 #     # the .flow() command below generates batches of randomly transformed images
64 #     # and saves the results to the `preview/` directory
65 #
66 #     i = 0
67 #
68 #     for batch in datagen.flow(x,
69 #                               batch_size=1,
70 #                               save_to_dir='C:\\Users\\Administrator\\Desktop\\result\\',#生成后的图像保存路径
71 #                               save_prefix=filename,
72 #                               save_format='jpg'):
73 #         i += 1
74 #         if i > 100:
75 #             break  # otherwise the generator would loop indefinitely
View Code

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-386595-1-1.html 上篇帖子: 虚拟化技术原理 下篇帖子: xml html xhtml html5
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表