Accessing an image tensor with floating point indices

Hello everyone !

I have an image tensor of dimension (batch x channels x height x width) and a floating point index (x’, y’).

Is there a way to access the corresponding interpolated pixel value in the image? i.e. I would like to do something like the following.

bs, nc, h, w = 1, 3, 256, 256
image_tensor = torch.randn(bs, nc, h, w)
x, y = 5.6, 128.4
interpolated_pixel_value = image_tensor[0, :, y, x]

Essentially, I would like to do a texture look up with floating point co-ordinates in an “auto-diff”'able way. I looked up nn.Embedding, but it seems to me that it supports only integer indices.

Sincerely appreciate your time & help ! :slight_smile:

I think grid_sample might work for your use case. :slight_smile:

1 Like

That works perfectly. Thank you so much !

1 Like