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张量具有不同的值
基本上
torchvision.transforms.Resize()
默认情况下使用PIL.Image.BILINEAR
插值而在代码中,您只需使用
cv2.resize
,它不使用任何插值比如说
您将看到
b
和c
略有不同编辑:
实际上opencv文档说
但是是的,它不会给出与
PIL
相同的结果编辑2:
这也在文档中
显然
给出与
c
几乎相同的结果。但不幸的是,这些并不能回答这个问题相关问题 更多 >
编程相关推荐