pythonnumpy

Sampling numpy array with float indices (similar to pytorch grid_sample)


Is there some way of sampling a numpy array with float indices, using bilinear interpolation to get the intermediate values? For example, given the 1D array:

arr=np.array([0,1])

I would like arr[0.5] to return 0.5, since that index lies between 0 and 1. For a 2D example:

arr=np.array([[0,1],[2,3]])

arr[0.5, 0.5] should return 1.5. In pytorch this functionality is provided by torch.nn.grid_sample, I'd like to compare performance to doing this in numpy for my application.


Solution

  • Do not know if it can be achieved with pure numpy. Personally, I use Opencv remap function as an alternative to pytorch grid_sample. It has a python binding and supports numpy array.

    See OpenCV documentation on remap

    Edit: Scipy interp also looks good.

    Scipy interp2d