簡體   English   中英

在將圖像提供給 CoreML 模型之前如何預處理圖像?

[英]How do I preprocess the image before giving it to CoreML Model?

我創建了一個圖像相似性模型並使用參考數據圖像對其進行了測試。 我測試了 turicreate 模型,我得到了參考數據圖像的零距離,當將此代碼與 coreml 模型一起使用時,同樣的情況又回來了:

image = tc.image_analysis.resize(reference_data[0]['image'], *reversed(model.input_image_shape))
image = PIL.Image.fromarray(image.pixel_data)
mlmodel.predict({'image':image})`

但是,當在 iOS 中使用模型作為 VNCoreMLModel 時,沒有參考圖像測試返回零距離,而且大多數甚至不是最短距離,即參考圖像 0 與參考 id 78 的距離最短。由於coreml 模型在 python 中工作,我認為這是一個預處理問題,所以我在將其傳遞給 CoreMLModel 之前自己對圖像進行了預處理。 這樣做為我提供了與最短距離的參考圖像匹配的參考 ID 的一致輸出 - 是的。 距離仍然不是零,所以我試圖做任何我能想到的事情來影響圖像以獲得一些差異,但我無法讓它更接近零。 預處理代碼:

+ (CVPixelBufferRef)pixelBufferForImage:(UIImage *)image sideLength:(CGFloat)sideLength {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sideLength, sideLength), YES, image.scale);
    [image drawInRect:CGRectMake(0, 0, sideLength, sideLength)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CFStringRef keys[2] = {kCVPixelBufferCGImageCompatibilityKey, kCVPixelBufferCGBitmapContextCompatibilityKey};
    CFBooleanRef values[2] = {kCFBooleanTrue, kCFBooleanTrue};
    CFDictionaryRef attrs = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CVPixelBufferRef buffer;
    int status = CVPixelBufferCreate(kCFAllocatorDefault, (int)(sideLength), (int)(sideLength), kCVPixelFormatType_32ARGB, attrs, &buffer);
    if (status != kCVReturnSuccess) {
        return nil;
    }

    CVPixelBufferLockBaseAddress(buffer, kCVPixelBufferLock_ReadOnly);
    void *data = CVPixelBufferGetBaseAddress(buffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
    CGContextRef context = CGBitmapContextCreate(data, sideLength, sideLength, 8, CVPixelBufferGetBytesPerRow(buffer), colorSpace, kCGImageAlphaNoneSkipFirst);

    CGContextTranslateCTM(context, 0, sideLength);
    CGContextScaleCTM(context, 1.0, -1.0);

    UIGraphicsPushContext(context);
    [resizedImage drawInRect:CGRectMake(0, 0, sideLength, sideLength)];
    UIGraphicsPopContext();
    CVPixelBufferUnlockBaseAddress(buffer, kCVPixelBufferLock_ReadOnly);
    return buffer;
}

mlmodel 使用大小為 (224, 224) 的 RGB 圖像

我還能對圖像做些什么來改善我的結果?

我和你在同一條船上。 由於圖像預處理涉及使用模糊、從 RGB 到灰度的轉換等步驟。 使用 Objective C++ 包裝器會更容易。 下面的鏈接很好地理解了如何使用頭類鏈接它。

https://www.timpoulsen.com/2019/using-opencv-in-an-ios-app.html

希望能幫助到你!

圖片來源: https : //medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b

學分:https://medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM