深度學習中的資料增廣
問題一:為什麼需要大量的資料
當訓練機器學習模型的時候,實際上實在調整它的引數,使得可以跟一個特定的輸入符合。優化的目標是 chase that sweet spot where our model’s loss is low。當前最好的神經網路擁有的引數量是上百萬的量級。 因此,有這麼多的引數,就需要a proportional amount of examples 來學習這些引數。
此外,通過資料增廣提升資料集中的相關資料,能防止網路學習到不相關的特徵,更多的學到更資料有關的效能,顯著的提升整體的效能。
問題二:在什麼地方做資料增廣?
- offline augmentation: 適合相對小一些的資料集;原始資料集的數量跟採用的增廣方法成正比。
- online augmentation: 適合大一些的資料集;承擔不起向前者那樣的成倍增廣,更適合on the mini-batches做增廣。 一些機器學習框架也支援被GPU加速過的線上增廣。
Popular augmentation techniques
1. Flip
水平或者垂直翻轉影象。
# NumPy.'img' = A single image. flip_1 = np.fliplr(img) # 水平翻轉 # TensorFlow. 'x' = A placeholder for an image. shape = [height, width, channels] x = tf.placeholder(dtype = tf.float32, shape = shape) flip_2 = tf.image.flip_up_down(x) flip_3 = tf.image.flip_left_right(x) flip_4 = tf.image.random_flip_up_down(x) flip_5 = tf.image.random_flip_left_right(x)
2. Rotation
對這個操作需要特別注意的是:影象的維數不會被保留。
3. Scale
影象可以被向內或向外縮放,當向外縮放的時候,最終的影象大小比原始影象大,很多框架從中crop跟原圖一樣大的部分。
# Scikit Image. 'img' = Input Image, 'scale' = Scale factor # For details about 'mode', checkout the interpolation section below. scale_out = skimage.transform.rescale(img, scale=2.0, mode='constant') scale_in = skimage.transform.rescale(img, scale=0.5, mode='constant') # Don't forget to crop the images back to the original size (for # scale_out)
4. Crop
不像縮放,裁剪僅僅從原始影象隨機取樣,然後resize到跟原來一樣大。
# TensorFlow. 'x' = A placeholder for an image.
original_size = [height, width, channels]
x = tf.placeholder(dtype = tf.float32, shape = original_size)
# Use the following commands to perform random crops
crop_size = [new_height, new_width, channels]
seed = np.random.randint(1234)
x = tf.random_crop(x, size = crop_size, seed = seed)
output = tf.images.resize_images(x, size = original_size)
5. Translation
平移僅僅包括將影象沿著X或者Y方向移動。
6. Gaussian Noise
當網路嘗試去學習高頻特徵的時候很容易過擬合。零均值高斯噪聲能有效的distorting高斯噪聲,這也意味著低頻部分(通常是想要的部分)也會損毀,但是你的網路能從中學到目標資訊。Adding just the right amount of noise can enhance the learning capability.
還可以加椒鹽噪聲,視覺效果類似於高斯噪聲,但是資訊損失的更少。
#TensorFlow. 'x' = A placeholder for an image.
shape = [height, width, channels]
x = tf.placeholder(dtype = tf.float32, shape = shape)
# Adding Gaussian noise
noise = tf.random_normal(shape=tf.shape(x), mean=0.0, stddev=1.0,
dtype=tf.float32)
output = tf.add(x, noise)
牢記腦中:當做資料增廣的時候,要確保不要增加不相關的資料。
相關資源
作者:EdwardMa 連結:https://www.jianshu.com/p/ffab1d022d2c 來源:簡書 簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。