【发布时间】:2017-11-30 22:07:28
【问题描述】:
我一直在按照 CoreML 示例在 ios 11 中开发图像识别应用程序。但是我注意到在 ios 中调用模型时的结果与在 mac/python 中使用 coremltools 的结果有所不同。我认为差异可能在于图像加载部分。 Python 代码使用 Pillow 加载图像,但 xcode 使用 CoreImage。我粘贴了如下关键代码。希望有人能帮忙指出问题。
输入图像也是 299*299 jpg。因此,在任何一个实现中都不应该发生任何调整大小。谢谢。
python 代码
import coremltools
from PIL import Image
from keras.preprocessing import image
import numpy as np
IMG_PATH='./test.jpg'
img = image.load_img(IMG_PATH)
model=coremltools.models.MLModel("./Inceptionv3.mlmodel")
res = model.predict({'image':img})
ios 代码
self.image = [CIImage imageWithContentsOfURL:fileURL];
self.model = [[[Inceptionv3 alloc] init] model];
VNCoreMLModel *m = [VNCoreMLModel modelForMLModel: self.model error:nil];
VNCoreMLRequest *rq = [[VNCoreMLRequest alloc] initWithModel: m completionHandler: (VNRequestCompletionHandler) ^(VNRequest *request, NSError *error){
NSArray *results = [request.results copy];
NSString *top_results = @"";
for(int index = 0; index < kNumResults; index++)
{
VNClassificationObservation *res = ((VNClassificationObservation *)(results[index]));
NSString *tmp = [top_results stringByAppendingFormat: @"- %d %.4f %@\n ", index, res.confidence,res.identifier];
top_results = [tmp copy];
}
self.label_prob = [top_results copy];
}];
NSDictionary *d = [[NSDictionary alloc] init];
NSArray *a = @[rq];
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCIImage:self.image options:d];
dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);
dispatch_sync(myCustomQueue, ^{
[handler performRequests:a error:nil];
});
区别:
CoreML top-5 军装:0.254365 西装,西装套装:0.198099 温莎领带:0.077577 防弹背心:0.068461 漫画书:0.022226
coremltools top-5 军装:0.458214044571 防弹背心:0.115854650736 西装,西装套装:0.115854650736 温莎领带:0.0413092523813 pickelhaube:0.0201325211674
测试图像 original 已预先调整为 299*299 以进行测试。
【问题讨论】:
-
“但是我注意到结果有所不同......”你能详细说明这种差异是什么吗?
-
#CoreML top-5 军装:0.254365 西装,西装套装:0.198099 温莎领带:0.077577 防弹背心:0.068461 漫画书:0.022226 #coremltools top-5 军装:0.458214044571 防弹背心:0.115854 , 西装: 0.115854650736 温莎领带: 0.0413092523813 pickelhaube: 0.0201325211674 测试图片在link, 已预先调整为299*299进行测试。