41

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.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Jehan Dastoor
  • 647
  • 1
  • 8
  • 15
  • 1
    Uninstall tf-nightly if you don't need it. – Innat Apr 06 '21 at 10:28
  • I had the same issue trying to import the to_categorical() method from keras.utils.np_utils. I didn't find a direct solution, but I managed to bypass the error by importing it from tensorflow.keras.utils instead... – JS Lavertu Apr 27 '21 at 15:34

15 Answers15

60

Instead of:

import keras

Try:

from tensorflow import keras 
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Anusha
  • 717
  • 3
  • 3
  • Still the same error on your new line: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/context.py) – High GPA Aug 12 '22 at 02:34
37

These commands fixed the issue:

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu
Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20
thomle295
  • 393
  • 2
  • 3
26

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

MD Gazuruddin
  • 540
  • 5
  • 9
15

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

bart-khalid
  • 198
  • 1
  • 9
6

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.

Kelvin Lee
  • 61
  • 2
6

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 :)

aH6y
  • 436
  • 4
  • 8
4

I just solved this problem for myself.

# Instead of this:
from keras.preprocessing import image

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

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
Skully
  • 2,882
  • 3
  • 20
  • 31
  • from keras.preprocessing.image import ImageDataGenerator - change it with from tensorflow.keras.preprocessing.image import ImageDataGenerator – Nihat Quliyev Nov 04 '21 at 19:38
  • 1
    Please use comments section under the question post for "non-answer" comments like "In my case I changed to and it worked for me.", thanks! – Dusan Gligoric Nov 04 '21 at 20:47
2
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!

blazej
  • 927
  • 4
  • 11
  • 21
1

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.

1

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.

Madhav M K
  • 77
  • 8
1

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
1
pip install --upgrade tensorflow

Then restart the kernal

zahid
  • 11
  • 1
0

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
0

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
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '22 at 19:17