【问题标题】:Convert MobileNet from Keras to CoreML将 MobileNet 从 Keras 转换为 CoreML
【发布时间】:2019-03-23 14:05:55
【问题描述】:

我正在使用 Keras 2.1.3,我想将 MobileNet 转换为 CoreML:

from keras.applications import MobileNet
from keras.applications.mobilenet import relu6
from keras.applications.mobilenet import DepthwiseConv2D

import coremltools.converters.keras as k

def save_model():
    model = MobileNet(input_shape=(128,128,3), include_top=False)
    model.save('temp.h5')

def convert():
    model = k.convert('temp.h5',
                      input_names=['input'],
                      output_names=['output'],
                      model_precision='float16',
                      custom_conversion_functions={'relu6': relu6, 'DepthwiseConv2D': DepthwiseConv2D})
    model.save('temp.model')

save_model()
convert()

这给出了一个错误:ValueError: Unknown activation function:relu6

【问题讨论】:

    标签: python keras coreml coremltools


    【解决方案1】:

    对于 Keras 2.2.4 和 Tensorflow 1.12.0,我找到了解决方案。

    保存模型权重和架构,例如:

    model_json = model.to_json()
    open('architecture.json', 'w').write(model_json)
    model.save_weights('weights.h5', overwrite=True)
    

    为了将模型转换为 CoreML .mlmodel 我使用:

    import coremltools
    
    from keras.layers import DepthwiseConv2D, ReLU
    from pathlib import Path
    from keras.models import model_from_json
    from tensorflow.python.keras.utils.generic_utils import CustomObjectScope
    
    model_architecture = './Networks/architecture.json'
    model_weights = './Networks/weights.h5'
    
    model_structure = Path(model_architecture).read_text()
    
    with CustomObjectScope({'relu6': ReLU ,'DepthwiseConv2D': DepthwiseConv2D}):
        model = model_from_json(model_structure)
        model.load_weights(model_weights)
    
        output_labels = ['0', '1', '2', '3', '4', '5', '6']
        coreml_model = coremltools.converters.keras.convert(
            model, input_names=['image'], output_names=['output'],
            class_labels=output_labels, image_input_names='image')
    
        coreml_model.save('ModelX.mlmodel')
    

    【讨论】:

      【解决方案2】:

      这是基于https://github.com/apple/coremltools/issues/38的解决方案

      from keras.applications import MobileNet
      import keras
      
      import coremltools.converters.keras as k
      from keras.utils.generic_utils import CustomObjectScope
      
      def save_model():
          model = MobileNet(input_shape=(128,128,3), include_top=False)
          model.save('temp.h5')
      
      def convert():
          with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,
                                  'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
              model = k.convert("temp.h5",
                                input_names=['input'],
                                output_names=['output'],
                                model_precision='float16')
      
          model.save('temp.mlmodel')
      
      save_model()
      convert()
      

      【讨论】:

        猜你喜欢
        • 2020-01-24
        • 2017-11-12
        • 2017-11-30
        • 2017-12-10
        • 2020-03-26
        • 2020-05-08
        • 2020-11-28
        • 2020-12-21
        • 2017-11-23
        相关资源
        最近更新 更多