我正在尝试使用一个预训练模型。问题发生在这里
模型不是应该接受一个简单的彩色图像吗?为什么它期望一个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)