【问题标题】:Core ML coremltools AttributeError: module 'keras.applications.mobilenet' has no attribute 'relu6'Core ML coremltools AttributeError:模块“keras.applications.mobilenet”没有属性“relu6”
【发布时间】:2018-10-10 10:13:59
【问题描述】:

我们正在尝试将 .h5 Keras 模型转换为 .mlmodel 模型,我的代码如下:

from keras.models import load_model
import keras
from keras.applications import MobileNet
from keras.layers import DepthwiseConv2D

from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    model = load_model('CNN_tourist_11.h5', custom_objects={'relu6': MobileNet})

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                    input_names="image",
                                                    image_input_names="image",
                                                    class_labels= output_labels,)

coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')

我们查找6天前提出的类似问题,我们也导入了MobileNet,但仍然显示此错误:

AttributeError: module 'keras.applications.mobilenet' has no attribute 'relu6'

我的 TensorFlow 版本是 1.10.0 而 Keras 版本是 2.2.2

如果有人能就为什么一直显示此错误向我们提供建议,我们将不胜感激,非常感谢。

【问题讨论】:

  • 这个问题之前已经回答过了,请在提问前先搜索类似的,看看答案是否已经在这里了。
  • @MatiasValdenegro 我看到了 6 天前提出的问题,我做了你建议的“从 keras.applications 导入 MobileNet”,但它仍然不起作用。对不起,类似的问题,但我确实查找了这个问题,谢谢。
  • 那么你需要具体说明什么不起作用,尽可能地为你的问题添加更多细节。
  • @MatiasValdenegro 我已经修改了我的问题,感谢您的提醒。

标签: keras coremltools relu


【解决方案1】:

我修改了我的代码:

from keras.models import load_model
import keras
from keras.applications import MobileNet
from keras.layers import DepthwiseConv2D

#from keras.utils.generic_utils import CustomObjectScope

#with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    #model = load_model('CNN_tourist_11.h5', custom_objects={'relu6': MobileNet})

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                    input_names="image",
                                                    image_input_names="image",
                                                    class_labels= output_labels,)

coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')

打印输出的部分内容:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/keras/_layers2.py in <module>()
      8 if _keras.__version__ >= _StrictVersion('2.2.0'):
      9     from keras.layers import DepthwiseConv2D
---> 10     from keras_applications.mobilenet import relu6
     11 else:
     12     from keras.applications.mobilenet import DepthwiseConv2D, relu6

ImportError: cannot import name 'relu6'

【讨论】:

  • @MatiasValdenegro 我修改了我的代码,这里是打印输出
【解决方案2】:

实际上,@Rex 本人在 cmets 中回答了这个问题。我只是想把它作为遇到相同问题的人的“答案”简单明了地说明。

这个问题不是关于 keras,而是关于 coremltools。需要在coremltools文件中找到_layers2.py,注释掉from keras_application.mobilenet import relu6

  • [YOUR_PYTHON_INSTALL_DIR]/lib/python3.6/site-packages/coremltools/converters/keras/_layers2.py找到_layer2.py

  • 像这样注释掉这行:

    if _keras.__version__ >= _StrictVersion('2.2.0'):
        from keras.layers import DepthwiseConv2D
    # Modified by KF 10/16/2018
    #    from keras_applications.mobilenet import relu6
    else:
       from keras.applications.mobilenet import DepthwiseConv2D, relu6
    

然后,在您的代码中,删除所有与 Keras 相关的导入,它们不相关;

对于'Sequential' object has no attribute 'SerializeToString' 错误,请使用coreml_model.save 而不是tools.utils.save_spec()

# from keras.models import load_model
# import keras
# from keras.applications import MobileNet
# from keras.layers import DepthwiseConv2D

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                input_names="image",
                                                image_input_names="image",
                                                class_labels= output_labels,)

#coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
coreml_model.save('place10.mlmodel')

问题解决了。

【讨论】:

    【解决方案3】:

    在最新的 Keras 版本中,MobileNet 的组件已与 Keras 的其余层集成,因此它们不再作为 mobilenet 包的一部分提供。那你需要把代码改成:

    from keras.models import load_model
    import keras
    from keras.applications import MobileNet
    from keras.layers import DepthwiseConv2D
    
    model = load_model('CNN_tourist_11.h5')
    
    output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                        input_names="image",
                                                        image_input_names="image",
                                                        class_labels= output_labels,)
    
    coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
    

    【讨论】:

    • 我通过删除 keras/_layers2.py 中的以下两行来解决了这个问题:'from keras_applications.mobilenet import relu6' 和 'from keras.applications.mobilenet import DepthwiseConv2D, relu6' 但是如果我之后还有一个问题:“MLModel”对象没有属性“SerializeToString”哈哈,但感谢您的帮助。
    • 我的 Keras 版本是 2.2.4,tensorflow 1.10.0,还是遇到同样的问题,谁能帮忙?
    • @KFZ 请提出你自己的问题,根本不清楚“同样的问题”是什么。
    • @MatiasValdenegro 与这个问题相比,字面上是相同的问题,逐字逐句。唯一的区别是我提到的版本。如果我提出另一个问题,你会说请不要问重复的问题。
    • @KFZ Matias Valdenegro 几乎是对的,但如果你输入他粘贴的代码,它会提示“无法导入名称'relu6'”,你唯一要做的就是穿上 Keras 套装你安装了,在 Keras 文件中,有一个文件名“_layers2.py”。在“_layers2.py”中,删除'from keras_applications.mobilenet import relu6'和'from keras.applications.mobilenet import DepthwiseConv2D, relu6',问题就解决了。
    猜你喜欢
    • 2019-03-09
    • 2021-02-01
    • 2017-09-20
    • 2018-06-06
    • 2019-10-23
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多