我正在尝试使用预先训练的模型。这是问题发生的地方
模型不是应该拍摄简单的彩色图像吗?为什么它期待 4 维输入?
eval()
方法是:
def eval(file):
image = io.imread(file)
plt.imshow(image)
image = cv2.resize(image, (160,40)).transpose((2,1,0))
output = model(torch.tensor(image[np.newaxis,...]).float())[0].squeeze().detach().numpy()
return decode_prob(output)
而eval('image.png')
的输出:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-66-0b951c2596f8> in <module>()
----> 1 eval('/content/image.png')
5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
344 _pair(0), self.dilation, self.groups)
345 return F.conv2d(input, weight, self.bias, self.stride,
--> 346 self.padding, self.dilation, self.groups)
347
348 def forward(self, input):
RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 4, 160, 40] to have 3 channels, but got 4 channels instead
您正在加载的图像是 4 通道图像。 您可以使用cv2读取它并将其转换为3通道RGB图像
def eval(file):
image = cv2.imread(file)
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGB)
plt.imshow(image)
image = cv2.resize(image, (160,40)).transpose((2,1,0))
output = model(torch.tensor(image[np.newaxis,...]).float())[0].squeeze().detach().numpy()
return decode_prob(output)