segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

Module 'keras.utils.generic_utils' has no attribute 'get_custom_objects'

Open usher233 opened this issue 2 years ago • 9 comments

A few days ago, I can still use it however today when I import segmentation_models this happened. I wander how can I fix this?

usher233 avatar Feb 10 '23 19:02 usher233

The same to me:

/usr/local/lib/python3.8/dist-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.generic_utils' has no attribute 'get_custom_objects'"

rodiegeology avatar Feb 10 '23 19:02 rodiegeology

Keras version 2.11.0? Google Colab?

  • Downgrade keras to 2.10.0 and restart the environment
!pip install keras==2.10.0
import keras
print(keras.__version__)
  • Or downgrade tensorflow like to 2.6.4 and restart the environment
!pip install tensorflow==2.6.4
import tensorflow as tf
print(tf.__version__)

ryuuzaki42 avatar Feb 10 '23 22:02 ryuuzaki42

!pip install tensorflow==2.9.0 also works - thanks

tonyboston-au avatar Feb 12 '23 02:02 tonyboston-au

It worked! Thanks!

rodiegeology avatar Feb 12 '23 23:02 rodiegeology

Installing an older TF/Keras version is just a workaround. Can we get segmentation models to support TF 2.11?

JPasterkampRotec avatar Feb 21 '23 09:02 JPasterkampRotec

I am using tensorflow 2.12 and got this to work by setting the env variable SM_FRAMEWORK="tf.keras"

lucaslcode avatar Mar 26 '23 00:03 lucaslcode

I am using tensorflow 2.12 and got this to work by setting the env variable SM_FRAMEWORK="tf.keras"

Thanks for this @lucaslcode ! Was having trouble deploying an image segmentation model on hugging faces. I'd like to add to this. When I ran the code in Google Colab, I replaced all the instances of 'init_keras_custom_objects' in keras.py with 'init_tfkeras_custom_objects' and got it running on there and used tensorflow 2.11.0

# Open the file in write mode
with open('/usr/local/lib/python3.9/dist-packages/efficientnet/keras.py', 'r') as f:
    # Read the contents of the file
    contents = f.read()

# Replace the string
new_contents = contents.replace('init_keras_custom_objects', 'init_tfkeras_custom_objects')

# Open the file in write mode again and write the modified contents
with open('/usr/local/lib/python3.9/dist-packages/efficientnet/keras.py', 'w') as f:
    f.write(new_contents)

hrootscraft avatar Mar 28 '23 07:03 hrootscraft

Hi just import this line os.environ["SM_FRAMEWORK"] = "tf.keras"

ehsanro avatar May 24 '23 14:05 ehsanro

Hi just import this line os.environ["SM_FRAMEWORK"] = "tf.keras"

Thank you, that is my current solution. However it would be better if segmentation models would support the newest TF versions.

JPasterkampRotec avatar May 24 '23 14:05 JPasterkampRotec