【问题标题】:AttributeError: 'module' object has no attribute ' SeparableConv1D' while converting Keras to Core ML ModelAttributeError:“模块”对象在将 Keras 转换为 Core ML 模型时没有属性“SeparableConv1D”
【发布时间】:2019-10-23 02:51:22
【问题描述】:

我正在关注tutorial 关于使用 Keras 和 CoreML 进行机器学习的内容,当我开始运行以下代码并将 Keras 模型转换为 CoreML 时。我明白了:

AttributeError: 'module' 对象没有属性 'SeparableConv1D'

我应该改变哪里来解决这个问题?

这是我运行的代码:

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

coreml_mnist = coremltools.converters.keras.convert(
    'best_model.09-0.03.h5', input_names=['image'], output_names=['output'], 
    class_labels=output_labels, image_input_names='image')

这是我得到的详细信息:

AttributeError                            Traceback (most recent call last)
<ipython-input-73-8fa50f6bbeb9> in <module>()
     10 coreml_mnist = coremltools.converters.keras.convert(
     11     'best_model.08-0.03.h5', input_names=['image'], output_names=['output'],
---> 12     class_labels=output_labels, image_input_names='image')

/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.pyc in convert(model, input_names, output_names, image_input_names, input_name_shape_dict, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions)
    758                       predicted_probabilities_output,
    759                       add_custom_layers,
--> 760                       custom_conversion_functions=custom_conversion_functions)
    761 
    762     return _MLModel(spec)

/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.pyc in convertToSpec(model, input_names, output_names, image_input_names, input_name_shape_dict, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    554                                            add_custom_layers=add_custom_layers,
    555                                            custom_conversion_functions=custom_conversion_functions,
--> 556                                            custom_objects=custom_objects)
    557     else:
    558         raise RuntimeError(

/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras2_converter.pyc in _convert(model, input_names, output_names, image_input_names, input_name_shape_dict, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    207     # Build network graph to represent Keras model
    208     graph = _topology2.NetGraph(model)
--> 209     graph.build()
    210 
    211     # The graph should be finalized before executing this

/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_topology2.pyc in build(self, is_top_level)
    748             self.insert_1d_permute_layers()
    749             self.insert_permute_for_spatial_bn()
--> 750             self.defuse_activation()
    751             self.remove_internal_input_layers()
    752 

/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_topology2.pyc in defuse_activation(self)
    508                 isinstance(k_layer, _keras.layers.Conv1D) or
    509                 isinstance(k_layer, _keras.layers.SeparableConv2D) or
--> 510                 isinstance(k_layer, _keras.layers.SeparableConv1D) or
    511                 isinstance(k_layer, _keras.layers.Dense)):
    512 

AttributeError: 'module' object has no attribute 'SeparableConv1D'

【问题讨论】:

  • 您使用的是哪个 Keras 版本?
  • @MatiasValdenegro keras 版本 2.0.6
  • 该教程不使用 SeparableConv1D 层。这些是你自己加的吗?如果不是,则错误消息可能具有误导性。您使用的是哪个版本的 coremltools?
  • @MatthijsHollemans 不,我没有添加任何内容。该问题与示例的 requirements.txt 中使用的 Keras 版本有关。

标签: keras mnist coreml


【解决方案1】:

您似乎正在使用 coremltools 不支持的 keras 版本,因为 SeparableConv1D 是在 keras 2.0.6 之后添加的,您应该将 keras 升级到最新版本才能正常工作。

【讨论】:

  • 是的,你完全正确。在项目文件夹中,有一个 requirements.txt,其中包含 keras==2.0.6,将其更改为最新的 Keras 即可解决问题。谢谢你:)
【解决方案2】:

我对@9​​87654321@ 也有同样的问题,正如其他人所说,keras 的版本不支持SeparableConv1D

但是,仅更新keras 的版本而不更新tensorflow 会导致jupyter notebook 中出现其他错误。我能够使用sys 模块在jupyter notebook 中直接安装兼容版本的kerastensorflow。在撰写本文时,keras 的最新版本是 2.2.4,tensorflow 的一个兼容版本(至少针对本教程进行了测试)是 1.7.0。

您可以运行以下python代码进行安装:

import sys
!{sys.executable} -m pip install tensorflow==1.7.0
!{sys.executable} -m pip install keras==2.2.4

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 2018-12-29
    • 2019-05-21
    • 1970-01-01
    • 2010-10-02
    • 2021-01-28
    • 1970-01-01
    • 2017-09-01
    • 2010-11-18
    相关资源
    最近更新 更多