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

I pre-process my images like this:

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

I don’t want resize the images with opencv and convert them to Tensor. So I do resize operation like this:

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)

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

1 Like

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.

For me cv2 resize sounds like a proper image resizing. Even if I have never explored it, torch interpolate sounds like an agnostic interpolation

If there is any difference, it will be this one.

But the image looks like this. It seems that little differences between them.

1 Like

It’s more probable that you find differences downsampling than upsampling :slight_smile:

Anyway you can plot the MSE to check it instead of just trying to manually find them