【问题标题】:Coremltools TypeError: Keras layer type not supportedCoremltools TypeError:不支持 Keras 层类型
【发布时间】:2020-12-16 14:01:30
【问题描述】:

我训练了一个自定义 YOLOv4 模型。然后我使用以下方法将权重转换为 .h5:

https://github.com/david8862/keras-YOLOv3-model-set/tree/master/tools/model_converter

之后我尝试将 .h5 转换为 coreml:

# Custom activation function
from keras.layers import Activation
from keras import backend as K
from keras.utils.generic_utils import get_custom_objects

def mish(x):
  return x * K.tanh(K.softplus(x))

get_custom_objects().update({'mish': Activation(mish)})

from keras.models import load_model
from keras.utils import CustomObjectScope
from keras.initializers import glorot_uniform

with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
    model = load_model('yolov4_custom.h5', compile=False)

mlModel = coremltools.converters.keras.convert(model,
                                        input_names='image',
                                        image_input_names='image',
                                        input_name_shape_dict={'image': [None, 416, 416, 3]})

调用 ..keras.convert(....) 时出现错误:

TypeError: 不是 支持

【问题讨论】:

    标签: keras yolo coreml coremltools


    【解决方案1】:

    Keras 中的后端数学运算(在您的情况下为 tanh 和 softplus)不是可以转换的层。而是尝试使用函数的 Layer 或 Activation 版本来重构函数。

    一般来说,如果你想要一个 CoreML 不支持的自定义层或激活,你需要在 convert 方法中指定自定义函数并提供一个 Swift/Objective-C 实现。以下是为 Swish 激活执行此类操作的一个很好的指南:https://machinethink.net/blog/coreml-custom-layers/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 2020-02-21
      • 2020-09-05
      • 2020-07-21
      • 2020-02-29
      • 2020-09-29
      • 2020-06-24
      • 2017-06-03
      相关资源
      最近更新 更多