The CNN model takes an image tensor of size (112x112) as input and gives (1x512) size tensor as output.
Using Opencv function cv2.resize() or using Transform.resize in pytorch to resize the input to (112x112) gives different outputs.
What's the reason for this? (I understand that the difference in the underlying implementation of opencv resizing vs torch resizing might be a cause for this, But I'd like to have a detailed understanding of it)
import cv2
import numpy as np
from PIL import image
import torch
import torchvision
from torchvision import transforms as trans
# device for pytorch
device = torch.device('cuda:0')
torch.set_default_tensor_type('torch.cuda.FloatTensor')
model = torch.jit.load("traced_facelearner_model_new.pt")
model.eval()
# read the example image used for tracing
image=cv2.imread("videos/example.jpg")
test_transform = trans.Compose([
trans.ToTensor(),
trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
test_transform2 = trans.Compose([
trans.Resize([int(112), int(112)]),
trans.ToTensor(),
trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
resized_image = cv2.resize(image, (112, 112))
tensor1 = test_transform(resized_image).to(device).unsqueeze(0)
tensor2 = test_transform2(Image.fromarray(image)).to(device).unsqueeze(0)
output1 = model(tensor1)
output2 = model(tensor2)
The output1 and output2 tensors have different values.