ผู้มอบสิทธิ์ 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 รองรับโมเดลแบบลอย (FP32 และ FP16)
การลองใช้ผู้รับมอบสิทธิ์ Core ML ในโมเดลของคุณเอง
ผู้ที่ได้รับมอบสิทธิ์ Core ML ได้รวมอยู่ใน 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']
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) }
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 ในอุปกรณ์ที่ไม่มี 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])
Objective-C
TFLCoreMLDelegateOptions* coreMLOptions = [[TFLCoreMLDelegateOptions alloc] init]; coreMLOptions.enabledDevices = TFLCoreMLDelegateEnabledDevicesAll; TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc] initWithOptions:coreMLOptions]; // Initialize interpreter with delegate
C
TfLiteCoreMlDelegateOptions options; options.enabled_devices = TfLiteCoreMlDelegateAllDevices; TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(&options); // Initialize interpreter with delegate
การใช้การมอบสิทธิ์ Metal(GPU) เป็นทางเลือกสำรอง
เมื่อไม่มีการสร้างการมอบสิทธิ์ Core ML คุณจะใช้การมอบสิทธิ์โลหะเพื่อรับสิทธิประโยชน์ด้านประสิทธิภาพได้ ตัวอย่างต่อไปนี้จะแสดงวิธีการดำเนินการดังกล่าว
Swift
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
C
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 ระบบจะตั้งค่าเวอร์ชัน Conversion เป้าหมายเป็นเวอร์ชันล่าสุดโดยค่าเริ่มต้น แต่คุณเปลี่ยนการตั้งค่านี้ได้โดยการตั้งค่า coreMLVersion
(ใน Swift coreml_version
ใน C API) ในตัวเลือก "มอบสิทธิ์" เป็นเวอร์ชันเก่า
Ops ที่รองรับ
ผู้มอบสิทธิ์ Core ML รองรับการดำเนินการต่อไปนี้
- เพิ่ม
- เฉพาะรูปร่างบางรูปเท่านั้นที่ประกาศได้ ในเลย์เอาต์ tensor ของ Core ML รูปร่าง Tensor ต่อไปนี้จะออกอากาศได้
[B, C, H, W]
,[B, C, 1, 1]
,[B, 1, H, W]
,[B, 1, 1, 1]
- เฉพาะรูปร่างบางรูปเท่านั้นที่ประกาศได้ ในเลย์เอาต์ tensor ของ Core ML รูปร่าง Tensor ต่อไปนี้จะออกอากาศได้
- ค่าเฉลี่ยพูล 2 วัน
- Concat
- ควรต่อกันตามแกนช่อง
- Conv2D
- น้ำหนักและการให้น้ำหนักควรคงที่
- DepthwiseConv2D
- น้ำหนักและการให้น้ำหนักควรคงที่
- เชื่อมต่อกันทั้งหมด (หรือที่เรียกว่า Dense หรือ InnerProduct)
- น้ำหนักและการให้น้ำหนักพิเศษ (หากมี) ควรคงที่
- รองรับเฉพาะเคสแบบกลุ่มเท่านั้น มิติข้อมูลอินพุตควรเป็น 1 ยกเว้นมิติข้อมูลสุดท้าย
- ฮาร์ดสวิช
- โลจิสติกส์ (หรือที่เรียกว่าซิกมอยด์)
- พูล 2 มิติสูงสุด
- MirrorPad
- รองรับเฉพาะอินพุต 4D ที่ใช้โหมด
REFLECT
เท่านั้น ระยะห่างจากขอบควรมีค่าคงที่และอนุญาตเฉพาะขนาด H และ W เท่านั้น
- รองรับเฉพาะอินพุต 4D ที่ใช้โหมด
- มล
- เฉพาะรูปร่างบางรูปเท่านั้นที่ประกาศได้ ในเลย์เอาต์ tensor ของ Core ML รูปร่าง Tensor ต่อไปนี้จะออกอากาศได้
[B, C, H, W]
,[B, C, 1, 1]
,[B, 1, H, W]
,[B, 1, 1, 1]
- เฉพาะรูปร่างบางรูปเท่านั้นที่ประกาศได้ ในเลย์เอาต์ tensor ของ Core ML รูปร่าง Tensor ต่อไปนี้จะออกอากาศได้
- Pad และ PadV2
- รองรับเฉพาะอินพุต 4D เท่านั้น ระยะห่างจากขอบควรคงที่และอนุญาตให้ใช้สำหรับขนาด H และ W เท่านั้น
- เรลู
- ReluN1To1
- Relu6
- ปรับรูปร่าง
- ระบบจะไม่รองรับเมื่อ Core ML เวอร์ชันเป้าหมายคือ 2 เท่านั้น แต่ไม่รองรับเมื่อกำหนดเป้าหมาย Core ML 3
- ResizeBilinear
- SoftMax
- ตานห์
- TransposeConv
- น้ำหนักควรคงที่
ความคิดเห็น
สำหรับปัญหา โปรดสร้างปัญหา GitHub พร้อมด้วยรายละเอียดที่จำเป็นทั้งหมดในการทำซ้ำ
คำถามที่พบบ่อย
- หากกราฟมีการดำเนินการที่ไม่รองรับ จะรองรับ CoreML แทน CPU ไหม
- ได้
- การมอบสิทธิ์ CoreML ทำงานบน iOS Simulator ได้ไหม
- ได้ ไลบรารีนี้มีเป้าหมาย x86 และ x86_64 ดังนั้นจึงสามารถทำงานใน เครื่องมือจำลองได้ แต่คุณจะไม่เห็นการเพิ่มประสิทธิภาพเหนือ CPU
- TensorFlow Lite และการมอบสิทธิ์ CoreML รองรับ MacOS ไหม
- TensorFlow Lite ได้รับการทดสอบใน iOS เท่านั้น แต่ไม่ทดสอบใน MacOS
- ระบบรองรับการดำเนินการ TF Lite ที่กำหนดเองไหม
- ไม่ การมอบสิทธิ์ CoreML ไม่รองรับการดำเนินการที่กำหนดเองและจะใช้ CPU แทน