Using torch::grid_sampler_2d from C++ API

Hi,

I’m using the C+±API to implement an optical flow method.

I successfully implemented a convolutional network for computing optical flow on very low-resolution (24x32) image, I can scale that flow and I want to warp the image so I can continue estimating optical flow on a higher-resolution level.

I can’t find the values for the interpolation mode and border mode, neither in the docs nor in the code and this doesn’t compile:
torch::grid_sampler_2d(img, flow, torch::kBilinear, torch::kBorder, true);

So, where can I find the correct constants?

Does the equivalent Python code uses torch.nn.functional.grid_sample? We can look at this for how to map it to the equivalent C++ code: https://github.com/pytorch/pytorch/blob/4baadd54d7c28eac0a2fa07686d593a376e6bd5f/test/cpp/api/functional.cpp#L440-L502.

1 Like

Thank you for your reply. Seems like grid_sampler_2d is more like a low-level function, not to be used by “normal” users. I use grid_sample now and it works as expected:

namespace F = torch::nn::functional;
F::grid_sample(img1_t, map_t, F::GridSampleFuncOptions()
.mode(torch::kBilinear)
.padding_mode(torch::kBorder)
.align_corners(true));

1 Like