Tensorflow Lite Core ML का प्रतिनिधि

TensorFlow Lite Core ML का प्रतिनिधि, Core ML फ़्रेमवर्क पर TensorFlow Lite मॉडल चलाने की सुविधा देता है. इससे iOS डिवाइसों पर मॉडल का अनुमान तेज़ी से मिलता है.

साथ काम करने वाले iOS वर्शन और डिवाइस:

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

इस्तेमाल किए जा सकने वाले मॉडल

फ़िलहाल, 'कोर एमएल डेलिगेट', फ़्लोट (FP32 और FP16) मॉडल के साथ काम करता है.

अपने मॉडल पर, एमएल डेलीगेट को आज़माकर देखना

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

target 'YourProjectName'
  pod 'TensorFlowLiteSwift/CoreML', '~> 2.4.0'  # Or TensorFlowLiteObjC/CoreML

OR

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

सबसे सही तरीके

जिन डिवाइसों में न्यूरल इंजन नहीं है उन पर कोर एमएल डेलिगेट का इस्तेमाल करना

डिफ़ॉल्ट रूप से, कोर एमएल डेलिगेट को सिर्फ़ तब बनाया जाएगा, जब डिवाइस में न्यूरल इंजन मौजूद हो. अगर डेलिगेट नहीं बनाया गया है, तो null दिखेगा. अगर आपको अन्य एनवायरमेंट (जैसे, सिम्युलेटर) पर कोर एमएल डेलीगेट चलाना है, तो 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
      

फ़ॉलबैक के तौर पर मेटल(जीपीयू) डेलिगेट का इस्तेमाल करना.

अगर मुख्य एमएल प्रतिनिधि नहीं बनाया जाता है, तो परफ़ॉर्मेंस के फ़ायदे पाने के लिए भी, मेटल डेलिगेट का इस्तेमाल किया जा सकता है. नीचे दिए गए उदाहरण में यह करने का तरीका बताया गया है:

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

पुराना Core ML वर्शन इस्तेमाल किया जा रहा है

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

काम करने वाले ops

नीचे दिए गए ऑपरेशन, कोर एमएल डेलीगेट के साथ काम करते हैं.

  • जोड़ें
    • सिर्फ़ कुछ खास आकार ब्रॉडकास्ट किए जा सकते हैं. Core ML टेंसर लेआउट में, फ़ॉलोर के ये आकार ब्रॉडकास्ट किए जा सकते हैं. [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1].
  • औसतPool2D
  • Concat
    • चैनल के ऐक्सिस पर स्ट्रिंग जोड़ने की प्रोसेस की जानी चाहिए.
  • कन्वर्ज़न 2D
    • वेट और बायस हमेशा एक जैसा होना चाहिए.
  • डेप्थ के हिसाब से Convert2D
    • वेट और बायस हमेशा एक जैसा होना चाहिए.
  • पूरी तरह से कनेक्ट किया गया (इसे Dense या इनरप्रॉडक्ट भी कहा जाता है)
    • वेट और बायस (मौजूद होने पर) स्थिर होने चाहिए.
    • सिर्फ़ सिंगल-बैच केस के साथ काम करता है. आखिरी डाइमेंशन को छोड़कर, इनपुट डाइमेंशन 1 होने चाहिए.
  • हार्डस्विश
  • लॉजिस्टिक (यानी सिगमॉइड)
  • मैक्सपूल2D
  • MirrorPad
    • सिर्फ़ REFLECT मोड में 4D इनपुट काम करता है. पैडिंग (जगह) स्थायी होनी चाहिए. इसकी अनुमति सिर्फ़ H और W डाइमेंशन के लिए है.
  • मल
    • सिर्फ़ कुछ खास आकार ब्रॉडकास्ट किए जा सकते हैं. Core ML टेंसर लेआउट में, फ़ॉलोर के ये आकार ब्रॉडकास्ट किए जा सकते हैं. [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1].
  • पैड और PadV2
    • सिर्फ़ 4D इनपुट को सपोर्ट किया जाता है. पैडिंग (जगह) स्थिर होनी चाहिए. साथ ही, इसकी अनुमति सिर्फ़ H और W डाइमेंशन के लिए है.
  • रेलु
  • ReluN1टो1
  • Relu6
  • आकार बदलें
    • यह सुविधा सिर्फ़ तब काम करती है, जब टारगेट कोर एमएल 3 वर्शन को टारगेट किया गया हो. यह सुविधा कोर एमएल 3 को टारगेट करने पर काम नहीं करती.
  • ResizeBilinear
  • SoftMax
  • तान
  • TransposeConv
    • वज़न एक जैसे ही होने चाहिए.

सुझाव/राय दें या शिकायत करें

समस्याओं के लिए, कृपया सभी ज़रूरी जानकारी के साथ GitHub की समस्या बनाएं.

अक्सर पूछे जाने वाले सवाल

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

API