ผู้รับมอบสิทธิ์ Tensorflow Lite Core ML

ผู้รับมอบสิทธิ์ TensorFlow Lite Core ML ช่วยให้สามารถเรียกใช้โมเดล TensorFlow Lite บน เฟรมเวิร์ก Core ML ได้ ซึ่งส่งผลให้การอนุมานโมเดลบนอุปกรณ์ iOS เร็วขึ้น

เวอร์ชันและอุปกรณ์ iOS ที่รองรับ:

  • iOS 12 และใหม่กว่า ใน iOS เวอร์ชันเก่า ผู้รับมอบสิทธิ์ Core ML จะถอยกลับไปใช้ CPU โดยอัตโนมัติ
  • ตามค่าเริ่มต้น ผู้รับมอบสิทธิ์ Core ML จะเปิดใช้งานบนอุปกรณ์ที่มี A12 SoC และใหม่กว่า (iPhone Xs และใหม่กว่า) เท่านั้น เพื่อใช้ Neural Engine เพื่อการอนุมานที่เร็วขึ้น หากคุณต้องการใช้ผู้รับมอบสิทธิ์ Core ML บนอุปกรณ์รุ่นเก่าด้วย โปรดดู แนวทางปฏิบัติที่ดีที่สุด

รุ่นที่รองรับ

ขณะนี้ผู้รับมอบสิทธิ์ Core ML รองรับรุ่น float (FP32 และ FP16)

ลองใช้ผู้รับมอบสิทธิ์ Core ML กับโมเดลของคุณเอง

ผู้ร่วมประชุม Core ML รวมอยู่ในการเปิดตัว TensorFlow lite CocoaPods ทุกคืนแล้ว หากต้องการใช้ผู้รับมอบสิทธิ์ Core ML ให้เปลี่ยนพ็อด 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)
    }
  

วัตถุประสงค์-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 บนอุปกรณ์ที่ไม่มี Neural Engine

ตามค่าเริ่มต้น ผู้รับมอบสิทธิ์ Core ML จะถูกสร้างขึ้นหากอุปกรณ์มี Neural Engine เท่านั้น และจะส่งคืน null หากไม่ได้สร้างผู้รับมอบสิทธิ์ หากคุณต้องการรันผู้รับมอบสิทธิ์ Core ML ในสภาพแวดล้อมอื่น (เช่น ตัวจำลอง) ให้ส่ง .all เป็นตัวเลือกในขณะที่สร้างผู้รับมอบสิทธิ์ใน 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])
      

วัตถุประสงค์-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
      

การใช้ผู้รับมอบสิทธิ์ Metal (GPU) เป็นทางเลือก

เมื่อไม่ได้สร้างผู้รับมอบสิทธิ์ Core ML คุณยังสามารถใช้ ผู้รับมอบสิทธิ์ Metal เพื่อรับประโยชน์ด้านประสิทธิภาพได้ ตัวอย่างต่อไปนี้แสดงวิธีการทำเช่นนี้:

สวิฟท์

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

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

วัตถุประสงค์-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) เพื่อกำหนดความพร้อมใช้งานของ Neural Engine ดู รหัส สำหรับรายละเอียดเพิ่มเติม หรือคุณสามารถใช้ชุดอุปกรณ์ที่ปฏิเสธรายการของคุณเองโดยใช้ไลบรารีอื่น เช่น 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
    • น้ำหนักและความลำเอียงควรคงที่
  • Conv2D เชิงลึก
    • น้ำหนักและความลำเอียงควรคงที่
  • เชื่อมต่ออย่างสมบูรณ์ (aka Dense หรือ InnerProduct)
    • น้ำหนักและความลำเอียง (ถ้ามี) ควรคงที่
    • รองรับเฉพาะเคสชุดเดียวเท่านั้น ขนาดอินพุตควรเป็น 1 ยกเว้นมิติสุดท้าย
  • ฮาร์ดสวิช
  • โลจิสติกส์ (aka Sigmoid)
  • แม็กซ์พูล2ดี
  • มิเรอร์แพด
    • รองรับเฉพาะอินพุต 4D พร้อมโหมด REFLECT เท่านั้น การเสริมควรคงที่ และใช้ได้เฉพาะกับขนาด H และ W เท่านั้น
  • มูล
    • สามารถออกอากาศได้เฉพาะบางรูปร่างเท่านั้น ในเค้าโครงเทนเซอร์ Core ML รูปร่างเทนเซอร์ต่อไปนี้สามารถออกอากาศได้ [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1] .
  • แพดและแพดV2
    • รองรับอินพุต 4D เท่านั้น การเสริมควรคงที่ และใช้ได้เฉพาะกับขนาด H และ W เท่านั้น
  • เรลู
  • รีลูN1To1
  • เรลู6
  • ปรับรูปร่างใหม่
    • รองรับเมื่อกำหนดเป้าหมาย Core ML เวอร์ชัน 2 เท่านั้น ไม่รองรับเมื่อกำหนดเป้าหมาย Core ML 3
  • ปรับขนาดBilinear
  • ซอฟท์แม็กซ์
  • ทานห์
  • ย้ายConv
    • น้ำหนักควรคงที่

ข้อเสนอแนะ

สำหรับปัญหา โปรดสร้างปัญหา GitHub โดยมีรายละเอียดที่จำเป็นทั้งหมดในการทำซ้ำ

คำถามที่พบบ่อย

  • CoreML มอบสิทธิ์รองรับทางเลือกสำรองไปยัง CPU หากกราฟมีการดำเนินการที่ไม่รองรับหรือไม่
    • ใช่
  • ผู้รับมอบสิทธิ์ CoreML ทำงานบน iOS Simulator หรือไม่
    • ใช่. ไลบรารีนี้มีเป้าหมาย x86 และ x86_64 เพื่อให้สามารถทำงานบนเครื่องจำลองได้ แต่คุณจะไม่เห็นการเพิ่มประสิทธิภาพบน CPU
  • ผู้รับมอบสิทธิ์ TensorFlow Lite และ CoreML รองรับ MacOS หรือไม่
    • TensorFlow Lite ได้รับการทดสอบบน iOS เท่านั้น แต่ไม่ใช่ MacOS
  • รองรับ TF Lite แบบกำหนดเองหรือไม่
    • ไม่ ผู้รับมอบสิทธิ์ CoreML ไม่รองรับการดำเนินการแบบกำหนดเอง และจะเลือกใช้ CPU แทน

API