segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

module 'keras.utils' has no attribute 'generic_utils'

Open AbtinDjavadifar opened this issue 3 years ago • 40 comments

Hi,

I'm trying to run multiclass segmentation (camvid).ipynb example and get an error after running the following block:

import segmentation_models as sm

Here is the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-93aee672c619> in <module>
----> 1 import segmentation_models as sm
      2 
      3 # segmentation_models could also use `tf.keras` if you do not have Keras installed
      4 # or you could switch to other framework using `sm.set_framework('tf.keras')`

E:\PycharmProjects\Autometrics\Spattering_segmentation\segmentation_models\__init__.py in <module>
----> 1 from .segmentation_models import *

E:\PycharmProjects\Autometrics\Spattering_segmentation\segmentation_models\segmentation_models\__init__.py in <module>
     96 _framework = os.environ.get('SM_FRAMEWORK', _DEFAULT_KERAS_FRAMEWORK)
     97 try:
---> 98     set_framework(_framework)
     99 except ImportError:
    100     other = _TF_KERAS_FRAMEWORK_NAME if _framework == _KERAS_FRAMEWORK_NAME else _KERAS_FRAMEWORK_NAME

E:\PycharmProjects\Autometrics\Spattering_segmentation\segmentation_models\segmentation_models\__init__.py in set_framework(name)
     66     if name == _KERAS_FRAMEWORK_NAME:
     67         import keras
---> 68         import efficientnet.keras  # init custom objects
     69     elif name == _TF_KERAS_FRAMEWORK_NAME:
     70         from tensorflow import keras

~\AppData\Roaming\Python\Python37\site-packages\efficientnet\keras.py in <module>
     15 preprocess_input = inject_keras_modules(model.preprocess_input)
     16 
---> 17 init_keras_custom_objects()

~\AppData\Roaming\Python\Python37\site-packages\efficientnet\__init__.py in init_keras_custom_objects()
     69     }
     70 
---> 71     keras.utils.generic_utils.get_custom_objects().update(custom_objects)
     72 
     73 

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

Can anyone help me to solve this?

AbtinDjavadifar avatar Jul 19 '20 22:07 AbtinDjavadifar

What version of Keras do you have installed? Is it the same as the version required by the version of Segmentation Models you currently have installed?

JordanMakesMaps avatar Jul 21 '20 18:07 JordanMakesMaps

same here

  File "/home/nvidia/pellame-g4f/lam/backend/lambackend/services/analysis_svc.py", line 6, in <module>
    import segmentation_models as sm
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/segmentation_models/__init__.py", line 98, in <module>
    set_framework(_framework)
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/segmentation_models/__init__.py", line 68, in set_framework
    import efficientnet.keras  # init custom objects
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/efficientnet/keras.py", line 17, in <module>
    init_keras_custom_objects()
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/efficientnet/__init__.py", line 71, in init_keras_custom_objects
    keras.utils.generic_utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils' has no attribute 'generic_utils'

Installed versions (inside a v.env. on Jetson TX2):

  • Keras 2.4.3
  • Keras-Applications 1.0.8
  • segmentation-models 1.0.1
  • tensorflow 2.2.0+nv20.6

visual-engines avatar Jul 22 '20 16:07 visual-engines

What version of Keras do you have installed? Is it the same as the version required by the version of Segmentation Models you currently have installed?

Yes, it's the same.

AbtinDjavadifar avatar Jul 22 '20 16:07 AbtinDjavadifar

same here

  File "/home/nvidia/pellame-g4f/lam/backend/lambackend/services/analysis_svc.py", line 6, in <module>
    import segmentation_models as sm
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/segmentation_models/__init__.py", line 98, in <module>
    set_framework(_framework)
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/segmentation_models/__init__.py", line 68, in set_framework
    import efficientnet.keras  # init custom objects
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/efficientnet/keras.py", line 17, in <module>
    init_keras_custom_objects()
  File "/home/nvidia/virtualenvs/venv_lam/lib/python3.6/site-packages/efficientnet/__init__.py", line 71, in init_keras_custom_objects
    keras.utils.generic_utils.get_custom_objects().update(custom_objects)
AttributeError: module 'keras.utils' has no attribute 'generic_utils'

Installed versions (inside a v.env. on Jetson TX2):

  • Keras 2.4.3
  • Keras-Applications 1.0.8
  • segmentation-models 1.0.1
  • tensorflow 2.2.0+nv20.6

I changed keras.utils.generic_utils.get_custom_objects().update(custom_objects) to keras.utils.get_custom_objects().update(custom_objects) in .../lib/python3.6/site-packages/efficientnet/__init__.py and it solved the issue.

AbtinDjavadifar avatar Jul 22 '20 17:07 AbtinDjavadifar

*TLDR *: export SM_FRAMEWORK=tf.keras before running your code.

error happens in efficientnet.keras package but not in efficientnet.tfkeras. But importing segmentation_models initializes keras backend by default as per (Line 48, segmentation_models/init.py):

def framework(): """Return name of Segmentation Models framework"""

return _KERAS_FRAMEWORK

So, sm.set_framework cannot be called before this error happens. You can either modify the init.py to return _TF_KERAS_FRAMEWORK_NAME or export it in command line as follows. export SM_FRAMEWORK=tf.keras before running your code.

On Thu, Jul 23, 2020 at 12:26 PM Zhanglibingo [email protected] wrote:

you can install tensorflow-gpu by conda install tensorflow-gpu==1.14.0 again.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/qubvel/segmentation_models/issues/374#issuecomment-662848300, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFCHUQMKU7D2LK6D73456DR47NIBANCNFSM4PBUBKTQ .

4g avatar Jul 23 '20 07:07 4g

@qubvel Can someone have a look? We are unable to reuse your code. (Practically we can via forking your code.) But it is better if we can fix it in stable branch.

vikalpjain91 avatar Aug 05 '20 02:08 vikalpjain91

Hi, I used segmentation-models for building segmentation in google Colab . my code worked correctly until yesterday. But today I have this error: AttributeError: module 'keras.utils' has no attribute 'generic_utils' How can i fix it?

m1vahid avatar Aug 05 '20 08:08 m1vahid

@JordanMakesMaps any update? Can you please provide workaround? I believed requirements.txt should be correct atleast to make it work!

vikalpjain91 avatar Aug 09 '20 21:08 vikalpjain91

Hi all this is the correct requirements to make it working! working after multiple trial and errors. !pip install keras==2.3.1 !pip install tensorflow==2.1.0 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0

vikalpjain91 avatar Aug 09 '20 21:08 vikalpjain91

Same Issue: https://github.com/qubvel/segmentation_models/issues/380

vikalpjain91 avatar Aug 09 '20 21:08 vikalpjain91

Hi all this is the correct requirements to make it working! working after multiple trial and errors. !pip install keras==2.3.1 !pip install tensorflow==2.1.0 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0

Thanks. Could you start training using tf.keras instead of keras? I really need to do that as I want to convert the trained model to tf lite and the training has to be done via tf.keras.

AbtinDjavadifar avatar Aug 10 '20 01:08 AbtinDjavadifar

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

sezazqureshi avatar Aug 12 '20 07:08 sezazqureshi

tensorflow = "<2.3" keras = "<2.4" albumentations = "==0.4.6" segmentation-models = "==1.0.1"

davidkwast avatar Aug 20 '20 15:08 davidkwast

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

This solved it for me on Google Colab. You are a life saver.

namanbansalcodes avatar Nov 06 '20 02:11 namanbansalcodes

downgrade the tensorflow to 2.1.0

pip install tensorflow==2.1.0

tarunnith avatar Nov 17 '20 14:11 tarunnith

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

Solved my problem on Kaggle thank you.

kool7 avatar Nov 30 '20 09:11 kool7

Hi all this is the correct requirements to make it working! working after multiple trial and errors. !pip install keras==2.3.1 !pip install tensorflow==2.1.0 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0

Thanks @vikalpjain91. Uninstalling everything, then downloading/installing in this order: tensorflow keras segmentation-models

worked for me.

JordanMakesMaps avatar Dec 22 '20 22:12 JordanMakesMaps

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

IT worked for me

harshwalia36 avatar Jan 22 '21 13:01 harshwalia36

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

It works.Thanks a lot!

jiajia0818-fighting avatar Jan 26 '21 03:01 jiajia0818-fighting

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

big thanks this work to me

OmarSalah26 avatar Jan 27 '21 18:01 OmarSalah26

Hi all this is the correct requirements to make it working! working after multiple trial and errors. !pip install keras==2.3.1 !pip install tensorflow==2.1.0 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0

Thanks dude

kaushik25T avatar Feb 05 '21 06:02 kaushik25T

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

Worked for me ! Thanks

nikhilwohlig avatar Apr 01 '21 08:04 nikhilwohlig

%env SM_FRAMEWORK=tf.keras try this before importing segmentation models , it solved my problem.

Worked for me ! Thanks

this works

wndxwilson avatar Apr 05 '21 15:04 wndxwilson

I ran into the same problem (using tensorflow 2.3.2) and I can't downgrade TF due to other dependencies. Is there any chance that this codebase is getting an update so that the more current versions of TF can be used with it?

markusuwe avatar Apr 23 '21 07:04 markusuwe

I ran into the same problem (using tensorflow 2.3.2) and I can't downgrade TF due to other dependencies. Is there any chance that this codebase is getting an update so that the more current versions of TF can be used with it?

Replacing all instances of import keras.whatever with import tensorflow.keras.whatever

Fixes the issue for me in tensorflow 2.4.0 and 2.5.0-rc1.

JoshLikesBeer avatar Apr 23 '21 17:04 JoshLikesBeer

Please create a PR if you cone up with solution

qubvel avatar Apr 23 '21 17:04 qubvel

For kaggle installing in this order solved my issue:- !pip install tensorflow==2.1.0 !pip install keras==2.3.1 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0 !pip install -U segmentation-models==1.0.1

%env SM_FRAMEWORK=tf.keras

Link to kaggle notebook - https://www.kaggle.com/shubhamdivakar/qubvel-segmentation-code/

shubham10divakar avatar May 16 '21 07:05 shubham10divakar

I have the same issue, but i try above of them. !pip install tensorflow==2.1.0 !pip install keras==2.3.1 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0 !pip install -U segmentation-models==1.0.1

%env SM_FRAMEWORK=tf.keras

But I try it on jupyter notebook still has the problem. Is there has any method to fix it?

P66094125 avatar May 17 '21 14:05 P66094125

I have the same issue, but i try above of them. !pip install tensorflow==2.1.0 !pip install keras==2.3.1 !pip install keras_applications==1.0.8 !pip install image-classifiers==1.0.0 !pip install efficientnet==1.0.0 !pip install -U segmentation-models==1.0.1

%env SM_FRAMEWORK=tf.keras

But I try it on jupyter notebook still has the problem. Is there has any method to fix it?

I have not worked on the Jupyter notebook but for the kaggle %env SM_FRAMEWORK=tf.keras woks fine.

tarunnith avatar May 18 '21 05:05 tarunnith

I ran into the same problem (using tensorflow 2.3.2) and I can't downgrade TF due to other dependencies. Is there any chance that this codebase is getting an update so that the more current versions of TF can be used with it?

Replacing all instances of import keras.whatever with import tensorflow.keras.whatever

Fixes the issue for me in tensorflow 2.4.0 and 2.5.0-rc1.

While converting from keras to tensorflow.keras, I am facing some issues like cannot convert following;

from keras.engine.topology import Network

Can you help me how to convert it to tf.keras. Tq.

zahidmb avatar May 26 '21 07:05 zahidmb

import os
os.environ["SM_FRAMEWORK"] = "tf.keras" #before the import
from segmentation_models import PSPNet

Solved the issue for me

francescotaioli avatar Jun 23 '21 07:06 francescotaioli

I am using Keras from TensorFlow. Using TensorFlow 2.5, no problem after following:

Option 1: In the start of notebook, change the environment variable as %env SM_FRAMEWORK=tf.keras

Option 2: Another way to change environment variable. import segmentation_models as sm sm.set_framework('tf.keras')

Problem solved for TensorFlow 2.5.

creativesalam avatar Jul 07 '21 00:07 creativesalam

%env SM_FRAMEWORK=tf.keras

try this before importing segmentation models , it solved my problem.

Thank you saviour .

udayzee05 avatar Oct 18 '21 15:10 udayzee05

%env SM_FRAMEWORK=tf.keras try this before importing segmentation models , it solved my problem.

This solved it for me on Google Colab. You are a life saver.

Worked for me as well!

salimsoltani28 avatar Jun 15 '22 09:06 salimsoltani28