如何将此TensorFlow图像分类模型转换为核心ML?

发布于 2025-02-04 08:10:07 字数 1404 浏览 4 评论 0原文

我正在学习TensorFlow,并希望将图像分类模型转换为用于iOS应用中的Core ML。

this tensorflow Image分类图教程与我想为培训所做的接近匹配,但我无法弄清楚如何将其转换为核心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%,因此我认为我缺少转换的一些参数。

上导入Coremltools作为CT我确实会收到一些警告,例如“警告:root:Tensorflow版本2.8.2版本2.8.2尚未使用Coremltools进行测试。您可能会遇到意外的错误。”但是我猜那不是问题,因为转换没有报告任何错误。

基于在此处进行信息,我也尝试在图像输入上设置标尺:

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

…但是,这使情况变得更糟,因为它对每个图像都是蒲公英的信心约为315%。其他一些设置秤 /偏见的尝试都导致了同一件事。

在这一点上,我不确定还要尝试什么。任何帮助都将受到赞赏!

I’m learning TensorFlow and want to convert an image classification model to Core ML for use in an iOS app.

This TensorFlow image classification tutorial is a close match to what I want to do for the training, but I haven’t been able to figure out how to convert that to Core ML.

Here’s what I’ve tried, adding the following to the end of the Colab notebook for the tutorial:

# 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')

That successfully creates an mlmodel file, but when I download the file and open it in Xcode to test it (under the “Preview” tab) it shows results like “Roses 900% Confidence” and “Tulips 1,120% Confidence”. For my uses, the confidence percentage needs to be from 0 to 100%, so I think I’m missing some parameter for the conversion.

On import coremltools as ct I do get some warnings like “WARNING:root:TensorFlow version 2.8.2 has not been tested with coremltools. You may run into unexpected errors.” but I’m guessing that’s not the problem since the conversion doesn’t report any errors.

Based on information here, I’ve also tried setting a scale on the image input:

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

… but that made things worse as it then has around 315% confidence that every image is a dandelion. A few other attempts at setting a scale / bias all resulted in the same thing.

At this point I’m not sure what else to try. Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2025-02-11 08:10:07

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

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

SoftMax功能将您的输出转换为所需的概率。

The last layer of your model should be something like this:

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

The softmax function transforms your output into the probabilities you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文