我正在使用 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"))
Here is the full error:
如有任何意见或建议,我们将不胜感激。谢谢。
考虑到后端设置为tensorflow。您应该将 Keras 会话设置为张量流图
from tensorflow import Graph, Session
from keras import backend
model = 'model path'
graph1 = Graph()
with graph1.as_default():
session1 = Session(graph=graph1)
with session1.as_default():
model_1.load_model(model) # depends on your model type
model2 = 'model path2'
graph2 = Graph()
with graph2.as_default():
session2 = Session(graph=graph2)
with session2.as_default():
model_2.load_model(model2) # depends on your model type
并用于预测
K.set_session(session#)
with graph#.as_default():
prediction = model_#.predict(img_data)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)