1. 程式人生 > >深度學習中的數據增強技術(二)

深度學習中的數據增強技術(二)

data ont return per 分享圖片 slice google ordering res

上篇文章介紹了深度學習中數據增強技術的應用,及部分TensorFlow實現代碼。廢話不說了,這篇接著介紹:

TensorFlow實現圖片數據增強

註:類似的方法放在了代碼中註釋的部分,實際調試時可以分別打開運行

③圖像色彩調整

import  matplotlib.pyplot as plt
import tensorflow as tf

# 讀取圖片
image_data = tf.gfile.FastGFile("data/dog3.jpg", br).read()
# 4中調整圖片大小的方法,實際應用中可以根據需求組合調整
with tf.Session() as sess:
   img_data 
= tf.image.decode_jpeg(image_data) plt.imshow(img_data.eval()) plt.show() # 將圖片數據轉換成實數類型 img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32) # 亮度 # adjusted = tf.image.adjust_brightness(img_data,0.5) # adjusted = tf.clip_by_value(adjusted, 0.0, 1.0) # adjusted = tf.image.adjust_brightness(img_data, -0.5)
# adjusted = tf.clip_by_value(adjusted, 0.0, 1.0) # adjusted = tf.image.random_brightness(img_data,0.5) # 對比度 # adjusted = tf.image.adjust_contrast(img_data,0.5) # adjusted = tf.image.adjust_contrast(img_data, 5) # adjusted = tf.image.random_contrast(img_data,0.1,5) # 色相 # adjusted = tf.image.adjust_hue(img_data,0.3)
# adjusted = tf.image.adjust_hue(img_data, 0.1) # adjusted = tf.image.adjust_hue(img_data, 0.9) # adjusted = tf.image.adjust_hue(img_data, 0.6) # adjusted = tf.image.random_hue(img_data,0.5) # 飽和度 adjusted = tf.image.adjust_saturation(img_data,-5) # adjusted = tf.image.adjust_saturation(img_data, 5) # adjusted = tf.image.random_saturation(img_data,2,5) # 圖像標準化 均值為0 方差變為1 # adjusted = tf.image.per_image_standardization(img_data) plt.imshow(adjusted.eval()) plt.show()

圖片效果

原圖:

技術分享圖片

處理後:

技術分享圖片技術分享圖片技術分享圖片技術分享圖片

④標註、裁剪

import  matplotlib.pyplot as plt
import tensorflow as tf

# 讀取圖片
image_data = tf.gfile.FastGFile("data/dog3.jpg", br).read()
# 
with tf.Session() as sess:
   img_data = tf.image.decode_jpeg(image_data)
   plt.imshow(img_data.eval())
   plt.show()

   # 將圖片數據轉換成實數類型
   img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
   # 將圖片縮小一些,這樣可視化能讓標註框更加清楚
   img_data = tf.image.resize_images(img_data,[180,267],method=1)
   batched = tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0)
   boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.28,0.36,0.62,0.56]]])
   result = tf.image.draw_bounding_boxes(batched,boxes=boxes)
   plt.imshow(result[0].eval())
   plt.show()
   # print(result)
   # 隨機截取圖片
   begin,size,bbox_for_draw =tf.image.sample_distorted_bounding_box(tf.shape(img_data),bounding_boxes=boxes,min_object_covered=0.4)
   batched = tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0)
   image_with_box = tf.image.draw_bounding_boxes(batched,bbox_for_draw)
   distored_image = tf.slice(img_data,begin,size=size)
   plt.imshow(distored_image.eval())
   plt.show()
 

圖片效果

技術分享圖片技術分享圖片

關註公眾號“從機器學習到深度學習那些事”獲取更多最新資料

代碼整合總結

import tensorflow as tf
import numpy as np
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)
   elif color_ordering == 1:
       image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
       image = tf.image.random_brightness(image, max_delta=32. / 255)
       image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
       image = tf.image.random_hue(image, max_delta=0.2)
   elif color_ordering == 2:
       image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
       image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
       image = tf.image.random_hue(image, max_delta=0.2)
       image = tf.image.random_brightness(image, max_delta=32. / 255)
       # 還可以添加其他的排列
   else:
       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_hue(image, max_delta=0.2)
       image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
   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.dtype != tf.float32:
       image = tf.image.convert_image_dtype(image, dtype=tf.float32)
   # 隨機截取圖像,減少需要關註的物體大小對圖像識別算法的影響
   # 為圖像生成一個隨機distorted的邊框,Generate a single randomly distorted bounding box for an image
   bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox,min_object_covered=0.4)
   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(2))
   return distorted_image

image_raw_data = tf.gfile.FastGFile("./data/dog1.jpg", br).read()

with tf.Session() as sess:
   image_data = tf.image.decode_jpeg(image_raw_data)
   bboxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])

   # 運行6次獲得6中不同的圖片,展示出來
   for i in range(6):
       result = preprocess_for_train(image_data, 299, 299, bboxes)
       plt.imshow(result.eval())
       plt.show()

參考資料

21個項目玩轉深度學習

TensorFlow實戰Google深度學習框架

技術分享圖片

深度學習中的數據增強技術(二)