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

Question:

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.

Asked By: Jehan Dastoor

||

Answers:

Instead of:

import keras

Try:

from tensorflow import keras 
Answered By: Anusha

These commands fixed the issue:

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu
Answered By: thomle295

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.

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.

Answered By: Alexandr Haymin

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.

Answered By: Kelvin Lee

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

Answered By: bart-khalid

I just solved this problem for myself.

# Instead of this:
from keras.preprocessing import image

# Do this:
from tensorflow.keras.preprocessing import image
Answered By: Jacob Torres
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!

Answered By: user16656561

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

Answered By: MD Gazuruddin

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 🙂

Answered By: aH6y

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
Answered By: Nihat Quliyev

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
Answered By: Riccardo Amadio
pip install --upgrade tensorflow

Then restart the kernal

Answered By: zahid

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
Answered By: Nevilmaloba

I use Spyder by Mac. At the first time, no matter I installed Tensorflow==2.1 or Tensorflow==2.0.0, which any version of Tensorflow, they didn’t work for keras. The below is my first codes and they all didn’t work.

import keras
from keras.models import Model, Sequential
from keras.layers import Input
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import RepeatVector
from keras.layers import TimeDistributed

But after I changed to the following below, they all work like others above.

from tensorflow import keras
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import RepeatVector
from tensorflow.keras.layers import TimeDistributed
Answered By: Adela Li 2022
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.