Is there any differences between torch.nn.functional.interpolate and cv2.resize?

Han_Bing: <p>I pre-process my images like this:</p>
<pre><code class="lang-auto">def preprocess(self, img):

        if isinstance(img, str):
            img = cv2.imread(img)

        # Convert image from rgb to bgr for Compatible with original caffe model.
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img = img.transpose(2, 0, 1)
        img = torch.FloatTensor(img, device=self.device)
        img = func.imnormalize(img)
        img = img.unsqueeze(0)

        return img
</code></pre>
<p>I don’t want resize the images with opencv and convert them to Tensor. So I do resize operation like this:</p>
<pre><code class="lang-auto">import cv2
import numpy as np
import torch

def imnormalize(img):
    """
    Normalize pixel value from (0, 255) to (-1, 1) 
    """

    img = (img - 127.5) * 0.0078125
    return img

def denorm(img):
    img = img * 128  + 127.5
    return img

img = cv2.imread('images/office5.jpg')
cv2_img = cv2.resize(img, (1000, 1000))

img_tensor = torch.Tensor(img.transpose(2, 0, 1)).unsqueeze(0)
img_tensor = imnormalize(img_tensor)
img_tensor = torch.nn.functional.interpolate(img_tensor, size=(1000, 1000), mode='bilinear')
img_tensor = denorm(img_tensor)
img_tensor = img_tensor.squeeze().numpy().transpose(1, 2, 0)
img_tensor = np.round(img_tensor).astype(np.uint8)



cv2.imshow('cv2_img', cv2_img)
cv2.imshow('torch_img', img_tensor)
 
cv2.waitKey(0)
</code></pre>
<p>Is there any differences between torch.nn.functional.interpolate and cv2.resize?</p>

JuanFMontesinos: <p>Well, I’m not sure about what I’m gonna say but, when you resize an image, some filters are typically applied as some artifacts may appear.</p>
<p>For me cv2 resize sounds like a proper image resizing. Even if I have never explored it, torch interpolate sounds like an agnostic interpolation</p>
<p>If there is any difference, it will be this one.</p>

Han_Bing: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3" title="Screenshot%20from%202019-02-14%2009-08-34"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3_2_690x297.jpeg" alt="Screenshot%20from%202019-02-14%2009-08-34" data-base62-sha1="vmUNOi9LHmvZ2nveNr5aN7UkfTB" width="690" height="297" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3_2_690x297.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3_2_1035x445.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbda8c576838cb4bfcf255ea5745a0cb36b8a2f3_2_1380x594.jpeg 2x" data-dominant-color="7D7E51"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202019-02-14%2009-08-34</span><span class="informations">2498×1078 962 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
But the image looks like this. It seems that little differences between them.</p>

JuanFMontesinos: <p>It’s more probable that you find differences downsampling than upsampling <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Anyway you can plot the MSE to check it instead of just trying to manually find them</p>

Bairavi_Balaguru: <p>Hi<br>
This might help finding differences in resizing and interpolation methods across pytorch, opencv, PIL and tensorflow.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://zuru.tech/blog/the-dangers-behind-image-resizing">
  <header class="source">
      <img src="https://zuru.tech/favicon.png" class="site-icon" width="32" height="32">

      <a href="https://zuru.tech/blog/the-dangers-behind-image-resizing" target="_blank" rel="noopener nofollow ugc" title="08:00AM - 09 June 2024">Blog - Zuru Tech – 9 Jun 24</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/322;"><img src="https://zuru-blog-prod.s3.eu-south-1.amazonaws.com/The_dangers_behind_image_resizing_aea675eb68/The_dangers_behind_image_resizing_aea675eb68.png" class="thumbnail" width="690" height="323"></div>

<h3><a href="https://zuru.tech/blog/the-dangers-behind-image-resizing" target="_blank" rel="noopener nofollow ugc">The dangers behind image resizing</a></h3>

  <p>The expectation that the behaviour for image resizing is the same among the libraries can cause unforeseen problems...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>