keras: AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

Needed to do: cp ./lib64/python3.9/site-packages/tensorflow/python/keras/utils/generic_utils.py ./lib/python3.9/site-packages/keras/utils/generic_utils.py in my virtualenv.

tensorflow-2.5.0rc3-cp39-cp39-manylinux2010_x86_64.whl
Keras-2.4.3-py2.py3-none-any.whl
Python 3.9.4

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 42
  • Comments: 42 (1 by maintainers)

Commits related to this issue

Most upvoted comments

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

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

dear keras-team, do you have any plan to release a newer stable version of Keras? Currently, the stable Keras 2.4.3 does not work with TensorFlow 2.5.0 due to the error reported in this issue.

Had the same issue, after I uninstalled all tensorflows and keras-nightly, I reinstalled tensorflow with pip install tensorflow --upgrade --force-reinstall and it worked.

replace: import keras.utils as generic_utils

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

Trust me the above might be a long solution but will work in the long run.

I’m not sure the status of solving the underlying issue(s) for this bug, but something I’ve discovered is that the order of installing packages matters here. When creating a new virtual environment (Python 3.8), using the following command

pip install keras tensorflow pillow

results in the traceback below when importing some modules. However, if you instead do:

pip install keras
pip install tensorflow
pip install pillow

the error doesn’t occur.

Traceback:

 File "imgTest.py", line 2, in <module>
    from keras.preprocessing import image
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/keras/__init__.py", line 20, in <module>
    from . import initializers
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/keras/initializers/__init__.py", line 124, in <module>
    populate_deserializable_objects()
  File "/Users/matthewlehew/Developer/googleimages/imageprojectvenv/lib/python3.8/site-packages/keras/initializers/__init__.py", line 82, in populate_deserializable_objects
    generic_utils.populate_dict_with_module_objects(
AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

Same Issue I had also seen,…

I update the importing header keras instead with tensorflow… Its working… all my model

from tensorflow.keras import Sequential from tensorflow.keras.models import load_model from tensorflow.keras.layers import Dense,LSTM

Had the same issue, after I uninstalled all tensorflows and keras-nightly, I reinstalled tensorflow with pip install tensorflow --upgrade --force-reinstall and it worked.

THANK YOU SO MUCH!!! YOU’RE A GOD!!

Yes it worked for me thanks everyone. Replacing keras with tensorflow.keras will definitely run your program

@aliwelchoo @khanfarhan10 The tensorflow.keras works fine, but using keras directly is the only way to get plaidml-keras and using Metal and AMD GPU during learning on Mac

pip install keras-self-attention==0.50.0

this is works for me

Closing this issue since its resolved. Feel free to post a new issue if still have problems. Thanks!

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

Where and what is the file i must edit this?

If you work on ubuntu and conda:

cp ./anaconda3/envs/{ENV}/lib/python3.9/site-packages/tensorflow/python/keras/utils/generic_utils.py ./anaconda3/envs/{ENV}/lib/python3.9/site-packages/keras/utils/generic_utils.py

{ENV} = your conda environment name

Replaced everything of keras to tf.keras

This is the way to go. Tensorflow will ensure the compatibility with keras if you use the sub-package.

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

This really solved the problem here! Thankssss

Thanks for responding, I used tensorflow.keras instead of keras and it worked

On Wed, Jun 2, 2021, 15:28 Ch.V.S. Phaneendhra @.***> wrote:

This sorted me out. I have uninstalled tensorflow, keras and keras-nightly and i have reinstalled again tensorflow and keras only. I hope this might be helpful.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14632#issuecomment-852912062, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANRLWQZMFUIHZKMGGXXFTCTTQYBTHANCNFSM44HJHL4A .

Hi Farhan,

Yeah I did that and it worked. Thanks very much

On Tue, Jun 1, 2021, 09:30 Farhan Hai Khan @.***> wrote:

@anasfathi0 https://github.com/anasfathi0 Instead of using the keras library explicitly, use tensorflow backend and call keras implicitly.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/keras-team/keras/issues/14632#issuecomment-851795978, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANRLWQ7UW2P4JVB57RDU4KTTQRO7DANCNFSM44HJHL4A .