<s> """<STR_LIT>""" <EOL> import numpy <EOL> import scipy <EOL> from scipy import sparse as scipy_sparse <EOL> from six . moves import xrange <EOL> import theano <EOL> import theano . sparse <EOL> from theano import sparse , gof , Op , tensor <EOL> from theano . sparse . basic import Remove0 , remove0 <EOL> from theano . sparse import ( <EOL> SpSum , sp_sum , <EOL> ColScaleCSC , RowScaleCSC , col_scale , row_scale , <EOL> Diag , diag , SquareDiagonal , square_diagonal , <EOL> EnsureSortedIndices , ensure_sorted_indices , clean ) <EOL> def register_specialize ( lopt , * tags , ** kwargs ) : <EOL> theano . compile . optdb [ '<STR_LIT>' ] . register ( <EOL> ( kwargs and kwargs . pop ( '<STR_LIT:name>' ) ) or lopt . __name__ , lopt , '<STR_LIT>' , <EOL> * tags ) <EOL> class ConvolutionIndices ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> @ staticmethod <EOL> def sparse_eval ( inshp , kshp , nkern , strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) , mode = '<STR_LIT>' ) : <EOL> ( dx , dy ) = strides <EOL> return convolution_indices . evaluate ( inshp , kshp , ( dx , dy ) , <EOL> nkern , mode = mode , ws = False ) <EOL> @ staticmethod <EOL> def conv_eval ( inshp , kshp , strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) , mode = '<STR_LIT>' ) : <EOL> ( dx , dy ) = strides <EOL> return convolution_indices . evaluate ( inshp , kshp , ( dx , dy ) , <EOL> mode = mode , ws = True ) <EOL> @ staticmethod <EOL> def evaluate ( inshp , kshp , strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) , nkern = <NUM_LIT:1> , mode = '<STR_LIT>' , ws = True ) : <EOL> """<STR_LIT>""" <EOL> ( dx , dy ) = strides <EOL> N = numpy <EOL> if N . size ( inshp ) == <NUM_LIT:2> : <EOL> inshp = ( <NUM_LIT:1> , ) + inshp <EOL> inshp = N . array ( inshp ) <EOL> kshp = N . array ( kshp ) <EOL> ksize = N . prod ( kshp ) <EOL> kern = ksize - <NUM_LIT:1> - N . arange ( ksize ) <EOL> fulloutshp = inshp [ <NUM_LIT:1> : ] + kshp - <NUM_LIT:1> <EOL> if mode == '<STR_LIT>' : <EOL> s = - <NUM_LIT:1> <EOL> else : <EOL> s = <NUM_LIT:1> <EOL> outshp = N . int64 ( N . ceil ( ( inshp [ <NUM_LIT:1> : ] + s * kshp - s * <NUM_LIT:1> ) / N . array ( [ dy , dx ] , dtype = '<STR_LIT:float>' ) ) ) <EOL> if any ( outshp <= <NUM_LIT:0> ) : <EOL> err = '<STR_LIT>' , kshp , '<STR_LIT>' , ( dx , dy ) , '<STR_LIT>' , inshp <EOL> raise ValueError ( err ) <EOL> outsize = N . prod ( outshp ) <EOL> insize = N . prod ( inshp ) <EOL> if mode == '<STR_LIT>' : <EOL> lbound = N . array ( [ kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> , kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> ] ) <EOL> ubound = lbound + ( inshp [ <NUM_LIT:1> : ] - kshp + <NUM_LIT:1> ) <EOL> else : <EOL> lbound = N . zeros ( <NUM_LIT:2> ) <EOL> ubound = fulloutshp <EOL> topleft = N . array ( [ kshp [ <NUM_LIT:0> ] - <NUM_LIT:1> , kshp [ <NUM_LIT:1> ] - <NUM_LIT:1> ] ) <EOL> botright = topleft + inshp [ <NUM_LIT:1> : ] <EOL> if ws : <EOL> spmatshp = ( outsize * N . prod ( kshp ) * inshp [ <NUM_LIT:0> ] , insize ) <EOL> else : <EOL> spmatshp = ( nkern * outsize , insize ) <EOL> spmat = scipy_sparse . lil_matrix ( spmatshp ) <EOL> z , zz = <NUM_LIT:0> , <NUM_LIT:0> <EOL> tapi , ntaps = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for fmapi in xrange ( inshp [ <NUM_LIT:0> ] ) : <EOL> for n in xrange ( nkern ) : <EOL> for oy in N . arange ( lbound [ <NUM_LIT:0> ] , ubound [ <NUM_LIT:0> ] , dy ) : <EOL> for ox in N . arange ( lbound [ <NUM_LIT:1> ] , ubound [ <NUM_LIT:1> ] , dx ) : <EOL> l = <NUM_LIT:0> <EOL> for ky in oy + N . arange ( kshp [ <NUM_LIT:0> ] ) : <EOL> for kx in ox + N . arange ( kshp [ <NUM_LIT:1> ] ) : <EOL> if ( all ( ( ky , kx ) >= topleft ) and <EOL> all ( ( ky , kx ) < botright ) ) : <EOL> iy , ix = N . array ( ( ky , kx ) ) - topleft <EOL> col = iy * inshp [ <NUM_LIT:2> ] + ix + fmapi * N . prod ( inshp [ <NUM_LIT:1> : ] ) <EOL> if mode == '<STR_LIT>' : <EOL> ( y , x ) = ( oy , ox ) <EOL> else : <EOL> ( y , x ) = ( oy , ox ) - topleft <EOL> ( y , x ) = N . array ( [ y , x ] ) / ( dy , dx ) <EOL> if ws : <EOL> row = ( ( y * outshp [ <NUM_LIT:1> ] + x ) * <EOL> inshp [ <NUM_LIT:0> ] * ksize + l + fmapi * <EOL> ksize ) <EOL> else : <EOL> row = y * outshp [ <NUM_LIT:1> ] + x <EOL> spmat [ row + n * outsize , col ] = tapi + <NUM_LIT:1> <EOL> ntaps += <NUM_LIT:1> <EOL> tapi += <NUM_LIT:1> <EOL> l += <NUM_LIT:1> <EOL> if spmat . format != '<STR_LIT>' : <EOL> spmat = spmat . tocsc ( ) . sorted_indices ( ) <EOL> else : <EOL> spmat = spmat . sorted_indices ( ) <EOL> if ws : <EOL> kmap = None <EOL> else : <EOL> kmap = N . zeros ( ntaps , dtype = '<STR_LIT:int>' ) <EOL> k = <NUM_LIT:0> <EOL> for j in xrange ( spmat . shape [ <NUM_LIT:1> ] ) : <EOL> for i_idx in xrange ( spmat . indptr [ j ] , spmat . indptr [ j + <NUM_LIT:1> ] ) : <EOL> if spmat . data [ i_idx ] != <NUM_LIT:0> : <EOL> kmap [ k ] = spmat . data [ i_idx ] - <NUM_LIT:1> <EOL> k += <NUM_LIT:1> <EOL> assert spmat . format == '<STR_LIT>' <EOL> sptype = '<STR_LIT>' <EOL> if <NUM_LIT:0> and mode == '<STR_LIT>' : <EOL> spmat = spmat . tocsr ( ) <EOL> rval = ( spmat . indices [ : spmat . size ] , <EOL> spmat . indptr , spmatshp , sptype , outshp ) <EOL> if kmap is not None : <EOL> rval += ( kmap , ) <EOL> return rval <EOL> def perform ( self , node , inputs , outputs ) : <EOL> ( inshp , kshp ) = inputs <EOL> ( out_indices , out_indptr , spmat_shape ) = outputs <EOL> indices , indptr , spmatshp , outshp = self . evaluate ( inshp , kshp ) <EOL> out_indices [ <NUM_LIT:0> ] = indices <EOL> out_indptr [ <NUM_LIT:0> ] = indptr <EOL> spmat_shape [ <NUM_LIT:0> ] = numpy . asarray ( spmatshp ) <EOL> convolution_indices = ConvolutionIndices ( ) <EOL> def applySparseFilter ( kerns , kshp , nkern , images , imgshp , <EOL> step = ( <NUM_LIT:1> , <NUM_LIT:1> ) , bias = None , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if numpy . size ( imgshp ) == <NUM_LIT:2> : <EOL> imgshp = ( <NUM_LIT:1> , ) + imgshp <EOL> indices , indptr , spmat_shape , sptype , outshp , kmap = convolution_indices . sparse_eval ( imgshp , kshp , nkern , step , mode ) <EOL> sparsew = theano . sparse . CSM ( sptype , kmap ) ( kerns , indices , <EOL> indptr , spmat_shape ) <EOL> output = sparse . structured_dot ( sparsew , images . T ) . T <EOL> if bias is not None : <EOL> output += bias <EOL> return output , numpy . hstack ( ( nkern , outshp ) ) <EOL> def convolve ( kerns , kshp , nkern , images , imgshp , step = ( <NUM_LIT:1> , <NUM_LIT:1> ) , bias = None , <EOL> mode = '<STR_LIT>' , flatten = True ) : <EOL> """<STR_LIT>""" <EOL> N = numpy <EOL> kern_size = N . int64 ( N . prod ( kshp ) ) <EOL> if N . size ( imgshp ) == <NUM_LIT:2> : <EOL> imgshp = ( <NUM_LIT:1> , ) + imgshp <EOL> indices , indptr , spmat_shape , sptype , outshp = convolution_indices . conv_eval ( imgshp , kshp , step , mode ) <EOL> csc = theano . sparse . CSM ( sptype ) ( N . ones ( indices . size ) , indices , <EOL> indptr , spmat_shape ) <EOL> patches = ( sparse . structured_dot ( csc , images . T ) ) . T <EOL> pshape = tensor . stack ( [ images . shape [ <NUM_LIT:0> ] * tensor . as_tensor ( N . prod ( outshp ) ) , tensor . as_tensor ( imgshp [ <NUM_LIT:0> ] * kern_size ) ] ) <EOL> patch_stack = tensor . reshape ( patches , pshape , ndim = <NUM_LIT:2> ) <EOL> output = tensor . dot ( patch_stack , kerns . T ) <EOL> if bias is not None : <EOL> output += bias <EOL> newshp = tensor . stack ( [ images . shape [ <NUM_LIT:0> ] , tensor . as_tensor ( N . prod ( outshp ) ) , tensor . as_tensor ( nkern ) ] ) <EOL> tensout = tensor . reshape ( output , newshp , ndim = <NUM_LIT:3> ) <EOL> output = tensor . DimShuffle ( ( False , ) * tensout . ndim , ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) ) ( tensout ) <EOL> if flatten : <EOL> output = tensor . flatten ( output , <NUM_LIT:2> ) <EOL> return output , N . hstack ( ( nkern , outshp ) ) <EOL> def max_pool ( images , imgshp , maxpoolshp ) : <EOL> """<STR_LIT>""" <EOL> N = numpy <EOL> poolsize = N . int64 ( N . prod ( maxpoolshp ) ) <EOL> if N . size ( imgshp ) == <NUM_LIT:2> : <EOL> imgshp = ( <NUM_LIT:1> , ) + imgshp <EOL> indices , indptr , spmat_shape , sptype , outshp = convolution_indices . conv_eval ( imgshp , maxpoolshp , <EOL> maxpoolshp , mode = '<STR_LIT>' ) <EOL> csc = theano . sparse . CSM ( sptype ) ( N . ones ( indices . size ) , indices , <EOL> indptr , spmat_shape ) <EOL> patches = sparse . structured_dot ( csc , images . T ) . T <EOL> pshape = tensor . stack ( [ images . shape [ <NUM_LIT:0> ] * tensor . as_tensor ( N . prod ( outshp ) ) , <EOL> tensor . as_tensor ( imgshp [ <NUM_LIT:0> ] ) , <EOL> tensor . as_tensor ( poolsize ) ] ) <EOL> patch_stack = tensor . reshape ( patches , pshape , ndim = <NUM_LIT:3> ) <EOL> out1 = tensor . max ( patch_stack , axis = <NUM_LIT:2> ) <EOL> pshape = tensor . stack ( [ images . shape [ <NUM_LIT:0> ] , <EOL> tensor . as_tensor ( N . prod ( outshp ) ) , <EOL> tensor . as_tensor ( imgshp [ <NUM_LIT:0> ] ) ] ) <EOL> out2 = tensor . reshape ( out1 , pshape , ndim = <NUM_LIT:3> ) <EOL> out3 = tensor . DimShuffle ( out2 . broadcastable , ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) ) ( out2 ) <EOL> return tensor . flatten ( out3 , <NUM_LIT:2> ) , outshp </s>
