TensorFlow Lite Core ML デリゲートにより、Core ML フレームワークで TensorFlow Lite モデルを実行できるため、iOS デバイスでのモデルの推論が高速化されます。
サポートされている iOS のバージョンとデバイス:
- iOS 12 以降。古い iOS バージョンでは、Core ML デリゲートは自動的に CPU にフォールバックします。
- デフォルトでは、Core ML デリゲートは、Neural Engine を使用して推論を高速化するために、A12 SoC 以降(iPhone Xs 以降)を搭載したデバイスでのみ有効になります。古いデバイスでも Core ML デリゲートを使用する場合は、ベスト プラクティスをご覧ください。
サポートされているモデル
現在、Core ML デリゲートは浮動小数点数(FP32 と FP16)モデルをサポートしています。
独自のモデルで Core ML デリゲートを試す
Core ML デリゲートは、TensorFlow Lite CocoaPods のナイトリー リリースにすでに含まれています。Core ML デリゲートを使用するには、TensorFlow Lite Pod を変更して、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) }
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);
ベスト プラクティス
Neural Engine のないデバイスでの Core ML デリゲートの使用
デフォルトでは、Core ML デリゲートは、デバイスに Neural Engine が搭載されている場合にのみ作成され、デリゲートが作成されない場合は null
を返します。他の環境(シミュレータなど)で Core ML デリゲートを実行する場合は、Swift でデリゲートを作成するときに、オプションとして .all
を渡します。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
デリゲート作成ロジックは、デバイスのマシン ID(iPhone11,1 など)を読み取り、Neural Engine を使用できるかどうかを判断します。詳しくは、コードをご覧ください。または、DeviceKit などの他のライブラリを使用して、拒否リスト デバイスの独自のセットを実装することもできます。
古い Core ML バージョンの使用
iOS 13 は Core ML 3 をサポートしていますが、Core ML 2 モデル仕様で変換すると、モデルが適切に機能する可能性があります。対象の変換バージョンはデフォルトで最新バージョンに設定されていますが、デリゲート オプションで coreMLVersion
(Swift では、C API では coreml_version
)を古いバージョンに設定することで変更できます。
サポートされているオペレーション
Core ML デリゲートでは、次のオペレーションがサポートされています。
- 以下を追加
- ブロードキャストできるのは、特定のシェイプのみです。Core ML のテンソル レイアウトでは、次のテンソル形状をブロードキャストできます。
[B, C, H, W]
、[B, C, 1, 1]
、[B, 1, H, W]
、[B, 1, 1, 1]
。
- ブロードキャストできるのは、特定のシェイプのみです。Core ML のテンソル レイアウトでは、次のテンソル形状をブロードキャストできます。
- 平均プール 2D
- 連結
- 連結はチャネル軸に沿って行う必要があります。
- Conv2D
- 重みとバイアスは一定でなければなりません。
- DepthwiseConv2D
- 重みとバイアスは一定でなければなりません。
- Fully Connected(別名 Dense または InnerProduct)
- 重みとバイアス(存在する場合)は一定である必要があります。
- 単一バッチのケースのみをサポートしています。入力ディメンションは、最後のディメンションを除いて 1 にする必要があります。
- ハードスウィッシュ
- ロジスティクス(別名シグモイド)
- MaxPool2D
- MirrorPad
REFLECT
モードの 4D 入力のみがサポートされています。パディングは一定にする必要があり、H と W のディメンションでのみ使用できます。
- Mul
- ブロードキャストできるのは、特定のシェイプのみです。Core ML のテンソル レイアウトでは、次のテンソル形状をブロードキャストできます。
[B, C, H, W]
、[B, C, 1, 1]
、[B, 1, H, W]
、[B, 1, 1, 1]
。
- ブロードキャストできるのは、特定のシェイプのみです。Core ML のテンソル レイアウトでは、次のテンソル形状をブロードキャストできます。
- Pad と PadV2
- 4D 入力のみがサポートされています。パディングは一定にする必要があります。また、H と W のディメンションでのみ使用できます。
- レル
- ReluN1 対 1
- 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 にフォールバックします。