Search code examples
tensorflowkerasdeep-learningartificial-intelligencesemantic-segmentation

issue while using segmentation-models library


from tensorflow import keras
from segmentation_models import PSPNet

While running this, I am facing the error below:

AttributeError                            Traceback (most recent call last)
<ipython-input-14-fbd9360b4944> in <module>()
      1 
      2 from tensorflow import keras
----> 3 from segmentation_models import PSPNet

3 frames
/usr/local/lib/python3.6/dist-packages/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

/usr/local/lib/python3.6/dist-packages/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

/usr/local/lib/python3.6/dist-packages/efficientnet/keras.py in <module>()
     15 preprocess_input = inject_keras_modules(model.preprocess_input)
     16 
---> 17 init_keras_custom_objects()

/usr/local/lib/python3.6/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' has no attribute 'generic_utils'

I installed segmentation-models library using pip as the provided instruction (link) I would be appreciated if someone can help me with how to fix it. I simply copy the code from the instruction, and all I found on the net was just the same. Should it be something wrong with the installation?

Please guide me through this!:)


Solution

  • You are facing this issue because you are using Tensorflow version >= 2.2. To fix this problem either you have to use Tensorflow 2.1/2.0 or Tensorflow 1.x (i.e 1.15.2)

    Please follow below steps to perform Image segmentation using Segmentation models using TF 2.1.

        !pip install q tensorflow==2.1
        !pip install segmentation-models
        
        import tensorflow as tf
        from segmentation_models import PSPNet
        
        #Instantiate PSPNet model
        model = PSPNet()
    
        #Display model summary
        model.summary()