【问题标题】:module 'keras.engine' has no attribute 'Layer'模块“keras.engine”没有属性“层”
【发布时间】:2021-08-26 12:27:31
【问题描述】:

----> 6 from mrcnn.model import MaskRCNN

/usr/local/lib/python3.7/dist-packages/mrcnn/model.py in () 253 254 --> 255 类 ProposalLayer(KE.Layer): 256 """接收anchor分数并选择一个子集作为proposals通过 257到第二阶段。过滤是基于锚分数和

AttributeError: 模块 'keras.engine' 没有属性 'Layer'

【问题讨论】:

    标签: python machine-learning keras conv-neural-network artificial-intelligence


    【解决方案1】:

    安装tensorflow,版本如下

    pip uninstall tensorflow -y
    pip uninstall keras -y
    pip install tensorflow==2.4.3
    pip install keras==2.4.0
    

    在上面之后,会出现一些错误。您可以按照以下步骤解决它们。

    @Error: [模块 'tensorflow' 没有属性 XXXXXXXX]

    model.py 或您的代码中,使用tf.compat.v1 解析一些api,例如tf.compat.v1.Sessionimport tensorflow.compat.v1 as tf

    @Error: [ValueError: 试图将 'shape' 转换为张量并失败。错误:不支持任何值。]

    mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
    

    用这个 if-else 代码块替换:

    if s[1]==None:
        mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
    else:
        mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
    

    @Error: [ValueError: None values not supported.]

    indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
    

    替换为

    indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
    

    @Error: [AttributeError: module 'keras.engine.saving' 没有属性 'load_weights_from_hdf5_group_by_name']

    from keras import saving
    

    替换为

    from tensorflow.python.keras.saving import hdf5_format
    

    saving.load_weights_from_hdf5_group(f, layers)
    saving.load_weights_from_hdf5_group_by_name(f, layers)
    

    替换为

    hdf5_format.load_weights_from_hdf5_group(f, layers)
    hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
    

    参考:

    【讨论】:

      【解决方案2】:

      我在 github 问题讨论中发现了这个,它对我有用。

      你需要卸载那些:

      pip uninstall keras -y
      pip uninstall keras-nightly -y
      pip uninstall keras-Preprocessing -y
      pip uninstall keras-vis -y
      pip uninstall tensorflow -y
      pip uninstall h5py -y
      

      并强加这些版本:

      pip install tensorflow==1.13.1
      pip install keras==2.0.8
      pip install h5py==2.10.0
      

      【讨论】:

      • 另一个(更好的?)选项是使用 Tensorflow 升级工具(到 v2)和这篇文章中的说明:github.com/matterport/Mask_RCNN/issues/… 这将允许您在较新版本的 TF 和 Keras 中运行 Matterport 的 MaskRCNN。
      【解决方案3】:

      我在运行项目时遇到了这个问题。 https://github.com/matterport/Mask_RCNN

      在文件model.py中, 有一条线

      import keras.engine as KE

      我改成

      import keras.engine.topology as KE

      问题就消失了。

      【讨论】:

      • keras=2.6.0 没有拓扑模块
      【解决方案4】:

      这不是严格意义上的重复,但在这里可以找到类似的问题:AttributeError: module 'keras.engine' has no attribute 'input_layer'

      本质上,keras 的许多导入和属性错误都源于 keras 会根据您是使用 CPU 还是使用 GPU 或 ASIC 来更改其导入。某些引擎类并非在每种情况下都被导入。

      改为使用from keras.layers import Layer 并使用该层类代替引擎中的层类。

      【讨论】:

        猜你喜欢
        • 2018-12-13
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 2020-03-17
        • 2020-06-09
        • 2021-01-07
        • 1970-01-01
        相关资源
        最近更新 更多