Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

422
Views
Flask and Keras model Error ''_thread._local' object has no attribute 'value''?

I am using the following: python 3.6.4

Flask = 1.1.1,

Keras = 2.3.0,

TensorFlow = 1.14.0, I have a Flask server that gets pictures from the clients. using Keras model with a TensorFlow back-end I try to get a prediction from a pre-trained model.

I am using the following function to upload the model( as part of a class)


 model_path = self.conf["model_path"] // path in conf to model
 self.model = load_model(model_path)  // uploading the model
 self.model._make_predict_function()
 p_log.info("model had been upload successfully ")

and I use the following line for prediction:

cm_prediction = self.model.predict([face, reye, leye, fg])[0]

Until today I didn't have any problem, always got a prediction. now I get the following error:

Traceback (most recent call last):
  File "D:\code_project\path to project", line 75, in predict
    cm_prediction = self.model.predict([face, reye, leye, fg])[0]
  File "D:\code_project\path to project", line 1462, in predict
    callbacks=callbacks)
  File "D:\code_project\predictserver\venv\lib\site-packages\keras\engine\training_arrays.py", line 276, in predict_loop
    callbacks.model.stop_training = False
  File "D:\code_project\predictserver\venv\lib\site-packages\keras\engine\network.py", line 323, in __setattr__
    super(Network, self).__setattr__(name, value)
  File "D:\code_project\predictserver\venv\lib\site-packages\keras\engine\base_layer.py", line 1215, in __setattr__
    if not _DISABLE_TRACKING.value:
AttributeError: '_thread._local' object has no attribute 'value'

I have a simple Flask server running:

if __name__ == '__main__':
    pre = predictor()
    # app.run(debug=True)
    app.run(host='0.0.0.0', port=12345)

The model is always being uploaded.

If I am running the program without the Flask server, hence giving manually input, I get a prediction, but as soon as the server is on the error appears and I stop getting a predictions

I tried to look on the web for some similar problem but didnt found any, if someone knows what the problem and how to solve it, I will appreciate sharing it.

over 4 years ago · Santiago Trujillo
19 answers
Answer question

0

No need to downgrade Keras or disable multi-threading. Use Keras with TensorFlow as back-end:

from tensorflow.keras.models import load_model
over 4 years ago · Santiago Trujillo Report

0

For Django : Use this command to run the server

python manage.py runserver --nothreading --noreload

it works perfectly fine for me

over 4 years ago · Santiago Trujillo Report

0

If you are using tensorflow 2.2 version, downgrading Keras to 2.2.5 will not help you because tensorflow 2.2 will need a keras version greater than 2.3. In that case, defining the graph variable will do the trick for you.

so in your app.py, add these two lines of code at the top.

global graph
graph = tf.compat.v1.get_default_graph()
over 4 years ago · Santiago Trujillo Report

0

Downgrading of Keras and Tensorflow versions does not work. Even setting Threaded=False in app.py does not solve the provlem on its own. You also need to set debug = False.Following works without any failure.

if __name__ == '__main__':
app.run(debug=False,threaded=False)
over 4 years ago · Santiago Trujillo Report

0

I tried all the above and here's what I found:

  1. downgrading Keras didn't work, even regular non-flask calls failed to load models
  2. tb._SYMBOLIC_SCOPE.value = True didn't work either
  3. finally threaded=False AND debug=False worked.
over 4 years ago · Santiago Trujillo Report

0

downgrading Keras didn't work tb._SYMBOLIC_SCOPE.value = True didn't work threaded=False AND debug=False didn't work

from keras.models import model_from_json

to

from tensorflow.keras.models import model_from_json

worked

over 4 years ago · Santiago Trujillo Report

0

This work for me:

you must put it just before the creation of the model.

import keras.backend.tensorflow_backend as tb tb._SYMBOLIC_SCOPE.value = True

over 4 years ago · Santiago Trujillo Report

0

None of these solutions worked for me. I switched from Flask to Bottle. Bottle is also a fast, simple and lightweight WSGI micro web-framework for Python.

To Install Bottle

pip insatll bottle

After that, all syntaxes are same as Flask

from bottle import route, run, template

@route('/hello')
def index():
    return "Hello World"

run(host='localhost', port=8080)
over 4 years ago · Santiago Trujillo Report

0

No need to downgrade your library versions. I had the same issue but I only tweaked the flask parameter.

app.run("0.0.0.0", 5005, threaded=False)

this made it finally run my code !

Let me know If you are still struggling.

over 4 years ago · Santiago Trujillo Report

0

If it is still relevant, I fixed this problem just by changing

from keras.models import Sequential

from keras.layers import Dense, Dropout, LSTM

to

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM

So, no need to turn off multithreading.

over 4 years ago · Santiago Trujillo Report

0

please make sure you should make the value of threaded=False Example : for flask :

if name == 'main':
    app.run(threaded=False)
over 4 years ago · Santiago Trujillo Report

0

Same problem when loading multiple Keras models via Flask. To solve the problem instead of using:

from keras.models import model_from_json

I used this:

from tensorflow.keras.models import model_from_json

In the future instead of installing keras I will use tensorflow.keras.

I hope it helps.

over 4 years ago · Santiago Trujillo Report

0

There is no need to downgrade the package versions. If you are using Keras then in Flask server do app.run(host=<HOST>, port=<PORT>, threaded=False) or in terminal do flask run --without-threads. However, I will suggest to use tensorflow.keras instead of keras, so that you don't have to disable multi-threading.

over 4 years ago · Santiago Trujillo Report

0

I had the same problem with my Keras models served via Flask on Google App Engine. Considering suggestions found in this thread and other places online I tried the following, none of which solved the original problem:

  • Downgrading to older versions of Tensorflow and/or Keras caused my models fail to load.
  • Using app.run(threaded=False) had no effect at all.
  • Setting the graph context with tensorflow.compat.v1.get_default_graph or tensorflow.python.keras.backend.get_graph caused some other errors.

Eventually the hint found here brought the solution and my app started returning valid results for all requests without any thread-related issues after I added these two lines to the code:

import keras.backend.tensorflow_backend as tb
tb._SYMBOLIC_SCOPE.value = True
over 4 years ago · Santiago Trujillo Report

0

I solved this problem by:

  1. Re-installing the latest versions of tensorflow, keras and flask (maybe order matters here...) inside the environment I used to run app.py
  2. Importing keras from tensorflow

Current versions:

  • tensorflow==2.1.0
  • keras==2.3.1
  • tensorflow.keras==2.2.4-tf
  • flask==1.1.1
over 4 years ago · Santiago Trujillo Report

0

I had the same problem when building a docker container today, which had worked perfectly before. Fixed it by downgrading Keras version to 2.2.4.

over 4 years ago · Santiago Trujillo Report

0

So after a long night, Keras had released a new version 2.3.0 in Sep 17,19. As part of revision update I did, I updated all libraries, Keras among them. Since I did it the message appeared.

After I downgraded back to Keras 2.2.5 The problem disappeared.

over 4 years ago · Santiago Trujillo Report

0

I had the same problem with Keras 2.3.0.

Another fix for those that don't want to downgrade is to set threaded=False in app.run().

over 4 years ago · Santiago Trujillo Report

0

If you're having issues and are a little slow like myself, set debug=False as well

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!