1. 程式人生 > >機器學習中影象增強的方法

機器學習中影象增強的方法

       在訓練模型時,資料是很重要的一部分。在這個資料為王的年代,要想訓練出優秀的模型,資料是不可或缺的一部分。但是很多。但是大多數情況下,資料的獲取並不那麼容易,所以這時候就可以採取影象增強的方法,可以在一定程度上緩解過擬合的問題。(但這個也只是在某種程度上可以緩解,如果資料量屬實太小,效果也不太明顯)

       話不多說,現 在正式介紹相關的方法。

              1.隨機調整圖片的飽和度,對比度,以及色調(但也要符合實際的情況,調整範圍不易過大)

              2.隨機對圖片進行裁剪,翻轉,旋轉變化(同樣的,也要符合實際情況)

                  方法2還是很吃顯示卡的,可能會跑不起來

              3.在影象中隨機新增高斯噪聲(在資料量充足的情況下,一般也會使用該方法進行增強)

              4. PCA Jittering,最早是由Alex在他2012年贏得ImageNet競賽的那篇NIPS中提出來的. 我們首先按照RGB三個顏                   色通道計算了均值和標準差,對網路的輸入資料進行規範化,隨後我們在整個訓練集上計算了協方差矩陣,進行特徵                   分解,得到特徵向量和特徵值,用來做PCA Jittering。

              5.Crop Sampling,就是怎麼從原始影象中進行縮放裁剪獲得網路的輸入。比較常用的有2種方法:一是使用Scale                       Jittering,VGG和ResNet模型的訓練都用了這種方法。二是尺度和長寬比增強變換,最早是Google提出來訓練他                   們的Inception網路的。

              6.有監督的Crop,在Bolei今年CVPR文章的啟發下,提出了有監督的資料增強方法。我們首先按照通常方法訓練一個模                   型,然後用這個模型去生成真值標籤的Class Activation Map(或者說Heat Map), 這個Map指示了目標物體出                     現在不同位置的概率. 我們依據這個概率,在Map上隨機選擇一個位置,然後映射回原圖,在原圖那個位置附近去做                    Crop。

 以下是 Tensorflow 實戰 Google 深度學習框架的 提到的一些影象資料增強的方法           

import matplotlib.pyplot as plt  
  
def distort_color(image, color_ordering=0):  
    if color_ordering == 0:  
        image = tf.image.random_brightness(image, max_delta=32. / 255.)#亮度  
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)#飽和度  
        image = tf.image.random_hue(image, max_delta=0.2)#色相  
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)#對比度  
    if color_ordering == 1:  
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)  
        image = tf.image.random_hue(image, max_delta=0.2)  
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)  
        image = tf.image.random_brightness(image, max_delta=32. / 255.)  
    if color_ordering == 2:  
        image = tf.image.random_hue(image, max_delta=0.2)  
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)  
        image = tf.image.random_brightness(image, max_delta=32. / 255.)  
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)  
    if color_ordering == 3:  
        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)  
        image = tf.image.random_brightness(image, max_delta=32. / 255.)  
        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)  
        image = tf.image.random_hue(image, max_delta=0.2)  
    return tf.clip_by_value(image, 0.0, 1.0)  
  
def preprocess_for_train(image, height, width, bbox):  #影象的翻轉  
    if bbox is None:  
        bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])  
    if image.dytpe != tf.float32:  
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)  
    bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox)  
    distorted_image = tf.slice(image, bbox_begin, bbox_size)  
    distorted_image = tf.image.resize_images(distorted_image, height, width, method=np.random.randint(4))  
    distorted_image = tf.image.random_flip_left_right(distorted_image)  
    distorted_image = distort_color(distorted_image, np.random.randint(4))  
    return distorted_image  
  
image_raw_data = tf.gfile.FastGFile("").read()  
with tf.Session() as sess:  
    img_data = tf.image.decode_jpeg(image_raw_data)  
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])  
    result = preprocess_for_train(img_data, 299, 299, boxes)