Repository navigation
Description
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes, found here (https://github.com/viaboxxsystems/deeplearning-showcase/blob/tensorflow_2.0/flaskApp.py)
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MAC OSX 10.14.4
- TensorFlow version (use command below): 2.0.0-alpha0
- Python version: Python 3.6.5
You can collect some of this information using our environment capture
python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"
v1.12.0-9492-g2c319fb415 2.0.0-alpha0
Describe the current behavior
when running "flaskApp.py", After loading the model and trying to classify an image using "predict", it fails with the error:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.
Describe the expected behavior
a result of image classification should be returned.
Code to reproduce the issue
Steps to reproduce:
git clone https://github.com/viaboxxsystems/deeplearning-showcase.gitgit checkout tensorflow_2.0- (if needed)
pip3 install -r requirements.txt export FLASK_APP=flaskApp.py- start the app with
flask run - using Postman or curl send any image of a dog or cat to the app

OR
curl -X POST \
http://localhost:5000/net/MobileNet \
-H 'Postman-Token: ea35b79b-b34d-4be1-a80c-505c104050ec' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F image=@/Users/haitham.b/Projects/ResearchProjects/CNNs/deeplearning-showcase/data/sample/valid/dogs/dog.1008.jpg
Other info / logs
E0430 13:36:10.374372 123145501933568 app.py:1761] Exception on /net/MobileNet [POST]
Traceback (most recent call last):
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py", line 97, in use_net_to_classify_image
prediction, prob = predict(net_name, image)
File "/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py", line 59, in predict
output_probability = net_models[cnn_name].predict(post_processed_input_images)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1167, in predict
callbacks=callbacks)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 352, in model_iteration
batch_outs = f(ins_batch)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3096, in __call__
run_metadata=self.run_metadata)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1440, in __call__
run_metadata_ptr)
File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 548, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.
[[{{node softmax/MatMul/ReadVariableOp}}]]
Activity
[-]Error while reading resource variable softmax/kernel from Container: localhost[/-][+]At Runtime : "Error while reading resource variable softmax/kernel from Container: localhost"[/+]haitham-b commentedon May 15, 2019
Any updates on this?
jvishnuvardhan commentedon May 22, 2019
This is not Build/Installation or Bug/Performance issue. Please post this kind of support questions at Stackoverflow. There is a big community to support and learn from your questions. GitHub is mainly for addressing bugs in installation and performance. Thanks!
MischaPanch commentedon May 22, 2019
I had the same issue in tensorflow 1.13.1 which I have resolved by creating a reference to the session that is used for loading the models and then to set it to be used by keras in each request. I.e. I have done the following:
and then in each request (i.e. in each thread):
dsaichand3 commentedon May 23, 2019
You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?
MischaPanch commentedon May 23, 2019
Thank you and you are very welcome :).
As far as I understand, the problem is that tensorflow graphs and sessions are not thread safe. So by default a new session (which does not contain any previously loaded weights, models a.s.o.) is created for each thread, i.e. for each request. By saving the global session that contains all your models and setting it to be used by keras in each thread the problem is solved.
jvishnuvardhan commentedon May 24, 2019
Closing this out since I understand it to be resolved, but please let me know if I'm mistaken. Thanks!
16 remaining items
akash-harijan commentedon May 5, 2020
import session in TF 2.X
tf.compat.v1.Session()import keras.backend.get_session in TF 2.X
tf.compat.v1.keras.backend.get_session()AllanNyasha commentedon May 10, 2020
This worked for me , thanks
vamsikri1234 commentedon May 13, 2020
Man you are genius and awesome , you just saved my project , Thank you so muuch
Fix the self.model.predict crash
amalmathews commentedon Jun 2, 2020
I have used the given solution but didn't work for me still, I'm getting the error
tochile commentedon Jun 25, 2020
Pls it's showing some_custom_config not defined
tochile commentedon Jun 25, 2020
Pls it's showing some_custom_config not defined
juan-csv commentedon Jul 2, 2020
work's for me, thanks!!
lminer commentedon Jan 11, 2021
Am I correct that the only way to solve this issue in TF2 is to disable eager execution?
minniekabra commentedon May 17, 2021
Hi, I am using tensorflow2.4.1 and getting similar error.. Below is my code & error. Can you help?
Code pasted on stack overflow:
https://stackoverflow.com/questions/67575144/error-while-reading-resource-variable-encoder-conv2-1-b-from-container-localhos
omarabdullalghafoor commentedon May 24, 2021
Hi brother , I have same problem in this : can be solve it ?
https://stackoverflow.com/questions/67659336/tensor-tensorflatten-reshape0-shape-2622-dtype-float32-is-not-an-ele
Yessmin commentedon Jun 9, 2021
tf 2.5 please any solution ?
I've disabled the eager mode but am still getting the same error
dorbittonn commentedon Dec 5, 2021
thanks for your answer, Im having problems particularly, using tf 1.14.
still getting this error when trying to run 2 different models one after the other.
I get the error in model.fit. I paid attention to the fact that if Im not passing tensorboard callback the error is gone.
Anyone has an idea what it could be?
NeighborhoodCoding commentedon Dec 7, 2021
from tensorflow.python.keras.backend import set_session
from tensorflow.python.keras.models import load_model
tf_config = some_custom_config
sess = tf.Session(config=tf_config)
graph = tf.get_default_graph()
IMPORTANT: models have to be loaded AFTER SETTING THE SESSION for keras!
Otherwise, their weights will be unavailable in the threads after the session there has been set
set_session(sess)
model = load_model(...)
and then in each request (i.e. in each thread):
global sess
global graph
with graph.as_default():
set_session(sess)
model.predict(...)