def random_shift(x, wrg, hrg, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest', cval=0.0):
 (h, w) = (x.shape[row_axis], x.shape[col_axis])
 tx = (np.random.uniform((- hrg), hrg) * h)
 ty = (np.random.uniform((- wrg), wrg) * w)
 translation_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]])
 transform_matrix = translation_matrix
 x = apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
 return x

def random_shift ( x , wrg , hrg , row_axis = 1 , col_axis = 2 , channel_axis = 0 , fill_mode = 'nearest' , cval = 0.0 ) : ( h , w ) = ( x . shape [ row_axis ] , x . shape [ col_axis ] ) tx = ( np . random . uniform ( ( - hrg ) , hrg ) * h ) ty = ( np . random . uniform ( ( - wrg ) , wrg ) * w ) translation_matrix = np . array ( [ [ 1 , 0 , tx ] , [ 0 , 1 , ty ] , [ 0 , 0 , 1 ] ] ) transform_matrix = translation_matrix x = apply_transform ( x , transform_matrix , channel_axis , fill_mode , cval ) return x
performs a random spatial shift of a numpy image tensor .