टेन्सरफ्लो लाइट अनुमान

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

यह पृष्ठ वर्णन करता है कि TensorFlow Lite दुभाषिया तक कैसे पहुंचें और C++, Java और Python का उपयोग करके अनुमान कैसे लगाएं, साथ ही प्रत्येक समर्थित प्लेटफ़ॉर्म के लिए अन्य संसाधनों के लिंक भी।

महत्वपूर्ण अवधारणाएँ

TensorFlow Lite अनुमान आम तौर पर निम्नलिखित चरणों का पालन करता है:

  1. एक मॉडल लोड हो रहा है

    आपको .tflite मॉडल को मेमोरी में लोड करना होगा, जिसमें मॉडल का निष्पादन ग्राफ़ शामिल है।

  2. डेटा परिवर्तित करना

    मॉडल के लिए कच्चा इनपुट डेटा आमतौर पर मॉडल द्वारा अपेक्षित इनपुट डेटा प्रारूप से मेल नहीं खाता है। उदाहरण के लिए, मॉडल के अनुकूल होने के लिए आपको किसी छवि का आकार बदलने या छवि प्रारूप को बदलने की आवश्यकता हो सकती है।

  3. चल रहा अनुमान

    इस चरण में मॉडल को निष्पादित करने के लिए TensorFlow Lite API का उपयोग करना शामिल है। इसमें कुछ चरण शामिल हैं जैसे दुभाषिया का निर्माण, और टेंसर आवंटित करना, जैसा कि निम्नलिखित अनुभागों में वर्णित है।

  4. आउटपुट की व्याख्या करना

    जब आप मॉडल अनुमान से परिणाम प्राप्त करते हैं, तो आपको टेंसर की व्याख्या सार्थक तरीके से करनी चाहिए जो आपके एप्लिकेशन में उपयोगी हो।

    उदाहरण के लिए, एक मॉडल केवल संभावनाओं की सूची लौटा सकता है। यह आप पर निर्भर है कि आप संभावनाओं को प्रासंगिक श्रेणियों में मैप करें और इसे अपने अंतिम-उपयोगकर्ता के सामने प्रस्तुत करें।

समर्थित प्लेटफार्म

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

ज्यादातर मामलों में, एपीआई डिज़ाइन उपयोग में आसानी से अधिक प्रदर्शन को प्राथमिकता देता है। TensorFlow Lite को छोटे उपकरणों पर तेजी से अनुमान लगाने के लिए डिज़ाइन किया गया है, इसलिए इसमें कोई आश्चर्य नहीं होना चाहिए कि एपीआई सुविधा की कीमत पर अनावश्यक प्रतियों से बचने की कोशिश करते हैं। इसी तरह, TensorFlow API के साथ स्थिरता एक स्पष्ट लक्ष्य नहीं था और भाषाओं के बीच कुछ भिन्नता की उम्मीद की जानी थी।

सभी पुस्तकालयों में, TensorFlow Lite API आपको मॉडल लोड करने, इनपुट फ़ीड करने और अनुमान आउटपुट पुनर्प्राप्त करने में सक्षम बनाता है।

एंड्रॉइड प्लेटफार्म

एंड्रॉइड पर, TensorFlow Lite का अनुमान जावा या C++ API का उपयोग करके किया जा सकता है। जावा एपीआई सुविधा प्रदान करते हैं और सीधे आपके एंड्रॉइड गतिविधि कक्षाओं में उपयोग किए जा सकते हैं। C++ API अधिक लचीलापन और गति प्रदान करते हैं, लेकिन जावा और C++ परतों के बीच डेटा स्थानांतरित करने के लिए JNI रैपर लिखने की आवश्यकता हो सकती है।

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

TensorFlow Lite एंड्रॉइड रैपर कोड जनरेटर

मेटाडेटा के साथ उन्नत TensorFlow Lite मॉडल के लिए, डेवलपर्स प्लेटफ़ॉर्म विशिष्ट रैपर कोड बनाने के लिए TensorFlow Lite एंड्रॉइड रैपर कोड जनरेटर का उपयोग कर सकते हैं। रैपर कोड एंड्रॉइड पर ByteBuffer के साथ सीधे इंटरैक्ट करने की आवश्यकता को हटा देता है। इसके बजाय, डेवलपर्स Bitmap और Rect जैसे टाइप किए गए ऑब्जेक्ट के साथ TensorFlow Lite मॉडल के साथ इंटरैक्ट कर सकते हैं। अधिक जानकारी के लिए, कृपया TensorFlow Lite एंड्रॉइड रैपर कोड जनरेटर देखें।

आईओएस प्लेटफार्म

iOS पर, TensorFlow Lite स्विफ्ट और ऑब्जेक्टिव-सी में लिखी गई देशी iOS लाइब्रेरी के साथ उपलब्ध है। आप C API का उपयोग सीधे ऑब्जेक्टिव-सी कोड में भी कर सकते हैं।

स्विफ्ट , ऑब्जेक्टिव-सी और सी एपीआई का उपयोग करने के बारे में विवरण के लिए नीचे देखें, या ट्यूटोरियल और उदाहरण कोड के लिए आईओएस क्विकस्टार्ट का पालन करें।

लिनक्स प्लेटफार्म

लिनक्स प्लेटफ़ॉर्म ( रास्पबेरी पाई सहित) पर, आप C++ और Python में उपलब्ध TensorFlow Lite API का उपयोग करके अनुमान चला सकते हैं, जैसा कि निम्नलिखित अनुभागों में दिखाया गया है।

एक मॉडल चलाना

TensorFlow Lite मॉडल को चलाने में कुछ सरल चरण शामिल हैं:

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

निम्नलिखित अनुभाग वर्णन करते हैं कि ये चरण प्रत्येक भाषा में कैसे किए जा सकते हैं।

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

प्लेटफ़ॉर्म: एंड्रॉइड

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

जावा में, आप मॉडल लोड करने और मॉडल अनुमान को चलाने के लिए Interpreter क्लास का उपयोग करेंगे। कई मामलों में, यह एकमात्र एपीआई हो सकती है जिसकी आपको आवश्यकता है।

आप .tflite फ़ाइल का उपयोग करके एक Interpreter प्रारंभ कर सकते हैं:

public Interpreter(@NotNull File modelFile);

या MappedByteBuffer के साथ:

public Interpreter(@NotNull MappedByteBuffer mappedByteBuffer);

दोनों ही मामलों में, आपको एक वैध TensorFlow Lite मॉडल प्रदान करना होगा या API IllegalArgumentException फेंक देगा। यदि आप किसी Interpreter प्रारंभ करने के लिए MappedByteBuffer उपयोग करते हैं, तो इसे Interpreter के पूरे जीवनकाल तक अपरिवर्तित रहना चाहिए।

किसी मॉडल पर अनुमान चलाने का पसंदीदा तरीका हस्ताक्षरों का उपयोग करना है - टेन्सरफ्लो 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 कनवर्टर को दिए गए क्रम से मेल खाना चाहिए।

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

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

यदि opName मॉडल में एक वैध ऑपरेशन नहीं है, तो यह एक IllegalArgumentException फेंकता है।

यह भी सावधान रहें कि Interpreter के पास संसाधन हैं। मेमोरी लीक से बचने के लिए, संसाधनों को उपयोग के बाद जारी किया जाना चाहिए:

interpreter.close();

जावा के साथ एक उदाहरण प्रोजेक्ट के लिए, एंड्रॉइड छवि वर्गीकरण नमूना देखें।

समर्थित डेटा प्रकार (जावा में)

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

  • float
  • int
  • long
  • byte

String प्रकार भी समर्थित हैं, लेकिन वे आदिम प्रकारों की तुलना में अलग तरह से एन्कोड किए गए हैं। विशेष रूप से, एक स्ट्रिंग टेंसर का आकार टेंसर में स्ट्रिंग्स की संख्या और व्यवस्था को निर्धारित करता है, प्रत्येक तत्व स्वयं एक चर लंबाई वाली स्ट्रिंग होती है। इस अर्थ में, टेन्सर के (बाइट) आकार की गणना अकेले आकार और प्रकार से नहीं की जा सकती है, और परिणामस्वरूप स्ट्रिंग्स को एकल, फ्लैट ByteBuffer तर्क के रूप में प्रदान नहीं किया जा सकता है। आप इस पृष्ठ में कुछ उदाहरण देख सकते हैं।

यदि Integer और Float जैसे बॉक्स्ड प्रकारों सहित अन्य डेटा प्रकारों का उपयोग किया जाता है, तो एक IllegalArgumentException फेंक दिया जाएगा।

इनपुट

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

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

आउटपुट

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

स्विफ्ट में एक मॉडल लोड करें और चलाएं

प्लेटफ़ॉर्म: आईओएस

स्विफ्ट एपीआई कोकोपोड्स के 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...
}

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

प्लेटफ़ॉर्म: आईओएस

ऑब्जेक्टिव-सी एपीआई कोकोपोड्स के 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... */ }

ऑब्जेक्टिव-सी कोड में सी एपीआई का उपयोग करना

वर्तमान में ऑब्जेक्टिव-सी एपीआई प्रतिनिधियों का समर्थन नहीं करता है। ऑब्जेक्टिव-सी कोड वाले प्रतिनिधियों का उपयोग करने के लिए, आपको सीधे अंतर्निहित सी एपीआई को कॉल करने की आवश्यकता है।

#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++ में एक मॉडल लोड करें और चलाएं

प्लेटफ़ॉर्म: एंड्रॉइड, आईओएस और लिनक्स

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 देखें।

पायथन में एक मॉडल लोड करें और चलाएं

प्लेटफ़ॉर्म: लिनक्स

एक अनुमान चलाने के लिए पायथन एपीआई tf.lite मॉड्यूल में प्रदान की गई है। जिससे, आपको एक मॉडल लोड करने और एक अनुमान चलाने के लिए ज्यादातर केवल tf.lite.Interpreter आवश्यकता होती है।

निम्नलिखित उदाहरण दिखाता है कि .tflite फ़ाइल को लोड करने और यादृच्छिक इनपुट डेटा के साथ अनुमान चलाने के लिए पायथन दुभाषिया का उपयोग कैसे करें:

यदि आप परिभाषित सिग्नेचरडिफ के साथ सेव्डमॉडल से कनवर्ट कर रहे हैं तो यह उदाहरण अनुशंसित है। 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'])

एक अन्य उदाहरण यदि मॉडल में सिग्नेचरडिफ्स परिभाषित नहीं है।

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 ( tf.lite.TFLiteConverter ) के साथ जोड़ सकते हैं, जिससे आप अपने केरस मॉडल को 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...

अधिक पायथन नमूना कोड के लिए, label_image.py देखें।

गतिशील आकार मॉडल के साथ अनुमान चलाएँ

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

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

सी++ उदाहरण:

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

पायथन उदाहरण:

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