#NEWLINE# @ layer_register ( ) #NEWLINE# def ImageSample( inputs ) : #NEWLINE# #INDENT# '\n    Sample the template image, using the given coordinate, by bilinear interpolation.\n    It mimics the same behavior described in:\n    `Spatial Transformer Networks <http://arxiv.org/abs/1506.02025>`_.\n\n    :param input: [template, mapping]. template of shape NHWC. mapping of\n        shape NHW2, where each pair of the last dimension is a (y, x) real-value\n        coordinate.\n    :returns: a NHWC output tensor.\n    ' #NEWLINE# ( template , mapping ) = inputs #NEWLINE# assert ( ( template . get_shape ( ) . ndims == 4 ) and ( mapping . get_shape ( ) . ndims == 4 ) ) #NEWLINE# mapping = tf . maximum ( mapping , 0.0 ) #NEWLINE# lcoor = tf . cast ( mapping , tf . int32 ) #NEWLINE# ucoor = ( lcoor + 1 ) #NEWLINE# diff = ( mapping - tf . cast ( lcoor , tf . float32 ) ) #NEWLINE# neg_diff = ( 1.0 - diff ) #NEWLINE# ( lcoory , lcoorx ) = tf . split ( 3 , 2 , lcoor ) #NEWLINE# ( ucoory , ucoorx ) = tf . split ( 3 , 2 , ucoor ) #NEWLINE# lyux = tf . concat ( 3 , [ lcoory , ucoorx ] ) #NEWLINE# uylx = tf . concat ( 3 , [ ucoory , lcoorx ] ) #NEWLINE# ( diffy , diffx ) = tf . split ( 3 , 2 , diff ) #NEWLINE# ( neg_diffy , neg_diffx ) = tf . split ( 3 , 2 , neg_diff ) #NEWLINE# return tf . add_n ( [ ( ( sample ( template , lcoor ) * neg_diffx ) * neg_diffy ) , ( ( sample ( template , ucoor ) * diffx ) * diffy ) , ( ( sample ( template , lyux ) * neg_diffy ) * diffx ) , ( ( sample ( template , uylx ) * diffy ) * neg_diffx ) ] , name = 'sampled' )