Image preprocessing is a very simple, by improving the diversity of training data, and then to the training model of the recall rate, adaptability has a very big lifting effect.
In addition to training, you need more training times, for example, I have a rotation of each picture, then the number of training will be increased by one times. This means that the diversity of training sets increases and the number of training sessions increases.
Code:
Import TensorFlow as TF from scipy import Misc import NumPy as NP #随机旋转图片 def random_rotate_image (image_file, num): w ITH TF. Graph (). As_default (): Tf.set_random_seed (666) file_contents = Tf.read_file (image_file) image = TF. Image.decode_image (file_contents, channels=3) image_rotate_en_list = [] def random_rotate_image_func (image ): #旋转角度范围 angle = Np.random.uniform (low=-30.0, high=30.0) return misc.imrotate (image, Angle, ' bicubic ') for I in range (num): Image_rotate = Tf.py_func (Random_rotate_image_func, [image], t f.uint8) Image_rotate_en_list.append (Tf.image.encode_png (image_rotate)) with TF. Session () as Sess:sess.run (Tf.global_variables_initializer ()) Sess.run (tf.local_variables_initial
Izer ()) results = Sess.run (image_rotate_en_list) for idx,re in Enumerate (results): With open (' data/' +str (idx) + '. png ', ' WB 'As F:f.write (re) #随机左右翻转图片 def random_flip_image (image_file, num): with TF. Graph (). As_default (): Tf.set_random_seed (666) file_contents = Tf.read_file (image_file) image = TF. Image.decode_image (file_contents, channels=3) image_flip_en_list = [] for i in range (num): IMA
Ge_flip = tf.image.random_flip_left_right (image) Image_flip_en_list.append (Tf.image.encode_png (Image_flip)) With TF. Session () as Sess:sess.run (Tf.global_variables_initializer ()) Sess.run (tf.local_variables_initial Izer ()) results = Sess.run (image_flip_en_list) for idx,re in Enumerate (results): W ITH open (' data/' +str (idx) + '. png ', ' WB ') as F:f.write (re) #随机变化图片亮度 def random_brightness_image (Image_ File, num): with TF. Graph (). As_default (): Tf.set_random_seed (666) file_contents = Tf.read_file (image_file) image = TF. Image. Decode_image (File_contents, channels=3) image_bright_en_list = [] for i in range (num): Image_ Bright = tf.image.random_brightness (image, max_delta=0.3) image_bright_en_list.append (Tf.image.encode_png (imag e_bright)) with TF. Session () as Sess:sess.run (Tf.global_variables_initializer ()) Sess.run (tf.local_variables_initial
Izer ()) results = Sess.run (image_bright_en_list) for idx,re in Enumerate (results): With open (' data/' +str (idx) + '. png ', ' WB ') as F:f.write (re) #随机裁剪图片 def random_crop_image (Image_file, num): With TF. Graph (). As_default (): Tf.set_random_seed (666) file_contents = Tf.read_file (image_file) image = TF. Image.decode_image (file_contents, channels=3) image_crop_en_list = [] for i in range (num): #裁剪 After the picture resolution remains 160x160,3 Channel Image_crop = tf.random_crop (image, [3]) Image_cRop_en_list.append (Tf.image.encode_png (Image_crop)) with TF. Session () as Sess:sess.run (Tf.global_variables_initializer ()) Sess.run (tf.local_variables_initial Izer ()) results = Sess.run (image_crop_en_list) for idx,re in Enumerate (results): W ITH open (' data/' +str (idx) + '. png ', ' WB ') as F:f.write (re) if __name__ = = ' __main__ ': #处理图片, 20 random Process and save the processed picture to the same path as the input picture under Random_brightness_image (' Data/test.png ', 20)
Operating Effect:
Random cropping
Random brightness
Random rotation
Random Flip
For more image processing operations, please see the official TensorFlow documentation http://www.tensorfly.cn/tfdoc/api_docs/python/image.html