File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/src/main.py", line 37, in model_predict
with backend.get_session().graph.as_default() as g:
File "/Users/Upasana/Documents/playground/deploy-keras-model-in-production/venv-tf2/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 379, in get_session
'`get_session` is not available '
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
RuntimeError: get_session is not available when using TensorFlow 2.0
Upasana | December 01, 2019 | 2 min read | 5,951 views | Flask - Python micro web framework
Here, we will see how we can upgrade our code to work with tensorflow 2.0.
This error is usually faced when we are loading pre-trained model with tensorflow session/graph or we are building flask api over a pre-trained model and loading model in tensorflow graph to avoid collision of sessions while application is getting multiple requests at once or say in case of multi-threadinng
After tensorflow 2.0 upgrade, i also started facing above error in one of my project when i had built api of pre-trained model with flask. So i looked around in tensorflow 2.0 documents to find a workaround, to avoid this runtime error and upgrade my code to work with tensorflow 2.0 as well rather than downgrading it to tensorflow 1.x .
I had a project on which i had written tutorial as well on how to build Flask api on trained keras model of text classification and then use it in production
But this project was not working after tensorflow upgrade and was facing runtime error.
Stacktrace of error was something like below:
with backend.get_session().graph.as_default() as g:
model = SentimentService.get_model1()
def load_deep_model(self, model):
json_file = open('./src/mood-saved-models/' + model + '.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("./src/mood-saved-models/" + model + ".h5")
loaded_model._make_predict_function()
return loaded_model
get_session
is removed in tensorflow 2.0 and hence not available.
so, in order to load saved model we switched methods. Rather than using keras’s load_model
, we used tensorflow to load model so that we can load model using distribution strategy.
The tf.distribute.Strategy
API provides an abstraction for distributing your training across multiple processing units.
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = SentimentService.get_model1()
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("./src/mood-saved-models/"model + ".h5")
return loaded_model
This worked and solved the problem with runtime error of get_session not available in tensorflow 2.0 . You can refer to Tensorflow 2.0 upgraded article too
Hope, this will solve your problem too. Thanks for following this article.
Top articles in this category:
- Deploying Keras Model in Production with TensorFlow 2.0
- Google Data Scientist interview questions with answers
- When using Gaussian mixture model, how do you know it is applicable
- Top 100 interview questions on Data Science & Machine Learning
- Find if credit card number is valid or not
- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Explain a probability distribution that is not normal and how to apply that