टेन्सरफ़्लो लाइट कोर एमएल प्रतिनिधि

TensorFlow Lite Core ML प्रतिनिधि , Core ML फ्रेमवर्क पर TensorFlow Lite मॉडल चलाने में सक्षम बनाता है, जिसके परिणामस्वरूप iOS उपकरणों पर तेज़ मॉडल अनुमान होता है।

समर्थित iOS संस्करण और डिवाइस:

  • आईओएस 12 और बाद का संस्करण। पुराने iOS संस्करणों में, Core ML प्रतिनिधि स्वचालित रूप से CPU पर वापस आ जाएगा।
  • डिफ़ॉल्ट रूप से, तेजी से अनुमान लगाने के लिए न्यूरल इंजन का उपयोग करने के लिए कोर एमएल प्रतिनिधि केवल A12 SoC और बाद के संस्करण (iPhone Xs और बाद के संस्करण) वाले उपकरणों पर सक्षम किया जाएगा। यदि आप पुराने उपकरणों पर भी कोर एमएल डेलीगेट का उपयोग करना चाहते हैं, तो कृपया सर्वोत्तम अभ्यास देखें

समर्थित मॉडल

कोर एमएल प्रतिनिधि वर्तमान में फ्लोट (एफपी32 और एफपी16) मॉडल का समर्थन करता है।

कोर एमएल प्रतिनिधि को अपने मॉडल पर आज़माना

कोर एमएल प्रतिनिधि पहले से ही TensorFlow lite CocoaPods की रात्रिकालीन रिलीज़ में शामिल है। कोर एमएल प्रतिनिधि का उपयोग करने के लिए, अपने Podfile में उप-विशिष्ट CoreML शामिल करने के लिए अपने TensorFlow लाइट पॉड को बदलें।

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)
    }
  

उद्देश्य सी


    // 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);
      

सर्वोत्तम प्रथाएं

न्यूरल इंजन के बिना उपकरणों पर कोर एमएल प्रतिनिधि का उपयोग करना

डिफ़ॉल्ट रूप से, कोर एमएल प्रतिनिधि केवल तभी बनाया जाएगा जब डिवाइस में न्यूरल इंजन हो, और यदि प्रतिनिधि नहीं बनाया गया है तो null वापस आ जाएगा। यदि आप कोर एमएल डेलीगेट को अन्य परिवेशों (उदाहरण के लिए, सिम्युलेटर) पर चलाना चाहते हैं, तो स्विफ्ट में डेलीगेट बनाते समय .all एक विकल्प के रूप में पास करें। C++ (और ऑब्जेक्टिव-सी) पर, आप TfLiteCoreMlDelegateAllDevices पास कर सकते हैं। निम्नलिखित उदाहरण दिखाता है कि यह कैसे करना है:

तीव्र

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

उद्देश्य सी

    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
      

फ़ॉलबैक के रूप में मेटल (जीपीयू) प्रतिनिधि का उपयोग करना।

जब कोर एमएल प्रतिनिधि नहीं बनाया गया है, तो वैकल्पिक रूप से आप प्रदर्शन लाभ प्राप्त करने के लिए मेटल प्रतिनिधि का उपयोग कर सकते हैं। निम्नलिखित उदाहरण दिखाता है कि यह कैसे करना है:

तीव्र

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

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

उद्देश्य सी

    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) को पढ़ता है। अधिक विवरण के लिए कोड देखें. वैकल्पिक रूप से, आप डिवाइस किट जैसे अन्य पुस्तकालयों का उपयोग करके अस्वीकृत उपकरणों के अपने स्वयं के सेट को कार्यान्वित कर सकते हैं।

पुराने कोर एमएल संस्करण का उपयोग करना

हालाँकि iOS 13 Core ML 3 को सपोर्ट करता है, लेकिन जब इसे Core ML 2 मॉडल विनिर्देश के साथ परिवर्तित किया जाता है तो मॉडल बेहतर काम कर सकता है। लक्ष्य रूपांतरण संस्करण डिफ़ॉल्ट रूप से नवीनतम संस्करण पर सेट है, लेकिन आप पुराने संस्करण के लिए प्रतिनिधि विकल्प में coreMLVersion (स्विफ्ट में, C API में coreml_version ) सेट करके इसे बदल सकते हैं।

समर्थित ऑप्स

निम्नलिखित ऑप्स कोर एमएल प्रतिनिधि द्वारा समर्थित हैं।

  • जोड़ना
    • केवल कुछ आकृतियाँ ही प्रसारण योग्य हैं। कोर एमएल टेंसर लेआउट में, निम्नलिखित टेंसर आकार प्रसारण योग्य हैं। [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1]
  • औसतपूल2डी
  • concat
    • संयोजन चैनल अक्ष के साथ किया जाना चाहिए।
  • Conv2D
    • वज़न और पूर्वाग्रह स्थिर रहना चाहिए.
  • गहराईवारConv2D
    • वज़न और पूर्वाग्रह स्थिर रहना चाहिए.
  • पूरी तरह से कनेक्टेड (उर्फ सघन या आंतरिक उत्पाद)
    • वज़न और पूर्वाग्रह (यदि मौजूद हो) स्थिर होना चाहिए।
    • केवल सिंगल-बैच केस का समर्थन करता है। अंतिम आयाम को छोड़कर, इनपुट आयाम 1 होना चाहिए।
  • हार्डस्विश
  • लॉजिस्टिक (उर्फ सिग्मॉइड)
  • मैक्सपूल2डी
  • मिररपैड
    • REFLECT मोड के साथ केवल 4D इनपुट समर्थित है। पैडिंग स्थिर होनी चाहिए, और केवल एच और डब्ल्यू आयामों के लिए इसकी अनुमति है।
  • एमयूएल
    • केवल कुछ आकृतियाँ ही प्रसारण योग्य हैं। कोर एमएल टेंसर लेआउट में, निम्नलिखित टेंसर आकार प्रसारण योग्य हैं। [B, C, H, W] , [B, C, 1, 1] , [B, 1, H, W] , [B, 1, 1, 1]
  • पैड और पैडV2
    • केवल 4D इनपुट समर्थित है. पैडिंग स्थिर होनी चाहिए, और केवल एच और डब्ल्यू आयामों के लिए इसकी अनुमति है।
  • रेलु
  • ReluN1To1
  • Relu6
  • आकृति बदलें
    • केवल तभी समर्थित है जब लक्ष्य कोर एमएल संस्करण 2 है, कोर एमएल 3 को लक्षित करते समय समर्थित नहीं है।
  • आकार बिलिनियर
  • सॉफ्टमैक्स
  • तनह
  • ट्रांसपोज़ कन्व
    • वज़न स्थिर रहना चाहिए.

प्रतिक्रिया

मुद्दों के लिए, कृपया पुनरुत्पादन के लिए सभी आवश्यक विवरणों के साथ एक GitHub मुद्दा बनाएं।

सामान्य प्रश्न

  • यदि किसी ग्राफ़ में असमर्थित ऑप्स हैं तो क्या CoreML सीपीयू को फ़ॉलबैक का समर्थन सौंपता है?
    • हाँ
  • क्या CoreML प्रतिनिधि iOS सिम्युलेटर पर काम करता है?
    • हाँ। लाइब्रेरी में x86 और x86_64 लक्ष्य शामिल हैं ताकि यह सिम्युलेटर पर चल सके, लेकिन आपको सीपीयू पर प्रदर्शन में वृद्धि नहीं दिखेगी।
  • क्या TensorFlow Lite और CoreML प्रतिनिधि MacOS का समर्थन करते हैं?
    • TensorFlow Lite का परीक्षण केवल iOS पर किया गया है, MacOS पर नहीं।
  • क्या कस्टम टीएफ लाइट ऑप्स समर्थित है?
    • नहीं, CoreML प्रतिनिधि कस्टम ऑप्स का समर्थन नहीं करता है और वे CPU पर वापस आ जाएंगे।

शहद की मक्खी