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

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

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

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

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

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

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

इस गाइड में LiteRT अनुवादक को ऐक्सेस करने और C++, Java, और Python का इस्तेमाल करके अनुमान लगाना.

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

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

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

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

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

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

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

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

iOS पर, LiteRT, Swift और Objective-C वाली iOS लाइब्रेरी में उपलब्ध है. आप C एपीआई 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 को शुरू करने के लिए 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 आउटपुट टेंसर के इंडेक्स को उनसे जुड़ी जानकारी के साथ मैप करती है आउटपुट डेटा.

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

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

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

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

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

interpreter.close();

Java के साथ किसी प्रोजेक्ट का उदाहरण देखने के लिए, Android ऑब्जेक्ट की पहचान करने का उदाहरण ऐप्लिकेशन है.

ऐसे डेटा टाइप जिन्हें PostgreSQL में प्रोसेस किया जा सकता है

LiteRT का इस्तेमाल करने के लिए, इनपुट और आउटपुट टेंसर का डेटा टाइप ये बुनियादी टाइप शामिल हैं:

  • float
  • int
  • long
  • byte

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

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

इनपुट

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

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

आउटपुट

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

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 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 को कॉल करना होगा एपीआई.

#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 सिर्फ़ तब उपलब्ध होता है, जब बेज़ल का इस्तेमाल किया जा रहा हो.

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)

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

अगर आप सेव किए गए मॉडल से बताए गए सिग्नेचरडेफ़.

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