我正在使用 keras 的预训练模型,但在尝试进行预测时出现了错误。我在烧瓶服务器中有以下代码:
from NeuralNetwork import *
@app.route("/uploadMultipleImages", methods=["POST"])
def uploadMultipleImages():
uploaded_files = request.files.getlist("file[]")
getPredictionfunction = preTrainedModel["VGG16"]
for file in uploaded_files:
path = os.path.join(STATIC_PATH, file.filename)
result = getPredictionfunction(path)
这就是我在 NeuralNetwork.py 文件中的内容:
vgg16 = VGG16(weights='imagenet', include_top=True)
def getVGG16Prediction(img_path):
model = vgg16
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x) #ERROR HERE
return sort(decode_predictions(pred, top=3)[0])
preTrainedModel["VGG16"] = getVGG16Prediction
但是,在下面运行此代码不会产生任何错误:
if __name__ == "__main__":
STATIC_PATH = os.getcwd()+"/static"
print(preTrainedModel["VGG16"](STATIC_PATH+"/18.jpg"))
非常感谢任何意见或建议。谢谢你。