LiteRT का इस्तेमाल शुरू करना

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

LiteRT इन्फ़रेंस आम तौर पर इन चरणों का पालन करता है:

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

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

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

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

इस गाइड में, LiteRT इंटरप्रेटर को ऐक्सेस करने और C++, Java, और Python का इस्तेमाल करके अनुमान लगाने का तरीका बताया गया है.

डेटा डालने और 360 डिग्री में, वीडियो चलाने की सुविधा देने वाले प्लैटफ़ॉर्म

TensorFlow inference API, मोबाइल और एम्बेड किए गए सबसे सामान्य प्लैटफ़ॉर्म के लिए उपलब्ध हैं. जैसे, Android, iOS, और Linux. ये कई प्रोग्रामिंग भाषाओं में उपलब्ध हैं.

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

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

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

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

ज़्यादा जानकारी के लिए, C++ और Java सेक्शन देखें या Android क्विकस्टार्ट को फ़ॉलो करें.

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

iOS पर, LiteRT Swift और Objective-C iOS लाइब्रेरी में उपलब्ध है. C API का इस्तेमाल सीधे Objective-C कोड में भी किया जा सकता है.

Swift, Objective-C, और C API सेक्शन देखें या iOS क्विकस्टार्ट का पालन करें.

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

Linux प्लैटफ़ॉर्म पर, C++ में उपलब्ध LiteRT एपीआई का इस्तेमाल करके अनुमान लगाया जा सकता है.

मॉडल लोड करना और उसे चलाना

LiteRT मॉडल को लोड करने और चलाने के लिए, यह तरीका अपनाएं:

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

Android (Java)

LiteRT के साथ अनुमान लगाने के लिए Java API को मुख्य रूप से Android के साथ इस्तेमाल करने के लिए डिज़ाइन किया गया है. इसलिए, यह Android लाइब्रेरी की डिपेंडेंसी के तौर पर उपलब्ध है: com.google.ai.edge.litert.

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

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

public Interpreter(@NotNull File modelFile);

या MappedByteBuffer के साथ:

public Interpreter(@NotNull MappedByteBuffer mappedByteBuffer);

इन दोनों मामलों में, आपको मान्य LiteRT मॉडल देना होगा. ऐसा न करने पर, एपीआई IllegalArgumentException दिखाता है. अगर Interpreter का इस्तेमाल करके Interpreter को शुरू किया जाता है, तो Interpreter के पूरे लाइफ़टाइम के दौरान इसमें कोई बदलाव नहीं किया जाना चाहिए.MappedByteBuffer

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

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

Interpreter क्लास, ऑपरेशन के नाम का इस्तेमाल करके किसी भी मॉडल इनपुट या आउटपुट का इंडेक्स पाने के लिए, आसान फ़ंक्शन भी उपलब्ध कराती है:

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

अगर opName मॉडल में मान्य ऑपरेशन नहीं है, तो यह IllegalArgumentException दिखाता है.

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

interpreter.close();

Java का इस्तेमाल करके बनाए गए उदाहरण प्रोजेक्ट के लिए, Android ऑब्जेक्ट डिटेक्शन का उदाहरण देने वाला ऐप्लिकेशन देखें.

डेटा टाइप, जो इस्तेमाल किए जा सकते हैं

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

  • float
  • int
  • long
  • byte

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

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

इनपुट

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

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

आउटपुट

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

iOS (Swift)

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

सबसे पहले, आपको 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)

Objective-C API, Cocoapods के LiteRTObjC Pod में उपलब्ध है.

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

@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... */ }

Objective-C कोड में C API

Objective-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++

LiteRT के साथ इंटरफ़ेस चलाने के लिए C++ API, Android, iOS, और Linux प्लैटफ़ॉर्म के साथ काम करता है. iOS पर C++ API सिर्फ़ तब उपलब्ध होता है, जब bazel का इस्तेमाल किया जा रहा हो.

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

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++ के साथ LiteRT का सबसे आसान इस्तेमाल इस तरह किया जाता है:

// 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 needed.
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

इनफ़रेंस चलाने के लिए Python API, मॉडल को लोड करने और इनफ़रेंस चलाने के लिए Interpreter का इस्तेमाल करता है.

LiteRT पैकेज इंस्टॉल करें:

$ python3 -m pip install ai-edge-litert

LiteRT इंटरप्रेटर इंपोर्ट करना

from ai_edge_litert.interpreter import Interpreter
Interpreter = Interpreter(model_path=args.model.file)

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

अगर आपको SignatureDef के साथ SavedModel से कनवर्ट करना है, तो हम इस उदाहरण का इस्तेमाल करने का सुझाव देते हैं.

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 LiteRT model in LiteRT Interpreter
from ai_edge_litert.interpreter import Interpreter
interpreter = 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 LiteRT model and allocate tensors.
from ai_edge_litert.interpreter import Interpreter
interpreter = Interpreter(TFLITE_FILE_PATH)
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 फ़ाइल के तौर पर लोड करने के बजाय, अपने कोड को LiteRT कंपाइलर के साथ जोड़ा जा सकता है. इससे आपको अपने Keras मॉडल को LiteRT फ़ॉर्मैट में बदलने और फिर अनुमान लगाने की सुविधा मिलती है:

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 LiteRT format
converter = tf.lite.TFLiteConverter.from_keras_model(tf.keras.models.Model(inputs=[img], outputs=[out]))
tflite_model = converter.convert()

# Load the LiteRT model and allocate tensors.
from ai_edge_litert.interpreter import Interpreter
interpreter = Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

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

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

डाइनैमिक शेप मॉडल के साथ अनुमान लगाना

अगर आपको डाइनैमिक इनपुट शेप के साथ कोई मॉडल चलाना है, तो अनुमान लगाने से पहले इनपुट शेप का साइज़ बदलें. अगर ऐसा नहीं होता है, तो TensorFlow मॉडल में मौजूद None शेप को LiteRT मॉडल में मौजूद 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 LiteRT model in LiteRT Interpreter
from ai_edge_litert.interpreter import Interpreter
interpreter = 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()