最近發(fā)布集成了Keras應(yīng)用的Flask API站點(diǎn)萤悴,發(fā)現(xiàn)與Keras相關(guān)的模型加載不起來稳摄,錯(cuò)誤分別為:
1. KeyError: Layer: Name
解決方法:
Keras更新到2.2.4:pip install --upgrade keras==2.2.4
2. TypeError: Cannot interpret feed_dict key as Tensor
完整錯(cuò)誤為: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的啟動(dòng)模式不能設(shè)置為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()