模块'keras.utils.generic_utils'在导入segmentation_models时没有属性'get_custom_objects'

问题描述 投票:0回答:4

我正在使用segmentation_models库开发google colab。使用它的第一周它工作得很好,但现在我似乎无法再导入该库了。这是当我执行 importegmentation_models as sm 时出现的错误消息:

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-3-6f48ce46383f> in <module>
      1 import tensorflow as tf
----> 2 import segmentation_models as sm

            3 frames

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

Colab 使用 TensorFlow 版本 2.11.0。

我没有找到有关此特定错误消息的任何信息。有谁知道问题可能出在哪里?

python tensorflow keras image-segmentation
4个回答
6
投票

有时会遇到同样的问题。我是如何解决的:

  1. 打开文件keras.py,将所有'init_keras_custom_objects'更改为'init_tfkeras_custom_objects'。

  2. keras.py 的位置在错误消息中。在你的情况下,它应该位于 /usr/local/lib/python3.8/dist-packages/efficientnet/


6
投票

试试这个。有用。我在谷歌colab上查了一下

!pip install -U -q segmentation-models
!pip install -q tensorflow==2.2.1
!pip install -q keras==2.5
import os
os.environ["SM_FRAMEWORK"] = "tf.keras"

from tensorflow import keras
import segmentation_models as sm

5
投票

谢谢,它有效,但我刚刚这样做了:

import os
os.environ["SM_FRAMEWORK"] = "tf.keras"

from tensorflow import keras
import segmentation_models as sm

0
投票

指的是新版本keras的变化,而分割模型是用旧版本的keras构建的 在 Linux 中我运行了这些代码:

  1. cat (错误路径)
  2. sed -i 's/keras.utils.generic_utils.get_custom_objects()/keras.utils.get_custom_objects()/g' 错误路径
© www.soinside.com 2019 - 2024. All rights reserved.