ขอขอบคุณที่เข้าร่วม Google I/O ดูเซสชั่นทั้งหมดตามความต้องการ ดูตามความต้องการ

ผู้รับมอบสิทธิ์ 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 pod ของคุณเพื่อรวม CoreML ข้อมูล Podfile CoreML คุณ

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']

Swift

    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 ...
  

ค (จนถึง 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 ได้ ตัวอย่างต่อไปนี้แสดงวิธีการทำสิ่งนี้:

Swift

    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 เพื่อรับประโยชน์ด้านประสิทธิภาพได้ ตัวอย่างต่อไปนี้แสดงวิธีการทำสิ่งนี้:

Swift

    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) ในตัวเลือกผู้รับมอบสิทธิ์เป็นเวอร์ชันเก่ากว่า

ปฏิบัติการที่รองรับ

ops ต่อไปนี้ได้รับการสนับสนุนโดยตัวแทน Core ML

  • เพิ่ม
    • เฉพาะบางรูปร่างเท่านั้นที่สามารถแพร่ภาพได้ ในเลย์เอาต์ Core ML tensor รูปร่างตามเทนเซอร์สามารถแพร่ภาพได้ [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1] .
  • AveragePool2D
  • คอนแคท
    • การต่อกันควรทำตามแนวแกนของช่อง
  • Conv2D
    • น้ำหนักและอคติควรคงที่
  • DepthwiseConv2D
    • น้ำหนักและอคติควรคงที่
  • เชื่อมต่ออย่างสมบูรณ์ (aka Dense หรือ InnerProduct)
    • น้ำหนักและอคติ (ถ้ามี) ควรคงที่
    • รองรับเฉพาะเคสแบทช์เดียว ขนาดอินพุตควรเป็น 1 ยกเว้นมิติสุดท้าย
  • ฮาร์ดสวิช
  • โลจิสติกส์ (aka Sigmoid)
  • MaxPool2D
  • MirrorPad
    • รองรับเฉพาะอินพุต 4D พร้อมโหมด REFLECT การเติมควรเป็นแบบคงที่ และอนุญาตสำหรับขนาด H และ W เท่านั้น
  • มูล
    • เฉพาะบางรูปร่างเท่านั้นที่สามารถแพร่ภาพได้ ในเลย์เอาต์ Core ML tensor รูปร่างตามเทนเซอร์สามารถแพร่ภาพได้ [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1] .
  • แพดและแพดV2
    • รองรับเฉพาะอินพุต 4D การเติมควรเป็นแบบคงที่ และอนุญาตสำหรับขนาด H และ W เท่านั้น
  • Relu
  • ReluN1To1
  • Relu6
  • ก่อร่างใหม่
    • รองรับเฉพาะเมื่อเป้าหมาย Core ML เวอร์ชัน 2 ไม่รองรับเมื่อกำหนดเป้าหมาย Core ML 3
  • ปรับขนาดBilinear
  • SoftMax
  • Tanh
  • TransposeConv
    • น้ำหนักควรคงที่

ข้อเสนอแนะ

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

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

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

APIs