Libtorch: resize function

How can I resize a tensor to a smaller size in libtorch? such as {1, 3, 704, 704} -> {1, 3, 224, 224}. I have tried the tensor.resize_( {1, 3, 224, 224}) method. But I found that it just returned a small region(224x224) of original image. please help me . thanks.

You probably want interpolation?

Best regards


Yes, I want to do the down sampling。Does libtorch have any down sampling method? thanks

Yes, what is it that interpolate is lacking for your purpose?

a = torch.arange(1_486_848.).view(1, 3, 704, 704)
b = torch.nn.functional.interpolate(a, size=(224, 224))

Best regards


thanks. But I am using libtorch with C++ API. I cannot find the interpolate fuction in libtorch.

I find the torch::grid_sampler() in libtorch. And it works! Thanks for your help。 :)



could you share the code snippet for grid_sampler?


I was in trouble because I couldn’t find an API when I wanted to interpolate with C ++. I was more intuitive and easy to use upsample_bilinear2d in API library near interpolate.

at::Tensor masks_a = at::upsample_bilinear2d(masks.unsqueeze(0), {img_size, img_size}, false);

torch::nn::functional::interpolate() should be what you want.