RuntimeError: 给定groups=1,权重大小为[32, 3, 3, 3],期望输入[1, 4, 160, 40]有3个通道,但实际得到了4个通道

我正在尝试使用一个预训练模型。问题发生在这里

模型不是应该接受一个简单的彩色图像吗?为什么它期望一个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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注