【问题标题】:How would I convert this TensorFlow image classification model to Core ML?如何将此 TensorFlow 图像分类模型转换为 Core ML?
【发布时间】:2022-06-14 04:10:10
【问题描述】:

我正在学习 TensorFlow,想将图像分类模型转换为 Core ML 以在 iOS 应用中使用。

这个TensorFlow image classification tutorial 与我想要为培训做的事情非常匹配,但我无法弄清楚如何将其转换为 Core ML。

这是我尝试过的,将以下内容添加到教程的 Colab 笔记本的末尾:

# install coremltools
!pip install coremltools

# import coremltools
import coremltools as ct

# define the input type
image_input = ct.ImageType()

# create classifier configuration with the class labels
classifier_config = ct.ClassifierConfig(class_names)

# perform the conversion
coreml_model = ct.convert(
    model, inputs=[image_input], classifier_config=classifier_config,
)

# print info about the converted model
print(coreml_model)

# save the file
coreml_model.save('my_coreml_model')

这成功创建了一个 mlmodel 文件,但是当我下载该文件并在 Xcode 中打开它进行测试时(在“预览”选项卡下),它会显示“玫瑰 900% 置信度”和“郁金香 1,120% 置信度”之类的结果。对于我的使用,置信度百分比需要从 0 到 100%,所以我认为我缺少一些用于转换的参数。

import coremltools as ct 上,我确实收到了一些警告,例如“WARNING:root:TensorFlow version 2.8.2 has not been testing with coremltools.你可能会遇到意想不到的错误。”但我猜这不是问题,因为转换不会报告任何错误。

基于information here,我也尝试在图像输入上设置比例:

image_input = ct.ImageType(scale=1/255.0)

……但这让事情变得更糟,因为它有大约 315% 的信心认为每张图片都是蒲公英。其他一些设置比例/偏差的尝试都导致了同样的结果。

在这一点上,我不确定还能尝试什么。任何帮助表示赞赏!

【问题讨论】:

    标签: tensorflow coreml coremltools


    【解决方案1】:

    模型的最后一层应该是这样的:

    layers.Dense(num_classes, activation='softmax')
    

    softmax 函数将您的输出转换为您需要的概率。

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2019-05-06
      • 2018-01-03
      • 2020-07-26
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多