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

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

我们正在尝试将.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

如果有人能告诉我们为什么一直显示此错误,我们将不胜感激,非常感谢。

在最新的 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')

我修改了我的代码:

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'

其实这个问题是@Rex在评论里自己回答的。对于遇到同样问题的人,我只是想把它说清楚和简短 "Answer"。

这个问题不是关于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')

问题已解决。