数据增强

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

数据增强的代码实现

# -*- coding:utf-8 -*-
# 数据增强
# 1.翻转变换flip
# 2.随机修剪random crop
# 3.色彩抖动color jittering
# 4.平移变换shift
# 5.尺度变换scale
# 6.对比度变换contrast
# 7.噪声扰动noise
# 8.旋转变换/反射变换 Rotation/reflection from PIL import Image,ImageEnhance,ImageOps,ImageFile
import numpy as np
import random
import threading,os,time
import logging logger = logging.getLogger(__name__)
ImageFile.LOAD_TRUNCATED_IMAGES = True class DataAugmentation:
#包含数据增强的八种方式
def __init__(self):
pass @staticmethod
def openImage(image):
return Image.open(image,mode="r") @staticmethod
def randomRotation(image,mode=Image.BICUBIC):
# 对图像进行任意0~360度旋转
# param mode 邻近插值,双线性插值,双三次B样条插值(default)
# param image PIL的图像image
# return 旋转之后的图像
random_angle = np.random.randint(1,360)
return image.rotate(random_angle,mode) @staticmethod
def randomCrop(image):
#对图像随意剪切,考虑到图像大小范围(68*68),使用一个一个大于(36*36)的窗口进行截图
#param image:PIL的图像image
#return:剪切之后的图像
image_width = image.size[0]
image_height = image.size[1]
crop_win_size = np.random.randint(40,68)
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)
return image.crop(random_region) @staticmethod
def randomColor(image):
#对图像进行颜色抖动
#param image:PIL的图像image
#return:有颜色色差的图像image #随机因子
random_factor = np.random.randint(0, 31) / 10.
#调整图像的饱和度
color_image = ImageEnhance.Color(image).enhance(random_factor)
#随机因子
random_factor = np.random.randint(10,21) / 10.
#调整图像的亮度
brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)
#随机因子
random_factor = np.random.randint(10,21) / 10.
#调整图像的对比度
contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)
#随机因子
random_factor = np.random.randint(0,31) / 10.
#调整图像锐度
sharpness_image = ImageEnhance.Sharpness(contrast_image).enhance(random_factor)
return sharpness_image @staticmethod
def randomGaussian(image,mean=0.2,sigma=0.3):
#对图像进行高斯噪声处理
#param image:
#return def gaussianNoisy(im,mean=0.2,sigma=0.3):
#对图像做高斯噪音处理
# param im:单通道图像
# param mean:偏移量
# param sigma:标准差
#return:
for _i in range(len(im)):
im[_i] += random.gauss(mean,sigma)
return im #将图像转化为数组
img = np.asanyarray(image)
#将数组改为读写模式
img.flags.writeable = True
width,height = img.shape[:2]
#对image的R,G,B三个通道进行分别处理
img_r = gaussianNoisy(img[:,:,0].flatten(), mean, sigma)
img_g = gaussianNoisy(img[:,:,1].flatten(), mean, sigma)
img_b = gaussianNoisy(img[:,:,2].flatten(), mean, sigma)
img[:,:,0] = img_r.reshape([width,height])
img[:,:,1] = img_g.reshape([width,height])
img[:,:,2] = img_b.reshape([width,height])
return Image.fromarray(np.uint8(img)) @staticmethod
def saveImage(image,path):
image.save(path) def makeDir(path):
try:
if not os.path.exists(path):
if not os.path.isfile(path):
os.makdirs(path)
return 0
else:
return 1
except Exception, e:
print str(e)
return -1 def imageOps(func_name, image, des_path, file_name, times = 5):
funcMap = {"randomRotation": DataAugmentation.randomRotation,
"randomCrop":DataAugmentation.randomCrop,
"randomColor":DataAugmentation.randomColor,
"randomGaussian":DataAugmentation.randomGaussian
}
if funcMap.get(func_name) is None:
logger.error("%s is not exist" , func_name)
return -1 for _i in range(0,times,1):
new_image = funcMap[func_name](image)
DataAugmentation.saveImage(new_image,os.path.join(des_path,func_name + str(_i) + file_name)) opsList = {"randomRotation", "randomCrop", "randomColor", "randomGaussian"} def threadOPS(path,new_path):
#多线程处理事务
#param src_path:资源文件
#param des_path:目的地文件
#return: if os.path.isdir(path):
img_names = os.listdir(path)
else:
img_names = [path]
for img_name in img_names:
print img_name
tmp_img_name = os.path.join(path,img_name)
print tmp_img_name
if os.path.isdir(tmp_img_name):
if makeDir(os.path.join(new_path,img_name)) != -1:
threadOPS(tmp_img_name,os.path.join(new_path,img_name))
else:
print 'create new dir failure'
return -1
elif tmp_img_name.split('.')[1] != "DS_Store":
image = DataAugmentation.openImage(tmp_img_name)
threadImage = [0] * 5
_index = 0
for ops_name in opsList:
threadImage[_index] = threading.Thread(target=imageOps,args=(ops_name,image,new_path,img_name))
threadImage[_index].start()
_index += 1
time.sleep(0.2) if __name__ == '__main__':
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. layui进度条bug

    对于动态及生成的进度条,在渲染时候要使用element.init();element.init();element.progress('demo', percent+'%');

  2. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

  3. deepin linux 15.3安装完eclipse启动报错An error has occurred.

    原因是系统中安装了jdk9 导致的. 卸载jdk9就可以了 $ java -version Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings ...

  4. Action方法调用

    一.Action访问路径 Action的访问路径是由struts.xml文件中配置的Action所在包的命名空间,Action的名字和常struts.action.extension共同决定的 例如: ...

  5. 15.3.14 DP练习2

    拦截导弹 题目 某国为了防御敌国的导弹突击,发展出一种导弹拦截系统. 可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以到达随意的高度.可是以后每一发炮弹都不能高于前一发的高度. 某天,雷达捕捉 ...

  6. SlidingMenu.jar 抽屉使用,避免了使用libaray的不兼容的尴尬

         尽管说这个东西出来了一段时间了,可是好东西还是要分享一下的. 之前我们使用的都是libaray.各种出错.团队开发也不好操作.不知道 哪个好心人干脆把这个工具封装成一个jar,真心为你点赞. ...

  7. 基于ffmpeg 直播推流和播放rtmp (IOS源码)

    ios直播推流每秒能达到30帧,比安卓要强,视频采用软编码的话手机会发烫,得采用码编码,播放视频采用opengl渲染. ffmpeg初始化代码如下: int init_Code(int width, ...

  8. CentOS下搭建wordpress全流程

    突然,想搭建一个个人博客,所以就在网上搜了搜相关内容,好多人推荐wordpress,那么就打算光荣地采用wordpress了.以下是安装过程中遇到的所有的坑,现在总结如下,希望给安装wordpress ...

  9. 微信抢红包微信 PHP代码实现

    header("Content-Type: text/html;charset=utf-8");//输出不乱码,你懂的 $total=10;//红包总额 $num=8;// 分成8 ...

  10. [Django] 问题记录追踪表

    关注的网站: https://simpleisbetterthancomplex.com/ Linux部署 1. Ubuntu下vsftpd安装部署 2. Utuntu下Django+Apache+W ...