【问题标题】:mlmodel converted from coremltools4.0 is much slower than mlmodel converted from tfcoreml从coremltools4.0转换的mlmodel比从tfcoreml转换的mlmodel慢很多
【发布时间】:2021-02-17 01:22:21
【问题描述】:

我使用 tensorflow 1.15 设计了一个简单的网络,并使用 tfcoreml 和 coremltools4.0 将其转换为 mlmodels。我在系统版本ios13.5.1的iPhoneXS上测试过,发现从coremltools4.0转换的mlmodel比从tfcoreml慢很多。

tensorflow中的原始网络如下:

graph = tf.Graph()
with graph.as_default():
    x = tf.placeholder(tf.float32, shape=[1, 1000, 1000, 4], name="input")
    y = tf.layers.conv2d(x, 4, 1, padding='same', activation=tf.nn.relu)
    output_names = [y.op.name]
  1. 使用 tfcoreml 转换为 mlmodel
# using tfcoreml
coreml_save_tfcoreml_file = model_dir + "/debug_tfcoreml.mlmodel"
tfcoreml.convert(tf_model_path=frozen_graph_file,
                    mlmodel_path=coreml_save_tfcoreml_file,
                    output_feature_names=["conv2d/Relu:0"],  # name of the output tensor (appended by ":0")
                    input_name_shape_dict={"input": [1, 1000, 1000, 4]},  # input tensor[1, height, width, channel]
                    minimum_ios_deployment_target='12')

  1. 使用 coremltools4.0 转换 mlmodel:
coreml_save_coremltools_file = model_dir + "/debug_coremltools.mlmodel"
mlmodel = ct.convert(frozen_graph_file, source='tensorflow')
mlmodel.save(coreml_save_coremltools_file)

我在同一个 iPhoneXS 设备上测试了两个 mlmodel,无论是在 cpu 和 cpu+gpu 上还是使用 ANE,#2 都比 #1 花费更多的时间。

为了确认 mlmodel 在 ANE 上运行,我在网络中插入了 10 个 1x1 卷积层以添加计算量。它们确实都在 ANE 上运行,时间成本为 (ALL

似乎coremltools4.0通过插入transpose层将数据类型从HWC更改为CHW格式。但是 tf2coreml 直接接受 CHW 格式。

如何去掉coremltools4.0转换中的转置层,以验证该层是否是导致性能下降的原因?

【问题讨论】:

    标签: tensorflow coreml coremltools


    【解决方案1】:

    是的,我想我找到了原因。我使用“MLModel Surgery”来修改 mlmodel,删除两个转置层,并将输入形状和输出形状信息修改为 NCHW 格式。那么使用coretools4.0的性能与tfcoreml相同。 :)

    【讨论】:

    • 我在尝试将 u2net 模型转换为 ML 时遇到了同样的问题。加载 ML 模型非常慢(30 秒)。我正在使用本指南:rockyshikoku.medium.com/…。转换时我应该改变什么?
    猜你喜欢
    • 2021-07-09
    • 2017-11-11
    • 2018-09-12
    • 2019-05-22
    • 2020-09-15
    • 2020-05-23
    • 2019-12-18
    • 2019-10-06
    • 2021-12-05
    相关资源
    最近更新 更多