Data augmentation in deep learning

Go directly to the code:

#encoding:utf-8
'''
tf reference link: https://tensorflow.google.cn/api_guides/python/image
Increase the amount of data, reduce overfitting, and enhance the generalization ability of the model
can also be used when predicting
'''
import numpy as np
import them
import math
import tensorflow as tf
from skimage import io
import random
import matplotlib.pyplot as plt

def read_image(image_path):
	image_raw_data = tf.gfile.FastGFile(image_path,'rb').read()
	image_data = tf.image.decode_png(image_raw_data)
	return image_data

'''
	#Image size adjustment, zoom in and zoom out
	Different sizes
	tf.image.resize_images(img,size,size,method), 0, default bilinear interpolation; 1, nearest neighbor algorithm;
                                              2, bicubic interpolation; 3, area interpolation
'''
def resize_image(image_data):
	
	res = []
	image_biliner = tf.image.resize_images(image_data,[256,256],method=0)	

	image_nn = tf.image.resize_images(image_data,[256,256],method=1)
	image_bicubic = tf.image.resize_images(image_data,[256,256],method=2)
	image_area = tf.image.resize_images(image_data,[256,256],method=3)

	res.append(tf.to_int32(image_biliner))
	res.append(tf.to_int32(image_nn))
	res.append(tf.to_int32(image_bicubic))
	res.append(tf.to_int32(image_area))
	
	return res

'''
#crop
Identify objects in different locations
'''
def crop_image(image_data):
	res = []
	# Crop in the middle or fill with 0 around
	image_crop = tf.image.resize_image_with_crop_or_pad(image_data,256,256)	
	image_pad = tf.image.resize_image_with_crop_or_pad(image_data,512,512)
	
	#Crop the center area of ​​the image according to the ratio
	image_center_crop = tf.image.central_crop(image_data,0.5)

	#Random cropping (common method)
	image_random_crop0 = tf.random_crop(image_data,[300,300,3])
	image_random_crop1 = tf.random_crop(image_data,[300,300,3])

	res.append(tf.to_int32(image_crop))
	res.append(tf.to_int32(image_pad))
	res.append(tf.to_int32(image_center_crop))
	res.append(tf.to_int32(image_random_crop0))	
	res.append(tf.to_int32(image_random_crop1))

	return res

'''
	#rotate (mirror)
	Image rotation will not affect the recognition results, it can be rotated at multiple angles, so that the model can recognize objects from different angles
	When the angle of rotation or translation is small, maxpooling can be used to ensure the invariance of rotation and translation.
'''
def flip_image(image_data):

	#mirror
	res = []
	#flip up and down
	image_up_down_flip = tf.image.flip_up_down(image_data)

	#flip left and right
	image_left_right_filp = tf.image.flip_left_right(image_data)

	#diagonal rotation
	image_transpose = tf.image.transpose_image(image_data)

	#Rotate 90 degrees
	image_rot1 = tf.image.rot90(image_data,1)
	image_rot2 = tf.image.rot90(image_data,2)
	image_rot3 = tf.image.rot90(image_data,3)

	res.append(tf.to_int32(image_up_down_flip))
	res.append(tf.to_int32(image_left_right_filp))
	res.append(tf.to_int32(image_transpose))
	res.append(tf.to_int32(image_rot1))
	res.append(tf.to_int32(image_rot2))
	res.append(tf.to_int32(image_rot3))

	return res

#image color adjustment
'''
	Simulate more images in different scenarios based on the original data
	brightness (brightness), adapt to objects under different lighting
	Constrast (contrast), hue (color), saturation (saturation)
	Customizable and random
'''
def color_image(image_data):
	res = []

	image_random_brightness = tf.image.random_brightness(image_data,0.5)
	image_random_constrast = tf.image.random_contrast(image_data,0,1)
	image_random_hue = tf.image.random_hue(image_data,0.5)
	image_random_saturation = tf.image.random_saturation(image_data,0,1)

	#color space transformation
	images_data = tf.to_float(image_data)
	image_hsv_rgb = tf.image.rgb_to_hsv(images_data)
	# image_gray_rgb = tf.image.rgb_to_grayscale(image_data)
	# image_gray_rgb = tf.expand_dims(image_data[2],1)

	res.append(tf.to_int32(image_random_brightness))
	res.append(tf.to_int32(image_random_constrast))
	res.append(tf.to_int32(image_random_hue))
	res.append(tf.to_int32(image_random_saturation))
	res.append(tf.to_int32(image_hsv_rgb))
	return res

#add noise
def PCA_Jittering(img):

	img_size = img.size/3
	print(img.size,img_size)
	img1= img.reshape(int(img_size),3)
	img1 = np.transpose(img1)
	img_cov = np.cov ([img1 [0], img1 [1], img1 [2]])  
	#Calculate matrix eigenvectors
	lamda, p = np.linalg.eig (img_cov)

	p = np.transpose(p)  
	#generate random numbers from a normal distribution
	alpha1 = random.normalvariate(0,0.2)  
	alpha2 = random.normalvariate(0,0.2)  
	alpha3 = random.normalvariate(0,0.2)  

	v = np.transpose((alpha1*lamda[0], alpha2*lamda[1], alpha3*lamda[2])) #Add disturbance  
	add_num = np.dot(p,v)  

	img2 = np.array([img[:,:,0]+add_num[0], img[:,:,1]+add_num[1], img[:,:,2]+add_num[2]])  

	img2 = e.g. swapaxes (img2,0,2)  
	img2 = e.g. swapaxes (img2,0,1)  

	return img2

def main(_):
	image_path = 'dog.png'
	image_data = read_image(image_path)
	img = tf.image.per_image_standardization(image_data)
	resize = resize_image(image_data)
	crop = crop_image(image_data)
	flip = flip_image(image_data)
	color = color_image(image_data)
	init = tf.global_variables_initializer()

	with tf.Session() as sess:
		sess.run(init)
		img, resize_res, crop_res, flip_res, color_res = sess.run([img,
			resize,crop,flip,color])
		
		res = []
		res.append(resize_res)
		res.append(crop_res)
		res.append(flip_res)
		res.append(color_res)

		for cat in res:
			fig = plt.figure()
			num = 1
			for i in cat:	
				x = math.ceil(len(cat)/2) #round up				
				fig.add_subplot(2,x,num)	
				plt.imshow(i)
				num = num+1
			plt.show()
		img = PCA_Jittering(img)
		plt.imshow(img)
		plt.show()
if __name__ == '__main__':
	
	tf.app.run()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161723&siteId=291194637