【问题标题】:CoreML frameworks generated different results from coremltoolsCoreML 框架从 coremltools 生成了不同的结果
【发布时间】: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进行测试。

标签: ios coreml


【解决方案1】:

我遇到了类似的问题,但使用 Xcode 10 的 CreateML 工具创建的模型。虽然 CreateML 为我提供了出色的准确率和召回率,但我发现将模型与 Vision 框架一起使用时性能非常下降。

我偶然发现,如果在将图像传递给请求处理程序之前将其转换为数据,我可以获得更好的性能。那就是:

表现不佳: let handler = VNImageRequestHandler(cgImage: myCGImage, options: [:])

表现不错: let imageData = UIImagePNGRepresentation(UIImage(cgImage: myCGImage)!)! let handler = VNImageRequestHandler(data: imageData, options: [:])

不知道为什么会这样。

【讨论】:

  • 是的,我注意到了同样的事情,而且我的头撞到墙上好几天了。这是一个错误吗?
  • 我在 iOS beta 3 上的表现要差得多。我的解决方法不再奏效。我不得不恢复到 2。我今天要尝试 beta 4,看看它是否已解决。
【解决方案2】:

我遇到了类似的问题,我认为这与在 macOS 和 IOS 设备上使用不同的 Float 有关。不幸的是,我没有找到解决问题的方法,因为这似乎是系统级别的问题。

【讨论】:

    猜你喜欢
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2020-05-07
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多