<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import numpy <EOL> import theano <EOL> from theano import Op , Apply <EOL> import theano . tensor as T <EOL> from theano . gradient import grad_not_implemented <EOL> from theano . gradient import grad_undefined <EOL> class Images2Neibs ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __init__ ( self , mode = '<STR_LIT>' ) : <EOL> if mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . mode = mode <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ + "<STR_LIT>" % self . mode <EOL> def __setstate__ ( self , d ) : <EOL> self . __dict__ . update ( d ) <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mode = '<STR_LIT>' <EOL> def make_node ( self , ten4 , neib_shape , neib_step = None ) : <EOL> """<STR_LIT>""" <EOL> ten4 = T . as_tensor_variable ( ten4 ) <EOL> neib_shape = T . as_tensor_variable ( neib_shape ) <EOL> if neib_step is None : <EOL> neib_step = neib_shape <EOL> else : <EOL> neib_step = T . as_tensor_variable ( neib_step ) <EOL> assert ten4 . ndim == <NUM_LIT:4> <EOL> assert neib_shape . ndim == <NUM_LIT:1> <EOL> assert neib_step . ndim == <NUM_LIT:1> <EOL> return Apply ( self , [ ten4 , neib_shape , neib_step ] , <EOL> [ T . matrix ( dtype = ten4 . type . dtype ) ] ) <EOL> def grad ( self , inp , grads ) : <EOL> x , neib_shape , neib_step = inp <EOL> gz , = grads <EOL> if self . mode in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if ( neib_shape is neib_step or <EOL> neib_shape == neib_step or <EOL> ( hasattr ( neib_shape , "<STR_LIT>" ) and <EOL> neib_shape . equals ( neib_step ) ) ) : <EOL> return [ neibs2images ( gz , neib_shape , x . shape , mode = self . mode ) , <EOL> grad_undefined ( self , <NUM_LIT:1> , neib_shape ) , <EOL> grad_undefined ( self , <NUM_LIT:2> , neib_step ) ] <EOL> if self . mode in [ '<STR_LIT>' ] : <EOL> def pos2map ( pidx , pgz , prior_result , neib_shape , neib_step ) : <EOL> '''<STR_LIT>''' <EOL> nrows , ncols = neib_shape <EOL> rstep , cstep = neib_step <EOL> batch_size , num_channels , rows , cols = prior_result . shape <EOL> i = pidx // ncols <EOL> j = pidx - ( i * ncols ) <EOL> result_indices = prior_result [ : , : , <EOL> i : ( rows - nrows + i + <NUM_LIT:1> ) : rstep , <EOL> j : ( cols - ncols + j + <NUM_LIT:1> ) : cstep ] <EOL> newshape = ( batch_size , num_channels ) + ( ( rows - nrows ) // rstep + <NUM_LIT:1> , ) + ( ( cols - ncols ) // cstep + <NUM_LIT:1> , ) <EOL> return T . inc_subtensor ( result_indices , pgz . reshape ( newshape ) ) <EOL> indices = T . arange ( neib_shape [ <NUM_LIT:0> ] * neib_shape [ <NUM_LIT:1> ] ) <EOL> pgzs = gz . dimshuffle ( ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> result , _ = theano . scan ( fn = pos2map , <EOL> sequences = [ indices , pgzs ] , <EOL> outputs_info = T . zeros ( x . shape ) , <EOL> non_sequences = [ neib_shape , neib_step ] ) <EOL> grad_input = result [ - <NUM_LIT:1> ] <EOL> return [ grad_input , <EOL> grad_undefined ( self , <NUM_LIT:1> , neib_shape ) , <EOL> grad_undefined ( self , <NUM_LIT:2> , neib_step ) ] <EOL> return [ grad_not_implemented ( self , <NUM_LIT:0> , x ) , <EOL> grad_undefined ( self , <NUM_LIT:1> , neib_shape ) , <EOL> grad_undefined ( self , <NUM_LIT:2> , neib_step ) ] <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:5> , ) <EOL> def perform ( self , node , inp , out_ ) : <EOL> ten4 , neib_shape , neib_step = inp <EOL> z , = out_ <EOL> if type ( self ) != Images2Neibs : <EOL> raise theano . gof . utils . MethodNotDefined ( ) <EOL> def CEIL_INTDIV ( a , b ) : <EOL> if a % b : <EOL> return ( a // b ) + <NUM_LIT:1> <EOL> else : <EOL> return a // b <EOL> grid_c = - <NUM_LIT:1> <EOL> grid_d = - <NUM_LIT:1> <EOL> assert ten4 . ndim == <NUM_LIT:4> <EOL> assert neib_shape . ndim == <NUM_LIT:1> <EOL> assert neib_shape . shape [ <NUM_LIT:0> ] == <NUM_LIT:2> <EOL> assert neib_step . ndim == <NUM_LIT:1> <EOL> assert neib_step . shape [ <NUM_LIT:0> ] == <NUM_LIT:2> <EOL> c , d = neib_shape <EOL> step_x , step_y = neib_step <EOL> mode = self . mode <EOL> if mode == "<STR_LIT>" : <EOL> if ( c % <NUM_LIT:2> != <NUM_LIT:1> ) or ( d % <NUM_LIT:2> != <NUM_LIT:1> ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if ( ten4 . shape [ <NUM_LIT:2> ] < c ) or ( ten4 . shape [ <NUM_LIT:3> ] < d ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( c , d , ten4 . shape [ <NUM_LIT:2> ] , ten4 . shape [ <NUM_LIT:3> ] ) ) <EOL> grid_c = CEIL_INTDIV ( ten4 . shape [ <NUM_LIT:2> ] , step_x ) <EOL> grid_d = CEIL_INTDIV ( ten4 . shape [ <NUM_LIT:3> ] , step_y ) <EOL> elif mode == "<STR_LIT>" : <EOL> if ( ten4 . shape [ <NUM_LIT:2> ] < c ) or ( ( ( ten4 . shape [ <NUM_LIT:2> ] - c ) % step_x ) != <NUM_LIT:0> ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( c , step_x , ten4 . shape [ <NUM_LIT:2> ] ) ) <EOL> if ( ten4 . shape [ <NUM_LIT:3> ] < d ) or ( ( ( ten4 . shape [ <NUM_LIT:3> ] - d ) % step_y ) != <NUM_LIT:0> ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( d , step_y , ten4 . shape [ <NUM_LIT:3> ] ) ) <EOL> grid_c = <NUM_LIT:1> + ( ( ten4 . shape [ <NUM_LIT:2> ] - c ) // step_x ) <EOL> grid_d = <NUM_LIT:1> + ( ( ten4 . shape [ <NUM_LIT:3> ] - d ) // step_y ) <EOL> elif mode == "<STR_LIT>" : <EOL> grid_c = <NUM_LIT:1> + ( ( ten4 . shape [ <NUM_LIT:2> ] - c ) // step_x ) <EOL> grid_d = <NUM_LIT:1> + ( ( ten4 . shape [ <NUM_LIT:3> ] - d ) // step_y ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % mode ) <EOL> z_dim0 = grid_c * grid_d * ten4 . shape [ <NUM_LIT:1> ] * ten4 . shape [ <NUM_LIT:0> ] <EOL> z_dim1 = c * d <EOL> z [ <NUM_LIT:0> ] = numpy . empty ( ( z_dim0 , z_dim1 ) , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> nb_batch = ten4 . shape [ <NUM_LIT:0> ] <EOL> nb_stack = ten4 . shape [ <NUM_LIT:1> ] <EOL> height = ten4 . shape [ <NUM_LIT:2> ] <EOL> width = ten4 . shape [ <NUM_LIT:3> ] <EOL> wrap_centered_idx_shift_x = c // <NUM_LIT:2> <EOL> wrap_centered_idx_shift_y = d // <NUM_LIT:2> <EOL> for n in range ( nb_batch ) : <EOL> for s in range ( nb_stack ) : <EOL> for a in range ( grid_c ) : <EOL> for b in range ( grid_d ) : <EOL> z_row = b + grid_d * ( a + grid_c * ( s + nb_stack * n ) ) <EOL> for i in range ( c ) : <EOL> ten4_2 = i + a * step_x <EOL> if mode == "<STR_LIT>" : <EOL> ten4_2 -= wrap_centered_idx_shift_x <EOL> if ten4_2 < <NUM_LIT:0> : <EOL> ten4_2 += height <EOL> elif ten4_2 >= height : <EOL> ten4_2 -= height <EOL> for j in range ( d ) : <EOL> ten4_3 = j + b * step_y <EOL> if mode == "<STR_LIT>" : <EOL> ten4_3 -= wrap_centered_idx_shift_y <EOL> if ten4_3 < <NUM_LIT:0> : <EOL> ten4_3 += width <EOL> elif ten4_3 >= width : <EOL> ten4_3 -= width <EOL> z_col = j + d * i <EOL> z [ <NUM_LIT:0> ] [ z_row , z_col ] = ten4 [ n , s , ten4_2 , ten4_3 ] <EOL> def infer_shape ( self , node , input_shape ) : <EOL> in_shape = input_shape [ <NUM_LIT:0> ] <EOL> c , d = node . inputs [ <NUM_LIT:1> ] <EOL> step_x , step_y = node . inputs [ <NUM_LIT:2> ] <EOL> if self . mode == '<STR_LIT>' : <EOL> grid_c = T . ceil_intdiv ( in_shape [ <NUM_LIT:2> ] , step_x ) <EOL> grid_d = T . ceil_intdiv ( in_shape [ <NUM_LIT:3> ] , step_y ) <EOL> elif self . mode == '<STR_LIT>' : <EOL> grid_c = <NUM_LIT:1> + ( ( in_shape [ <NUM_LIT:2> ] - c ) // step_x ) <EOL> grid_d = <NUM_LIT:1> + ( ( in_shape [ <NUM_LIT:3> ] - d ) // step_y ) <EOL> elif self . mode == '<STR_LIT>' : <EOL> grid_c = <NUM_LIT:1> + ( ( in_shape [ <NUM_LIT:2> ] - c ) // step_x ) <EOL> grid_d = <NUM_LIT:1> + ( ( in_shape [ <NUM_LIT:3> ] - d ) // step_y ) <EOL> z_dim0 = grid_c * grid_d * in_shape [ <NUM_LIT:1> ] * in_shape [ <NUM_LIT:0> ] <EOL> z_dim1 = c * d <EOL> return [ ( z_dim0 , z_dim1 ) ] <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> ten4 , neib_shape , neib_step = inp <EOL> z , = out <EOL> fail = sub [ '<STR_LIT>' ] <EOL> mode = self . mode <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def images2neibs ( ten4 , neib_shape , neib_step = None , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return Images2Neibs ( mode ) ( ten4 , neib_shape , neib_step ) <EOL> def neibs2images ( neibs , neib_shape , original_shape , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> neibs = T . as_tensor_variable ( neibs ) <EOL> neib_shape = T . as_tensor_variable ( neib_shape ) <EOL> original_shape = T . as_tensor_variable ( original_shape ) <EOL> new_neib_shape = T . stack ( [ original_shape [ - <NUM_LIT:1> ] // neib_shape [ <NUM_LIT:1> ] , <EOL> neib_shape [ <NUM_LIT:1> ] ] ) <EOL> output_2d = images2neibs ( neibs . dimshuffle ( '<STR_LIT:x>' , '<STR_LIT:x>' , <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> new_neib_shape , mode = mode ) <EOL> if mode == '<STR_LIT>' : <EOL> valid_shape = list ( original_shape ) <EOL> valid_shape [ <NUM_LIT:2> ] = ( valid_shape [ <NUM_LIT:2> ] // neib_shape [ <NUM_LIT:0> ] ) * neib_shape [ <NUM_LIT:0> ] <EOL> valid_shape [ <NUM_LIT:3> ] = ( valid_shape [ <NUM_LIT:3> ] // neib_shape [ <NUM_LIT:1> ] ) * neib_shape [ <NUM_LIT:1> ] <EOL> output_4d = output_2d . reshape ( valid_shape ) <EOL> for d in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> pad_shape = list ( output_4d . shape ) <EOL> pad_shape [ d ] = original_shape [ d ] - valid_shape [ d ] <EOL> output_4d = T . concatenate ( [ output_4d , T . zeros ( pad_shape ) ] , axis = d ) <EOL> elif mode == '<STR_LIT>' : <EOL> output_4d = output_2d . reshape ( original_shape ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % mode ) <EOL> return output_4d </s>
