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

इस गाइड में, डिवाइस पर LiteRT मॉडल को चलाने की प्रोसेस के बारे में बताया गया है इनपुट डेटा के आधार पर अनुमान लगा सकता है. ऐसा 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++ API ज़्यादा विकल्प और रफ़्तार देते हैं, लेकिन इनके लिए ज़रूरी Java और C++ लेयर के बीच डेटा ले जाने के लिए JNI रैपर लिखते हैं.

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

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

iOS पर LiteRT इन भाषाओं में उपलब्ध है Swift और मकसद-सी iOS लाइब्रेरी. आप C एपीआई Objective-C कोड में सबमिट करते हैं.

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

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

Linux प्लैटफ़ॉर्म पर, LiteRT API का इस्तेमाल करके अनुमान लगाए जा सकते हैं C++.

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

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. अगर आपMappedByteBuffer Interpreter है, तो यह 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 को दिए गए क्रम से मेल खाना चाहिए कन्वर्टर.

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 फेंका जाएगा.

इनपुट

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

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

आउटपुट

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

iOS (स्विफ़्ट)

स्विफ़्ट एपीआई 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 एपीआई 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 एपीआई में किसी दूसरे व्यक्ति को संपर्कों का ऐक्सेस नहीं दिया जा सकता. जिन लोगों को ईमेल खाते का ऐक्सेस दिया गया है उनका इस्तेमाल करने के लिए 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)

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

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

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