Keras: 'thread._local' object has no attribute 'value' error

Created on 21 Sep 2019  ·  100Comments  ·  Source: keras-team/keras

I am loading a model from an hdf5 file with using keras.models.load_model. when i try to predict using that model the following error raises:

'thread._local' object has no attribute 'value'

the code is:

'''
from keras.preprocessing.image import img_to_array
from keras.models import load_model

model_path = (''/home/pi/classifier/models/model.hdf5'')
classifier = load_model(model_path, compile=False)
preds = classifier.predict(img_to_array(image))
'''
where image is numpy array read by opencv

Platform: Raspberry Pi 4
OS: Raspbian Buster
TF version: 1.13.1
Keras version: 2.3.0

Most helpful comment

Hello. Today we've faced the same problem. We've founded that Version ImageAI 2.1.5 works with tensorflow v 1.14.0 and keras v.2.2.5 so use:

pip install tensorflow==1.14
pip install keras==2.2.5

It's solved our problem.

All 100 comments

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

The issue is still reflecting for me in ubuntu linux 18.0.4
keras : 2.3.0

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/form/

Django Version: 2.2.5
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'DLPAPI']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/home/pratikkumarpandey/dlpenv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner

  1. response = get_response(request)

File "/home/pratikkumarpandey/dlpenv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response

  1. response = self.process_exception_by_middleware(e, request)

File "/home/pratikkumarpandey/dlpenv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response

  1. response = wrapped_callback(request, callback_args, *callback_kwargs)

File "/home/pratikkumarpandey/DLP_PROJECT/DLPAPI/views.py" in dlpdataview

  1. result(recordNUM)

File "/home/pratikkumarpandey/DLP_PROJECT/DLPAPI/DLP/dlpExplainable.py" in result

  1. a,b,c,d= getExplanationByRecord(val1)

File "/home/pratikkumarpandey/DLP_PROJECT/DLPAPI/DLP/dlpExplainable.py" in getExplanationByRecord

  1. dataFrame, context, inputVector, DNNPrediction, predictionClass = getTextPrediction(recordNum)

File "/home/pratikkumarpandey/DLP_PROJECT/DLPAPI/DLP/dlpExplainable.py" in getTextPrediction

  1. prediction = model.predict(inputVector, verbose=0)

File "/home/pratikkumarpandey/dlpenv/lib/python3.7/site-packages/keras/engine/training.py" in predict

  1. callbacks=callbacks)

I have the same issues
I'm running python script with keras 2.2.5 on my laptop and everything is fine
But the same script with keras 2.3.0 on another computer gives this error

OS: Windows 10
TF: 1.14.0

@DenVys thank you so much!
We were shocked that our multithreaded KTrain loaded model was working at one place and not working in another.
Thank you again!

So I has the same issue, I am running an image classification model on an ubuntu server!
I uninstalled Keras since I had Installed it using PIp, So I run

pip uninstall keras

and then I installed it again using

conda install keras

It solved the issue!

Hello. Today we've faced the same problem. We've founded that Version ImageAI 2.1.5 works with tensorflow v 1.14.0 and keras v.2.2.5 so use:

pip install tensorflow==1.14
pip install keras==2.2.5

It's solved our problem.

can confirm.
keras==2.2.5 solve the problem.

I'm trying to use keras in flask, it raise an error:
'thread._local' object has no attribute 'value' error

@fchollet
It looks like this is still a problem as people said
Maybe we need to reopen this issue?

@DenVys As he said, the fix is on master. It looks like everyone who is still having the problem is not using master.

That being said, @fchollet I installed from master and am still getting this error with Flask.

I've tried Keras 2.2.5 but the issue still persists

For those who don't need the multithreading, and still get blocked by this issue try setting threaded=False in app.run

For those who don't need the multithreading, and still get blocked by this issue try setting threaded=False in app.run

it's working for me
Example:
app.run(host="0.0.0.0",port=5000,threaded=False)

Running keras inside a thread with TF2 will still crash like that. I think this is still a big issue and should still be open.
This example will reproduce the error:

Tensorflow: 2.0
Keras: 2.3.1

from keras.models import Sequential, load_model
from keras.layers import Dense
from threading import Thread

m = Sequential()
m.add(Dense(10,input_shape=(10,)))
m.compile("sgd","mse")
m.save("test.h5")

def load():
    load_model("test.h5")

t = Thread(target=load)
t.start()
t.join()

Yeah, I spent a bit of time and haven't yet found a combinations of versions that lets me use TF/Keras in a thread (aka, using it with Flask in default threading config) while letting me use TF > 2.0.

There is no problem in keras code people who are using it with different session are actually facing this issue.
For example if you are trying to call predict function from Django ,it will create two different session so you just have to take care of global graph and make sure there is same session in both ,than your problem will be solved

Confirming that there is still a problem with TF 2.0 and Keras 2.3.1, as described by @cbodenst above.

I'm running into the issue while using ray.tune to train a Keras model. Using TF 1.14.0 and Keras 2.3.1 seems to work.

Upgrading from keras 2.3.0 to 2.3.1 solved the problem for me...

@mletombe I have keras version 2.3.1 still having the issue.
can u tell me the version of tensorflow you are using.

I'm using tensorflow 1.13.0-rc2, python 3.6, win 10...

I'm using keras 2.3.1, tensorflow 2.0.0, python 3.6, linux Mint 19.2 Cinnamon.
My project is based on Waitress, so I can't use "threaded=False".

I found an UGLY workaround....
In the main "post manager" function I put this code...

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

And this solved the problem for me...
I hope it will be useful to find a solution

thanks @ccasadei it worked for me.

Yes @ccasadei - this worked for me as well. I encountered another variable causing the similar 'value' attribute problem - _DISABLE_TRACKING. I set it to True/False - didn't seem to matter. Obviously this is not a solution just a workaround. But it does get threading working with keras 2.3.1 / tensorflow 2.0

I still encounter this problem.
OS: Centos
Packages:
Keras==2.3.1
tensorflow==2.0.0
Flask==1.1.1

  File "/home/mullerh/.local/share/virtualenvs/ml-uqLOMuwl/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

I use falcon instead, and it works fine.

I am encountering it as well trying to follow Tune's documentation.
Mac OSX 10.15.1
Keras==2.3.1
tensorflow==2.0.0
EDIT: I got this keras example VAE code working with the aforementioned fix applied:

import torch.optim as optim
from ray import tune
from ray.tune.examples.mnist_pytorch import get_data_loaders, ConvNet, train, test
from keras.layers import Lambda, Input, Dense
from keras.models import Model
from keras.datasets import mnist
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K
from keras.datasets import mnist
import numpy as np
import tensorflow as tf


def sampling(args):
    """Reparameterization trick by sampling from an isotropic unit Gaussian.

    # Arguments
        args (tensor): mean and log of variance of Q(z|X)

    # Returns
        z (tensor): sampled latent vector
    """

    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    # by default, random_normal has mean = 0 and std = 1.0
    epsilon = K.random_normal(shape=(batch, dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon


# MNIST dataset. These lines used to be at the start of train_mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
image_size = x_train.shape[1]
original_dim = image_size * image_size
x_train = np.reshape(x_train, [-1, original_dim])
x_test = np.reshape(x_test, [-1, original_dim])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

def train_mnist(config):
    import keras.backend.tensorflow_backend as tb
    tb._SYMBOLIC_SCOPE.value = True

    # network parameters
    input_shape = (original_dim, )
    intermediate_dim = 512
    batch_size = 128
    latent_dim = 2
    epochs = 50

    # VAE model = encoder + decoder
    # build encoder model
    inputs = Input(shape=input_shape, name='encoder_input')
    x = Dense(intermediate_dim, activation='relu')(inputs)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)
    # use reparameterization trick to push the sampling out as input
    # note that "output_shape" isn't necessary with the TensorFlow backend
    z = Lambda(sampling, output_shape=(latent_dim,),
               name='z')([z_mean, z_log_var])

    # instantiate encoder model
    encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')

    # build decoder model
    latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
    x = Dense(intermediate_dim, activation='relu')(latent_inputs)
    outputs = Dense(original_dim, activation='sigmoid')(x)

    # instantiate decoder model
    decoder = Model(latent_inputs, outputs, name='decoder')

    # instantiate VAE model
    outputs = decoder(encoder(inputs)[2])
    vae = Model(inputs, outputs, name='vae_mlp')

    models = (encoder, decoder)
    data = (x_test, y_test)
    reconstruction_loss = binary_crossentropy(inputs,
                                              outputs)
    reconstruction_loss *= original_dim
    kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
    kl_loss = K.sum(kl_loss, axis=-1)
    kl_loss *= -0.5
    vae_loss = K.mean(reconstruction_loss + kl_loss)
    vae.add_loss(vae_loss)
    vae.compile(optimizer='adam')
    vae.fit(x_train,
            epochs=epochs,
            batch_size=batch_size,
            validation_data=(x_test, None))


analysis = tune.run(
    train_mnist, config={"lr": tune.grid_search([0.001, 0.01, 0.1])})

print("Best config: ", analysis.get_best_config(metric="mean_accuracy"))

# Get a dataframe for analyzing trial results.
df = analysis.dataframe()
print(df)

If you're using Django and trying to predict, I stored and loaded my model into models.py and it worked. It didn't work using the as_default_graph() for me, so I followed this example: https://medium.com/@aoll.motion/integrate-deep-learning-with-keras-in-django-c49611fecb68
I hope this helps someone else trying to figure out the problem.

can confirm.
keras==2.2.5 solve the problem.

I'm trying to use keras in flask, it raise an error:
'thread._local' object has no attribute 'value' error

Yes it's actually working, I confirm too.

I am trying to use the tensorflow/keras as server with multithreading.
Same error with:
keras==2.3.1
tensorflow==2.0.0

Solve the issue with:
tensorflow==1.15

Ive just pulled in the Keras from master with tensorflow 2.0 and its still broken. @fchollet

Ive just pulled in the Keras from master with tensorflow 2.0 and its still broken. @fchollet

Problem is with the tensorflow 2.0, you can have the latest keras version but downgrade the tensorflow version to 1.15 or 1.14

I am trying to use the tensorflow/keras as server with multithreading.
Same error with:
keras==2.3.1
tensorflow==2.0.0
Solve the issue with:
tensorflow==1.15 @caigen

keras==2.3.1
tensorflow==2.0.X
I had the seem issue.When I turned tensorflow into 1.15,I got another issue...then I solved the issue with:
keras==2.2.5
tensorflow==1.14
Thanks : )

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

i've tried installing from 'master', not working either. so sad...

Hello. Today we've faced the same problem. We've founded that Version ImageAI 2.1.5 works with tensorflow v 1.14.0 and keras v.2.2.5 so use:

pip install tensorflow==1.14
pip install keras==2.2.5

It's solved our problem.

thanks a million!! it works to me. with global graph https://github.com/keras-team/keras/issues/2397#issuecomment-306687500

My model was trained on Keras 2.3.X version...
so if I downgrade to tf == 1.14 and keras==2.25, need to re-train the model...

Tried threaded=False but doesn't work either...any suggestion?
Python 3.7.5 on macOS Catalina

import tensorflow as tf
from keras import backend as K
from keras.models import load_model
from multiprocessing import Process
import numpy as np

K.clear_session()

class CNN:

def __init__(self, model_path):
    self.cnn_model = load_model(model_path)
    self.cnn_model.predict(np.array([[0,0]])) # warmup
    self.session = K.get_session()
    self.graph = tf.get_default_graph()
    self.graph.finalize() # finalize

def preproccesing(self, data):
    # dummy
    return data

def query_cnn(self, data):
    X = self.preproccesing(data)
    with self.session.as_default():
        with self.graph.as_default():
            prediction = self.cnn_model.predict(X)
    print(prediction)
    return prediction

cnn = CNN("dummymodel")

multiprocessing crash the error, tf 1.14, keras 2.3.0
AttributeError: 'thread._local' object has no attribute 'value'

import tensorflow as tf
from keras import backend as K
from keras.models import load_model
from multiprocessing import Process
import numpy as np

K.clear_session()

class CNN:

def __init__(self, model_path):
    self.cnn_model = load_model(model_path)
    self.cnn_model.predict(np.array([[0,0]])) # warmup
    self.session = K.get_session()
    self.graph = tf.get_default_graph()
    self.graph.finalize() # finalize

def preproccesing(self, data):
    # dummy
    return data

def query_cnn(self, data):
    X = self.preproccesing(data)
    with self.session.as_default():
        with self.graph.as_default():
            prediction = self.cnn_model.predict(X)
    print(prediction)
    return prediction

cnn = CNN("dummymodel")

multiprocessing crash the error, tf 1.14, keras 2.3.0
AttributeError: 'thread._local' object has no attribute 'value'

Try Keras 2.2.5

Hello. Today we've faced the same problem. We've founded that Version ImageAI 2.1.5 works with tensorflow v 1.14.0 and keras v.2.2.5 so use:

pip install tensorflow==1.14
pip install keras==2.2.5

It's solved our problem.

If you've not solved yet even though you've changed the version to tensorflow==1.14 keras=2.3.0, you might be able to fix it by changing the versions of tensorboard and tensorflow-estimator to 1.14
pip install tensorboard==1.14
pip install tensorflow-estimator==1.14

I've solved my problem with that! Hope it helps!

I'm using keras 2.3.1, tensorflow 2.0.0, python 3.6, win10.
My project is based on streamlit, so I can't use "threaded=False".
I need to use keras 2.3.1, tensorflow 2.0.0.

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

not work for me!
@fchollet @pratikpc

I'm using keras 2.3.1, tensorflow 2.0.0, python 3.6, win10.
My project is based on streamlit, so I can't use "threaded=False".
I need to use keras 2.3.1, tensorflow 2.0.0.

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

not work for me!
@fchollet @pratikpc

Try keras 2.2.5 and tf 1.15.

@jackexu
Thanks for reply. But my project need to use the new characteristics of keras2.3.1 and tf2.0.
I cannot load model under keras 2.x.
So this put me into dilemma!
Hope to solve the problem using keras2.3.1 and tf2.0.

@jackexu
Thanks for reply. But my project need to use the new characteristics of keras2.3.1 and tf2.0.
I cannot load model under keras 2.x.
So this put me into dilemma!
Hope to solve the problem using keras2.3.1 and tf2.0.

I don't think it will be solved soon...so I built a venv and re-trained the model under 2.2.5...

Under app.py, adding threaded=False under app.run() solved the issue for me i.e app.run(threaded=False)

I see this issue using keras 2.3.1, tf 2.0.0. Specifically if I create a model in the data generation method of a generator, and then use the generator with fit_generator. I am using jupyter notebook for testing, with Windows 7, Anaconda with Python 3.7.3

If I instead use a model in the generator that was created directly in a cell in the notebook I do not see the issue. I then cannot use the max queue size and workers parameters with that model as it eventually hangs.

Any news on when Keras may fix this issue? Downgrading is not the best way to resolve this ...

Just use Tensorflow 2.0 and

from tensorflow.keras.models import model_from_json
instead of
from keras.models import model_from_json

was solved this problem for me.

Hi all,
Tensorflow 2.0.0
Keras 2.3.1

You can just use
model = tensorflow.keras.models.load_model(model_path)
instead of
keras.models.load_model(model_path)
it works for me!
https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model?version=stable

you can also check these depending on the tensorflow version:
tf.compat.v1.keras.models.load_model(model_path)
tf.compat.v2.keras.models.load_model(model_path)

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

thanks , it works for me +1.

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS

In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS

In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Do I need to make these all change?

from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, Callback
from keras.layers import Dense, Lambda
from keras.optimizers import Adam

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Do I need to make these all change?

from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, Callback
from keras.layers import Dense, Lambda
from keras.optimizers import Adam

Yes.
Try to replace keras by tensorflow.keras.

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Do I need to make these all change?

from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, Callback
from keras.layers import Dense, Lambda
from keras.optimizers import Adam

Yes.
Try to replace keras by tensorflow.keras.

Thank you very much for your help!
It works well!

Yes @ccasadei - this worked for me as well. I encountered another variable causing the similar 'value' attribute problem - _DISABLE_TRACKING. I set it to True/False - didn't seem to matter. Obviously this is not a solution just a workaround. But it does get threading working with keras 2.3.1 / tensorflow 2.0

Where did you _DISABLE_TRACKING?

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

Can you provide us the link?

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

Can you provide us the link?

https://github.com/keras-team/keras

if anyone is tying to integrate the model with Django need to make the graph global and clear the session . I am going to write how

Before loading your trained model write these lines

config = tf.ConfigProto(
intra_op_parallelism_threads=1,
allow_soft_placement=True
)
session = tf.Session(config=config)
keras.backend.set_session(session)

Than while model prediction write in this way

with session.as_default():
with session.graph.as_default():
prediction = model_name.predict(input_arguments)

while calling your function through views.py file
import below file

from keras.backend import clear_session

and just before calling the function of your model prediction write

clear_session()

use these versions it will solve the problem
pip install tensorflow=1.15
pip install keras==2.3.1

The Problem is with Tensorflow.
1- You need to upgrade keras to latest version.
2- Downgrade tensorflow to 1.15 using
pip unintall tensorflow
pip install tensorflow==1.15
It worked for me.

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

This also works for me, Thank you so much.

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Do I need to make these all change?

from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, Callback
from keras.layers import Dense, Lambda
from keras.optimizers import Adam

Yes.
Try to replace keras by tensorflow.keras.

Try to replace keras by tensorflow.keras.
Issue solved.

Tried to replace keras by tensorflow.keras. It worked for me. and issue resolved.
tensorflow: 2.0.0
keras: 2.3.1

It works for me if I do these two things:

  1. app.run(threaded=False)
  2. export FLASK_DEBUG=0

My env:
keras 2.3.1
tensorflow 2.1.0
Mac OS
flask 1.1.1
python 3.7.4

I noticed with FLASK_DEBUG=1, I see the message "Using TensorFlow backend." twice at startup.

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

Holy crap! Been looking at this problem for a LONG TIME. This worked for me. Thank you so much!!!

Replacing keras by tensorflow.keras does NOT work for me.
And yes, if I disable threads in Flask it will work, but that is not an option in production at all!

what is the right solution to this?

Just use Tensorflow 2.0 and

from tensorflow.keras.models import model_from_json
instead of
from keras.models import model_from_json

was solved this problem for me.

It works!

Hello. Today we've faced the same problem. We've founded that Version ImageAI 2.1.5 works with tensorflow v 1.14.0 and keras v.2.2.5 so use:

pip install tensorflow==1.14
pip install keras==2.2.5

It's solved our problem.

thanks this actually worked, I was trying from days

For those who don't need the multithreading, and still get blocked by this issue try setting threaded=False in app.run

But what can we do in the case of the Django app?

#

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

#
#

Use

from tensorflow.keras.models import load_model
instead of
from keras.models import load_model

#
                  thank!!! can this solves in flask  both
                          TF 2.0.0    Keras 2.3.1

For Django :
Use this

python manage.py runserver --nothreading --noreload

it works for me

Just use Tensorflow 2.0 and

from tensorflow.keras.models import model_from_json
instead of
from keras.models import model_from_json

was solved this problem for me.

Works for me!

This worked for me:

I found an UGLY workaround....
In the main "post manager" function I put this code...

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

where as changing to tensorflow.keras instead of keras didn't (but I am importing from config, not json, that may be why)

thanks @ccasadei

for the record, I'm using keras 2.3.1, tensorflow 2.1.0, python 3.7.6, flask 1.0.3 using Anaconda on Win 10. However, I am now getting the _DISABLE_TRACKING error when try to run threaded mode on my server (Ubuntu 18.04 (Keras 2.3.0, TF 2.1.0, Python 3.6.6 and flask 1.0.2). I don't have the option to upgrade the keras on the server

can confirm.
keras==2.2.5 solve the problem.

I'm trying to use keras in flask, it raise an error:
'thread._local' object has no attribute 'value' error

please make sure you should make the value of threaded=False
Example :
for flask :
if __name__ == '__main__':
app.run(threaded=False)

For those who don't need the multithreading, and still get blocked by this issue try setting threaded=False in app.run

it's working for me
Example:
app.run(host="0.0.0.0",port=5000,threaded=False)

Thanks, mate.
It wokred for me

For Django :
Use this

python manage.py runserver --nothreading --noreload

it works for me

thank you, it works

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Do I need to make these all change?

from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, Callback
from keras.layers import Dense, Lambda
from keras.optimizers import Adam

Yes.
Try to replace keras by tensorflow.keras.

Thanks this worked for me

keras 2.3.1
TensorFlow 2.0.0
use flask freamwork
MacOS
In

from keras import Input
inp = Input(batch_shape=[batch_size, 39 * 10])

tell me

File "/Users/obsidian/source/voice-print-demo/backend/train_cli.py", line 50, in triplet_softmax_model
    inp = Input(batch_shape=[batch_size, 39 * 10])
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/Users/obsidian/source/voice-print-demo/backend/venv/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

can you help me?

Try to use:
from tensorflow.keras import Input
instead of
from keras import Input

Unfortunately, this works when I make changes inside the mtcnn package, and when using a requirements.txt, I'll have to add an extra note, to change from keras; to from tensorflow.keras :( any other better solutino?

Downgrading Keras from 2.3.0 to 2.2.5 sovled my problem but I have encountered another problem, "Unexpected keyword argument passed to optimizer: learning_rate". Because I trained my model in Keras 2.3.0 but now I'm trying to predict in 2.2.5.

I have solved this issue with:

model = load_model('path to model', compile=False)

Then compiled it with proper parameters. And it worked.

Tried to replace keras by tensorflow.keras. It worked for me. and issue resolved.
tensorflow: 2.0.0
keras: 2.3.1

It works for me. Thx.

I recommend checking out this solution by RobertLucian. It worked for me, hopefully this helps!

Try to use tf.keras

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

You beautiful human, thank you 😀

There is no problem in keras code people who are using it with different session are actually facing this issue.
For example if you are trying to call predict function from Django ,it will create two different session so you just have to take care of global graph and make sure there is same session in both ,than your problem will be solved

For Django :
Use this

python manage.py runserver --nothreading --noreload

it works for me

thanks man it worked for me:
device:2019macbook pro 13inch
tensorflow=2.2.0
keras=2.3.1
Django

having the same issue with tensorflow 2.2.0 and keras 2.3.1, but i need multithreading
i solved using tf.keras api

For Django :
Use this

python manage.py runserver --nothreading --noreload

it works for me

thanks man, it works for me (I change python to python3 because I use python3.6)
tensorflow = 2.2.0
keras = 2.3.1
django = 3.0.1

Confirm
Flask with threaded = False works
with Keras = 2.3.1 and Tensorflow 2.1.0
But how could use the thread pool?
And Keras-bert Keras-xlnet does not work for Tensorflow.keras

I'm using keras 2.3.1, tensorflow 2.0.0, python 3.6, linux Mint 19.2 Cinnamon.
My project is based on Waitress, so I can't use "threaded=False".

I found an UGLY workaround....
In the main "post manager" function I put this code...

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

And this solved the problem for me...
I hope it will be useful to find a solution

I'm using keras 2.3.1, tensorflow 2.0.0, python 3.6, linux Mint 19.2 Cinnamon.
My project is based on Waitress, so I can't use "threaded=False".

I found an UGLY workaround....
In the main "post manager" function I put this code...

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

And this solved the problem for me...
I hope it will be useful to find a solution

A million thanks @ccasadei it works for me

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

You beautiful human, thank you 😀

What a holy crap! and absolutely magic!

tensorflow==2.0.0
keras==2.3.1

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

Use
from tensorflow.keras.models import Sequential
instead of
from keras.models import Sequential
works for me

Thank you

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

Holy crap! Been looking at this problem for a LONG TIME. This worked for me. Thank you so much!!!

Use
from tensorflow.keras.models import load_model
instead of
from keras.models import load_model
works for me +1

Holy crap! Been looking at this problem for a LONG TIME. This worked for me. Thank you so much!!!

For me too! However, I used

from keras.models import model_from_json

which failed, now this works

from tensorflow.keras.models import model_from_json

It was an issue in Flask implementation:

keras==2.3.1

For those who don't need the multithreading, and still get blocked by this issue try setting threaded=False in app.run

Thanks a lot. It resolves my issue.

I was also getting the same error while I was trying to get predictions from a saved model and deploying them on streamlit.
The issue was resolved by replacing all occurrences of keras by tensorflow.keras even in all other packages that you are using like I was using mtcnn face recognizer so I had to edit its network.py file as it was using keras.

I'm facing a similar problem. But I'm not using Flask. I just want to start Keras process on a different thread.

class ConstructKeras():
    def fire_and_forget(f):
        def wrapped(self, patches_size):
            threading.Thread(target=f, args=(self,  patches_size)).start()

        return wrapped

    @fire_and_forget
    def run_code(self, patches_size):
        input_patch = Input( shape = (patches_size[0], patches_size[1], patches_size[2]), name = 'input' )

        flatten = Flatten( name = 'flatten' )( input_patch )

        encoder_1 = Dense( 256, activation = 'tanh', name='encoder_1' )( flatten )
        encoder_2 = Dense( 128, activation = 'tanh', name='encoder_2' )( encoder_1 )
        encoder_3 = Dense( 64, activation = 'tanh', name='encoder_3' )( encoder_2 )
        decoder_2 = Dense( 128, activation = 'tanh', name = 'decoder_2' )( encoder_3 )
        decoder_3 = Dense( 256, activation = 'tanh', name = 'decoder_3' )( decoder_2 )
        decoder_4 = Dense( 512, activation = 'linear', name = 'decoder_4' )( decoder_3 )

        reshape = Reshape( target_shape = ( patches_size[0], patches_size[1], patches_size[2] ), 
                           name = 'reshape' )( decoder_4 )

        model = Model( input_patch, reshape )

        model.compile( optimizer = 'adam', loss = 'mean_squared_error')
def exec_async():
    new_obj = ConstructKeras()

    new_obj.run_code([8,8,8])

    return 'Process started'



md5-163f0e3bdced9a97d7303d5a565b3b6d



msg = exec_async()
print(msg)
print('out')



md5-c825c911e228932969265e8c00289841



Process started
out

Exception in thread Thread-7:
Traceback (most recent call last):
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-12-b2523eb62f77>", line 10, in run_code
    input_patch = Input( shape = (patches_size[0], patches_size[1], patches_size[2]), name = 'input' )
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/site-packages/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/home/data_science/anaconda3/envs/web_api/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
    if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'

Can anyone help me?

threaded=False

it works for me, thanks

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

Please tell me how to do this.

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

Please tell me how to do this.

type this command in the shell prompt:

pip install git+https://github.com/keras-team/keras.git

https://stackoverflow.com/a/63322811/7184343

For Django here's the answer

As far as I understand, the issue can be solved only adopting single-threading, which for a production setting is pretty funny. Is this understanding correct?

This has been fixed recently. Please try to install Keras from the master branch on GitHub.

Please tell me how to do this.

type this command in the shell prompt:

pip install git+https://github.com/keras-team/keras.git

This is the easiest solution!

Hi all,

I don't know what's the status on this issue, but I experienced it as well and was able to run a threaded (app.run(threaded=True)) Flask API. I'm even able to create a new separate thread within each request.

Had to downgrade to Python 3.7 (x64), and here is the combination of versions I'm using:

flask==1.1.2
flask-cors==3.0.9
tensorboard==1.15.0
tensorflow==1.15.0
tensorflow-estimator==1.15.1
Keras==2.3.1
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  ·  3Comments

nryant picture nryant  ·  3Comments

braingineer picture braingineer  ·  3Comments

LuCeHe picture LuCeHe  ·  3Comments

KeironO picture KeironO  ·  3Comments