Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

At Runtime : "Error while reading resource variable softmax/kernel from Container: localhost" #28287

Closed
haitham-b opened this issue Apr 30, 2019 · 31 comments
Assignees
Labels
comp:ops OPs related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues

Comments

@haitham-b
Copy link

System information

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.git
  • git 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
    Screenshot 2019-04-30 at 16 10 57
    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}}]]
@haitham-b haitham-b changed the title Error while reading resource variable softmax/kernel from Container: localhost At Runtime : "Error while reading resource variable softmax/kernel from Container: localhost" Apr 30, 2019
@achandraa achandraa self-assigned this May 2, 2019
@achandraa achandraa added 2.0.0-alpha0 type:support Support issues comp:ops OPs related issues labels May 3, 2019
@achandraa achandraa assigned jvishnuvardhan and unassigned achandraa May 8, 2019
@haitham-b
Copy link
Author

Any updates on this?

@jvishnuvardhan
Copy link
Contributor

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!

@jvishnuvardhan jvishnuvardhan added the stat:awaiting response Status - Awaiting response from author label May 22, 2019
@MischaPanch
Copy link

MischaPanch commented 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:

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(...)

@dsaichand3
Copy link

dsaichand3 commented May 23, 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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

@MischaPanch
Copy link

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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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
Copy link
Contributor

Closing this out since I understand it to be resolved, but please let me know if I'm mistaken. Thanks!

@wangcj05
Copy link

wangcj05 commented Jul 11, 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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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.

I have the same issue with tensor flow version 1.13.1, the above solution works for me.

@aachoo
Copy link

aachoo commented Jul 18, 2019

I am having the same issues and was wondering what the value of some_custom_config was?

@MischaPanch
Copy link

I am having the same issues and was wondering what the value of some_custom_config was?

In case you want to configure your session (which I had to do), you can pass the config in this parameter. Else just leave it out.

@aachoo
Copy link

aachoo commented Jul 19, 2019

I am having the same issues and was wondering what the value of some_custom_config was?

In case you want to configure your session (which I had to do), you can pass the config in this parameter. Else just leave it out.

Thank you so much! Everything is running perfectly now.

@sangurocactus
Copy link

sangurocactus commented Jul 29, 2019

Thanks for providing the codes. I ran into similar error message while running BERT on Kera. I tried your solution but can't seem to get it to work. Any guidance is most appreciated!

<ipython-input-42-dc70f6f3f83d> in <module>()
     11     validation_data=([test_input_ids, test_input_masks, test_segment_ids], test_labels),
     12     epochs=1,
---> 13     batch_size=32
     14 )

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1456         ret = tf_session.TF_SessionRunCallable(self._session._session,
   1457                                                self._handle, args,
-> 1458                                                run_metadata_ptr)
   1459         if run_metadata:
   1460           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

FailedPreconditionError: Error while reading resource variable bert_layer_9_module/bert/encoder/layer_3/output/LayerNorm/gamma from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/bert_layer_9_module/bert/encoder/layer_3/output/LayerNorm/gamma/N10tensorflow3VarE does not exist.
	 [[{{node bert_layer_9/bert_layer_9_module_apply_tokens/bert/encoder/layer_3/output/LayerNorm/batchnorm/mul/ReadVariableOp}}]]```

@georgealexandruvlad
Copy link

I have a similar error when using Elmo embeddings from tf-hub inside a custom keras layer.

tensorflow.python.framework.errors_impl.FailedPreconditionError: 2 root error(s) found.
(0) Failed precondition: Error while reading resource variable ElmoEmbeddingLayer_module/bilm/CNN_high_0/b_carry from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/ElmoEmbeddingLayer_module/bilm/CNN_high_0/b_carry/class tensorflow::Var does not exist.
[[{{node ElmoEmbeddingLayer/ElmoEmbeddingLayer_module_apply_default/bilm/add/ReadVariableOp}}]]
(1) Failed precondition: Error while reading resource variable ElmoEmbeddingLayer_module/bilm/CNN_high_0/b_carry from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/ElmoEmbeddingLayer_module/bilm/CNN_high_0/b_carry/class tensorflow::Var does not exist.
[[{{node ElmoEmbeddingLayer/ElmoEmbeddingLayer_module_apply_default/bilm/add/ReadVariableOp}}]]
[[metrics/acc/Identity/_199]]
0 successful operations.
0 derived errors ignored.

@eliadl
Copy link

eliadl commented Aug 7, 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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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.

Thank you so much for this.

In my case I did it a bit differently, in case it helps anyone:

# on thread 1
session = tf.Session(graph=tf.Graph())
with session.graph.as_default():
    k.backend.set_session(session)
    model = k.models.load_model(filepath)

# on thread 2
with session.graph.as_default():
    k.backend.set_session(session)
    model.predict(x, **kwargs)

The novelty here is allowing for multiple models to be loaded (once) and used in multiple threads.
By default, the "default" Session and the "default" Graph are used while loading a model.
But here you create new ones.
Also note the Graph is stored in the Session object, which is a bit more convenient.

@eliadl
Copy link

eliadl commented Apr 17, 2020

@SungmanHong instead of tf.Session try to use tf.compat.v1.Session.

@lvenugopalan lvenugopalan added the TF 2.0 Issues relating to TensorFlow 2.0 label Apr 29, 2020
@akash-harijan
Copy link

In TF 2.x, there is no session(). how do I fix it? same problem is happening in my TF 2.x code.

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
Copy link

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:

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(...)

This worked for me , thanks

@vamsikri1234
Copy link

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:

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(...)

Man you are genius and awesome , you just saved my project , Thank you so muuch

FHof added a commit to FHof/Carla-RL that referenced this issue May 14, 2020
I have applied the suggestion from tensorflow/tensorflow#28287 (comment)
I'm not familiar with tensorflow, so I don't know if I've fixed the problem correctly.
Now the trainer is no longer stuck in the waiting state.
@amalmathews
Copy link

amalmathews commented Jun 2, 2020

In TF 2.x, there is no session(). how do I fix it? same problem is happening in my TF 2.x code.

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()

I have used the given solution but didn't work for me still, I'm getting the error

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py:49: UserWarning: Using a generator with `use_multiprocessing=True` and multiple workers may duplicate your data. Please consider using the `keras.utils.Sequence class.
  UserWarning('Using a generator with `use_multiprocessing=True`'
Epoch 1/4
---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-13-05f25cba3043> in <module>()
      4             learning_rate=config.LEARNING_RATE,
      5             epochs=4,
----> 6             layers='heads')

6 frames
/usr/local/lib/python3.6/dist-packages/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation)
   2350             max_queue_size=100,
   2351             workers=workers,
-> 2352             use_multiprocessing=True,
   2353         )
   2354         self.epoch = max(self.epoch, epochs)

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1730             use_multiprocessing=use_multiprocessing,
   1731             shuffle=shuffle,
-> 1732             initial_epoch=initial_epoch)
   1733 
   1734     @interfaces.legacy_generator_methods_support

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    218                                             sample_weight=sample_weight,
    219                                             class_weight=class_weight,
--> 220                                             reset_metrics=False)
    221 
    222                 outs = to_list(outs)

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
   1512             ins = x + y + sample_weights
   1513         self._make_train_function()
-> 1514         outputs = self.train_function(ins)
   1515 
   1516         if reset_metrics:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   3630 
   3631     fetched = self._callable_fn(*array_vals,
-> 3632                                 run_metadata=self.run_metadata)
   3633     self._call_fetch_callbacks(fetched[-len(self._fetches):])
   3634     output_structure = nest.pack_sequence_as(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1470         ret = tf_session.TF_SessionRunCallable(self._session._session,
   1471                                                self._handle, args,
-> 1472                                                run_metadata_ptr)
   1473         if run_metadata:
   1474           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

FailedPreconditionError: Error while reading resource variable anchors/Variable from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/anchors/Variable/N10tensorflow3VarE does not exist.
	 [[{{node ROI/ReadVariableOp}}]] ```


**PLEASE HELP!!!**

@tochile
Copy link

tochile commented Jun 25, 2020

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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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.

Pls it's showing some_custom_config not defined

@tochile
Copy link

tochile commented Jun 25, 2020

Pls it's showing some_custom_config not defined

@juan-csv
Copy link

juan-csv commented Jul 2, 2020

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:

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(...)

work's for me, thanks!!

@lminer
Copy link

lminer commented Jan 11, 2021

Am I correct that the only way to solve this issue in TF2 is to disable eager execution?

@minniekabra
Copy link

minniekabra commented May 17, 2021

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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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.

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
Copy link

from tensorflow.python.keras.backend import set_session
from tensorflow.python.keras.models import load_model

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
Copy link

Yessmin commented Jun 9, 2021

tf 2.5 please any solution ?
I've disabled the eager mode but am still getting the same error

@dorbittonn
Copy link

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:

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(...)

You are amazing!!!!! This is the best solution. Can you tell me why did it just work after adding session?

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.

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
Copy link

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(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:support Support issues
Projects
None yet
Development

No branches or pull requests