我有一个预先训练的模型,试图删除一个层,并在新模型上执行预测。然而,检索错误。
model = applications.VGG16(include_top=False, input_shape=(224, 224, 3), weights='imagenet')
layers = [l for l in model.layers]
x = layers[9].output
x = layers[11](x)
x = layers[12](x)
x = layers[13](x)
x = layers[14](x)
x = layers[15](x)
x = layers[16](x)
x = layers[17](x)
x = layers[18](x)
result_model = Model(inputs=layers[0].input, outputs=x)
img='/content/elephant.jpg'
img = image.load_img(img, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = result_model.predict(x)
print('Predicted:', 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, 14, 14, 512)
发布于 2021-03-17 22:37:40
你的神经网络没有输出层。decode_predictions
不能解码卷积层的输出,这是您执行include_top=False
时所得到的。执行以下操作:
model = applications.VGG16(include_top=True, input_shape=(224, 224, 3),
weights='imagenet')
https://stackoverflow.com/questions/66675147
复制相似问题