נציג Tensorflow Lite Core ML

הנציג של TensorFlow Lite Core ML מאפשר להריץ דגמי TensorFlow Lite על Core ML Framework , מה שמביא להסקת מודלים מהירה יותר במכשירי iOS.

גירסאות ומכשירי iOS נתמכים:

  • iOS 12 ואילך. בגרסאות iOS הישנות יותר, Core ML delegate יחזור אוטומטית ל-CPU.
  • כברירת מחדל, Core ML delegate יהיה זמין רק במכשירים עם A12 SoC ואילך (iPhone Xs ואילך) כדי להשתמש במנוע Neural להסקת הסקה מהירה יותר. אם ברצונך להשתמש ב-Core ML delegate גם במכשירים הישנים יותר, ראה שיטות עבודה מומלצות

דגמים נתמכים

נציג Core ML תומך כיום בדגמי Float (FP32 ו-FP16).

נסה את נציג Core ML על הדגם שלך

נציג Core ML כבר כלול במהדורה לילית של TensorFlow lite CocoaPods. כדי להשתמש ב-Core ML delegate, שנה את הפוד של TensorFlow lite כך שיכלול תת-מפרט CoreML ב- Podfile שלך.

target 'YourProjectName'
  pod 'TensorFlowLiteSwift/CoreML', '~> 2.4.0'  # Or TensorFlowLiteObjC/CoreML

אוֹ

# Particularily useful when you also want to include 'Metal' subspec.
target 'YourProjectName'
  pod 'TensorFlowLiteSwift', '~> 2.4.0', :subspecs => ['CoreML']

מָהִיר

    let coreMLDelegate = CoreMLDelegate()
    var interpreter: Interpreter

    // Core ML delegate will only be created for devices with Neural Engine
    if coreMLDelegate != nil {
      interpreter = try Interpreter(modelPath: modelPath,
                                    delegates: [coreMLDelegate!])
    } else {
      interpreter = try Interpreter(modelPath: modelPath)
    }
  

Objective-C


    // Import module when using CocoaPods with module support
    @import TFLTensorFlowLite;

    // Or import following headers manually
    # import "tensorflow/lite/objc/apis/TFLCoreMLDelegate.h"
    # import "tensorflow/lite/objc/apis/TFLTensorFlowLite.h"

    // Initialize Core ML delegate
    TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc] init];

    // Initialize interpreter with model path and Core ML delegate
    TFLInterpreterOptions* options = [[TFLInterpreterOptions alloc] init];
    NSError* error = nil;
    TFLInterpreter* interpreter = [[TFLInterpreter alloc]
                                    initWithModelPath:modelPath
                                              options:options
                                            delegates:@[ coreMLDelegate ]
                                                error:&error];
    if (error != nil) { /* Error handling... */ }

    if (![interpreter allocateTensorsWithError:&error]) { /* Error handling... */ }
    if (error != nil) { /* Error handling... */ }

    // Run inference ...
  

C (עד 2.3.0)

    #include "tensorflow/lite/delegates/coreml/coreml_delegate.h"

    // Initialize interpreter with model
    TfLiteModel* model = TfLiteModelCreateFromFile(model_path);

    // Initialize interpreter with Core ML delegate
    TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
    TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(NULL);  // default config
    TfLiteInterpreterOptionsAddDelegate(options, delegate);
    TfLiteInterpreterOptionsDelete(options);

    TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);

    TfLiteInterpreterAllocateTensors(interpreter);

    // Run inference ...

    /* ... */

    // Dispose resources when it is no longer used.
    // Add following code to the section where you dispose of the delegate
    // (e.g. `dealloc` of class).

    TfLiteInterpreterDelete(interpreter);
    TfLiteCoreMlDelegateDelete(delegate);
    TfLiteModelDelete(model);
      

שיטות עבודה מומלצות

שימוש ב-Core ML Delegate במכשירים ללא Neural Engine

כברירת מחדל, Core ML delegate ייווצר רק אם למכשיר יש מנוע עצבי, ויחזור null אם הנציג לא נוצר. אם ברצונך להפעיל את Core ML delegate בסביבות אחרות (לדוגמה, סימולטור), העבר את .all כאפשרות תוך יצירת delegate ב- Swift. ב-C++ (וב-Objective-C), אתה יכול להעביר את TfLiteCoreMlDelegateAllDevices . הדוגמה הבאה מראה כיצד לעשות זאת:

מָהִיר

    var options = CoreMLDelegate.Options()
    options.enabledDevices = .all
    let coreMLDelegate = CoreMLDelegate(options: options)!
    let interpreter = try Interpreter(modelPath: modelPath,
                                      delegates: [coreMLDelegate])
      

Objective-C

    TFLCoreMLDelegateOptions* coreMLOptions = [[TFLCoreMLDelegateOptions alloc] init];
    coreMLOptions.enabledDevices = TFLCoreMLDelegateEnabledDevicesAll;
    TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc]
                                          initWithOptions:coreMLOptions];

    // Initialize interpreter with delegate
  

ג

    TfLiteCoreMlDelegateOptions options;
    options.enabled_devices = TfLiteCoreMlDelegateAllDevices;
    TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(&options);
    // Initialize interpreter with delegate
      

שימוש בנציג מתכת (GPU) כחלופה.

כאשר הנציג Core ML אינו נוצר, לחלופין עדיין תוכל להשתמש ב-Metal delegate כדי לקבל הטבות בביצועים. הדוגמה הבאה מראה כיצד לעשות זאת:

מָהִיר

    var delegate = CoreMLDelegate()
    if delegate == nil {
      delegate = MetalDelegate()  // Add Metal delegate options if necessary.
    }

    let interpreter = try Interpreter(modelPath: modelPath,
                                      delegates: [delegate!])
  

Objective-C

    TFLDelegate* delegate = [[TFLCoreMLDelegate alloc] init];
    if (!delegate) {
      // Add Metal delegate options if necessary
      delegate = [[TFLMetalDelegate alloc] init];
    }
    // Initialize interpreter with delegate
      

ג

    TfLiteCoreMlDelegateOptions options = {};
    delegate = TfLiteCoreMlDelegateCreate(&options);
    if (delegate == NULL) {
      // Add Metal delegate options if necessary
      delegate = TFLGpuDelegateCreate(NULL);
    }
    // Initialize interpreter with delegate
      

ההיגיון של יצירת הנציג קורא את מזהה המכונה של המכשיר (למשל iPhone11,1) כדי לקבוע את זמינות המנוע העצבי שלו. עיין בקוד לפרטים נוספים. לחלופין, אתה יכול ליישם קבוצה משלך של התקני דניליסט באמצעות ספריות אחרות כגון DeviceKit .

משתמש בגרסת Core ML ישנה יותר

למרות ש-iOS 13 תומך ב-Core ML 3, הדגם עשוי לעבוד טוב יותר כאשר הוא יומר עם מפרט דגם Core ML 2. גרסת המרת היעד מוגדרת לגרסה העדכנית ביותר כברירת מחדל, אך ניתן לשנות זאת על ידי הגדרת coreMLVersion (ב-Swift, coreml_version ב-C API) באפשרות הנציג לגרסה ישנה יותר.

פעולות נתמכות

הפעולות הבאות נתמכות על ידי נציג Core ML.

  • לְהוֹסִיף
    • רק צורות מסוימות ניתנות לשידור. בפריסת טנזור Core ML, צורות הטנזור הבאות ניתנות לשידור. [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1] .
  • AveragePool2D
  • קונקט
    • שרשור צריך להיעשות לאורך ציר הערוץ.
  • Conv2D
    • משקל והטיה צריכים להיות קבועים.
  • DepthwiseConv2D
    • משקל והטיה צריכים להיות קבועים.
  • FullyConnected (המכונה גם צפוף או מוצר פנימי)
    • משקל והטיה (אם קיימים) צריכים להיות קבועים.
    • תומך רק במארז יחיד. מידות הקלט צריכים להיות 1, מלבד הממד האחרון.
  • קשה
  • לוגיסטיקה (המכונה גם סיגמואיד)
  • MaxPool2D
  • MirrorPad
    • רק קלט 4D עם מצב REFLECT נתמך. הריפוד צריך להיות קבוע, ומותר רק עבור מידות H ו-W.
  • מול
    • רק צורות מסוימות ניתנות לשידור. בפריסת טנזור Core ML, צורות הטנזור הבאות ניתנות לשידור. [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1] .
  • Pad ו- PadV2
    • רק קלט 4D נתמך. הריפוד צריך להיות קבוע, ומותר רק עבור מידות H ו-W.
  • רלו
  • ReluN1To1
  • Relu6
  • שִׁנוּי צוּרָה
    • נתמך רק כאשר יעד Core ML גרסה 2, לא נתמך כאשר מיקוד ל-Core ML 3.
  • שינוי גודלBilinear
  • SoftMax
  • טן
  • TransposeConv
    • משקל צריך להיות קבוע.

מָשׁוֹב

לבעיות, אנא צור בעיית GitHub עם כל הפרטים הדרושים לשחזור.

שאלות נפוצות

  • האם CoreML מאציל תמיכה ל-CPU אם גרף מכיל פעולות לא נתמכות?
    • כן
  • האם האצלת CoreML עובדת בסימולטור iOS?
    • כן. הספרייה כוללת יעדי x86 ו-x86_64 כך שהיא יכולה לפעול בסימולטור, אך לא תראה שיפור בביצועים על פני המעבד.
  • האם TensorFlow Lite ו-CoreML תומכים ב-MacOS?
    • TensorFlow Lite נבדק רק ב-iOS אך לא ב-MacOS.
  • האם אופציות TF Lite מותאמות אישית נתמכות?
    • לא, CoreML delegate אינו תומך באופציות מותאמות אישית והם יחזרו ל-CPU.

ממשקי API