ValueError:“decode_predictions”需要一批预测(即形状的二维数组(samples,1000))。找到形状为的数组:(1,7)

2024-05-29 00:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用VGG16和keras进行迁移学习(我的新模型中有7个类),因此我想使用内置解码预测方法输出模型的预测。但是,使用以下代码:

preds = model.predict(img)

decode_predictions(preds, top=3)[0]

我收到以下错误消息:

ValueError: decode_predictions expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 7)

现在我想知道为什么在我的再培训模式中只有7节课的时候会有1000人。在

我在stackoverflow(Keras: ValueError: decode_predictions expects a batch of predictions)上发现了一个类似的问题 )建议在模型定义时包含“inlcude_top=True”,以解决此问题:

^{pr2}$

我已经试过了,但是它仍然不起作用——给了我和以前一样的错误。任何关于如何解决这个问题的提示或建议都是非常感谢的。在


Tags: of模型top错误batcharray建议keras
1条回答
网友
1楼 · 发布于 2024-05-29 00:18:59

我怀疑您使用的是一些预先训练过的模型,例如resnet50,并且您正在导入decode_predictions,如下所示:

from keras.applications.resnet50 import decode_predictions

解码预测将一个(num_samples,1000)概率数组转换为原始imagenet类的类名。在

如果你想在7个不同的班级之间进行横向学习和分类,你需要这样做:

^{pr2}$

在拟合模型并计算预测之后,您必须使用importeddecode_predictions将类名手动分配给output number,而不使用

相关问题 更多 >

    热门问题