深さ学習のデータ強化(データセット拡張)方式と実現総括


前言:主にデータ強化(Data augmentation)でよく使われるいくつかの方法をまとめ、flip、回転(rotation)、スケール(scale)、クロップ(crop)、シフト(translation)など、opencv、numpy、tensorflow、その他のライブラリを使用して実現される関数をリストします.kerasにおけるデータ増強を実現するツールも示されている.データの強化は、データ量を効果的に向上させ、トレーニングデータセットを拡張することができます.しかし、万能ではなく、データの過度な信頼強化が負の効果をもたらし、ネットワークトレーニング時間を増やすこともあります.適宜使用する必要がある.
以下のようにまとめられています.
import cv2
import numpy as np
import tensorflow as tf
import imutils
import skimage
import pillow

path = '/home/zhangwei/workfiles/deeplearning/dogVScat/data/cat_1.jpg'
img = cv2.imread(path)
# tensorflow    
# img = tf.gfile.FastGFile(path).read()
# img_data = tf.image.decode_jpeg(img)


#----->   (flip)
#  opencv  
flip_11 = cv2.flip(img, 1, dst = None) # 1:  ,0:  ,-1:  

#  numpy  
flip_21 = np.fliplr(img) #     
flip_22 = np.transpose	#   

#  tensorfolw  
flip_31 = tf.image.flip_up_down(img) 
flip_32 = tf.image.flip_left_right	
flip_33 = tf.image.random_flip_up_down #     ,50%  
flip_34 = tf.image.random_flip_left_right
flip_35 = tf.image.transpose_image	#     


#---->   (rotation)
#  opencv  
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotation_11 = cv2.warpAffine(img, M, (w,h))
rotation_12 = imutils.rotate(img, angles)

#  numpy  
rotation_21 = np.rot90(img, k, axes)

#  tensorflow  
rotation_31 = tf.image.rot90(img, k = 1) # k:    
rotation_32 = tf.contrib.image.rotate(img, angles = 3.14) #       

#      
rotation_41 = skimage.transform.rotate(img, angles = 45, mode = 'reflect')


#---->   (scale)
#  opencv  
scale_11 = cv2.resize(img, dsize, fx, fy, interpolation)

#  numpy  
reshape() x
resize() x

#  tensorflow  
tf.image.resize_images(img, [M,N], method)
tf.image.resize_image_with_crop_or_pad(img, M, N)
tf.image.central_crop(img, factor)

#      
scale_41 = skimage.transform.rescale(img, scale, mode)


#---->  (crop)
#        

#  tensorflow  
crop_31 = tf.random_crop(img, size, seed)
crop_32 = tf.image.crop_to_bounding_box()


#---->  (translation)
#  opencv  
M = np.float32([[x1,y1,z1],[x2,y2,z2]])
translation_11 = cv2.warpAffine(img, M, (w,h))

#  numpy  
None

#  tensorflow  
translation_31 = tf.image.pad_to_bounding_box(img, pad_bottom, pad_right,height, width)


#----->      
#      
tf.image.adjust_brightness
tf.image.random_brightness
tf.iamge.adjust_constrast
tf.image.random_constrast
#     
#          


# !!!keras              ImageDataGenerator!!!#
from keras.preprocessing.image import ImageDataGenerator
#     
train_datagen = ImageDataGenerator(
	featurewise_center=False,	#     
    samplewise_center=False,	#      0
    featurewise_std_normalization = False,	#    /      
    samplewise_std_normalization = False,	#    /     
    zca_whitening = False,	# ZCA  
    rotation_range = 0.,	#       *
    width_shift_range = 0.,	#     0-1*
    height_shift_range = 0.,	#     0-1*
    shear_range = 0.,	#     *
    zoom_range = 0.,	#       *
    channel_shift_range = 0., #         ,    
    fill_mode = 'nearest',	#           ,    :constant,nearest,reflect,wrap
    cval = 0.0,	#      ,    
    horizontal_flip = False,	#       *
    vertical_flip = False,	#       *
    rescale = None,	#     
    preprocessing_function = None,	#      *
    data_format = K.image_data_format()
	)

#   (x,y)        ,x:  numpy  ,y:  numpy  
train_generator = train_datagen.flow_from_directory(
	directory: Any, #          
    target_size: Tuple[int, int] = (256, 256), #            
    color_mode: str = 'rgb', #       
    classes: Any = None, #    ,   
    class_mode: str = 'categorical', #    
    batch_size: int = 32, #     
    shuffle: bool = True, #     ,        
    seed: Any = None, #   ,       
    save_to_dir: Any = None, #     
    save_prefix: str = '', #     
    save_format: str = 'png', #     :png,jpeg
    follow_links: bool = False, #     
    subset: Any = None, #   (   )
    interpolation: str = 'nearest' #     
	)

#        ,        
model.fit_generator(
	generator = train_generator,  #     
	steps_per_epoch, #   
	epochs=1, #     
	verbose=1, #     ,0:   ,1:       ,2:  epoch     
	callbacks=None, #    
	validation_data=None, #         :tuple
	validation_steps=None, #  validation    ,      
	class_weight=None, #       
	max_q_size=10, #          
	workers=1, #      
	pickle_safe=False, #        
	initial_epoch=0 #        epoch    
	)