CoreML 错误:导入时源文件无效 mymodel.mlmodel

error with CoreML: source file is not valid , when importing mymodel.mlmodel

我在使用 CoreML 模型构建 ios 应用程序时遇到问题。 我在我的项目中使用了 this 示例代码来实现 coreML。 我可以 运行 使用回购中的给定模型从 github 克隆示例代码时,但是当我粘贴到我自己的模型时,我得到一个错误:

lexical preprocessor issue
    source file is not valid UTF-8
        SSD_model.mlmodel

我的模型是一个用于生成边界框的 SSD 模型(而不仅仅是像 repo 模型那样的对象检测),所以这可能会产生问题。我使用 tensorflow 对象检测 API 对其进行了训练,并使用为 SSD 模型制作的 this helpful blog post . I've verified that my model works well by testing it in another script 将其导出到 coreML。这是我现在如何导入模型的第一个示例的片段:

#import <CoreML/CoreML.h>
#import <Vision/Vision.h>
#import "SSD_model.mlmodel"

- (void)viewDidLoad {
    [super viewDidLoad];

    model = [[[SSD_model alloc] init] model]
    m = [VNCoreMLModel modelForMLModel: model error:nil];

我需要更改此 objective C 代码才能使其正常工作吗?或者我可能忘记的任何项目设置?谢谢!

你得到这个错误是因为这个 => #import "SSD_model.mlmodel"

本质上,编译器试图导入一些源代码,却找到了一些不是的东西。

您需要先编译此模型才能使用它。您可以通过 Xcode (it will autogenerate an interface for you) or by using APIs 编译它,在运行时编译模型(因此您可以更新模型而无需发布新版本的应用程序)。


注意:在您链接的示例中,作者导入了 SomeModel.h(我认为这是 ObjC 的自动生成 header)和 notSomeModel.mlmodel。所以我想这可能也是您打算做的。