#NEWLINE# def lecun_lcn( input , img_shape , kernel_shape , threshold = 0.0001 ) : #NEWLINE# #INDENT# "\n    Yann LeCun's local contrast normalization\n\n    Original code in Theano by: Guillaume Desjardins\n\n    Parameters\n    ----------\n    input : WRITEME\n    img_shape : WRITEME\n    kernel_shape : WRITEME\n    threshold : WRITEME\n    " #NEWLINE# input = input . reshape ( ( input . shape [ 0 ] , input . shape [ 1 ] , input . shape [ 2 ] , 1 ) ) #NEWLINE# X = tensor . matrix ( dtype = input . dtype ) #NEWLINE# X = X . reshape ( ( len ( input ) , img_shape [ 0 ] , img_shape [ 1 ] , 1 ) ) #NEWLINE# filter_shape = ( 1 , 1 , kernel_shape , kernel_shape ) #NEWLINE# filters = sharedX ( gaussian_filter ( kernel_shape ) . reshape ( filter_shape ) ) #NEWLINE# input_space = Conv2DSpace ( shape = img_shape , num_channels = 1 ) #NEWLINE# transformer = Conv2D ( filters = filters , batch_size = len ( input ) , input_space = input_space , border_mode = 'full' ) #NEWLINE# convout = transformer . lmul ( X ) #NEWLINE# mid = int ( numpy . floor ( ( kernel_shape / 2.0 ) ) ) #NEWLINE# centered_X = ( X - convout [ : , mid : ( - mid ) , mid : ( - mid ) , : ] ) #NEWLINE# transformer = Conv2D ( filters = filters , batch_size = len ( input ) , input_space = input_space , border_mode = 'full' ) #NEWLINE# sum_sqr_XX = transformer . lmul ( ( X ** 2 ) ) #NEWLINE# denom = tensor . sqrt ( sum_sqr_XX [ : , mid : ( - mid ) , mid : ( - mid ) , : ] ) #NEWLINE# per_img_mean = denom . mean ( axis = [ 1 , 2 ] ) #NEWLINE# divisor = tensor . largest ( per_img_mean . dimshuffle ( 0 , 'x' , 'x' , 1 ) , denom ) #NEWLINE# divisor = tensor . maximum ( divisor , threshold ) #NEWLINE# new_X = ( centered_X / divisor ) #NEWLINE# new_X = tensor . flatten ( new_X , outdim = 3 ) #NEWLINE# f = function ( [ X ] , new_X ) #NEWLINE# return f ( input )