qubvel / segmentation_models

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

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

Open usher233 opened 1 year ago

usher233 commented 1 year ago

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?

rodiegeology commented 1 year ago

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

ryuuzaki42 commented 1 year ago

Keras version 2.11.0? Google Colab?

!pip install keras==2.10.0
import keras
print(keras.__version__)
!pip install tensorflow==2.6.4
import tensorflow as tf
print(tf.__version__)
tonyboston-au commented 1 year ago

!pip install tensorflow==2.9.0 also works - thanks

rodiegeology commented 1 year ago

It worked! Thanks!

JPasterkampRotec commented 1 year ago

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

lucaslcode commented 1 year ago

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

hrootscraft commented 1 year ago

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)
ehsanro commented 1 year ago

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

JPasterkampRotec commented 1 year ago

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.