ImportError: 无法导入 segmentation_models as sm

huangapple go评论57阅读模式
英文:

ImportError : import segmentation_models as sm

问题

所以我想导入分割模型(segmentation_models)作为sm,并尝试使用以下命令进行pip安装:

pip install segmentation-models

然后我想使用以下命令导入它:

import segmentation_models as sm

但我收到以下错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/segmentation_models/__init__.py in <module>
     97 try:
---> 98     set_framework(_framework)
     99 except ImportError:

12 frames
ModuleNotFoundError: No module named 'tensorflow.keras'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/saving/saving_lib.py in <module>
     31 from keras import losses
     32 from keras.engine import base_layer
---> 33 from keras.optimizers import optimizer
     34 from keras.saving.serialization_lib import ObjectSharingScope
     35 from keras.saving.serialization_lib import deserialize_keras_object

ImportError: cannot import name 'optimizer' from 'keras.optimizers' (/usr/local/lib/python3.10/dist-packages/keras/optimizers.py)

FYI,我已经安装了TensorFlow和Keras,并且版本如下:

tensorflow                       2.12.0
keras                            2.12.0

有人能告诉我我做错了什么吗?

英文:

So i want to import segmentation_models as sm and i try to pip install using this :

pip install segmentation-models

Then i want to import it using this :

import segmentation_models as sm

And i get this error :

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/segmentation_models/__init__.py in &lt;module&gt;
     97 try:
---&gt; 98     set_framework(_framework)
     99 except ImportError:

12 frames
ModuleNotFoundError: No module named &#39;tensorflow.keras&#39;

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/saving/saving_lib.py in &lt;module&gt;
     31 from keras import losses
     32 from keras.engine import base_layer
---&gt; 33 from keras.optimizers import optimizer
     34 from keras.saving.serialization_lib import ObjectSharingScope
     35 from keras.saving.serialization_lib import deserialize_keras_object

ImportError: cannot import name &#39;optimizer&#39; from &#39;keras.optimizers&#39; (/usr/local/lib/python3.10/dist-packages/keras/optimizers.py)

FYI i already install tensorflow and keras, and here is the version of it :

tensorflow                       2.12.0
keras                            2.12.0

Can someone tell me what did i do wrong?

答案1

得分: 0

在代码部分不要翻译,只返回翻译好的部分:

"There is nothing wrong we did in the code. It is perfectly fine.
It is just that the latest Segmentation Models version happens to be 1.0.1 which dates way back in Jan 2020 when Keras version was 2.10.
Keras, however kept itself updated from time to time and now Keras won't support importing some of its modules which are automatically imported when we import segmentation models.
The solution to overcome the Import Error lies in downgrading Keras to its version compatible with segmentation-models 1.0.1.
I tried the same and it worked.
Here is the code for your reference :
pip install keras==2.10
import tensorflow as tf
from tensorflow import keras
Check version
keras.version
Output :
2.10.0
Install segmentation models
pip install segmentation-models==1.0.1
Output :
Segmentation Models: using keras framework.
I hope this works for you as well."

英文:

There is nothing wrong we did in the code. It is perfectly fine.

It is just that the latest Segmentation Models version happens to be 1.0.1 which dates way back in Jan 2020 when Keras version was 2.10.

Keras, however kept itself updated from time to time and now Keras won't support importing some of its modules which are automatically imported when we import segmentation models.

The solution to overcome the Import Error lies in downgrading Keras to its version compatible with segmentation-models 1.0.1.

I tried the same and it worked.

Here is the code for your reference :

pip install keras==2.10
import tensorflow as tf
from tensorflow import keras

Check version

keras.__version__

Output :

2.10.0

Install segmentation models

pip install segmentation-models==1.0.1

Output :

Segmentation Models: using `keras` framework.

I hope this works for you as well.

huangapple
  • 本文由 发表于 2023年6月29日 00:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定