Tensorflow लाइट कोर एमएल प्रतिनिधि

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

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

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

समर्थित मॉडल

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

अपने खुद के मॉडल पर कोर एमएल प्रतिनिधि की कोशिश कर रहा है

Core ML प्रतिनिधि पहले से ही TensorFlow lite CocoaPods के रात्रिकालीन रिलीज़ में शामिल है। Core ML प्रतिनिधि का उपयोग करने के लिए, अपने TensorFlow लाइट पॉड को अपने 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']

तीव्र

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

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

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

समर्थित ऑप्स

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

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

प्रतिपुष्टि

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

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

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

शहद की मक्खी