gpt4 book ai didi

objective-c - 如何通过 CoreML 使用 Objective-C 中的机器学习模型

转载 作者:行者123 更新时间:2023-11-30 08:40:57 24 4
gpt4 key购买 nike

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

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

Xcode imports mlmodel as Objective-C class

我需要将向量放入模型中并获取响应。

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

非常感谢。

最佳答案

这是使用我自己的图像识别模型对我有用的方法:

#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 - 如何通过 CoreML 使用 Objective-C 中的机器学习模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com