[英]CoreML MLModel cannot get .modelDescription parameter SqueezeNetInt8LUT
我正在嘗試使用 SqueezeNet 使用 Objective-C 對測試圖像(這只是我項目中的一項資產)進行預測。 我有點掙扎,部分原因是文檔示例都是用 Swift 編寫的。
我遇到的問題是,雖然我可以實例化一個squeezeNet 模型,但我無法在調試器和運行時獲取 .modelDescription 參數。
導致這種情況的代碼類似於:
@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
一旦我執行 NSLog,就會拋出異常。 有任何想法嗎? 我是不是初始化模型錯了? 盡管 po self.model 給出<SqueezeNetInt8LUT: 0x600001d90040>
上面的代碼有幾個問題,花一整天的時間閱讀 CoreML 文檔和示例並不明顯:
不要這樣做: @property (strong, nonatomic) MLModel* model;
而是做@property (strong, nonatomic) SqueezeNetInt8LUT* model;
我將 MLDictionaryFeatureProvider 中的條目命名為 @"mountain",但與普通字典不同的是,FeatureProviders 的鍵名必須與模型預期的輸入參數的名稱相匹配,如附圖所示。
所以這條線featureDict[@"mountain"] = imageFeature;
需要featureDict[@"image"] = imageFeature;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.