如何在Objective-C中使用CoreML的机器学习模型

时间:2022-09-07 08:22:10

I have Machine Vision project made in objective-C (a couple of thousands of lines). To complete it I need to import my machine My_model.mlmodel with the latest coreML library. (as digression My_model.mlmodel was created in Python with coremltools ) I am trying to instantiate it, but nothing works I couldn't find any tutorials or helps on this topic. Of course, I imported my model to the pure Swift project, and it works. So I attached Swift class to my project hoping to make it work this way, but here again, Xcode translate the model to "Objective-C generated interface for model" and the model is not visible in Swift class.

我有一个以Objective-C(几千行)制作的Machine Vision项目。要完成它,我需要使用最新的coreML库导入我的机器My_model.mlmodel。 (因为离题My_model.mlmodel是用Python在coremltools中创建的)我试图实例化它,但没有任何作用我找不到任何教程或帮助这个主题。当然,我将我的模型导入纯Swift项目,它的工作原理。所以我将Swift类附加到我希望以这种方式工作的项目中,但是在这里,Xcode将模型转换为“Objective-C生成的模型接口”,并且模型在Swift类中不可见。

Below picture shows that Xcode automatically imports .mlmodel as Objective-C class.

下图显示Xcode自动导入.mlmodel作为Objective-C类。

如何在Objective-C中使用CoreML的机器学习模型

I need to put vector in the model and get the response.

我需要在模型中放置矢量并获得响应。

Please help me; I am stacked a couple of lines from completing this project. How to use My_model.mlmodel inside of Objective-C Is there any work around or maybe straight easy way like in Swift

请帮帮我;从完成这个项目开始,我有几行。如何在Objective-C中使用My_model.mlmodel是否有任何解决方法或者像Swift一样直接简单的方法

Thanks a lot.

非常感谢。

1 个解决方案

#1


1  

Maybe, this project on obj-c will help you: https://github.com/freedomtan/SimpleInceptionV3-ObjC/tree/continuous-mobilenet/SimpleInceptionV3-ObjC

也许,obj-c上的这个项目将帮助你:https://github.com/freedomtan/SimpleInceptionV3-ObjC/tree/continuous-mobilenet/SimpleInceptionV3-ObjC

In my project I use this method to init my model

在我的项目中,我使用此方法来初始化我的模型

#import "my_model.h"

@property (nonatomic, strong) my_model *model;

- (void)configureModel {
    NSURL *modelUrl = [[NSBundle mainBundle] URLForResource:@"my_model" withExtension:@"mlmodelc"];
    NSError *error;
    self.model = [[my_model alloc] initWithContentsOfURL:modelUrl error:&error];
    if (error) {
        NSLog(@"cover search error: model not configure");
    }
}

Some explanations about why "mlmodelc": https://blog.zedge.net/developers-blog/hotswapping-machine-learning-models-in-coreml-for-iphone

关于为什么“mlmodelc”的一些解释:https://blog.zedge.net/developers-blog/hotswapping-machine-learning-models-in-coreml-for-iphone

#1


1  

Maybe, this project on obj-c will help you: https://github.com/freedomtan/SimpleInceptionV3-ObjC/tree/continuous-mobilenet/SimpleInceptionV3-ObjC

也许,obj-c上的这个项目将帮助你:https://github.com/freedomtan/SimpleInceptionV3-ObjC/tree/continuous-mobilenet/SimpleInceptionV3-ObjC

In my project I use this method to init my model

在我的项目中,我使用此方法来初始化我的模型

#import "my_model.h"

@property (nonatomic, strong) my_model *model;

- (void)configureModel {
    NSURL *modelUrl = [[NSBundle mainBundle] URLForResource:@"my_model" withExtension:@"mlmodelc"];
    NSError *error;
    self.model = [[my_model alloc] initWithContentsOfURL:modelUrl error:&error];
    if (error) {
        NSLog(@"cover search error: model not configure");
    }
}

Some explanations about why "mlmodelc": https://blog.zedge.net/developers-blog/hotswapping-machine-learning-models-in-coreml-for-iphone

关于为什么“mlmodelc”的一些解释:https://blog.zedge.net/developers-blog/hotswapping-machine-learning-models-in-coreml-for-iphone