'ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context'

My notebook was working up till today. At the beginning of my colab notebook I install tf-nightly, but now it is giving me this error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-589c442233c5> in <module>()
      7 import tensorflow as tf
      8 from tensorflow.keras import datasets, layers, models
----> 9 from keras.preprocessing import image
     10 from keras_preprocessing.image import ImageDataGenerator #check underscore or not
     11 from tensorflow.keras.preprocessing import image_dataset_from_directory

2 frames
/usr/local/lib/python3.7/dist-packages/keras/backend.py in <module>()
     35 from tensorflow.python.distribute import distribute_coordinator as dc
     36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
     38 from tensorflow.python.framework import config
     39 from keras import backend_config

ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/eager/context.py)

My code:

!pip install tf-nightly

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from keras.preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image_dataset_from_directory
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping

Installing tensorflow==2.1.0 did not work either.



Solution 1:[1]

These commands fixed the issue:

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

Solution 2:[2]

Instead of:

import keras

Try:

from tensorflow import keras 

Solution 3:[3]

Try the version of Keras 2.3.1

pip install keras==2.3.1

then also install that

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

Solution 4:[4]

instead of from keras.preprocessing import image do this: from tensorflow.keras.preprocessing import image

Solution 5:[5]

In my case with Google colab, I downgraded tensorflow to 2.2 and replaced all import keras.xxxx to import tensorflow.keras.xxxx. That fixed it.

Solution 6:[6]

I just solved this problem for myself.

# Instead of this:
from keras.preprocessing import image

# Do this:
from tensorflow.keras.preprocessing import image

Solution 7:[7]

I've updated

from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
from keras import layers, models, optimizers
from keras import backend as K

to

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras import backend as K

and it works :)

Solution 8:[8]

In my case I changed the import from the following:

from keras.preprocessing.image import ImageDataGenerator

To this:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

Solution 9:[9]

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

Works, but will interfere with the Tensorflow/NVIDIA CUDA Toolkit/NVIDIA cuDNN GPU-support set up. Better solution: unistall Keras 2.6.0, install Keras 2.4.3

The bug is fixed and GPU is running!

Solution 10:[10]

I had Tensorflow===2.2.0 and when I updated it to Tensorflow==2.4.0 this issue occurred.

I think there is a conflict of keras.models.load_model and the base version of tensorflow you are using.

Try running -

import tensorflow as tf
tf.keras.models.load_model(model_path)

It worked for me.

Solution 11:[11]

This strange problem corresponds with imports of tensorflow. If you change from keras.preprocessing import image to from tensorflow.keras.preprocessing import image it will work.

Solution 12:[12]

For me it just work upgrading tensorflow. Note: if you're using special tensorflow as gpu version, you must update this one because it's a separate module.

pip install --upgrade tensorflow

Solution 13:[13]

pip install --upgrade tensorflow

Then restart the kernal

Solution 14:[14]

I had the same problem after installing tensorflow cpu with the instructions from anaconda. I used the following codes

from keras_preprocessing.text import Tokenizer
from keras_preprocessing.sequence import pad_sequences