I have a python code running KERAS LSTM model perfectly fine when run directly using the python code (either through SPYDER or via Command Prompt).
Python (anaconda) : 3.6.7 FLASK : 0.12.3 KERAS : 2.2.4 TENSORFLOW : 1.10.0
When trying to run it as a FLASK app, the same code throws the below error : Error : The Session graph is empty. Add operations to the graph before calling run().
I had to set session and set seed so that the model results dont vary from different runs. I have used the code from Kera's docs (https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development) in my code and it runs absolutely fine when run as a simple python script.
The problem comes only when trying to run it in a Flask app (which isnt making sense).
The error in code comes at the line where I try to load the model from a saved h5 file.
Can someone help me with this problem ?
Have read mostly all the similar issues from google and StackOverflow but none of the options worked out.
I also checked KERAS's github repo and there seems to be the same problem open as a ticket since 1 year. (https://github.com/keras-team/keras/issues/10585)
FLASK app code (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)
Code I used to set session (taken from KERAS's documentation site) that is present in PREDICT.py : **
# 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)
**
Expected results are that the model file should be loaded properly to be used for prediction later.
Details of Error :
**
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().
**