Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用CoreML在objective-C中使用机器学习模型_Objective C_Xcode_Machine Learning_Coreml - Fatal编程技术网

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

如何用CoreML在objective-C中使用机器学习模型,objective-c,xcode,machine-learning,coreml,Objective C,Xcode,Machine Learning,Coreml,我用objective-C制作了几千行机器视觉项目。为了完成它,我需要导入我的机器my_model.mlmodel和最新的coreML库。作为题外话,My_model.mlmodel是使用coremltools在Python中创建的 我正在尝试实例化它,但没有任何效果。我找不到任何关于这个主题的教程或帮助。当然,我将我的模型导入到纯Swift项目中,并且它工作正常。 因此,我将Swift类附加到我的项目中,希望它能以这种方式工作,但这里,Xcode再次将模型转换为Objective-C生成的模型

我用objective-C制作了几千行机器视觉项目。为了完成它,我需要导入我的机器my_model.mlmodel和最新的coreML库。作为题外话,My_model.mlmodel是使用coremltools在Python中创建的 我正在尝试实例化它,但没有任何效果。我找不到任何关于这个主题的教程或帮助。当然,我将我的模型导入到纯Swift项目中,并且它工作正常。 因此,我将Swift类附加到我的项目中,希望它能以这种方式工作,但这里,Xcode再次将模型转换为Objective-C生成的模型接口,并且该模型在Swift类中不可见

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

我需要在模型中加入向量,然后得到响应

请帮帮我;我从完成这个项目中积累了几行。 如何在Objective-C中使用My_model.mlmodel 有没有像斯威夫特那样的简单方法


非常感谢。

也许,这个关于obj-c的项目将帮助您:

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

#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");
    }
}
关于mlmodelc的一些解释:

这是我使用自己的图像识别模型的工作方式:

#import <CoreML/CoreML.h>
#import <Vision/Vision.h>
#import "Your_Model_Here.h"    

- (void)readModelMLObjc{

            MLModel *model;
            VNCoreMLModel *m;
            VNCoreMLRequest *request;
            model = [[[Your_Model_Here alloc] init] model];

            m = [VNCoreMLModel modelForMLModel: model error:nil];
            request = [[VNCoreMLRequest alloc] initWithModel: m completionHandler: (VNRequestCompletionHandler) ^(VNRequest *request, NSError *error){
                dispatch_async(dispatch_get_main_queue(), ^{

                    NSInteger numberOfResults = request.results.count;
                    NSArray *results = [request.results copy];
                    VNClassificationObservation *topResult = ((VNClassificationObservation *)(results[0]));
                    NSString *messageLabel = [NSString stringWithFormat: @"%f: %@", topResult.confidence, topResult.identifier];
                    NSLog(@"%@", messageLabel);

                });
            }];

            request.imageCropAndScaleOption = VNImageCropAndScaleOptionCenterCrop;


            CIImage *coreGraphicsImage = [[CIImage alloc] initWithImage:image];

            dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
                VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCIImage:coreGraphicsImage  options:@{}];
                [handler performRequests:@[request] error:nil];
            });}

        }

我希望我帮了忙

如果你点击线性_模型旁边的小箭头,上面写着模型的Objective-C生成接口,那么Xcode会告诉你这个Obj-C代码是什么样子的。我试着选择这个选项,但我做错了什么。该模型甚至可以访问我的代码,但我不知道如何输入或输出数据。如果我把它放在我的代码中,编译器会发出警告:表达式结果未使用您可能有Objective-C中的任何示例吗?我试图用苹果文档网站上的所有可能的组合来实例化这个模型。Swift版本简单得多,但项目太大,无法将其转换为Swift。我试图通过我的项目中的Swift代码来实现这一点,但Xcode自动将模型转换为Objective-C,所以我又回到了原点。嗨,Alex,当我试图使用所选图像时,它崩溃了2019-08-09 23:22:24.460166+0600 SimpleInceptionV3 ObjC[8806:452968][MC]systemgroup.com.apple.configurationprofiles路径的系统组容器为/private/var/containers/Shared/systemgroup/systemgroup.com.apple.configurationprofiles 2019-08-09 23:22:24.460404+0600 SimpleInceptionV3 ObjC[8806:452968][MC MC。你能告诉我为什么它会崩溃吗?上帝保佑你,我们所有人!!呵呵