import efficientnet.keras as efn - AttributeError: module 'keras.utils' has no attribute 'generic_utils'

import efficientnet.keras as efn - AttributeError: module 'keras.utils' has no attribute 'generic_utils'

我正在尝试使用来自 https://github.com/qubvel/segmentation_models 的 EfficientNet。

所以,我通过 pip 安装了这个:

!pip install git+https://github.com/qubvel/segmentation_models

然后我尝试导入 efficientnet.keras:

import efficientnet.keras as efn

并得到这个错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-57-61d736540c72> in <module>()
----> 1 import efficientnet.keras as efn

1 frames
/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'

这很奇怪,因为它昨天可以正常工作,今天在一个笔记本上也能正常工作,但在其他笔记本上却出现了这个错误。有人知道怎么办吗?

在我看来,您使用的是 Colab,他们今天可能已经升级了一些软件包,所以我们遇到了同样的错误。我通过将 Keras 和 Tenserflow 软件包降级到以前的软件包(我只猜到了版本号)解决了这个问题:

!pip install q tensorflow==2.1
!pip install q keras==2.3.1

您仍然可以将 tensorflow 2.4.1 与分段模型 v.1.0.1 一起使用。

get_custom_objects() 已从 keras.utils.generic_utils 移动到 keras.utils

您可以:

keras.utils.generic_utils = keras.utils

只有在那之后你才能导入 segmentation models

这不是 100% 安全的解决方案,但在我的情况下它工作得很好。

!pip install tensorflow==2.1.0
!pip install keras==2.3.1
!pip install segmentation-models

试试这个它对我有用 google colab