数据增强

  在图像的深度学习中,为了丰富图像训练集,更好的提取图像特征,泛化模型(防止模型过拟合),一般都会对数据图像进行数据增强,数据增强,常用的方式,就是旋转图像,剪切图像,改变图像色差,扭曲图像特征,改变图像尺寸大小,增强图像噪音(一般使用高斯噪音)等,但需要注意,不要加入其它图像轮廓的噪音。在不同的任务背景下,我们可以通过图像的几何变换,使用一下一种或者多种组合数据增强变换来增加输入数据的量。
  1. 旋转|反射变换(Rotation/reflection):随机旋转图像一定角度;改变图像的内容朝向;
  2. 翻转变换(flip):沿这水平或者垂直方向翻转图像
  3. 缩放变换(zoom):按照一定的比例放大或者缩小图像
  4. 平移变换(shift):在图像平面上对图像以一定方式进行平移

数据增强的代码实现

  1. # -*- coding:utf-8 -*-
  2. # 数据增强
  3. # 1.翻转变换flip
  4. # 2.随机修剪random crop
  5. # 3.色彩抖动color jittering
  6. # 4.平移变换shift
  7. # 5.尺度变换scale
  8. # 6.对比度变换contrast
  9. # 7.噪声扰动noise
  10. # 8.旋转变换/反射变换 Rotation/reflection
  11.  
  12. from PIL import Image,ImageEnhance,ImageOps,ImageFile
  13. import numpy as np
  14. import random
  15. import threading,os,time
  16. import logging
  17.  
  18. logger = logging.getLogger(__name__)
  19. ImageFile.LOAD_TRUNCATED_IMAGES = True
  20.  
  21. class DataAugmentation:
  22. #包含数据增强的八种方式
  23. def __init__(self):
  24. pass
  25.  
  26. @staticmethod
  27. def openImage(image):
  28. return Image.open(image,mode="r")
  29.  
  30. @staticmethod
  31. def randomRotation(image,mode=Image.BICUBIC):
  32. # 对图像进行任意0~360度旋转
  33. # param mode 邻近插值,双线性插值,双三次B样条插值(default)
  34. # param image PIL的图像image
  35. # return 旋转之后的图像
  36. random_angle = np.random.randint(1,360)
  37. return image.rotate(random_angle,mode)
  38.  
  39. @staticmethod
  40. def randomCrop(image):
  41. #对图像随意剪切,考虑到图像大小范围(68*68),使用一个一个大于(36*36)的窗口进行截图
  42. #param image:PIL的图像image
  43. #return:剪切之后的图像
  44. image_width = image.size[0]
  45. image_height = image.size[1]
  46. crop_win_size = np.random.randint(40,68)
  47. random_region = ((image_width - crop_win_size ) >> 1 , (image_height - crop_win_size) >> 1 ,(image_width + crop_win_size) >> 1 , (image_height + crop_win_size) >> 1)
  48. return image.crop(random_region)
  49.  
  50. @staticmethod
  51. def randomColor(image):
  52. #对图像进行颜色抖动
  53. #param image:PIL的图像image
  54. #return:有颜色色差的图像image
  55.  
  56. #随机因子
  57. random_factor = np.random.randint(0, 31) / 10.
  58. #调整图像的饱和度
  59. color_image = ImageEnhance.Color(image).enhance(random_factor)
  60. #随机因子
  61. random_factor = np.random.randint(10,21) / 10.
  62. #调整图像的亮度
  63. brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)
  64. #随机因子
  65. random_factor = np.random.randint(10,21) / 10.
  66. #调整图像的对比度
  67. contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)
  68. #随机因子
  69. random_factor = np.random.randint(0,31) / 10.
  70. #调整图像锐度
  71. sharpness_image = ImageEnhance.Sharpness(contrast_image).enhance(random_factor)
  72. return sharpness_image
  73.  
  74. @staticmethod
  75. def randomGaussian(image,mean=0.2,sigma=0.3):
  76. #对图像进行高斯噪声处理
  77. #param image:
  78. #return
  79.  
  80. def gaussianNoisy(im,mean=0.2,sigma=0.3):
  81. #对图像做高斯噪音处理
  82. # param im:单通道图像
  83. # param mean:偏移量
  84. # param sigma:标准差
  85. #return:
  86. for _i in range(len(im)):
  87. im[_i] += random.gauss(mean,sigma)
  88. return im
  89.  
  90. #将图像转化为数组
  91. img = np.asanyarray(image)
  92. #将数组改为读写模式
  93. img.flags.writeable = True
  94. width,height = img.shape[:2]
  95. #对image的R,G,B三个通道进行分别处理
  96. img_r = gaussianNoisy(img[:,:,0].flatten(), mean, sigma)
  97. img_g = gaussianNoisy(img[:,:,1].flatten(), mean, sigma)
  98. img_b = gaussianNoisy(img[:,:,2].flatten(), mean, sigma)
  99. img[:,:,0] = img_r.reshape([width,height])
  100. img[:,:,1] = img_g.reshape([width,height])
  101. img[:,:,2] = img_b.reshape([width,height])
  102. return Image.fromarray(np.uint8(img))
  103.  
  104. @staticmethod
  105. def saveImage(image,path):
  106. image.save(path)
  107.  
  108. def makeDir(path):
  109. try:
  110. if not os.path.exists(path):
  111. if not os.path.isfile(path):
  112. os.makdirs(path)
  113. return 0
  114. else:
  115. return 1
  116. except Exception, e:
  117. print str(e)
  118. return -1
  119.  
  120. def imageOps(func_name, image, des_path, file_name, times = 5):
  121. funcMap = {"randomRotation": DataAugmentation.randomRotation,
  122. "randomCrop":DataAugmentation.randomCrop,
  123. "randomColor":DataAugmentation.randomColor,
  124. "randomGaussian":DataAugmentation.randomGaussian
  125. }
  126. if funcMap.get(func_name) is None:
  127. logger.error("%s is not exist" , func_name)
  128. return -1
  129.  
  130. for _i in range(0,times,1):
  131. new_image = funcMap[func_name](image)
  132. DataAugmentation.saveImage(new_image,os.path.join(des_path,func_name + str(_i) + file_name))
  133.  
  134. opsList = {"randomRotation", "randomCrop", "randomColor", "randomGaussian"}
  135.  
  136. def threadOPS(path,new_path):
  137. #多线程处理事务
  138. #param src_path:资源文件
  139. #param des_path:目的地文件
  140. #return:
  141.  
  142. if os.path.isdir(path):
  143. img_names = os.listdir(path)
  144. else:
  145. img_names = [path]
  146. for img_name in img_names:
  147. print img_name
  148. tmp_img_name = os.path.join(path,img_name)
  149. print tmp_img_name
  150. if os.path.isdir(tmp_img_name):
  151. if makeDir(os.path.join(new_path,img_name)) != -1:
  152. threadOPS(tmp_img_name,os.path.join(new_path,img_name))
  153. else:
  154. print 'create new dir failure'
  155. return -1
  156. elif tmp_img_name.split('.')[1] != "DS_Store":
  157. image = DataAugmentation.openImage(tmp_img_name)
  158. threadImage = [0] * 5
  159. _index = 0
  160. for ops_name in opsList:
  161. threadImage[_index] = threading.Thread(target=imageOps,args=(ops_name,image,new_path,img_name))
  162. threadImage[_index].start()
  163. _index += 1
  164. time.sleep(0.2)
  165.  
  166. if __name__ == '__main__':
  167. threadOPS("C:\Users\Acheron\PycharmProjects\CNN\pic-image\\train\images","C:\Users\Acheron\PycharmProjects\CNN\pic-image\\train\\newimages")

数据增强实验

原始的待进行数据增强的图像:

1.对图像进行颜色抖动

2.对图像进行高斯噪声处理

Deep Learning -- 数据增强的更多相关文章

  1. [基础]Deep Learning的基础概念

    目录 DNN CNN DNN VS CNN Example 卷积的好处why convolution? DCNN 卷积核移动的步长 stride 激活函数 active function 通道 cha ...

  2. Generalizing from a Few Examples: A Survey on Few-Shot Learning 小样本学习最新综述 | 三大数据增强方法

    目录 原文链接:小样本学习与智能前沿 01 Transforming Samples from Dtrain 02 Transforming Samples from a Weakly Labeled ...

  3. Deep Learning 16:用自编码器对数据进行降维_读论文“Reducing the Dimensionality of Data with Neural Networks”的笔记

    前言 论文“Reducing the Dimensionality of Data with Neural Networks”是深度学习鼻祖hinton于2006年发表于<SCIENCE > ...

  4. Deep Learning 11_深度学习UFLDL教程:数据预处理(斯坦福大学深度学习教程)

    理论知识:UFLDL数据预处理和http://www.cnblogs.com/tornadomeet/archive/2013/04/20/3033149.html 数据预处理是深度学习中非常重要的一 ...

  5. Deep learning:三十四(用NN实现数据的降维)

    数据降维的重要性就不必说了,而用NN(神经网络)来对数据进行大量的降维是从2006开始的,这起源于2006年science上的一篇文章:reducing the dimensionality of d ...

  6. 收藏:左路Deep Learning+右路Knowledge Graph,谷歌引爆大数据

    发表于2013-01-18 11:35| 8827次阅读| 来源sina微博 条评论| 作者邓侃 数据分析智能算法机器学习大数据Google 摘要:文章来自邓侃的博客.数据革命迫在眉睫. 各大公司重兵 ...

  7. #Deep Learning回顾#之LeNet、AlexNet、GoogLeNet、VGG、ResNet

    CNN的发展史 上一篇回顾讲的是2006年Hinton他们的Science Paper,当时提到,2006年虽然Deep Learning的概念被提出来了,但是学术界的大家还是表示不服.当时有流传的段 ...

  8. Deep Learning(深度学习)学习笔记整理

    申明:本文非笔者原创,原文转载自:http://www.sigvc.org/bbs/thread-2187-1-3.html 4.2.初级(浅层)特征表示 既然像素级的特征表示方法没有作用,那怎样的表 ...

  9. 【转载】Deep Learning(深度学习)学习笔记整理

    http://blog.csdn.net/zouxy09/article/details/8775360 一.概述 Artificial Intelligence,也就是人工智能,就像长生不老和星际漫 ...

随机推荐

  1. 使用 OpCache 提升 PHP 性能

    使用 OpCache 提升 PHP 性能 wdd2007wdd2007 .6k 1月29日 发布 推荐 推荐 收藏 收藏,.3k 浏览 OpCache 通过对 opcode 的缓存和优化来提升 PHP ...

  2. poj2392 Space Elevator(多重背包问题)

    Space Elevator   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8569   Accepted: 4052 ...

  3. InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!

    问题描述: centos 安装MySQL $yum install mysql-server 安装之后执行命令mysql 报错: 查看mysql的启动日志: [ERROR] InnoDB: auto- ...

  4. Create a new Docker Machine with the Hyper-V driver

    docker-machine就是docker工具集中提供的用来管理容器化主机的工具,用来管理运行在不同环境的主机,包括:本地虚拟机,远程虚拟机,公有云中的虚拟机都可以通过一个命令统一进行管理. 01. ...

  5. 转:Python的这几个技巧,简直屌爆了

    经使用Python编程有多年了,即使今天我仍然惊奇于这种语言所能让代码表现出的整洁和对DRY编程原则的适用.这些年来的经历让我学到了很多的小技巧和知识,大多数是通过阅读很流行的开源软件,如Django ...

  6. Mysql8.0.16 only_full_group_by

    [1]Mysql8.0.16 关于only_full_group_by问题 应公司业务的需求,安装了Mysql8.0.16版本,原来在Mysql5.6版本执行无恙的SQL语句: SELECT prod ...

  7. jQuery学习笔记1——操作属性

    一.获得和设置内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容, 得到匹配元素集合中每个元素的文本内容结合,包括他们的后代, 即由所有匹配 ...

  8. Java深入理解文章(转载)

    引用自:http://droidyue.com/ninki/ JVM运行时的数据区 http://droidyue.com/blog/2014/12/21/java-runtime-data-area ...

  9. CSS样式设置

    转载来自:http://www.imooc.com/article/2067水平居中设置-行内元素 水平居中 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align: ...

  10. jQuery ajax 动态append创建表格出现不兼容ie8

    非常多情况下.通过js(jQuery1.10)动态来创建一些样式,对页面的动态交互来说是非常方便的 可是不同的浏览器针对动态生成的不是非常兼容,在此遇见的不兼容ie8.跟各位分享下 代码: json数 ...