TensorFlow लाइट अनुमान

संग्रह की मदद से व्यवस्थित रहें अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.

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

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

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

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

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

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

  2. डेटा बदलना

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

एक मॉडल चलाना

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 लाइट कन्वर्टर को दिए गए क्रम से मेल खाना चाहिए।

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

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

यदि मॉडल में opName मान्य संचालन नहीं है, तो यह एक IllegalArgumentException को फेंकता है।

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

interpreter.close();

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

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

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

  • float
  • int
  • long
  • byte

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

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

इनपुट

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

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

आउटपुट

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

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

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

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

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

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

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

उद्देश्य-सी कोड में सी एपीआई का उपयोग करना

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

#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 लाइट मॉडल को इनकैप्सुलेट करता है और आप इसे दो अलग-अलग तरीकों से बना सकते हैं, यह इस बात पर निर्भर करता है कि मॉडल कहाँ संग्रहीत है:

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 मॉडल को TensorFlow लाइट प्रारूप में परिवर्तित कर सकते हैं और फिर अनुमान चलाएँ:

import numpy as np
import tensorflow as tf

img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
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
with tf.Session() as sess:
  converter = tf.lite.TFLiteConverter.from_session(sess, [img], [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 Lite कुछ सीमाओं के साथ TensorFlow संचालन के सबसेट का समर्थन करता है। संचालन और सीमाओं की पूरी सूची के लिए TF लाइट ऑप्स पृष्ठ देखें।