#NEWLINE# @ staticmethod #NEWLINE# def numpy_max_pool_2d_stride_padding( x , ds , ignore_border = True , st = None , padding = ( 0 , 0 ) , mode = 'max' ) : #NEWLINE# #INDENT# pad_h = padding [ 0 ] #NEWLINE# pad_w = padding [ 1 ] #NEWLINE# h = x . shape [ ( - 2 ) ] #NEWLINE# w = x . shape [ ( - 1 ) ] #NEWLINE# assert ( ds [ 0 ] > pad_h ) #NEWLINE# assert ( ds [ 1 ] > pad_w ) #NEWLINE# def pad_img( x ) : #NEWLINE# #INDENT# y = numpy . zeros ( ( x . shape [ 0 ] , x . shape [ 1 ] , ( x . shape [ 2 ] + ( pad_h * 2 ) ) , ( x . shape [ 3 ] + ( pad_w * 2 ) ) ) , dtype = x . dtype ) #NEWLINE# y [ : , : , pad_h : ( x . shape [ 2 ] + pad_h ) , pad_w : ( x . shape [ 3 ] + pad_w ) ] = x #NEWLINE# return y #NEWLINE# #UNINDENT# img_rows = ( h + ( 2 * pad_h ) ) #NEWLINE# img_cols = ( w + ( 2 * pad_w ) ) #NEWLINE# out_r = ( ( ( img_rows - ds [ 0 ] ) // st [ 0 ] ) + 1 ) #NEWLINE# out_c = ( ( ( img_cols - ds [ 1 ] ) // st [ 1 ] ) + 1 ) #NEWLINE# out_shp = list ( x . shape [ : ( - 2 ) ] ) #NEWLINE# out_shp . append ( out_r ) #NEWLINE# out_shp . append ( out_c ) #NEWLINE# ( ds0 , ds1 ) = ds #NEWLINE# ( st0 , st1 ) = st #NEWLINE# output_val = numpy . zeros ( out_shp ) #NEWLINE# y = pad_img ( x ) #NEWLINE# func = numpy . max #NEWLINE# if ( mode == 'sum' ) : #NEWLINE# #INDENT# func = numpy . sum #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( mode != 'max' ) : #NEWLINE# #INDENT# func = numpy . average #NEWLINE# #UNINDENT# #UNINDENT# inc_pad = ( mode == 'average_inc_pad' ) #NEWLINE# for k in numpy . ndindex ( * x . shape [ : ( - 2 ) ] ) : #NEWLINE# #INDENT# for i in range ( output_val . shape [ ( - 2 ) ] ) : #NEWLINE# #INDENT# ii_st = ( i * st [ 0 ] ) #NEWLINE# ii_end = builtins . min ( ( ii_st + ds [ 0 ] ) , img_rows ) #NEWLINE# if ( not inc_pad ) : #NEWLINE# #INDENT# ii_st = builtins . max ( ii_st , pad_h ) #NEWLINE# ii_end = builtins . min ( ii_end , ( h + pad_h ) ) #NEWLINE# #UNINDENT# for j in range ( output_val . shape [ ( - 1 ) ] ) : #NEWLINE# #INDENT# jj_st = ( j * st [ 1 ] ) #NEWLINE# jj_end = builtins . min ( ( jj_st + ds [ 1 ] ) , img_cols ) #NEWLINE# if ( not inc_pad ) : #NEWLINE# #INDENT# jj_st = builtins . max ( jj_st , pad_w ) #NEWLINE# jj_end = builtins . min ( jj_end , ( w + pad_w ) ) #NEWLINE# #UNINDENT# patch = y [ k ] [ ii_st : ii_end , st0 : jj_end ] #NEWLINE# output_val [ k ] [ ( i , j ) ] = func ( patch ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return output_val