31

When I import keras, the error above pops up even though it was working fine yesterday.

How do I resolve this error?

I am working on windows 10 my keras version is: 2.2.4 my tensorflow version is: 2.2.0rc2

complete error traceback is seen below as such:

Traceback (most recent call last):

    from keras import models
  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\__init__.py", line 3, in <module>

    from . import utils
  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\__init__.py", line 6, in <module>

    from . import conv_utils

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>

    from .. import backend as K

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\__init__.py", line 1, in <module>

    from .load_backend import epsilon

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\load_backend.py", line 90, in <module>

    from .tensorflow_backend import *

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>

    import tensorflow as tf

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\__init__.py", line 41, in <module>

    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\__init__.py", line 84, in <module>

    from tensorflow.python import keras

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\__init__.py", line 27, in <module>

    from tensorflow.python.keras import models

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\models.py", line 24, in <module>

    from tensorflow.python.keras import metrics as metrics_module

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\metrics.py", line 37, in <module>

    from tensorflow.python.keras.engine import base_layer

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 51, in <module>

    from tensorflow.python.keras import initializers

  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\initializers\__init__.py", line 127, in <module>

    populate_deserializable_objects()
  File "C:\Users\world\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\initializers\__init__.py", line 85, in populate_deserializable_objects
    generic_utils.populate_dict_with_module_objects(
AttributeError: module 'tensorflow.python.keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'
3
  • 1
    Keras 2.2.4 does not support TF 2.0, you need to use Keras 2.3 for that to work. Commented Apr 10, 2020 at 11:59
  • I upgraded Keras and still the same error occurs. @MatiasValdenegro Commented Apr 10, 2020 at 12:42
  • After installing tf-nightly. The error became this : AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike' when loading a model using load_model() @MatiasValdenegro Commented Apr 10, 2020 at 15:01

17 Answers 17

19

change from keras import models to from tensorflow.keras import models
this solved the problem for me with tensorflow 2.5.0

Sign up to request clarification or add additional context in comments.

1 Comment

worked for me! I faced the error as I was trying to load CIFAR10 from keras datasets.
16

I had the same problem, and I have successfully solved this issue with downgrading tensorflow version to 2.1.0.

pip install tensorflow==2.1.0

4 Comments

Sorry just saw this but thank you! I resolved it by just reinstalling all the libraries again.
When I use this I get this error: ERROR: Could not find a version that satisfies the requirement tensorflow==2.4.1 (from versions: 2.5.0rc0, 2.5.0rc1) ERROR: No matching distribution found for tensorflow==2.4.1 Could I have any help?
@manarinian use the most recent version : this one 2.5.0rc0 or this on 2.5.0rc1 version pypi.org/project/tensorflow-cpu/#history
downgrading is not a good solution on the long term. thee solution to import tensorflow.keras.xxxx instead of import kreas.xxxx is better!
15

Changed from keras.utils import _____

to from tensorflow.python.keras.utils import _____

This worked for me while using TensorFlow 2.5.0

Comments

7

I encountered the same problem using Python 3.9 and Tensorflow 2.5. The problem for me was that those two are not yet compatible, and as such, the solution is to install python 3.8 instead, and possibly also downgrade Tensorflow 2.5 to Tensorflow 2.4.

Comments

6

Please copy "populate_dict_with_module_objects" function from this link (line 1162 to 1167) and add it to "generic_utils.py"

2 Comments

Perhaps isn't a long-time reproducible solution!
Exactly! The linked file is not even linked to a commit, and line 827 is a docstring (""") right now.
5

I've gotten around this by uninstalling Keras and changing anything I import from Keras to instead import from tensorflow.keras

So this:

    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.applications.vgg16 import preprocess_input
    from keras.applications.vgg16 import decode_predictions
    from keras.applications.vgg16 import VGG16

became this:

    from tensorflow.keras.preprocessing.image import load_img
    from tensorflow.keras.preprocessing.image import img_to_array
    from tensorflow.keras.applications.vgg16 import preprocess_input
    from tensorflow.keras.applications.vgg16 import decode_predictions
    from tensorflow.keras.applications.vgg16 import VGG16

and then I didn't have to amend the rest of my work

Comments

4
%env SM_FRAMEWORK=tf.keras

Try this code before importing segmentation models, it solved my problem.

1 Comment

Or always use tf.keras in your code. Don't use keras directly!
4

Replaced everything of keras to tf.keras !
Using Libs:

tensorflow-cpu==2.4.0
keras==2.4.0
import tensorflow as tf
from tensorflow import keras

Change layers as :

keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
tf.keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)

Change Optimizer:

keras.optimizers.Adam(lr=1e-5)
tf.keras.optimizers.Adam(lr=1e-5)

Change Operations:

keras.concatenate
tf.concat
keras.flatten
tf.keras.flatten
keras.sum
tf.keras.sum

Hope it helps someone stuck up there!

Comments

2

replace: from keras.utils import generic_utils

with: from tensorflow.python.keras.utils import generic_utils

Comments

2

Had this issue with tensorflow-gpu==2.5.0

I think the problem is caused some interaction between this two files (virtual env):

$ find env -name generic_utils.py
env/lib/python3.9/site-packages/tensorflow/python/keras/utils/generic_utils.py
env/lib/python3.9/site-packages/keras/utils/generic_utils.py

I added this import to env/lib/python3.9/site-packages/keras/utils/generic_utils.py:

from tensorflow.python.keras.utils.generic_utils import populate_dict_with_module_objects, to_snake_case

it seems worked.

See here: https://github.com/keras-team/keras/issues/14657

Comments

1

try uninstalling tf-nightly package with this pip uninstall tf-nightly and check python -c "import tensorflow". Hope this solves the problem

Comments

1

Try using :

pip install tensorflow --upgrade --force-reinstall

Comments

0

If using Google Colab, run this at the start of the notebook to avoid conflict with the default version provided by Colab. Also please note if tensorflow is already installed, it will restart the runtime. So, its better to do this at the start of the notebook rather than in the middle.

!pip install tensorflow==2.1.0

Comments

0

I installed Tensorflow and Keras using pip install tensorflow pip install keras and I got the same error in tensorflow 2.5.0. I uninstalled both and reinstalled with conda install tensorflow conda install keras

This resolved my error.

Comments

0

Try to replace

tensorflow.python.keras.utils.generic_utils

from this link.

1 Comment

Thanks for taking the time to contribute an answer. Please add context around the link so other users will have some idea what it is and why it’s there. You can add the most relevant part of an important link, in case the external resource is unreachable or goes permanently offline.
0

I upgraded keras and it worked. I used this command:

> pip install --upgrade keras

You might need to restart the kernel to see the effect.

Comments

0

Rather than downgrading or any other solution, directly import from tensorflow.keras

for eg: rather than using: from keras.models import Sequential use: from tensorflow.keras.model import Sequential

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.