1. 程式人生 > >TensorFlow影象資料預處理

TensorFlow影象資料預處理

寫在前面

在之前介紹的栗子中都是直接使用影象原始的畫素矩陣。但是如果在輸入前通過對影象的預處理,可以儘量避免模型收到無關因素的影響。在大部分影象識別問題中,通過影象預處理過程可以提高模型的準確率。

1. 影象編碼處理

我們平常提到的RGB影象可以看成一個三維矩陣,矩陣中的每個元素表示了影象上不同位置,不同顏色的亮度。但是影象在儲存時並不是直接記錄這些矩陣中的數字,而是記錄經過壓縮編碼之後的結果。所以在使用時還需要解碼的過程。TensorFlow提供了對jpg和png格式影象的編碼/解碼函式:tf.image.decode_jpeg()和tf.image.decode_png()。程式碼如下:

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    
    # 輸出解碼之後的三維矩陣。
    print(img_data.eval())
    img_data.set_shape([1797, 2673, 3])
    print(img_data.get_shape())

視覺化圖片:

#視覺化圖片
with tf.Session() as sess:
    plt.imshow(img_data.eval())
    plt.show()

2. 圖片大小調整

一般來說,我們獲取的影象大小是不統一的,但是神經網路輸入節點個數是固定的。所以在將影象的畫素作為輸入提供給神經網路之前,需要先將影象的大小統一。

(1)通過演算法使得新的影象儘量儲存原始影象上的所有資訊。TensorFlow提供了tf.image.resize_images()函式。

#重新調整圖片大小
with tf.Session() as sess:
    # 如果直接以0-255範圍的整數資料輸入resize_images,那麼輸出將是0-255之間的實數,
    # 不利於後續處理。建議在調整圖片大小前,先將圖片轉為0-1範圍的實數。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)

    #method引數對應該函式中不同的影象大小調整演算法:0=雙線性插值法, 1=最近鄰演算法, 2=雙三次插值法, 3=面積插值法
    resized = tf.image.resize_images(image_float, [300, 300], method=0)
    
    plt.imshow(resized.eval())
    plt.show()

(2)對影象進行裁剪或者填充

#裁剪和填充圖片
with tf.Session() as sess:    
    croped = tf.image.resize_image_with_crop_or_pad(resized, 100, 100)
    padded = tf.image.resize_image_with_crop_or_pad(resized, 1000, 1000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()

(3)通過比例調整影象的大小

#擷取中間50%的部分
with tf.Session() as sess:   
    central_cropped = tf.image.central_crop(resized, 0.5)
    plt.imshow(central_cropped.eval())
    plt.show()

3.影象翻轉

在很多影象識別問題彙總,影象的翻轉不會影響識別的結果,於是可以在訓練集中進行翻轉預處理增加訓練樣本。

#翻轉圖片
with tf.Session() as sess: 
    # 上下翻轉
    flipped1 = tf.image.flip_up_down(img_data)
    # 左右翻轉
    flipped2 = tf.image.flip_left_right(img_data)
    
    #對角線翻轉
    transposed = tf.image.transpose_image(resized)
    #plt.imshow(transposed.eval())
    #plt.show()
    
    # 以一定概率上下翻轉圖片。
    flipped = tf.image.random_flip_up_down(img_data)
    # 以一定概率左右翻轉圖片。
    flipped = tf.image.random_flip_left_right(img_data)

4. 影象色彩調整

對影象的色彩預處理目的和影象翻轉類似。

(1)調節亮度和對比度

with tf.Session() as sess:
    # 在進行一系列圖片調整前,先將圖片轉換為實數形式,有利於保持計算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    
    # 將圖片的亮度-0.5。
    #adjusted = tf.image.adjust_brightness(image_float, -0.5)
    
    # 將圖片的亮度-0.5
    #adjusted = tf.image.adjust_brightness(image_float, 0.5)
    
    # 在[-max_delta, max_delta)的範圍隨機調整圖片的亮度。
    adjusted = tf.image.random_brightness(image_float, max_delta=0.5)
    
    # 將圖片的對比度-5
    #adjusted = tf.image.adjust_contrast(image_float, -5)
    
    # 將圖片的對比度+5
    #adjusted = tf.image.adjust_contrast(image_float, 5)
    
    # 在[lower, upper]的範圍隨機調整圖的對比度。
    #adjusted = tf.image.random_contrast(image_float, lower, upper)

    # 在最終輸出前,將實數取值擷取到0-1範圍內。
    adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
    plt.imshow(adjusted.eval())

(2)調節色相和飽和度

with tf.Session() as sess:
    # 在進行一系列圖片調整前,先將圖片轉換為實數形式,有利於保持計算精度。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    
    adjusted = tf.image.adjust_hue(image_float, 0.1)
    #adjusted = tf.image.adjust_hue(image_float, 0.3)
    #adjusted = tf.image.adjust_hue(image_float, 0.6)
    #adjusted = tf.image.adjust_hue(image_float, 0.9)
    
    # 在[-max_delta, max_delta]的範圍隨機調整圖片的色相。max_delta的取值在[0, 0.5]之間。
    #adjusted = tf.image.random_hue(image_float, max_delta)
    
    # 將圖片的飽和度-5。
    #adjusted = tf.image.adjust_saturation(image_float, -5)
    # 將圖片的飽和度+5。
    #adjusted = tf.image.adjust_saturation(image_float, 5)
    # 在[lower, upper]的範圍隨機調整圖的飽和度。
    #adjusted = tf.image.random_saturation(image_float, lower, upper)
    
    # 將代表一張圖片的三維矩陣中的數字均值變為0,方差變為1。
    #adjusted = tf.image.per_image_whitening(image_float)
    
    # 在最終輸出前,將實數取值擷取到0-1範圍內。
    adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
    plt.imshow(adjusted.eval())
    plt.show()

5. 處理標註框

在很多影象識別問題中,影象中需要關注的物體會被標註框圈出來。

with tf.Session() as sess:         
    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
    
    # sample_distorted_bounding_box要求輸入圖片必須是實數型別。
    image_float = tf.image.convert_image_dtype(img_data, tf.float32)
    
    begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4)
    
    # 擷取後的圖片
    distorted_image = tf.slice(image_float, begin, size)
    plt.imshow(distorted_image.eval())
    plt.show()

    # 在原圖上用標註框畫出擷取的範圍。由於原圖的解析度較大(2673x1797),生成的標註框 
    # 在Jupyter Notebook上通常因邊框過細而無法分辨,這裡為了演示方便先縮小解析度。
    image_small = tf.image.resize_images(image_float, [180, 267], method=0)
    batchced_img = tf.expand_dims(image_small, 0)
    image_with_box = tf.image.draw_bounding_boxes(batchced_img, bbox_for_draw)
    print(bbox_for_draw.eval())
    plt.imshow(image_with_box[0].eval())
    plt.show()

上面就是利用tensorflow預處理影象資料的基本操作,完整程式碼樣例稍後上傳Github,包括了影象片段擷取,到影象大小調整再到影象翻轉以及色彩調整的整個過程。

以上~

2018.06.10