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)
cv2.waitKey(0)
Is there any differences between torch.nn.functional.interpolate and cv2.resize?