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

hel*_*per 5 python flask keras tensorflow

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 ")
Run Code Online (Sandbox Code Playgroud)

and I use the following line for prediction:

cm_prediction = self.model.predict([face, reye, leye, fg])[0]
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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.

hel*_*per 15

因此,经过漫长的夜晚,Keras在19年9月17日发布了新版本2.3.0。作为修订版本的一部分,我更新了所有库,其中包括Keras。自从我做到了,消息就出现了。

在我降级到Keras 2.2.5之后,问题消失了。


小智 6

如果它仍然相关,我只需更改即可解决此问题

from keras.models import Sequential

from keras.layers import Dense, Dropout, LSTM
Run Code Online (Sandbox Code Playgroud)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
Run Code Online (Sandbox Code Playgroud)

因此,无需关闭多线程。