TensorFlow Lite का अनुमान

अनुमान शब्द का मतलब है, डिवाइस पर TensorFlow Lite के मॉडल को लागू करने की प्रोसेस, ताकि इनपुट डेटा के आधार पर अनुमान लगाया जा सके. TensorFlow Lite वाले मॉडल की मदद से अनुमान लगाने के लिए, आपको इसे अनुवादक की मदद से चलाना होगा. TensorFlow Lite के अनुवाद की सुविधा को आसान और तेज़ चलने वाला डिज़ाइन किया गया है. अनुवादक, स्टैटिक ग्राफ़ ऑर्डरिंग और कस्टम (कम-डाइनैमिक) मेमोरी ऐलोलेटर का इस्तेमाल करता है, ताकि लोड, शुरू करने, और प्रोसेस होने में लगने वाला कम से कम समय पक्का किया जा सके.

इस पेज पर C++, Java, और Python का इस्तेमाल करके TensorFlow Lite के अनुवादक को ऐक्सेस करने और अनुमान लगाने का तरीका बताया गया है. साथ ही, हर इस प्लैटफ़ॉर्म पर काम करने वाले अन्य संसाधनों के लिंक भी दिए गए हैं.

अहम जानकारी

TensorFlow Lite के अनुमान के लिए, आम तौर पर यह तरीका अपनाया जाता है:

  1. कोई मॉडल लोड करना

    आपको .tflite मॉडल को मेमोरी में लोड करना होगा. इसमें मॉडल का एक्ज़ीक्यूशन ग्राफ़ भी शामिल होता है.

  2. डेटा में बदलाव करना

    मॉडल का रॉ इनपुट डेटा, आम तौर पर उस इनपुट डेटा फ़ॉर्मैट से मैच नहीं करता जो मॉडल की उम्मीद के मुताबिक होता है. उदाहरण के लिए, आपको किसी इमेज का साइज़ बदलना पड़ सकता है या मॉडल के साथ काम करने के लिए उसका फ़ॉर्मैट बदलना पड़ सकता है.

  3. अनुमान लगाया जा रहा है

    इस चरण में, मॉडल को एक्ज़ीक्यूट करने के लिए, TensorFlow Lite API का इस्तेमाल करना शामिल है. इसमें कुछ चरण शामिल हैं, जैसे कि इंटरप्रेटर बनाना और टेंसर असाइन करना, जैसा कि नीचे दिए गए सेक्शन में बताया गया है.

  4. आउटपुट को समझना

    जब आपको मॉडल अनुमान से नतीजे मिलते हैं, तो आपको सेंसर की इस तरह से व्याख्या करनी चाहिए जो आपके ऐप्लिकेशन के लिए काम का हो.

    उदाहरण के लिए, कोई मॉडल सिर्फ़ प्रॉबबिलिटी की सूची दिखा सकता है. यह आप पर निर्भर करता है कि आप संभावनाओं को प्रासंगिक कैटगरी में मैप करें और उसे अपने असली उपयोगकर्ता के सामने पेश करें.

इन प्लैटफ़ॉर्म पर चलाया जा सकता है

TensorFlow के अनुमान वाले एपीआई, मोबाइल/एम्बेड किए गए ज़्यादातर प्लैटफ़ॉर्म जैसे कि Android, iOS, और Linux के लिए कई प्रोग्रामिंग भाषाओं में उपलब्ध कराए जाते हैं.

ज़्यादातर मामलों में, एपीआई डिज़ाइन इस्तेमाल में आसानी से ज़्यादा, परफ़ॉर्मेंस को प्राथमिकता देता है. TensorFlow Lite को छोटे डिवाइसों पर तेज़ी से अनुमान लगाने के लिए डिज़ाइन किया गया है. इसलिए, इसमें कोई हैरानी की बात नहीं है कि एपीआई ज़रूरत के हिसाब से, ग़ैर-ज़रूरी कॉपी से बचने की कोशिश करते हैं. इसी तरह, TensorFlow API के साथ काम करने का लक्ष्य साफ़ तौर पर नहीं था और भाषा के बीच कुछ फ़र्क़ हो सकता है.

सभी लाइब्रेरी में, TensorFlow Lite API की मदद से मॉडल और फ़ीड इनपुट लोड किए जा सकते हैं. साथ ही, अनुमान के आउटपुट पाए जा सकते हैं.

Android प्लेटफ़ॉर्म

Android पर, TensorFlow Lite के अनुमान का इस्तेमाल Java या C++ एपीआई का इस्तेमाल करके किया जा सकता है. Java API सुविधा उपलब्ध कराते हैं और इनका इस्तेमाल सीधे आपकी Android गतिविधि क्लास में किया जा सकता है. C++ एपीआई ज़्यादा सुविधाजनक और तेज़ स्पीड देता है. हालांकि, हो सकता है कि Java और C++ लेयर के बीच डेटा ट्रांसफ़र करने के लिए, JNI रैपर लिखने की ज़रूरत हो.

C++ और Java का इस्तेमाल करने के बारे में जानकारी नीचे देखें. इसके अलावा, ट्यूटोरियल और कोड के उदाहरण के लिए, Android क्विकस्टार्ट का इस्तेमाल करें.

TensorFlow Lite के लिए Android रैपर कोड जनरेटर

मेटाडेटा की मदद से बेहतर बनाए गए TensorFlow Lite मॉडल के लिए, डेवलपर TensorFlow Lite के Android रैपर कोड जनरेटर का इस्तेमाल कर सकते हैं, ताकि प्लैटफ़ॉर्म के हिसाब से रैपर कोड बनाया जा सके. रैपर कोड, Android पर ByteBuffer से सीधे इंटरैक्ट करने की ज़रूरत को हटा देता है. इसके बजाय, डेवलपर Bitmap और Rect जैसे टाइप किए गए ऑब्जेक्ट की मदद से, TensorFlow Lite के मॉडल से इंटरैक्ट कर सकते हैं. ज़्यादा जानकारी के लिए, TensorFlow Lite का Android रैपर कोड जनरेटर देखें.

iOS प्लैटफ़ॉर्म

iOS पर TensorFlow Lite, नेटिव iOS लाइब्रेरी के साथ उपलब्ध है, जिन्हें Swift और Objective-C में लिखा गया है. C API का इस्तेमाल, सीधे ऑब्जेक्ट सी कोड में भी किया जा सकता है.

Swift, Objective-C, और C API इस्तेमाल करने के बारे में जानकारी यहां देखें. इसके अलावा, ट्यूटोरियल और कोड के उदाहरण के लिए, iOS क्विकस्टार्ट में दी गई जानकारी देखें.

Linux प्लैटफ़ॉर्म

Linux प्लैटफ़ॉर्म (इसमें Raspबेरी Pi भी शामिल हैं) पर C++ और Python में उपलब्ध TensorFlow Lite API का इस्तेमाल करके अनुमान चलाए जा सकते हैं, जैसा कि इन सेक्शन में दिखाया गया है.

मॉडल का इस्तेमाल करना

TensorFlow Lite मॉडल चलाने के लिए, कुछ आसान चरण पूरे करने होंगे:

  1. मॉडल को मेमोरी में लोड करें.
  2. मौजूदा मॉडल के आधार पर Interpreter बनाएं.
  3. इनपुट टेंसर वैल्यू सेट करें. (अगर पहले से तय किए गए साइज़ नहीं चाहिए, तो इनपुट टेंसर का साइज़ बदलें. हालांकि, ऐसा करना ज़रूरी नहीं है.)
  4. अनुमान शुरू करें.
  5. आउटपुट टेंसर वैल्यू पढ़ें.

इन सेक्शन में बताया गया है कि इन चरणों को हर भाषा में कैसे किया जा सकता है.

Java में मॉडल लोड करना और चलाना

प्लैटफ़ॉर्म: Android

TensorFlow Lite की मदद से अनुमान चलाने के लिए, Java API मुख्य रूप से Android पर इस्तेमाल करने के लिए डिज़ाइन किया गया है. इसलिए, यह Android लाइब्रेरी डिपेंडेंसी के तौर पर उपलब्ध है: org.tensorflow:tensorflow-lite.

Java में, आपको कोई मॉडल लोड करने और ड्राइव के मॉडल का अनुमान लगाने के लिए, Interpreter क्लास का इस्तेमाल करना होगा. कई मामलों में, आपको सिर्फ़ इस एपीआई की ज़रूरत पड़ सकती है.

.tflite फ़ाइल का इस्तेमाल करके, Interpreter शुरू किया जा सकता है:

public Interpreter(@NotNull File modelFile);

या MappedByteBuffer का इस्तेमाल करके:

public Interpreter(@NotNull MappedByteBuffer mappedByteBuffer);

दोनों ही मामलों में, आपको एक मान्य TensorFlow Lite मॉडल उपलब्ध कराना होगा या एपीआई थ्रो IllegalArgumentException करना होगा. अगर Interpreter को शुरू करने के लिए MappedByteBuffer का इस्तेमाल किया जाता है, तो Interpreter के पूरे सीज़न के लिए इसमें कोई बदलाव नहीं होना चाहिए.

किसी मॉडल पर अनुमान चलाने का पसंदीदा तरीका, सिग्नेचर का इस्तेमाल करना है - यह सुविधा, Tensorflow 2.5 से बदले गए मॉडल के लिए उपलब्ध है

try (Interpreter interpreter = new Interpreter(file_of_tensorflowlite_model)) {
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("input_1", input1);
  inputs.put("input_2", input2);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("output_1", output1);
  interpreter.runSignature(inputs, outputs, "mySignature");
}

runSignature तरीके में तीन आर्ग्युमेंट होते हैं:

  • इनपुट : सिग्नेचर में इनपुट नाम से किसी इनपुट ऑब्जेक्ट के इनपुट के लिए मैप.

  • आउटपुट : हस्ताक्षर में आउटपुट नाम से आउटपुट डेटा तक, आउटपुट मैपिंग के लिए मैप करता है.

  • हस्ताक्षर का नाम (ज़रूरी नहीं): हस्ताक्षर का नाम (अगर मॉडल में एक हस्ताक्षर है, तो इसे खाली छोड़ा जा सकता है).

अगर मॉडल के लिए कोई तय हस्ताक्षर मौजूद नहीं है, तो अनुमान चलाने का एक और तरीका. बस Interpreter.run() पर कॉल करें. उदाहरण के लिए:

try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
  interpreter.run(input, output);
}

run() वाला तरीका सिर्फ़ एक इनपुट लेता है और सिर्फ़ एक आउटपुट दिखाता है. इसलिए, अगर आपके मॉडल में कई इनपुट या एक से ज़्यादा आउटपुट हैं, तो इसके बजाय इनका इस्तेमाल करें:

interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);

इस मामले में, inputs में मौजूद हर एंट्री, इनपुट टेन्सर से मेल खाती है और map_of_indices_to_outputs, आउटपुट टेंसर के इंडेक्स को उससे जुड़े आउटपुट डेटा से मैप करती है.

दोनों ही मामलों में, टेंसर इंडेक्स उन वैल्यू के मुताबिक होना चाहिए जो आपने मॉडल बनाते समय TensorFlow Lite Converter को दिए थे. ध्यान रखें कि input में मौजूद टेंसर का क्रम, TensorFlow Lite Converter पर दिए गए क्रम से मेल खाना चाहिए.

Interpreter क्लास में आपको आसान फ़ंक्शन भी मिलते हैं. इनकी मदद से, किसी मॉडल के इनपुट या आउटपुट के इंडेक्स को हासिल किया जा सकता है. इसके लिए, कार्रवाई के नाम का इस्तेमाल किया जाता है:

public int getInputIndex(String opName);
public int getOutputIndex(String opName);

अगर opName, मॉडल में मान्य कार्रवाई नहीं है, तो यह IllegalArgumentException की जानकारी देता है.

साथ ही, ध्यान रखें कि Interpreter के पास संसाधनों का मालिकाना हक होता है. मेमोरी लीक से बचने के लिए, संसाधनों को इन चीज़ों का इस्तेमाल करके रिलीज़ किया जाना चाहिए:

interpreter.close();

Java वाले प्रोजेक्ट के उदाहरण के लिए, Android पर इमेज क्लासिफ़िकेशन का सैंपल देखें.

इस्तेमाल किए जा सकने वाले डेटा टाइप (Java में)

TensorFlow Lite का इस्तेमाल करने के लिए, इनपुट और आउटपुट टेंसर का डेटा टाइप, इन प्रिमिटिव टाइप में से एक होना चाहिए:

  • float
  • int
  • long
  • byte

String टाइप भी काम करते हैं, लेकिन उन्हें प्रिमिटिव टाइप से अलग कोड में बदला जाता है. खास तौर पर, टेन्सर स्ट्रिंग का आकार, Tensor में मौजूद स्ट्रिंग की संख्या और अरेंजमेंट के बारे में बताता है. इसमें हर एलिमेंट, वैरिएबल की लंबाई वाली स्ट्रिंग होती है. इस वजह से, Tensor के (बाइट) साइज़ का हिसाब सिर्फ़ आकार और टाइप से नहीं लगाया जा सकता. ऐसा होने पर, स्ट्रिंग को एक सपाट ByteBuffer आर्ग्युमेंट के तौर पर नहीं दिया जा सकता.

अगर Integer और Float जैसे बॉक्स में शामिल किए गए डेटा टाइप के साथ-साथ अन्य तरह का डेटा इस्तेमाल किया जाता है, तो IllegalArgumentException लगा दिया जाएगा.

इनपुट

हर इनपुट, इस्तेमाल किए जा सकने वाले प्रिमिटिव टाइप की कैटगरी या बहु-डाइमेंशन वाला अरे या सही साइज़ का रॉ ByteBuffer होना चाहिए. अगर इनपुट कोई अरे या कई डाइमेंशन वाला अरे है, तो उससे जुड़े इनपुट टेंसर का साइज़ अनुमान के समय पर अरे के डाइमेंशन के हिसाब से बदल दिया जाएगा. अगर इनपुट ByteBuffer है, तो कॉल करने वाले (कॉलर) को अनुमान चलाने से पहले (Interpreter.resizeInput() के ज़रिए) से जुड़े इनपुट के इनपुट का साइज़ मैन्युअल तरीके से बदलना चाहिए.

ByteBuffer का इस्तेमाल करते समय, डायरेक्ट बाइट बफ़र का इस्तेमाल करें, क्योंकि इससे Interpreter को ग़ैर-ज़रूरी कॉपी से बचने में मदद मिलती है. अगर ByteBuffer एक डायरेक्ट बाइट बफ़र है, तो इसका क्रम ByteOrder.nativeOrder() होना चाहिए. मॉडल अनुमान के लिए इस्तेमाल किए जाने के बाद, मॉडल अनुमान पूरा होने तक इसमें कोई बदलाव नहीं होना चाहिए.

आउटपुट

हर आउटपुट, इस्तेमाल किए जा सकने वाले प्रिमिटिव टाइप की कैटगरी या बहु-डाइमेंशन वाला अरे या सही साइज़ का बाइटबफ़र होना चाहिए. ध्यान दें कि कुछ मॉडल में डाइनैमिक आउटपुट होते हैं. इनमें इनपुट के हिसाब से, आउटपुट टेंसर का आकार अलग-अलग हो सकता है. मौजूदा JavaScript एपीआई के साथ इसे ठीक करने का कोई आसान तरीका नहीं है. हालांकि, प्लान किए गए एक्सटेंशन की मदद से ऐसा किया जा सकता है.

Swift में मॉडल लोड करें और चलाएं

प्लैटफ़ॉर्म: iOS

Swift API Cocoapods के TensorFlowLiteSwift पॉड में उपलब्ध है.

सबसे पहले, आपको TensorFlowLite मॉड्यूल इंपोर्ट करना होगा.

import TensorFlowLite
// Getting model path
guard
  let modelPath = Bundle.main.path(forResource: "model", ofType: "tflite")
else {
  // Error handling...
}

do {
  // Initialize an interpreter with the model.
  let interpreter = try Interpreter(modelPath: modelPath)

  // Allocate memory for the model's input `Tensor`s.
  try interpreter.allocateTensors()

  let inputData: Data  // Should be initialized

  // input data preparation...

  // Copy the input data to the input `Tensor`.
  try self.interpreter.copy(inputData, toInputAt: 0)

  // Run inference by invoking the `Interpreter`.
  try self.interpreter.invoke()

  // Get the output `Tensor`
  let outputTensor = try self.interpreter.output(at: 0)

  // Copy output to `Data` to process the inference results.
  let outputSize = outputTensor.shape.dimensions.reduce(1, {x, y in x * y})
  let outputData =
        UnsafeMutableBufferPointer<Float32>.allocate(capacity: outputSize)
  outputTensor.data.copyBytes(to: outputData)

  if (error != nil) { /* Error handling... */ }
} catch error {
  // Error handling...
}

ऑब्जेक्टिव-सी में मॉडल लोड करना और चलाना

प्लैटफ़ॉर्म: iOS

Objective-C एपीआई Cocoapods के TensorFlowLiteObjC पॉड में उपलब्ध है.

सबसे पहले, आपको TensorFlowLite मॉड्यूल इंपोर्ट करना होगा.

@import TensorFlowLite;
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"model"
                                                      ofType:@"tflite"];
NSError *error;

// Initialize an interpreter with the model.
TFLInterpreter *interpreter = [[TFLInterpreter alloc] initWithModelPath:modelPath
                                                                  error:&error];
if (error != nil) { /* Error handling... */ }

// Allocate memory for the model's input `TFLTensor`s.
[interpreter allocateTensorsWithError:&error];
if (error != nil) { /* Error handling... */ }

NSMutableData *inputData;  // Should be initialized
// input data preparation...

// Get the input `TFLTensor`
TFLTensor *inputTensor = [interpreter inputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }

// Copy the input data to the input `TFLTensor`.
[inputTensor copyData:inputData error:&error];
if (error != nil) { /* Error handling... */ }

// Run inference by invoking the `TFLInterpreter`.
[interpreter invokeWithError:&error];
if (error != nil) { /* Error handling... */ }

// Get the output `TFLTensor`
TFLTensor *outputTensor = [interpreter outputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }

// Copy output to `NSData` to process the inference results.
NSData *outputData = [outputTensor dataWithError:&error];
if (error != nil) { /* Error handling... */ }

ऑब्जेक्टिव-सी कोड में C API का इस्तेमाल करना

फ़िलहाल, Objective-C एपीआई डेलिगेट की सुविधा नहीं देता है. ऑब्जेक्टिव-सी कोड के साथ डेलिगेट का इस्तेमाल करने के लिए, आपको सीधे तौर पर मौजूद C API को कॉल करना होगा.

#include "tensorflow/lite/c/c_api.h"
TfLiteModel* model = TfLiteModelCreateFromFile([modelPath UTF8String]);
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();

// Create the interpreter.
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);

// Allocate tensors and populate the input tensor data.
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor =
    TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteTensorCopyFromBuffer(input_tensor, input.data(),
                           input.size() * sizeof(float));

// Execute inference.
TfLiteInterpreterInvoke(interpreter);

// Extract the output tensor data.
const TfLiteTensor* output_tensor =
    TfLiteInterpreterGetOutputTensor(interpreter, 0);
TfLiteTensorCopyToBuffer(output_tensor, output.data(),
                         output.size() * sizeof(float));

// Dispose of the model and interpreter objects.
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);

C++ में मॉडल लोड करना और चलाना

प्लैटफ़ॉर्म: Android, iOS, और Linux

C++ में, मॉडल को FlatBufferModel क्लास में सेव किया जाता है. इसमें TensorFlow Lite मॉडल शामिल होता है और इसे कई तरीकों से बनाया जा सकता है. यह इस बात पर निर्भर करता है कि मॉडल किस जगह सेव किया गया है:

class FlatBufferModel {
  // Build a model based on a file. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromFile(
      const char* filename,
      ErrorReporter* error_reporter);

  // Build a model based on a pre-loaded flatbuffer. The caller retains
  // ownership of the buffer and should keep it alive until the returned object
  // is destroyed. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
      const char* buffer,
      size_t buffer_size,
      ErrorReporter* error_reporter);
};

अब आपके पास मॉडल को FlatBufferModel ऑब्जेक्ट के तौर पर है, तो इसे Interpreter की मदद से एक्ज़ीक्यूट किया जा सकता है. एक FlatBufferModel को एक साथ एक से ज़्यादा Interpreter इस्तेमाल कर सकते हैं.

Interpreter एपीआई के अहम हिस्से नीचे कोड स्निपेट में दिखाए गए हैं. यह ध्यान रखना चाहिए कि:

  • टेन्सर, पूर्णांक से दिखाए जाते हैं, ताकि स्ट्रिंग की तुलनाओं (और स्ट्रिंग लाइब्रेरी पर किसी तय निर्भरता) से बचा जा सके.
  • एक साथ काम करने वाले थ्रेड से इंटरप्रेटर को ऐक्सेस नहीं किया जा सकता.
  • टेन्सर का साइज़ बदलने के तुरंत बाद, AllocateTensors() को कॉल करके, इनपुट और आउटपुट टेंसर के लिए मेमोरी के बंटवारे को ट्रिगर किया जाना चाहिए.

C++ के साथ TensorFlow Lite का सबसे आसान इस्तेमाल ऐसा दिखता है:

// Load the model
std::unique_ptr<tflite::FlatBufferModel> model =
    tflite::FlatBufferModel::BuildFromFile(filename);

// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);

// Resize input tensors, if desired.
interpreter->AllocateTensors();

float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.

interpreter->Invoke();

float* output = interpreter->typed_output_tensor<float>(0);

कोड के ज़्यादा उदाहरण के लिए, minimal.cc और label_image.cc देखें.

Python में मॉडल लोड करना और चलाना

प्लैटफ़ॉर्म: Linux

किसी अनुमान को चलाने के लिए, Python API को मॉडल लोड करने और अनुमान चलाने के लिए, सिर्फ़ tf.lite.Interpreter की ज़रूरत होती है.

नीचे दिए गए उदाहरण में, .tflite फ़ाइल लोड करने और रैंडम इनपुट डेटा के साथ अनुमान चलाने के लिए Python इंटरप्रेटर का इस्तेमाल करने का तरीका बताया गया है:

इस उदाहरण का सुझाव तब दिया जाता है, जब बदलाव किए जा रहे हों. TensorFlow के 2.5 वर्शन से शुरू

class TestModel(tf.Module):
  def __init__(self):
    super(TestModel, self).__init__()

  @tf.function(input_signature=[tf.TensorSpec(shape=[1, 10], dtype=tf.float32)])
  def add(self, x):
    '''
    Simple method that accepts single input 'x' and returns 'x' + 4.
    '''
    # Name the output 'result' for convenience.
    return {'result' : x + 4}


SAVED_MODEL_PATH = 'content/saved_models/test_variable'
TFLITE_FILE_PATH = 'content/test_variable.tflite'

# Save the model
module = TestModel()
# You can omit the signatures argument and a default signature name will be
# created with name 'serving_default'.
tf.saved_model.save(
    module, SAVED_MODEL_PATH,
    signatures={'my_signature':module.add.get_concrete_function()})

# Convert the model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
tflite_model = converter.convert()
with open(TFLITE_FILE_PATH, 'wb') as f:
  f.write(tflite_model)

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(TFLITE_FILE_PATH)
# There is only 1 signature defined in the model,
# so it will return it by default.
# If there are multiple signatures then we can pass the name.
my_signature = interpreter.get_signature_runner()

# my_signature is callable with input as arguments.
output = my_signature(x=tf.constant([1.0], shape=(1,10), dtype=tf.float32))
# 'output' is dictionary with all outputs from the inference.
# In this case we have single output 'result'.
print(output['result'])

एक और उदाहरण, अगर मॉडल में SignatureDefs तय न किया गया हो.

import numpy as np
import tensorflow as tf

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

मॉडल को पहले से बदली गई .tflite फ़ाइल के तौर पर लोड करने के विकल्प के तौर पर, अपने कोड को TensorFlow Lite Converter Python API के साथ जोड़ा जा सकता है. इससे अपने Keras मॉडल को TensorFlow Lite फ़ॉर्मैट में बदला जा सकता है और फिर अनुमान चलाया जा सकता है:

import numpy as np
import tensorflow as tf

img = tf.keras.Input(shape=(64, 64, 3), name="img")
const = tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
val = img + const
out = tf.identity(val, name="out")

# Convert to TF Lite format
converter = tf.lite.TFLiteConverter.from_keras_model(tf.keras.models.Model(inputs=[img], outputs=[out]))
tflite_model = converter.convert()

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

# Continue to get tensors and so forth, as shown above...

Python के सैंपल कोड के बारे में ज़्यादा जानने के लिए, label_image.py देखें.

डाइनैमिक आकार के मॉडल की मदद से अनुमान चलाएं

अगर आपको डाइनैमिक इनपुट शेप वाले मॉडल को चलाना है, तो अनुमान चलाने से पहले, इनपुट के आकार को बदलें. ऐसा न होने पर, Tensorflow मॉडल में None शेप को TFLite मॉडल में 1 के प्लेसहोल्डर से बदल दिया जाएगा.

नीचे दिए गए उदाहरण अलग-अलग भाषाओं में अनुमान चलाने से पहले इनपुट आकार का साइज़ बदलने का तरीका बताते हैं. सभी उदाहरणों में यह माना जाता है कि इनपुट के आकार को [1/None, 10] के तौर पर बताया गया है और इसे बदलकर [3, 10] करना ज़रूरी है.

C++ का उदाहरण:

// Resize input tensors before allocate tensors
interpreter->ResizeInputTensor(/*tensor_index=*/0, std::vector<int>{3,10});
interpreter->AllocateTensors();

Python का उदाहरण:

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(model_path=TFLITE_FILE_PATH)

# Resize input shape for dynamic shape model and allocate tensor
interpreter.resize_tensor_input(interpreter.get_input_details()[0]['index'], [3, 10])
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

इस्तेमाल की जा सकने वाली कार्रवाइयां

TensorFlow Lite, TensorFlow के कुछ सबसेट के साथ काम करता है, लेकिन कुछ सीमाओं के साथ है. कार्रवाइयों और सीमाओं की पूरी सूची देखने के लिए, TF Lite Ops पेज देखें.