简体   繁体   English

CoreML MLModel 无法获取 .modelDescription 参数 SqueezeNetInt8LUT

[英]CoreML MLModel cannot get .modelDescription parameter SqueezeNetInt8LUT

I am trying to use SqueezeNet to make a prediction on a test image (that is just an asset in my project) using Objective-C.我正在尝试使用 SqueezeNet 使用 Objective-C 对测试图像(这只是我项目中的一项资产)进行预测。 I am struggling quite a bit, in part because the documentation examples are all in Swift.我有点挣扎,部分原因是文档示例都是用 Swift 编写的。

The issue I am encountering is that while I can instantiate a squeezeNet model, I cannot get the .modelDescription parameter, neither in the debugger nor runtime.我遇到的问题是,虽然我可以实例化一个squeezeNet 模型,但我无法在调试器和运行时获取 .modelDescription 参数。

The code to causing this is similar to this:导致这种情况的代码类似于:

@interface TestViewController ()
@property (strong, nonatomic) MLModel* model;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"SqueezeNetInt8LUT" withExtension:@"mlmodelc"];
    MLModelConfiguration* config = [MLModelConfiguration new];
    self.model = [[SqueezeNetInt8LUT alloc] initWithContentsOfURL:modelURL error:nil];

}

- (IBAction)didTapTest:(id)sender {

    NSLog(@"%@", self.model.modelDescription);

    UIImage* testImage = [UIImage imageNamed:@"mountain"];
    MLImageConstraint* constraint = self.model.modelDescription.inputDescriptionsByName[@"image"].imageConstraint;

    MLFeatureValue* imageFeature = [MLFeatureValue featureValueWithCGImage:cgtest constraint:constraint options:nil error:nil];
    NSMutableDictionary* featureDict = [[NSMutableDictionary alloc] init];
    featureDict[@"mountain"] = imageFeature;
    MLDictionaryFeatureProvider* featureProv = [[MLDictionaryFeatureProvider new] initWithDictionary:featureDict error:nil];
    MLDictionaryFeatureProvider* pred = [self.model predictionFromFeatures:featureProv error:nil];
}
@end

Where the exception is thrown as soon as I do the NSLog.一旦我执行 NSLog,就会抛出异常。 Any ideas?有任何想法吗? Did I initialize the model wrong?我是不是初始化模型错了? Despite po self.model giving <SqueezeNetInt8LUT: 0x600001d90040>尽管 po self.model 给出<SqueezeNetInt8LUT: 0x600001d90040>

There were several issues with the above code that are not apparent from spending a whole day reading CoreML documentation and examples:上面的代码有几个问题,花一整天的时间阅读 CoreML 文档和示例并不明显:

  1. Do NOT do this: @property (strong, nonatomic) MLModel* model;不要这样做: @property (strong, nonatomic) MLModel* model; instead do @property (strong, nonatomic) SqueezeNetInt8LUT* model;而是做@property (strong, nonatomic) SqueezeNetInt8LUT* model;

  2. I named the entry in the MLDictionaryFeatureProvider @"mountain", but unlike a normal dictionary, the key name for FeatureProviders MUST match the name of the input parameter expected by the model, as shown in the image attached.我将 MLDictionaryFeatureProvider 中的条目命名为 @"mountain",但与普通字典不同的是,FeatureProviders 的键名必须与模型预期的输入参数的名称相匹配,如附图所示。 在此处输入图像描述

so the line所以这条线
featureDict[@"mountain"] = imageFeature; needed to be featureDict[@"image"] = imageFeature;需要featureDict[@"image"] = imageFeature;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM