Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
clime opened this issue May 6, 2021 · 42 comments · Fixed by ANTsX/ANTsPyNet#38
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@clime
Copy link

clime commented May 6, 2021

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
@nzw0301
Copy link
Contributor

nzw0301 commented May 21, 2021

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.

@Nikeshbajaj
Copy link

Same issue here, with tensoflow 2.5.0, updated verion of keras doesn't work. Any plans to fix this issue.

@YiminGao0113
Copy link

Same issue here!

@James-Ang
Copy link

same issue

@a0s
Copy link

a0s commented May 23, 2021

same issue here with plaidbench keras mobilenet (as described in https://www.micah.soy/posts/machine-learning-ai-on-macos-catalina-with-metal-gpu-support/)

@SpeedyCraftah
Copy link

same here with a standard keras installation

@zaid-kamil
Copy link

same issue
AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'
File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script exec(code, module.__dict__) File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\app.py", line 11, in <module> from enhancer import resolution_dlss File ".\enhancer.py", line 6, in <module> from keras.models import load_model File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\__init__.py", line 3, in <module> from . import utils File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\utils\__init__.py", line 26, in <module> from .vis_utils import model_to_dot File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module> from ..models import Model File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\models.py", line 10, in <module> from .engine.input_layer import Input File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\engine\__init__.py", line 3, in <module> from .input_layer import Input File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\engine\input_layer.py", line 7, in <module> from .base_layer import Layer File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\engine\base_layer.py", line 12, in <module> from .. import initializers File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\initializers\__init__.py", line 124, in <module> populate_deserializable_objects() File "C:\Users\xaidi\GitHub\Image-resolution-enhancer-using-DLSS\tf25\lib\site-packages\keras\initializers\__init__.py", line 82, in populate_deserializable_objects generic_utils.populate_dict_with_module_objects(

keras == 2.4.3
tensorflow==2.5.0
image

@khanfarhan10
Copy link

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

@khanfarhan10
Copy link

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

@aliwelchoo
Copy link

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

@afdeaf
Copy link

afdeaf commented May 24, 2021

same issue when import hyperas

@ghost
Copy link

ghost commented May 24, 2021

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.

@a0s
Copy link

a0s commented May 24, 2021

@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

@ghost
Copy link

ghost commented May 27, 2021

`# import tensorflow.compat.v2 as tf
import tensorflow as tf
from tf import keras
.
tf.keras.generic_utils.populate_dict_with_module_objects(
@keras_export('keras.initializers.serialize')
def serialize(initializer):
return tf.keras.generic_utils.serialize_keras_object(initializer)

@keras_export('keras.initializers.deserialize')
def deserialize(config, custom_objects=None):
"""Return an Initializer object from its config."""
populate_deserializable_objects()
return tf.keras.generic_utils.deserialize_keras_object(
config,
module_objects=LOCAL.ALL_OBJECTS,
custom_objects=custom_objects,
printable_module_name='initializer')

.

`

@MatthiasRakotomalala
Copy link

same issue

@Jeevanvarmap
Copy link

replace: import keras.utils as generic_utils

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

@Ammyvijay
Copy link

Ammyvijay commented May 28, 2021

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

@Maximevdm
Copy link

I'm having trouble with keras_self_attention import SeqSelfAttention.
It gives the error:
generic_utils.populate_dict_with_module_objects(
AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

Does someone know how to solve this problem?

@anasfathi0
Copy link

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

can someone explain to how to do this, i still new and do not understand

@wings-cofandt
Copy link

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

@khanfarhan10
Copy link

@anasfathi0 Instead of using the keras library explicitly, use tensorflow backend and call keras implicitly.

@wings-cofandt
Copy link

wings-cofandt commented Jun 1, 2021 via email

@phaneendhra-ch
Copy link

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.

@immanuelweule
Copy link

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!!

@mgioioso
Copy link

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.

theodonkulus added a commit to theodonkulus/ECE657 that referenced this issue Jul 15, 2021
Thanks to this had to run a fun pip install force reinstall
keras-team/keras#14632

Solution found here after reinstalling tensorflow with updated keras
https://stackoverflow.com/questions/67703871/importerror-cannot-import-name-to-categorical-from-keras-utils-usr-local

Things run now.
ravnoor added a commit to ravnoor/ANTsPyNet that referenced this issue Jul 16, 2021
@Well2333
Copy link

same issue here QwQ

@Well2333
Copy link

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

THANKSSSSSS, IT WORK!!!!!!

@BertramGilfoyle1
Copy link

BertramGilfoyle1 commented Aug 6, 2021

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

@nschang
Copy link

nschang commented Aug 16, 2021

Solved on google Colab (CUDA==11.0; tensorflow==1.15) by adding tensorflow.python. to keras. in import:

# NOT WORKING
>>> from keras.initializers import initializers_v1
>>> from keras.initializers import initializers_v2
>>> from keras.utils import generic_utils
>>> from keras.utils import tf_inspect as inspect

this gives an error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-fa830678264f> in <module>()
----> 1 from keras.utils import generic_utils
      2 from tensorflow.python.keras.utils import tf_inspect as inspect

8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
     80     v2_objs = {}
     81     base_cls = initializers_v2.Initializer
---> 82     generic_utils.populate_dict_with_module_objects(
     83         v2_objs,
     84         [initializers_v2],

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

this works:

>>> from tensorflow.python.keras.initializers import initializers_v1
>>> from tensorflow.python.keras.initializers import initializers_v2
>>> from tensorflow.python.keras.utils import generic_utils
>>> from tensorflow.python.keras.utils import tf_inspect as inspect

@Abdurrasith
Copy link

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?

@Gabriel0598
Copy link

Hello, i updated the TensorFlow through Anaconda Prompt with command line: pip install tensorflow --update, after it is in version 3.1.1. With it the problem was solved, it in my case, because i'm using the tensorflow through Anaconda platform.

@ymodak
Copy link
Collaborator

ymodak commented Sep 4, 2021

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

@ymodak ymodak closed this as completed Sep 4, 2021
hluetck added a commit to HelmchenLabSoftware/Cascade that referenced this issue Sep 6, 2021
going back to Tensorflow 2.2.1 as 2.5.1 is not compatible with Keras version. For details see keras-team/keras#14632
@mdrhmn
Copy link

mdrhmn commented Sep 20, 2021

I'm having trouble with keras_self_attention import SeqSelfAttention.
It gives the error:
generic_utils.populate_dict_with_module_objects(
AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

Does someone know how to solve this problem?

Have you managed to solve the issue?

@eman85
Copy link

eman85 commented Dec 29, 2021

pip install keras-self-attention==0.50.0

this is works for me

@yoyoisaboy
Copy link

yoyoisaboy commented Jan 6, 2022

from keras.utils import np_utils
change
from keras.utils.np_utils import *

sometime work...

@ravi-aii
Copy link

Reinstalling the tensorflow solved my same issue
pip install tensorflow --upgrade --force-reinstall --user

@mikechen66
Copy link

mikechen66 commented Nov 15, 2022

Please add the snippet of the code into the program of general_utils.py

1. Find out your path

For me, the path is listed as follows:

/home/user/miniconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py

2. Paste the code into the program of generic_utils.py


def populate_dict_with_module_objects(target_dict, modules, obj_filter):
  for module in modules:
    for name in dir(module):
      obj = getattr(module, name)
      if obj_filter(obj):
        target_dict[name] = obj

3. Initialize the dev tool

It will be working after initializing the dev tool (it is Terminal for me in Linux)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

Successfully merging a pull request may close this issue.