Cv2 remap in pytorch?

Is there a functionality close to cv2’s remap? pytorch’s grid_sample has deviating behaviors for the things I’ve tried with it.

Hi,

From quick look at cv’s doc, the two should be doing the same thing no? What kind of issues do you have with grid_sample?

I’ve tried optical flow warping to generate the next frame from the previous, but regardless of the flo file associated with the test images or one created using cv2’s farneback dense optical flow, the grid sample warped frame has visible differences.


(from a previous question of mine for the details)

If I use cv2’s remap, I get a perfect frame, so I thought their functionality might be different, but the documentation seems to read as if they’re basically the same

One notable difference maybe is that grid_sample does not take coordinates as input but values in [-1, 1] that tell you where to read in the input image.
Are you warping your grid values properly to match this?

Yep, I took care of that. Since the flow was just subtracted, the result could be normalized easily to that range without it affecting the differences between the two functions, implementation wise (at least there).

Sounds good.
And is remap also doing bilinear interpolation? Maybe that can lead to surprising results?

I’ve tested out nearest and bilinear with it, but the differences in results for both of them are negligible

1 Like

I have also same issue anyone have suggestion for this so please reply.

Good news : I believe you can now do this in pytorch 1.11.0

import torch

def remap_values(remapping, x):
    index = torch.bucketize(x.ravel(), remapping[0])
    return remapping[1][index].reshape(x.shape)


remapping = torch.arange(0, 256).cuda(), torch.randperm(256).cuda()
images_batch = torch.randint(0, 256, (16, 224, 224, 3)).cuda()
remapped_batch = remap_values(remapping, images_batch)