qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.77k stars 1.03k forks source link

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

Open AbtinDjavadifar opened 4 years ago

AbtinDjavadifar commented 4 years ago

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?

JordanMakesMaps commented 4 years ago

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?

visual-engines commented 4 years ago

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

AbtinDjavadifar commented 4 years ago

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 commented 4 years ago

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.

4g commented 4 years ago

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 notifications@github.com 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 .

vikalpjain91 commented 4 years ago

@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.

m1vahid commented 4 years ago

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?

vikalpjain91 commented 4 years ago

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

vikalpjain91 commented 4 years ago

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 commented 4 years ago

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

AbtinDjavadifar commented 4 years ago

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.

sezazqureshi commented 4 years ago

%env SM_FRAMEWORK=tf.keras

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

davidkwast commented 4 years ago

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

namanbansalcodes commented 4 years ago

%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.

tarunnith commented 4 years ago

downgrade the tensorflow to 2.1.0

pip install tensorflow==2.1.0

kool7 commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

Solved my problem on Kaggle thank you.

JordanMakesMaps commented 3 years ago

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.

harshwalia36 commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

IT worked for me

jiajia0818-fighting commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

It works.Thanks a lot!

OmarSalah26 commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

big thanks this work to me

kaushik25T commented 3 years ago

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

nikhilwohlig commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

Worked for me ! Thanks

wndxwilson commented 3 years ago

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

Worked for me ! Thanks

this works

markusuwe commented 3 years ago

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?

JoshLikesBeer commented 3 years ago

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.

qubvel commented 3 years ago

Please create a PR if you cone up with solution

shubham10divakar commented 3 years ago

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/

P66094125 commented 3 years ago

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?

tarunnith commented 3 years ago

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.

zahidmb commented 3 years ago

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.

francescotaioli commented 3 years ago
import os
os.environ["SM_FRAMEWORK"] = "tf.keras" #before the import
from segmentation_models import PSPNet

Solved the issue for me

creativesalam commented 3 years ago

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.

udayzee05 commented 3 years ago

%env SM_FRAMEWORK=tf.keras

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

Thank you saviour .

salimsoltani28 commented 2 years ago

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

Jordan-Pierce commented 1 year ago

%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.

This also solved it for me, thanks!

Deewourne commented 1 year ago

%env SM_FRAMEWORK=tf.keras

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

It also worked for me, could you please explain what this does?

sara2atek commented 1 year ago

hi! i work on google colab and I tried all the given solution but still has the problem. pleas i need help

creativesalam commented 1 year ago

hi! i work on google colab and I tried all the given solution but still has the problem. pleas i need help

Try the other option.

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

Masumekeshavarzi commented 1 year ago

It refers to changes in new version of keras while segmentation model is built with older versions of keras In Linux I ran these codes :

  1. cat (error path)
  2. sed -i 's/keras.utils.generic_utils.get_custom_objects()/keras.utils.get_custom_objects()/g' error path
keepfighting666 commented 6 months ago

%env SM_FRAMEWORK=tf.keras

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

it works.

myyyy1987 commented 4 months ago

%env SM_FRAMEWORK=tf.keras

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

Thanks! it works for me.