首页 文章

CoreML框架从coremltools生成了不同的结果

提问于
浏览
1

我一直在使用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 Windsor领带:0.077577防弹背心:0.068461漫画书:0.022226

coremltools top-5军装:0.458214044571防弹背心:0.115854650736西装,西服:0.115854650736温莎领带:0.0413092523813 pickelhaube:0.0201325211674

测试图像original预先调整为299 * 299进行测试 .

1 回答

  • 0

    我有一个类似的问题,但使用Xcode 10的CreateML工具创建的模型 . 虽然CreateML给了我极好的精确度和回忆率,但在使用Vision框架的模型时,我看到了非常低的性能 .

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

    Poor performance: let handler = VNImageRequestHandler(cgImage: myCGImage, options: [:])

    Good performance: let imageData = UIImagePNGRepresentation(UIImage(cgImage: myCGImage)!)! let handler = VNImageRequestHandler(data: imageData, options: [:])

    不知道为什么会这样 .

相关问题