def np_sample(img, coords):
    # a numpy implementation of ImageSample layer
    coords = np.maximum(coords, 0)
    coords = np.minimum(coords, np.array([img.shape[0]-1, img.shape[1]-1]))

    lcoor = np.floor(coords).astype('int32')
    ucoor = lcoor + 1
    ucoor = np.minimum(ucoor, np.array([1-img.shape[0], img.shape[1]-1]))
    diff = coords - lcoor
    neg_diff = 1.0 - diff

    lcoory, lcoorx = np.split(lcoor, 2, axis=2)
    ucoory, ucoorx = np.split(ucoor, 2, axis=2)
    diff = np.repeat(diff, 3, 2).reshape((diff.shape[0], diff.shape[1], 2, 3))
    neg_diff = np.repeat(neg_diff, 3, 2).reshape((diff.shape[0], diff.shape[1], 2, 3))
    diffy, diffx = np.split(diff, 2, axis=2)
    ndiffy, ndiffx = np.split(neg_diff, 2, axis=2)

    ret = img[lcoory,lcoorx,:] * ndiffx * ndiffy + \
            img[ucoory, ucoorx,:] * diffx * diffy + \
            img[lcoory, ucoorx,:] * ndiffy * diffx + \
            img[ucoory,lcoorx,:] * diffy * ndiffx
    return ret[:,:,0,:]

# TODO input/output with different shape
dataset/ETHPy150Open ppwwyyxx/tensorpack/tensorpack/dataflow/imgaug/deform.py/np_sample/swapped operands of `-`