<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import numpy as np <EOL> import re <EOL> from scipy import linalg <EOL> import scipy . ndimage as ndi <EOL> from six . moves import range <EOL> import os <EOL> import threading <EOL> def random_rotation ( x , rg , row_index = <NUM_LIT:1> , col_index = <NUM_LIT:2> , channel_index = <NUM_LIT:0> , <EOL> fill_mode = '<STR_LIT>' , cval = <NUM_LIT:0.> ) : <EOL> theta = np . pi / <NUM_LIT> * np . random . uniform ( - rg , rg ) <EOL> rotation_matrix = np . array ( [ [ np . cos ( theta ) , - np . sin ( theta ) , <NUM_LIT:0> ] , <EOL> [ np . sin ( theta ) , np . cos ( theta ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> h , w = x . shape [ row_index ] , x . shape [ col_index ] <EOL> transform_matrix = transform_matrix_offset_center ( rotation_matrix , h , w ) <EOL> x = apply_transform ( x , transform_matrix , channel_index , fill_mode , cval ) <EOL> return x <EOL> def random_shift ( x , wrg , hrg , row_index = <NUM_LIT:1> , col_index = <NUM_LIT:2> , channel_index = <NUM_LIT:0> , <EOL> fill_mode = '<STR_LIT>' , cval = <NUM_LIT:0.> ) : <EOL> h , w = x . shape [ row_index ] , x . shape [ col_index ] <EOL> tx = np . random . uniform ( - hrg , hrg ) * h <EOL> ty = np . random . uniform ( - wrg , wrg ) * w <EOL> translation_matrix = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , tx ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , ty ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> transform_matrix = translation_matrix <EOL> x = apply_transform ( x , transform_matrix , channel_index , fill_mode , cval ) <EOL> return x <EOL> def random_shear ( x , intensity , row_index = <NUM_LIT:1> , col_index = <NUM_LIT:2> , channel_index = <NUM_LIT:0> , <EOL> fill_mode = '<STR_LIT>' , cval = <NUM_LIT:0.> ) : <EOL> shear = np . random . uniform ( - intensity , intensity ) <EOL> shear_matrix = np . array ( [ [ <NUM_LIT:1> , - np . sin ( shear ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , np . cos ( shear ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> h , w = x . shape [ row_index ] , x . shape [ col_index ] <EOL> transform_matrix = transform_matrix_offset_center ( shear_matrix , h , w ) <EOL> x = apply_transform ( x , transform_matrix , channel_index , fill_mode , cval ) <EOL> return x <EOL> def random_zoom ( x , zoom_range , row_index = <NUM_LIT:1> , col_index = <NUM_LIT:2> , channel_index = <NUM_LIT:0> , <EOL> fill_mode = '<STR_LIT>' , cval = <NUM_LIT:0.> ) : <EOL> if len ( zoom_range ) != <NUM_LIT:2> : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' , zoom_range ) <EOL> if zoom_range [ <NUM_LIT:0> ] == <NUM_LIT:1> and zoom_range [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> zx , zy = <NUM_LIT:1> , <NUM_LIT:1> <EOL> else : <EOL> zx , zy = np . random . uniform ( zoom_range [ <NUM_LIT:0> ] , zoom_range [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> zoom_matrix = np . array ( [ [ zx , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , zy , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> h , w = x . shape [ row_index ] , x . shape [ col_index ] <EOL> transform_matrix = transform_matrix_offset_center ( zoom_matrix , h , w ) <EOL> x = apply_transform ( x , transform_matrix , channel_index , fill_mode , cval ) <EOL> return x <EOL> def random_barrel_transform ( x , intensity ) : <EOL> pass <EOL> def random_channel_shift ( x , intensity , channel_index = <NUM_LIT:0> ) : <EOL> x = np . rollaxis ( x , channel_index , <NUM_LIT:0> ) <EOL> min_x , max_x = np . min ( x ) , np . max ( x ) <EOL> channel_images = [ np . clip ( x_channel + np . random . uniform ( - intensity , intensity ) , min_x , max_x ) <EOL> for x_channel in x ] <EOL> x = np . stack ( channel_images , axis = <NUM_LIT:0> ) <EOL> x = np . rollaxis ( x , <NUM_LIT:0> , channel_index + <NUM_LIT:1> ) <EOL> return x <EOL> def transform_matrix_offset_center ( matrix , x , y ) : <EOL> o_x = float ( x ) / <NUM_LIT:2> + <NUM_LIT:0.5> <EOL> o_y = float ( y ) / <NUM_LIT:2> + <NUM_LIT:0.5> <EOL> offset_matrix = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , o_x ] , [ <NUM_LIT:0> , <NUM_LIT:1> , o_y ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> reset_matrix = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , - o_x ] , [ <NUM_LIT:0> , <NUM_LIT:1> , - o_y ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> transform_matrix = np . dot ( np . dot ( offset_matrix , matrix ) , reset_matrix ) <EOL> return transform_matrix <EOL> def apply_transform ( x , transform_matrix , channel_index = <NUM_LIT:0> , fill_mode = '<STR_LIT>' , cval = <NUM_LIT:0.> ) : <EOL> x = np . rollaxis ( x , channel_index , <NUM_LIT:0> ) <EOL> final_affine_matrix = transform_matrix [ : <NUM_LIT:2> , : <NUM_LIT:2> ] <EOL> final_offset = transform_matrix [ : <NUM_LIT:2> , <NUM_LIT:2> ] <EOL> channel_images = [ ndi . interpolation . affine_transform ( x_channel , final_affine_matrix , <EOL> final_offset , order = <NUM_LIT:0> , mode = fill_mode , cval = cval ) for x_channel in x ] <EOL> x = np . stack ( channel_images , axis = <NUM_LIT:0> ) <EOL> x = np . rollaxis ( x , <NUM_LIT:0> , channel_index + <NUM_LIT:1> ) <EOL> return x <EOL> def flip_axis ( x , axis ) : <EOL> x = np . asarray ( x ) . swapaxes ( axis , <NUM_LIT:0> ) <EOL> x = x [ : : - <NUM_LIT:1> , ... ] <EOL> x = x . swapaxes ( <NUM_LIT:0> , axis ) <EOL> return x <EOL> def array_to_img ( x , dim_ordering = '<STR_LIT>' , scale = True ) : <EOL> from PIL import Image <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . transpose ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> if scale : <EOL> x += max ( - np . min ( x ) , <NUM_LIT:0> ) <EOL> x /= np . max ( x ) <EOL> x *= <NUM_LIT:255> <EOL> if x . shape [ <NUM_LIT:2> ] == <NUM_LIT:3> : <EOL> return Image . fromarray ( x . astype ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> elif x . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> return Image . fromarray ( x [ : , : , <NUM_LIT:0> ] . astype ( '<STR_LIT>' ) , '<STR_LIT:L>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' , x . shape [ <NUM_LIT:2> ] ) <EOL> def img_to_array ( img , dim_ordering = '<STR_LIT>' ) : <EOL> if dim_ordering not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' , dim_ordering ) <EOL> x = np . asarray ( img , dtype = '<STR_LIT>' ) <EOL> if len ( x . shape ) == <NUM_LIT:3> : <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . transpose ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> elif len ( x . shape ) == <NUM_LIT:2> : <EOL> if dim_ordering == '<STR_LIT>' : <EOL> x = x . reshape ( ( <NUM_LIT:1> , x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> x = x . reshape ( ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' , x . shape ) <EOL> return x <EOL> def load_img ( path , grayscale = False ) : <EOL> from PIL import Image <EOL> img = Image . open ( path ) <EOL> if grayscale : <EOL> img = img . convert ( '<STR_LIT:L>' ) <EOL> else : <EOL> img = img . convert ( '<STR_LIT>' ) <EOL> return img <EOL> def list_pictures ( directory , ext = '<STR_LIT>' ) : <EOL> return [ os . path . join ( directory , f ) for f in os . listdir ( directory ) <EOL> if os . path . isfile ( os . path . join ( directory , f ) ) and re . match ( '<STR_LIT>' + ext + '<STR_LIT>' , f ) ] <EOL> class ImageDataGenerator ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , <EOL> featurewise_center = False , <EOL> samplewise_center = False , <EOL> featurewise_std_normalization = False , <EOL> samplewise_std_normalization = False , <EOL> zca_whitening = False , <EOL> rotation_range = <NUM_LIT:0.> , <EOL> width_shift_range = <NUM_LIT:0.> , <EOL> height_shift_range = <NUM_LIT:0.> , <EOL> shear_range = <NUM_LIT:0.> , <EOL> zoom_range = <NUM_LIT:0.> , <EOL> channel_shift_range = <NUM_LIT:0.> , <EOL> fill_mode = '<STR_LIT>' , <EOL> cval = <NUM_LIT:0.> , <EOL> horizontal_flip = False , <EOL> vertical_flip = False , <EOL> dim_ordering = '<STR_LIT>' ) : <EOL> self . __dict__ . update ( locals ( ) ) <EOL> self . mean = None <EOL> self . std = None <EOL> self . principal_components = None <EOL> self . lock = threading . Lock ( ) <EOL> if dim_ordering not in { '<STR_LIT>' , '<STR_LIT>' } : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , dim_ordering ) <EOL> self . dim_ordering = dim_ordering <EOL> if dim_ordering == "<STR_LIT>" : <EOL> self . channel_index = <NUM_LIT:1> <EOL> self . row_index = <NUM_LIT:2> <EOL> self . col_index = <NUM_LIT:3> <EOL> if dim_ordering == "<STR_LIT>" : <EOL> self . channel_index = <NUM_LIT:3> <EOL> self . row_index = <NUM_LIT:1> <EOL> self . col_index = <NUM_LIT:2> <EOL> if np . isscalar ( zoom_range ) : <EOL> self . zoom_range = [ <NUM_LIT:1> - zoom_range , <NUM_LIT:1> + zoom_range ] <EOL> elif len ( zoom_range ) == <NUM_LIT:2> : <EOL> self . zoom_range = [ zoom_range [ <NUM_LIT:0> ] , zoom_range [ <NUM_LIT:1> ] ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , zoom_range ) <EOL> self . batch_index = <NUM_LIT:0> <EOL> self . total_batches_seen = <NUM_LIT:0> <EOL> def reset ( self ) : <EOL> self . batch_index = <NUM_LIT:0> <EOL> def _flow_index ( self , N , batch_size = <NUM_LIT:32> , shuffle = False , seed = None ) : <EOL> while <NUM_LIT:1> : <EOL> index_array = np . arange ( N ) <EOL> if self . batch_index == <NUM_LIT:0> : <EOL> if shuffle : <EOL> if seed is not None : <EOL> np . random . seed ( seed + self . total_batches_seen ) <EOL> index_array = np . random . permutation ( N ) <EOL> current_index = ( self . batch_index * batch_size ) % N <EOL> if N >= current_index + batch_size : <EOL> current_batch_size = batch_size <EOL> self . batch_index += <NUM_LIT:1> <EOL> else : <EOL> current_batch_size = N - current_index <EOL> self . batch_index = <NUM_LIT:0> <EOL> self . total_batches_seen += <NUM_LIT:1> <EOL> yield ( index_array [ current_index : current_index + current_batch_size ] , <EOL> current_index , current_batch_size ) <EOL> def flow ( self , X , y , batch_size = <NUM_LIT:32> , shuffle = False , seed = None , <EOL> save_to_dir = None , save_prefix = '<STR_LIT>' , save_format = '<STR_LIT>' ) : <EOL> assert len ( X ) == len ( y ) <EOL> self . X = X <EOL> self . y = y <EOL> self . save_to_dir = save_to_dir <EOL> self . save_prefix = save_prefix <EOL> self . save_format = save_format <EOL> self . reset ( ) <EOL> self . flow_generator = self . _flow_index ( X . shape [ <NUM_LIT:0> ] , batch_size , <EOL> shuffle , seed ) <EOL> return self <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> with self . lock : <EOL> index_array , current_index , current_batch_size = next ( self . flow_generator ) <EOL> bX = np . zeros ( tuple ( [ current_batch_size ] + list ( self . X . shape ) [ <NUM_LIT:1> : ] ) ) <EOL> for i , j in enumerate ( index_array ) : <EOL> x = self . X [ j ] <EOL> x = self . random_transform ( x . astype ( '<STR_LIT>' ) ) <EOL> x = self . standardize ( x ) <EOL> bX [ i ] = x <EOL> if self . save_to_dir : <EOL> for i in range ( current_batch_size ) : <EOL> img = array_to_img ( bX [ i ] , self . dim_ordering , scale = True ) <EOL> fname = '<STR_LIT>' . format ( prefix = self . save_prefix , <EOL> index = current_index + i , <EOL> format = self . save_format ) <EOL> img . save ( os . path . join ( self . save_to_dir , fname ) ) <EOL> bY = self . y [ index_array ] <EOL> return bX , bY <EOL> def __next__ ( self ) : <EOL> return self . next ( ) <EOL> def standardize ( self , x ) : <EOL> img_channel_index = self . channel_index - <NUM_LIT:1> <EOL> if self . samplewise_center : <EOL> x -= np . mean ( x , axis = img_channel_index , keepdims = True ) <EOL> if self . samplewise_std_normalization : <EOL> x /= ( np . std ( x , axis = img_channel_index , keepdims = True ) + <NUM_LIT> ) <EOL> if self . featurewise_center : <EOL> x -= self . mean <EOL> if self . featurewise_std_normalization : <EOL> x /= ( self . std + <NUM_LIT> ) <EOL> if self . zca_whitening : <EOL> flatx = np . reshape ( x , ( x . size ) ) <EOL> whitex = np . dot ( flatx , self . principal_components ) <EOL> x = np . reshape ( whitex , ( x . shape [ <NUM_LIT:0> ] , x . shape [ <NUM_LIT:1> ] , x . shape [ <NUM_LIT:2> ] ) ) <EOL> return x <EOL> def random_transform ( self , x ) : <EOL> img_row_index = self . row_index - <NUM_LIT:1> <EOL> img_col_index = self . col_index - <NUM_LIT:1> <EOL> img_channel_index = self . channel_index - <NUM_LIT:1> <EOL> if self . rotation_range : <EOL> theta = np . pi / <NUM_LIT> * np . random . uniform ( - self . rotation_range , self . rotation_range ) <EOL> else : <EOL> theta = <NUM_LIT:0> <EOL> rotation_matrix = np . array ( [ [ np . cos ( theta ) , - np . sin ( theta ) , <NUM_LIT:0> ] , <EOL> [ np . sin ( theta ) , np . cos ( theta ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> if self . height_shift_range : <EOL> tx = np . random . uniform ( - self . height_shift_range , self . height_shift_range ) * x . shape [ img_row_index ] <EOL> else : <EOL> tx = <NUM_LIT:0> <EOL> if self . width_shift_range : <EOL> ty = np . random . uniform ( - self . width_shift_range , self . width_shift_range ) * x . shape [ img_col_index ] <EOL> else : <EOL> ty = <NUM_LIT:0> <EOL> translation_matrix = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , tx ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , ty ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> if self . shear_range : <EOL> shear = np . random . uniform ( - self . shear_range , self . shear_range ) <EOL> else : <EOL> shear = <NUM_LIT:0> <EOL> shear_matrix = np . array ( [ [ <NUM_LIT:1> , - np . sin ( shear ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , np . cos ( shear ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> if self . zoom_range [ <NUM_LIT:0> ] == <NUM_LIT:1> and self . zoom_range [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> zx , zy = <NUM_LIT:1> , <NUM_LIT:1> <EOL> else : <EOL> zx , zy = np . random . uniform ( self . zoom_range [ <NUM_LIT:0> ] , self . zoom_range [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> zoom_matrix = np . array ( [ [ zx , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , zy , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> transform_matrix = np . dot ( np . dot ( np . dot ( rotation_matrix , translation_matrix ) , shear_matrix ) , zoom_matrix ) <EOL> h , w = x . shape [ img_row_index ] , x . shape [ img_col_index ] <EOL> transform_matrix = transform_matrix_offset_center ( transform_matrix , h , w ) <EOL> x = apply_transform ( x , transform_matrix , img_channel_index , <EOL> fill_mode = self . fill_mode , cval = self . cval ) <EOL> if self . channel_shift_range != <NUM_LIT:0> : <EOL> x = random_channel_shift ( x , self . channel_shift_range , img_channel_index ) <EOL> if self . horizontal_flip : <EOL> if np . random . random ( ) < <NUM_LIT:0.5> : <EOL> x = flip_axis ( x , img_col_index ) <EOL> if self . vertical_flip : <EOL> if np . random . random ( ) < <NUM_LIT:0.5> : <EOL> x = flip_axis ( x , img_row_index ) <EOL> return x <EOL> def fit ( self , X , <EOL> augment = False , <EOL> rounds = <NUM_LIT:1> , <EOL> seed = None ) : <EOL> '''<STR_LIT>''' <EOL> X = np . copy ( X ) <EOL> if augment : <EOL> aX = np . zeros ( tuple ( [ rounds * X . shape [ <NUM_LIT:0> ] ] + list ( X . shape ) [ <NUM_LIT:1> : ] ) ) <EOL> for r in range ( rounds ) : <EOL> for i in range ( X . shape [ <NUM_LIT:0> ] ) : <EOL> aX [ i + r * X . shape [ <NUM_LIT:0> ] ] = self . random_transform ( X [ i ] ) <EOL> X = aX <EOL> if self . featurewise_center : <EOL> self . mean = np . mean ( X , axis = <NUM_LIT:0> ) <EOL> X -= self . mean <EOL> if self . featurewise_std_normalization : <EOL> self . std = np . std ( X , axis = <NUM_LIT:0> ) <EOL> X /= ( self . std + <NUM_LIT> ) <EOL> if self . zca_whitening : <EOL> flatX = np . reshape ( X , ( X . shape [ <NUM_LIT:0> ] , X . shape [ <NUM_LIT:1> ] * X . shape [ <NUM_LIT:2> ] * X . shape [ <NUM_LIT:3> ] ) ) <EOL> sigma = np . dot ( flatX . T , flatX ) / flatX . shape [ <NUM_LIT:1> ] <EOL> U , S , V = linalg . svd ( sigma ) <EOL> self . principal_components = np . dot ( np . dot ( U , np . diag ( <NUM_LIT:1.> / np . sqrt ( S + <NUM_LIT> ) ) ) , U . T ) <EOL> class GraphImageDataGenerator ( ImageDataGenerator ) : <EOL> '''<STR_LIT>''' <EOL> def next ( self ) : <EOL> bX , bY = super ( GraphImageDataGenerator , self ) . next ( ) <EOL> return { '<STR_LIT:input>' : bX , '<STR_LIT>' : bY } </s>
