“运行时错误:期望4维输入用于4维权重32 3 3,但得到的是大小为[3, 224, 224]的3维输入”?

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

模型不是应该接受一个简单的彩色图像吗?为什么它期望一个4维的输入?

RuntimeError                              Traceback (most recent call last)<ipython-input-51-d7abe3ef1355> in <module>()     33      34 # Forward pass the data through the model---> 35 output = model(data)     36 init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability     37 5 frames/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)    336                             _pair(0), self.dilation, self.groups)    337         return F.conv2d(input, self.weight, self.bias, self.stride,--> 338                         self.padding, self.dilation, self.groups)    339     340 RuntimeError: Expected 4-dimensional input for 4-dimensional weight 32 3 3, but got 3-dimensional input of size [3, 224, 224] instead

其中

inception = models.inception_v3()model = inception.to(device)

回答:

正如某人在他的评论中所写,PyTorch(以及大多数其他深度学习工具箱)期望一批图像作为输入。因此,您需要调用

output = model(data[None, ...])  

在您的输入data中插入一个单例“批次”维度。

请注意,您使用的模型可能期望不同的输入大小(3x229x229),而不是3x224x224。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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