torch transform.resize()与cv2.resize()的比较

2024-09-21 08:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

CNN模型采用大小为(112x112)的图像张量作为输入,并给出大小为(1x512)的张量作为输出

在pytorch中使用Opencv函数cv2.resize()或使用Transform.resize来调整输入到(112x112)的大小,会给出不同的输出

这是什么原因?(我理解opencv调整大小与torch调整大小的底层实现的差异可能是造成这种情况的原因,但我想对此有一个详细的了解)

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)

output1和output2张量具有不同的值


Tags: fromtestimageimporttransformodeldevice
1条回答
网友
1楼 · 发布于 2024-09-21 08:37:21

基本上torchvision.transforms.Resize()默认情况下使用PIL.Image.BILINEAR插值

而在代码中,您只需使用cv2.resize,它不使用任何插值

比如说

import cv2
from PIL import Image
import numpy as np

a = cv2.imread('videos/example.jpg')
b = cv2.resize(a, (112, 112))
c = np.array(Image.fromarray(a).resize((112, 112), Image.BILINEAR))

您将看到bc略有不同

编辑:

实际上opencv文档说

INTER_LINEAR - a bilinear interpolation (used by default)

但是是的,它不会给出与PIL相同的结果

编辑2:

这也在文档中

To shrink an image, it will generally look best with INTER_AREA interpolation

显然

d = cv2.resize(a, (112, 112), interpolation=cv2.INTER_AREA)

给出与c几乎相同的结果。但不幸的是,这些并不能回答这个问题

相关问题 更多 >

    热门问题