深度學習tensorflow 之 distorted_inputs

my_share發表於2020-10-11

cifar10_input.py檔案裡還有個功能強大的函式——distorted_inputs,可以在程式碼中找到其實現。它是針對train資料的,對train資料進行了變形處理,起了一個資料增廣的作用。在資料集比較小、資料量遠遠不夠的情況下,可以對圖片進行翻轉、隨機剪下等操作以增加資料,製作出更多的樣本,提高對圖片的利用率。

該函式的核心程式碼如下:

 

# Randomly crop a [height, width] section of the image.

distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

# Randomly flip the image horizontally.

distorted_image = tf.image.random_flip_left_right(distorted_image)

# Because these operations are not commutative, consider randomizing

# the order their operation.

distorted_image = tf.image.random_brightness(distorted_image,

max_delta=63)

distorted_image = tf.image.random_contrast(distorted_image,

lower=0.2, upper=1.8)

# Subtract off the mean and divide by the variance of the pixels.

float_image = tf.image.per_image_standardization(distorted_image)
  1.  

上述程式碼分別呼叫了不同的函式對圖片進行不同的變換,具體解釋如下:

  • tf.random_crop:為圖片隨機裁剪
  • tf.image.random_flip_left_right:隨機左右翻轉
  • tf.image.random_brightness:隨機亮度變化
  • tf.image.random_contrast:隨機對比度變化
  • tf.image.per_image_standardization:減去均值畫素,併除以畫素方差(圖片標準化)。

相關文章