【发布时间】:2019-05-14 19:22:11
【问题描述】:
当直接使用 python 代码(通过 SPYDER 或通过命令提示符)运行时,我有一个运行 KERAS LSTM 模型的 python 代码非常好。
Python(蟒蛇):3.6.7 烧瓶:0.12.3 凯拉斯:2.2.4 张量流:1.10.0
当尝试将其作为 FLASK 应用程序运行时,相同的代码会引发以下错误: 错误:会话图为空。在调用 run() 之前向图中添加操作。
我必须设置会话并设置种子,以便模型结果不会因不同的运行而变化。 我在我的代码中使用了 Kera 文档 (https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development) 中的代码,当作为简单的 python 脚本运行时,它运行得非常好。
问题仅在尝试在 Flask 应用程序中运行时出现(这没有意义)。
代码中的错误出现在我尝试从保存的 h5 文件加载模型的那一行。
有人可以帮我解决这个问题吗?
已经阅读了大部分来自 google 和 StackOverflow 的类似问题,但没有一个解决方案。
我还检查了 KERAS 的 github 存储库,自 1 年以来似乎存在与票证相同的问题。 (https://github.com/keras-team/keras/issues/10585)
FLASK 应用代码(app.py):
from flask import Flask, jsonify
from PREDICT import worker
app = Flask(__name__)
@app.route('/api/Model', methods=['GET', 'POST'])
def Model_predict():
worker()
return ('Model run')
if __name__ == '__main__':
app.run(debug=True)
我用来设置 PREDICT.py 中的会话(取自 KERAS 的文档站点)的代码: **
# The below is necessary for starting Numpy generated random numbers in a well-defined initial state.
np.random.seed(42)
# The below is necessary for starting core Python generated random numbers in a well-defined state.
rn.seed(12345)
# Force TensorFlow to use single thread.
# Multiple threads are a potential source of non-reproducible results.
# For further details, see: https://stackoverflow.com/questions/42022950/
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
from keras import backend as K
# The below tf.set_random_seed() will make random number generation in the TensorFlow backend have a well-defined initial state.
# For further details, see: https://www.tensorflow.org/api_docs/python/tf/set_random_seed
tf.set_random_seed(1234)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
**
预期的结果是模型文件应该被正确加载以便以后用于预测。
错误详情:
**
2019-05-15 00:19:19,121 ERROR : Error at Line : 363
2019-05-15 00:19:19,122 ERROR : Error : The Session graph is empty. Add operations to the graph before calling run().
2019-05-15 00:19:19,123 ERROR Stack Trace :
Traceback (most recent call last):
File "PREDICT.py", line 363, in worker
model = loading_model(output_path, weekno, prod)
File "PREDICT.py", line 104, in loading_model
m = load_model(model_name)
File "C:\Users\12345\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "C:\Users\12345\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 287, in _deserialize_model
K.batch_set_value(weight_value_tuples)
File "C:\Users\12345\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2470, in batch_set_value
get_session().run(assign_ops, feed_dict=feed_dict)
File "C:\Users\12345\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 877, in run
run_metadata_ptr)
File "C:\Users\12345\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1025, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
**
【问题讨论】:
标签: python-3.x tensorflow flask keras lstm