F::interpolate get wrong result using libtorch 1.71

LongLong: <p>Hi,<br>
while using libtorch 1.71 to resize feature, I found that <code>F::interpolate</code> would get the wrong result which is different with pytorch 1.71.</p>
<p>Here is my libtorch code.</p>
<pre><code>cv::Mat image = cv::imread("/home/lll/Pictures/test.jpg");


torch::Tensor image_tensor = torch::from_blob(image.data, {image.rows, image.cols, 3}, torch::kByte);

image_tensor = image_tensor.permute({2, 0, 1}).toType(torch::kFloat).div_(255);
image_tensor.sub_(0.5).div_(0.5);
image_tensor = image_tensor.unsqueeze(0);
image_tensor = image_tensor.to(torch::kCUDA);

namespace F = torch::nn::functional;
image_tensor = F::interpolate(
        image_tensor,
        F::InterpolateFuncOptions()
                .mode(torch::kBilinear)
                .size(std::vector&lt;int64_t&gt;({256, 256}))
                .align_corners(true)
);
image_tensor = image_tensor.mul(0.5).add(0.5).mul(255);
image_tensor = image_tensor.squeeze(0).permute({1, 2, 0}).toType(torch::kByte).to(torch::kCPU);

cv::Mat test_mat(256, 256, CV_8UC3);
std::memcpy((void *) test_mat.data, image_tensor.data_ptr(), sizeof(torch::kU8) * image_tensor.numel());
cv::imshow("test", test_mat);
cv::waitKey(0);
</code></pre>
<p>Here is my pytorch code.</p>
<pre><code>_transforms = transforms.Compose([
transforms.Normalize((0.5, 0.5, 0.5), (128 / 255., 128 / 255., 128 / 255.))])
image = cv2.imread("/home/lll/Pictures/test.jpg")

image = torch.from_numpy(image)
image = image.permute(2, 0, 1)
image = image.unsqueeze(0)
image = image.float()

image = _transforms(image)

image = F.interpolate(image, size=(256, 256), mode='bilinear', align_corners=True)
image = image.squeeze(0).permute(1, 2, 0)

image = image * 0.5 + 0.5
image = image.byte()
cv2.imshow("test", image.numpy())
cv2.waitKey(0)
</code></pre>
<p>I using opencv to visualize the result.<br>
This is the original image<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/3/a39e9e457a76e54426744666a7b5785ff3f24789.jpeg" alt="test" data-base62-sha1="nlrFgZGxu9hlLF7LCzzuwyitmOt" width="250" height="174"><br>
and this is the worng result.It seems to split the channels and merge them into a single image<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/2/92bb3a42e65dfa476aad500a3c5de14ac4782e8b.jpeg" alt="wrong" data-base62-sha1="kW2Qk4OB5dxh2K8tms4rOV15sKL" width="128" height="128"><br>
This is the correct result witch I want<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/7/37176209901fc701b16210120e0b04f4c4d99698.jpeg" alt="correct" data-base62-sha1="7RmmeA3RWk0N0u1cqqu8oYLTItG" width="128" height="128"></p>
<p><strong>Strangely enough, when I use Libtorch 1.4.0 with the same code,I could get the right results.</strong><br>
If you could help me I will appreciate it.</p>

ptrblck: <p>Based on the output it looks like the permutation failed in the libtorch example, but it looks correct to me (at least you are permuting it to channels-first, interpolate, and back to channels-last).</p>

LongLong: <p>Thank you for your reply,<br>
according to your hints I check the size of image before interpolate is (1,3,W,H), and the output feature size is (1,3,512,512), it looks the same on the pytorch side.<br>
Also, when I use <code>torch.jit.trace</code> and <code>save</code> to conver pytoch model to libtorch model which has <code>F.interpolate</code> in the model, the wrong result comes up again,I couldn’t solve it at all, because all model layers are compressed into a single file.</p>
<p>I suspect if there’s some problem with the interpolate function but I am not able to find the problem in the source code.<br>
When I went back to 1.4.0, all the errors were gone and everything was back to normal using the same code.</p>

LongLong: <p>Sloved! <a href="https://github.com/pytorch/pytorch/issues/52502#issuecomment-783003090" rel="noopener nofollow ugc">ref</a> thanks ngimel and ptrblck.</p>