【问题标题】:how to import segmentation-models with tensorflow keras? getting ModuleNotFoundError: No module named 'keras.legacy'如何使用 tensorflow keras 导入分割模型?获取 ModuleNotFoundError:没有名为“keras.legacy”的模块
【发布时间】:2021-05-14 05:10:03
【问题描述】:

我尝试使用安装分段模型。

!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

导致错误:ModuleNotFoundError: No module named 'keras.legacy'

下面是堆栈跟踪


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

已尝试的解决方案: 将 tensorflow 和 keras 都降级为

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

但它在 tensorflow.keras 和 keras 之间造成了冲突。 有没有办法使用 tensorflow.keras 来解决这个问题?

【问题讨论】:

    标签: keras tensorflow2.0


    【解决方案1】:

    到目前为止,以下方法正在起作用。 不要尝试使用特定版本的 segmentation_models 模块。

    #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
    

    现在segmentation_models 导入成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-12
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多