最近发布集成了Keras应用的Flask API站点,发现与Keras相关的模型加载不起来,错误分别为:
1. KeyError: Layer: Name
解决方法:
Keras更新到2.2.4:pip install --upgrade keras==2.2.4
2. TypeError: Cannot interpret feed_dict key as Tensor
完整错误为:TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_1:0", shape=(128, 784), dtype=float32) is not an element of this graph
解决方法:
- Flask的启动模式不能设置为DEBUG:
app.run(host='0.0.0.0', port=port, debug=False, use_reloader=False)
- 或者在load_model的地方做如下处理:
def initialforlstm(self):
if not os.path.exists(self.modelpath):
self.trainlstmmodel()
self.lstmmodel = load_model(self.modelpath, compile=False)
elif self.lstmmodel is None:
try:
keras.backend.clear_session()
except:
traceback.print_exc()
self.lstmmodel = load_model(self.modelpath, compile=False)
即加入keras.backend.clear_session()
网友评论