深度学习--数据增强
生活随笔
收集整理的這篇文章主要介紹了
深度学习--数据增强
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在深度學(xué)習(xí)中,為了避免出現(xiàn)過擬合(Overfitting),通常我們需要輸入充足的數(shù)據(jù)量.本頁(yè)面主要記錄下常用的數(shù)據(jù)增強(qiáng)(Data Augmentation)變換方法.
不同的任務(wù)背景下, 我們可以通過圖像的幾何變換, 使用以下一種或多種組合數(shù)據(jù)增強(qiáng)變換來增加輸入數(shù)據(jù)的量. 這里具體的方法都來自數(shù)字圖像處理的內(nèi)容, 相關(guān)的知識(shí)點(diǎn)介紹, 網(wǎng)上都有, 就不一一介紹了.
??? 旋轉(zhuǎn) | 反射變換(Rotation/reflection): 隨機(jī)旋轉(zhuǎn)圖像一定角度; 改變圖像內(nèi)容的朝向;
??? 翻轉(zhuǎn)變換(flip): 沿著水平或者垂直方向翻轉(zhuǎn)圖像;
??? 縮放變換(zoom): 按照一定的比例放大或者縮小圖像;
??? 平移變換(shift): 在圖像平面上對(duì)圖像以一定方式進(jìn)行平移;
??? 可以采用隨機(jī)或人為定義的方式指定平移范圍和平移步長(zhǎng), 沿水平或豎直方向進(jìn)行平移. 改變圖像內(nèi)容的位置;
??? 尺度變換(scale): 對(duì)圖像按照指定的尺度因子, 進(jìn)行放大或縮小; 或者參照SIFT特征提取思想, 利用指定的尺度因子對(duì)圖像濾波構(gòu)造尺度空間. 改變圖像內(nèi)容的大小或模糊程度;
??? 對(duì)比度變換(contrast): 在圖像的HSV顏色空間,改變飽和度S和V亮度分量,保持色調(diào)H不變. 對(duì)每個(gè)像素的S和V分量進(jìn)行指數(shù)運(yùn)算(指數(shù)因子在0.25到4之間), 增加光照變化;
??? 噪聲擾動(dòng)(noise): 對(duì)圖像的每個(gè)像素RGB進(jìn)行隨機(jī)擾動(dòng), 常用的噪聲模式是椒鹽噪聲和高斯噪聲;
??? 顏色變換(color): 在訓(xùn)練集像素值的RGB顏色空間進(jìn)行PCA, 得到RGB空間的3個(gè)主方向向量,3個(gè)特征值, p1, p2, p3, λ1, λ2, λ3. 對(duì)每幅圖像的每個(gè)像素Ixy=[IRxy,IGxy,IBxy]T進(jìn)行加上如下的變化:
[p1,p2,p3][α1λ1,α2λ2,α3λ3]T
其中:αi是滿足均值為0,方差為0.1的隨機(jī)變量.
注意: 幾何變換不改變像素值, 而是改變像素所在的位置. 通過Data Augmentation方法擴(kuò)張了數(shù)據(jù)集的范圍, 作為輸入時(shí), 以期待網(wǎng)絡(luò)學(xué)習(xí)到更多的圖像不變性特征.
代碼實(shí)現(xiàn)
作為實(shí)現(xiàn)部分, 這里介紹一下在python 環(huán)境下, 利用已有的開源代碼庫(kù)Keras作為實(shí)踐:
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.
環(huán)境搭建
<1> 實(shí)現(xiàn)環(huán)境: ubuntu 14.04, python;
<2> Keras 依賴庫(kù): http://blog.csdn.net/langb2014/article/details/51579491
??? python環(huán)境搭建:
??? 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(作為后端, 從實(shí)現(xiàn)本文的主要內(nèi)容看, 推薦安裝這個(gè))
??? TensorFlow(作為后端, 可以了解一下)
<3> 安裝 Keras
sudo pip install keras
?
python實(shí)現(xiàn)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import packages
from keras.preprocessing.image import ImageDataGenerator, 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
for batch in datagen.flow(x,?
????????????????????????? batch_size=1,
????????????????????????? save_to_dir='~/Desktop/preview', ?
????????????????????????? save_prefix='lena',?
????????????????????????? save_format='jpg'):
??? i += 1
??? if i > 20:
??????? break? # otherwise the generator would loop indefinitely
<2> 參數(shù)講解:
http://keras.io/preprocessing/image/
主要函數(shù):ImageDataGenerator 實(shí)現(xiàn)了大多數(shù)上文中提到的圖像幾何變換方法.
??? rotation_range: 旋轉(zhuǎn)范圍, 隨機(jī)旋轉(zhuǎn)(0-180)度;
??? width_shift and height_shift: 隨機(jī)沿著水平或者垂直方向,以圖像的長(zhǎng)寬小部分百分比為變化范圍進(jìn)行平移;
??? rescale: 對(duì)圖像按照指定的尺度因子, 進(jìn)行放大或縮小, 設(shè)置值在0 - 1之間,通常為1 / 255;
??? shear_range: 水平或垂直投影變換, http://keras.io/preprocessing/image/
??? zoom_range: 按比例隨機(jī)縮放圖像尺寸;
??? horizontal_flip: 水平翻轉(zhuǎn)圖像;
??? fill_mode: 填充像素, 出現(xiàn)在旋轉(zhuǎn)或平移之后.
不同的任務(wù)背景下, 我們可以通過圖像的幾何變換, 使用以下一種或多種組合數(shù)據(jù)增強(qiáng)變換來增加輸入數(shù)據(jù)的量. 這里具體的方法都來自數(shù)字圖像處理的內(nèi)容, 相關(guān)的知識(shí)點(diǎn)介紹, 網(wǎng)上都有, 就不一一介紹了.
??? 旋轉(zhuǎn) | 反射變換(Rotation/reflection): 隨機(jī)旋轉(zhuǎn)圖像一定角度; 改變圖像內(nèi)容的朝向;
??? 翻轉(zhuǎn)變換(flip): 沿著水平或者垂直方向翻轉(zhuǎn)圖像;
??? 縮放變換(zoom): 按照一定的比例放大或者縮小圖像;
??? 平移變換(shift): 在圖像平面上對(duì)圖像以一定方式進(jìn)行平移;
??? 可以采用隨機(jī)或人為定義的方式指定平移范圍和平移步長(zhǎng), 沿水平或豎直方向進(jìn)行平移. 改變圖像內(nèi)容的位置;
??? 尺度變換(scale): 對(duì)圖像按照指定的尺度因子, 進(jìn)行放大或縮小; 或者參照SIFT特征提取思想, 利用指定的尺度因子對(duì)圖像濾波構(gòu)造尺度空間. 改變圖像內(nèi)容的大小或模糊程度;
??? 對(duì)比度變換(contrast): 在圖像的HSV顏色空間,改變飽和度S和V亮度分量,保持色調(diào)H不變. 對(duì)每個(gè)像素的S和V分量進(jìn)行指數(shù)運(yùn)算(指數(shù)因子在0.25到4之間), 增加光照變化;
??? 噪聲擾動(dòng)(noise): 對(duì)圖像的每個(gè)像素RGB進(jìn)行隨機(jī)擾動(dòng), 常用的噪聲模式是椒鹽噪聲和高斯噪聲;
??? 顏色變換(color): 在訓(xùn)練集像素值的RGB顏色空間進(jìn)行PCA, 得到RGB空間的3個(gè)主方向向量,3個(gè)特征值, p1, p2, p3, λ1, λ2, λ3. 對(duì)每幅圖像的每個(gè)像素Ixy=[IRxy,IGxy,IBxy]T進(jìn)行加上如下的變化:
[p1,p2,p3][α1λ1,α2λ2,α3λ3]T
其中:αi是滿足均值為0,方差為0.1的隨機(jī)變量.
注意: 幾何變換不改變像素值, 而是改變像素所在的位置. 通過Data Augmentation方法擴(kuò)張了數(shù)據(jù)集的范圍, 作為輸入時(shí), 以期待網(wǎng)絡(luò)學(xué)習(xí)到更多的圖像不變性特征.
代碼實(shí)現(xiàn)
作為實(shí)現(xiàn)部分, 這里介紹一下在python 環(huán)境下, 利用已有的開源代碼庫(kù)Keras作為實(shí)踐:
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.
環(huán)境搭建
<1> 實(shí)現(xiàn)環(huán)境: ubuntu 14.04, python;
<2> Keras 依賴庫(kù): http://blog.csdn.net/langb2014/article/details/51579491
??? python環(huán)境搭建:
??? 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(作為后端, 從實(shí)現(xiàn)本文的主要內(nèi)容看, 推薦安裝這個(gè))
??? TensorFlow(作為后端, 可以了解一下)
<3> 安裝 Keras
sudo pip install keras
?
python實(shí)現(xiàn)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import packages
from keras.preprocessing.image import ImageDataGenerator, 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
for batch in datagen.flow(x,?
????????????????????????? batch_size=1,
????????????????????????? save_to_dir='~/Desktop/preview', ?
????????????????????????? save_prefix='lena',?
????????????????????????? save_format='jpg'):
??? i += 1
??? if i > 20:
??????? break? # otherwise the generator would loop indefinitely
<2> 參數(shù)講解:
http://keras.io/preprocessing/image/
主要函數(shù):ImageDataGenerator 實(shí)現(xiàn)了大多數(shù)上文中提到的圖像幾何變換方法.
??? rotation_range: 旋轉(zhuǎn)范圍, 隨機(jī)旋轉(zhuǎn)(0-180)度;
??? width_shift and height_shift: 隨機(jī)沿著水平或者垂直方向,以圖像的長(zhǎng)寬小部分百分比為變化范圍進(jìn)行平移;
??? rescale: 對(duì)圖像按照指定的尺度因子, 進(jìn)行放大或縮小, 設(shè)置值在0 - 1之間,通常為1 / 255;
??? shear_range: 水平或垂直投影變換, http://keras.io/preprocessing/image/
??? zoom_range: 按比例隨機(jī)縮放圖像尺寸;
??? horizontal_flip: 水平翻轉(zhuǎn)圖像;
??? fill_mode: 填充像素, 出現(xiàn)在旋轉(zhuǎn)或平移之后.
總結(jié)
以上是生活随笔為你收集整理的深度学习--数据增强的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC动态链接库的制作步骤和使用方法
- 下一篇: 深度学习论文资源(截至2016年)