____tz_zs
映像片段截取,映像大小調整,映像翻轉以及色彩調整的整個映像預先處理過程
案例來源《TensorFlow實戰Google深度學習架構》
原圖
處理後的圖片
# -*- coding: utf-8 -*-"""@author: tz_zs的圖片預先處理範例"""import tensorflow as tfimport numpy as npimport 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) 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]) # [[[ 0. 0. 1. 1.]]] # 轉換映像張量的類型 if image.dtype != tf.float32: image = tf.image.convert_image_dtype(image, dtype=tf.float32) # 隨機截取映像 # print(tf.shape(image).eval()) # [232 320 3] 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(2)) return distorted_imageimage_raw_data = tf.gfile.FastGFile("picture.jpg", "rb").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]]]) for i in range(6): result = preprocess_for_train(img_data, 299, 299, boxes) plt.imshow(result.eval()) plt.show()