tensorflow  数据增强

目录

1.按角度旋转图片

         2. 按比例翻转

3.图片缩放


说明:在以下方法中,可对一批图像进行处理,此处为了方便只用了一张图片作为例子来展示。

1.按角度旋转图片

import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from math import pi

'''按角度旋转图片'''


def rotate_images(X_imgs, start_angle, end_angle, n_images,image_size_w,image_size_h):
     X_rotate = []
     iterate_at = (end_angle - start_angle) / (n_images - 1)

     tf.reset_default_graph()
     X = tf.placeholder(tf.float32, shape=(None, image_size_w,image_size_h, 3))
     radian = tf.placeholder(tf.float32, shape=(len(X_imgs)))
     tf_img = tf.contrib.image.rotate(X, radian)
     with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())

          for index in range(n_images):
               degrees_angle = start_angle + index * iterate_at
               radian_value = degrees_angle * pi / 180  # Convert to radian
               radian_arr = [radian_value] * len(X_imgs)
               rotated_imgs = sess.run(tf_img, feed_dict={X: X_imgs, radian: radian_arr})
               X_rotate.extend(rotated_imgs)

     X_rotate = np.array(X_rotate, dtype=np.float32)
     return X_rotate

image = cv2.imread('plane.jpeg')
#image = tf.gfile.GFile('/home/ubuntu/images/output.jpg', 'wb'
b,g,r = cv2.split(image)
image = cv2.merge([r,g,b])
image = cv2.resize(image, (200,200),0,0, cv2.INTER_LINEAR)  #图像缩放
image = np.multiply(image, 1.0 / 255.0)
list_image = []
list_image.append(image)
list_image = np.array(list_image)
#scaled_imgs = central_scale_images(list_image, [1.30, 1.50, 1.80])
scaled_imgs = rotate_images(list_image, -90, 90, 5,200,200)
#image = scaled_imgs[0]/255.

plt.subplot(331), plt.imshow(image), plt.title('', fontsize='medium')
plt.subplot(332), plt.imshow(scaled_imgs[0]), plt.title('',fontsize='small')
plt.subplot(333), plt.imshow(scaled_imgs[1]), plt.title('', fontsize='small')
plt.subplot(334), plt.imshow(scaled_imgs[2]), plt.title('', fontsize='small')
plt.subplot(335), plt.imshow(scaled_imgs[3]), plt.title('', fontsize='small')
plt.subplot(336), plt.imshow(scaled_imgs[4]), plt.title('', fontsize='small')
#plt.subplot(337), plt.imshow(scaled_imgs[5]), plt.title('', fontsize='small')
#plt.subplot(258), plt.imshow(scaled_imgs[6]), plt.title('Translate bottom 20 percent', fontsize='medium')
plt.show()

结果:

2. 按比例翻转

from math import ceil, floor
import numpy as np
import tensorflow as tf
import cv2
import matplotlib.pyplot as plt

def get_translate_parameters(index,image_size_w,image_size_h):
    if index == 0:  # Translate left 20 percent
        offset = np.array([0.0, 0.3], dtype=np.float32)
        size = np.array([image_size_w, ceil(0.7 * image_size_h)], dtype=np.int32)
        w_start = 0
        w_end = int(ceil(0.7 * image_size_w))
        h_start = 0
        h_end = image_size_h
    elif index == 1:  # Translate right 20 percent
        offset = np.array([0.0, -0.3], dtype=np.float32)
        size = np.array([image_size_w, ceil(0.7 * image_size_h)], dtype=np.int32)
        w_start = int(floor((1 - 0.7) * image_size_w))
        w_end = image_size_w
        h_start = 0
        h_end = image_size_h
    elif index == 2:  # Translate top 20 percent
        offset = np.array([0.3, 0.0], dtype=np.float32)
        size = np.array([ceil(0.7 * image_size_w), image_size_h], dtype=np.int32)
        w_start = 0
        w_end = image_size_w
        h_start = 0
        h_end = int(ceil(0.7 * image_size_h))
    else:  # Translate bottom 20 percent
        offset = np.array([-0.3, 0.0], dtype=np.float32)
        size = np.array([ceil(0.7 * image_size_w), image_size_h], dtype=np.int32)
        w_start = 0
        w_end = image_size_w
        h_start = int(floor((1 - 0.7) * image_size_h))
        h_end = image_size_h

    return offset, size, w_start, w_end, h_start, h_end

def translate_images(X_imgs,image_size_w,image_size_h):
    offsets = np.zeros((len(X_imgs), 2), dtype=np.float32)
    n_translations = 4
    X_translated_arr = []

    tf.reset_default_graph()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(n_translations):
            X_translated = np.zeros((len(X_imgs), image_size_w,image_size_h, 3),
                                    dtype=np.float32)
            X_translated.fill(1.0)  # Filling background color
            base_offset, size, w_start, w_end, h_start, h_end = get_translate_parameters(i,image_size_w,image_size_h)
            offsets[:, :] = base_offset
            glimpses = tf.image.extract_glimpse(X_imgs, size, offsets)

            glimpses = sess.run(glimpses)
            X_translated[:, h_start: h_start + size[0], \
            w_start: w_start + size[1], :] = glimpses
            X_translated_arr.extend(X_translated)
    X_translated_arr = np.array(X_translated_arr, dtype=np.float32)
    return X_translated_arr

image =  cv2.imread('plane.jpeg')
b,g,r = cv2.split(image)
image = cv2.merge([r,g,b])
image = cv2.resize(image, (200,200),0,0, cv2.INTER_LINEAR)  #图像缩放
image = np.multiply(image, 1.0 / 255.0)
list_image = []
list_image.append(image)
list_image = np.array(list_image)
#scaled_imgs = central_scale_images(list_image, [1.30, 1.50, 1.80])
scaled_imgs = translate_images(list_image, 200,200)
#image = scaled_imgs[0]/255.

plt.subplot(231), plt.imshow(image), plt.title('original', fontsize='medium')
plt.subplot(232), plt.imshow(scaled_imgs[0]), plt.title('Translate left 20 percent', fontsize='medium')
plt.subplot(233), plt.imshow(scaled_imgs[1]), plt.title('Translate right 20 percent', fontsize='medium')
plt.subplot(234), plt.imshow(scaled_imgs[2]), plt.title('Translate top 20 percent', fontsize='medium')
plt.subplot(235), plt.imshow(scaled_imgs[3]), plt.title('Translate bottom 20 percent', fontsize='medium')
plt.show()

结果展示:

3.图片缩放

import numpy as np
import tensorflow as tf
import cv2
import matplotlib.pyplot as plt
#
# '''图片缩放'''
#
# # #image = tf.gfile.FastGFile('f-35-37.jpg','rb').read()
image = cv2.imread('plane.jpeg')
b,g,r = cv2.split(image)
image = cv2.merge([r,g,b])
image = cv2.resize(image, (200,200),0,0, cv2.INTER_LINEAR)  #图像缩放
image = np.multiply(image, 1.0 / 255.0)
list_image = []
list_image.append(image)

def central_scale_images(X_imgs, scales,image_w=200,image_h=200):
    # Various settings needed for Tensorflow operation
    boxes = np.zeros((len(scales), 4), dtype=np.float32)
    for index, scale in enumerate(scales):
        x1 = y1 = 0.5 - 0.5 * scale  # To scale centrally
        x2 = y2 = 0.5 + 0.5 * scale
        boxes[index] = np.array([y1, x1, y2, x2], dtype=np.float32)
    box_ind = np.zeros((len(scales)), dtype=np.int32)
    crop_size = np.array([image_w, image_h], dtype=np.int32)

    X_scale_data = []
    #tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape=(1, image_w, image_h, 3))
    # Define Tensorflow operation for all scales but only one base image at a time
    tf_img = tf.image.crop_and_resize(X, boxes, box_ind, crop_size)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for img_data in X_imgs:
            # img_data = np.multiply(image, 1.0 / 255.0)   #归一化  np.multiply 乘法
            batch_img = np.expand_dims(img_data, axis=0)
            scaled_imgs = sess.run(tf_img, feed_dict={X: batch_img})
            X_scale_data.extend(scaled_imgs)

    #X_scale_data = np.array(X_scale_data, dtype=np.float32)
    return X_scale_data


scaled_imgs = central_scale_images(list_image, [1.30, 1.50, 1.80])
#scaled_imgs = central_scale_images(list_image, [0.9, 0.7, 0.6])
#image = scaled_imgs[0]/255.
image2 = scaled_imgs[0]
plt.subplot(221), plt.imshow(image), plt.title('original')
plt.subplot(222), plt.imshow(image2), plt.title('1.30')
plt.subplot(223), plt.imshow(scaled_imgs[1]), plt.title('1.50')
plt.subplot(224), plt.imshow(scaled_imgs[2]), plt.title('1.80')
plt.show()

结果展示:

其他方法参考以下链接:https://medium.com/ymedialabs-innovation/data-augmentation-techniques-in-cnn-using-tensorflow-371ae43d5be9