Tushar Tiwari
Tushar Tiwari

Reputation: 121

how to import segmentation-models with tensorflow keras? getting ModuleNotFoundError: No module named 'keras.legacy'

I tried to install segmentation-models using.

!pip install -U segmentation-models==0.2.1  
import tensorflow as tf
import tensorflow.keras as keras
print(tf.__version__)
print(keras.__version__)

Output:
2.4.1
2.4.0


# Tried for import 
import segmentation_models as sm

Resulted in error: ModuleNotFoundError: No module named 'keras.legacy'

Following is the stack trace


stack Trace:
---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-16-7b5049dd4be0> in <module>()
----> 1 import segmentation_models as sm

6 frames

/usr/local/lib/python3.7/dist-packages/segmentation_models/__init__.py in <module>()
      4 
      5 from .unet import Unet
----> 6 from .fpn import FPN
      7 from .linknet import Linknet
      8 from .pspnet import PSPNet

/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/__init__.py in <module>()
----> 1 from .model import FPN
      2 

/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/model.py in <module>()
----> 1 from .builder import build_fpn
      2 from ..backbones import get_backbone, get_feature_layers
      3 from ..utils import freeze_model
      4 from ..utils import legacy_support
      5 

/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/builder.py in <module>()
      6 from keras.models import Model
      7 
----> 8 from .blocks import pyramid_block
      9 from ..common import ResizeImage
     10 from ..common import Conv2DBlock

/usr/local/lib/python3.7/dist-packages/segmentation_models/fpn/blocks.py in <module>()
      1 from keras.layers import Add
      2 
----> 3 from ..common import Conv2DBlock
      4 from ..common import ResizeImage
      5 from ..utils import to_tuple

/usr/local/lib/python3.7/dist-packages/segmentation_models/common/__init__.py in <module>()
      1 from .blocks import Conv2DBlock
----> 2 from .layers import ResizeImage

/usr/local/lib/python3.7/dist-packages/segmentation_models/common/layers.py in <module>()
      2 from keras.engine import InputSpec
      3 from keras.utils import conv_utils
----> 4 from keras.legacy import interfaces
      5 from keras.utils.generic_utils import get_custom_objects
      6 

ModuleNotFoundError: No module named 'keras.legacy'


---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

Solution Tried: To downgrade both tensorflow and keras to

!pip install tensorflow==2.2.0
!pip install keras==2.3.1

But it is creating conflict between the tensorflow.keras and keras. Is there any way to work this using tensorflow.keras?

Upvotes: 1

Views: 10057

Answers (1)

Tushar Tiwari
Tushar Tiwari

Reputation: 121

As of now the following approach is working. Do not try with specific version of segmentation_models module.

#install this way
!pip3 install tensorflow==2.2
!pip3 install keras==2.3.1
!pip3 install -U segmentation-models


import tensorflow as tf
import tensorflow.keras as keras
import segmentation_models as sm

Now segmentation_models imported successfully.

Upvotes: 2

Related Questions