【发布时间】: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]
- 使用 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')
- 使用 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