TensorFlow Lite टास्क लाइब्रेरी

TensorFlow Lite की टास्क लाइब्रेरी में, असरदार और इस्तेमाल में आसान कॉन्टेंट का एक सेट है ऐप्लिकेशन डेवलपर के लिए टास्क के हिसाब से बनी लाइब्रेरी, ताकि TFLite की मदद से मशीन लर्निंग अनुभव तैयार किया जा सके. यह लोकप्रिय मशीन लर्निंग के लिए ऑप्टिमाइज़ किए गए आउट-ऑफ़-बॉक्स मॉडल इंटरफ़ेस मुहैया कराता है टास्क, जैसे कि इमेज की कैटगरी तय करना, सवाल और जवाब वगैरह. मॉडल इंटरफ़ेस खास तौर पर हर टास्क को ध्यान में रखकर बनाया गया है, ताकि परफ़ॉर्मेंस और उपयोगिता. टास्क लाइब्रेरी, अलग-अलग प्लैटफ़ॉर्म पर काम करती है और यह इन डिवाइसों पर काम करती है Java, C++, और Swift.

टास्क लाइब्रेरी से क्या मिलेगा

  • मशीन लर्निंग का इस्तेमाल न करने वाले लोगों के लिए सटीक और अच्छी तरह से तय एपीआई
    कोड की सिर्फ़ पांच लाइनों में अनुमान लगाया जा सकता है. शक्तिशाली और इस्तेमाल करने में आसान एपीआई, टास्क लाइब्रेरी में बिल्डिंग ब्लॉक के तौर पर मौजूद होते हैं. इससे आपको एपीआई की मदद से, मोबाइल डिवाइसों पर TFLite की मदद से ML को डेवलप करना.

  • मुश्किल, लेकिन सामान्य डेटा प्रोसेसिंग
    कन्वर्ज़न करने के लिए, सामान्य विज़न और नैचुरल लैंग्वेज प्रोसेसिंग लॉजिक को सपोर्ट करता है अपने डेटा और मॉडल के लिए ज़रूरी डेटा फ़ॉर्मैट के बीच अंतर कर सकते हैं. यह जानकारी मिलती है जो ट्रेनिंग और अनुमान के लिए एक जैसा है और जिसे प्रोसेस किया जा सकता है.

  • बेहतर परफ़ॉर्मेंस
    डेटा प्रोसेसिंग में कुछ मिलीसेकंड से ज़्यादा नहीं लगता है. इससे यह पक्का किया जाता है कि TensorFlow Lite का इस्तेमाल करके, तेज़ी से अनुमान लगाने का अनुभव पाएं.

  • अलग-अलग तरह की सुविधा और पसंद के मुताबिक बनाने की सुविधा
    टास्क लाइब्रेरी के इन्फ़्रास्ट्रक्चर से मिलने वाले सभी फ़ायदों का इस्तेमाल किया जा सकता है और आसानी से अपना Android/iOS अनुमान API बना सकते हैं.

किए जा सकने वाले टास्क

यहां, इस्तेमाल किए जा सकने वाले अलग-अलग तरह के टास्क की सूची दी गई है. इस सूची के बढ़ने की उम्मीद है हम सीखने के ज़्यादा से ज़्यादा उदाहरण चालू करते रहेंगे.

डेलिगेट के साथ टास्क लाइब्रेरी चलाएं

डेलिगेट हार्डवेयर से तेज़ी लाने की सुविधा चालू करते हैं TensorFlow Lite के मॉडल में, डिवाइस पर मौजूद एक्सीलेरेटर का इस्तेमाल किया जाता है. जैसे, जीपीयू और कोरल एज टीपीयू. इस्तेमाल किया जा रहा है न्यूरल नेटवर्क ऑपरेशन के लिए डिज़ाइन किए गए टूल, लेटेंसी के लिहाज़ से काफ़ी फ़ायदेमंद होते हैं और ऊर्जा की बचत करते हैं. उदाहरण के लिए, जीपीयू 5x तक का रिज़ॉल्यूशन दे सकते हैं स्पीड बढ़ाना और कोरल एज TPU का अनुमान 10 गुना तक मिलता है. साथ ही, मोबाइल डिवाइसों के लिए, इंतज़ार के समय में अंतर दिखता है ये डेस्कटॉप सीपीयू से ज़्यादा तेज़ हैं.

टास्क लाइब्रेरी में, ऐप्लिकेशन को आसानी से कॉन्फ़िगर किया जा सकता है. साथ ही, इसे सेट अप करने के विकल्प भी मिलते हैं और डेलिगेट के वोटों का इस्तेमाल करेंगे. Task API में अब ये एक्सेलरेटर काम करते हैं:

Tasks Swift / Web API में, तेज़ी से काम करने की सुविधा जल्द ही उपलब्ध होगी.

Java में Android पर जीपीयू के इस्तेमाल का उदाहरण

पहला चरण. अपने मॉड्यूल के build.gradle में जीपीयू डेलिगेट प्लगिन लाइब्रेरी जोड़ें फ़ाइल:

dependencies {
    // Import Task Library dependency for vision, text, or audio.

    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

दूसरा चरण. टास्क के विकल्पों में जीपीयू डेलिगेट को कॉन्फ़िगर करें BaseOptions. उदाहरण के लिए, ObjectDetector में जीपीयू को इस तरह सेट अप किया जा सकता है:

// Turn on GPU delegation.
BaseOptions baseOptions = BaseOptions.builder().useGpu().build();
// Configure other options in ObjectDetector
ObjectDetectorOptions options =
    ObjectDetectorOptions.builder()
        .setBaseOptions(baseOptions)
        .setMaxResults(1)
        .build();

// Create ObjectDetector from options.
ObjectDetector objectDetector =
    ObjectDetector.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<Detection> results = objectDetector.detect(image);

Android पर C++ में जीपीयू के इस्तेमाल का उदाहरण

पहला चरण. अपने बेज़ल बिल्ड टारगेट में जीपीयू डेलिगेट प्लग इन पर निर्भर करें, जैसे:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:gpu_plugin", # for GPU
]

दूसरा चरण. टास्क के विकल्पों में जीपीयू डेलिगेट को कॉन्फ़िगर करें. उदाहरण के लिए, विज्ञापन देने वालों के लिए BertQuestionAnswerer में जीपीयू इस तरह से है:

// Initialization
BertQuestionAnswererOptions options;
// Load the TFLite model.
auto base_options = options.mutable_base_options();
base_options->mutable_model_file()->set_file_name(model_file);
// Turn on GPU delegation.
auto tflite_settings = base_options->mutable_compute_settings()->mutable_tflite_settings();
tflite_settings->set_delegate(Delegate::GPU);
// (optional) Turn on automatical fallback to TFLite CPU path on delegation errors.
tflite_settings->mutable_fallback_settings()->set_allow_automatic_fallback_on_execution_error(true);

// Create QuestionAnswerer from options.
std::unique_ptr<QuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();

// Run inference on GPU.
std::vector<QaAnswer> results = answerer->Answer(context_of_question, question_to_ask);

ज़्यादा बेहतर ऐक्सेलरेटर सेटिंग के बारे में जानें यहां पढ़ें.

Python में कोरल एज TPU को इस्तेमाल करने का उदाहरण

टास्क के बुनियादी विकल्पों में कोरल एज TPU को कॉन्फ़िगर करें. उदाहरण के लिए, आपके पास ImageClassifier में कोरल एज TPU को इस तरह सेट अप करें:

# Imports
from tflite_support.task import vision
from tflite_support.task import core

# Initialize options and turn on Coral Edge TPU delegation.
base_options = core.BaseOptions(file_name=model_path, use_coral=True)
options = vision.ImageClassifierOptions(base_options=base_options)

# Create ImageClassifier from options.
classifier = vision.ImageClassifier.create_from_options(options)

# Run inference on Coral Edge TPU.
image = vision.TensorImage.create_from_file(image_path)
classification_result = classifier.classify(image)

C++ में कोरल एज TPU के इस्तेमाल का उदाहरण

पहला चरण. यह अपने बेज़ल बिल्ड टारगेट में कोरल एज TPU डेलिगेट प्लगिन पर निर्भर करता है. जैसे:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin", # for Coral Edge TPU
]

दूसरा चरण. टास्क के विकल्पों में कोरल एज TPU को कॉन्फ़िगर करें. उदाहरण के लिए, आप सेट कर सकते हैं ImageClassifier में कोरल एज TPU को इस तरह से सेट अप करें:

// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Coral Edge TPU delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(Delegate::EDGETPU_CORAL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();

// Run inference on Coral Edge TPU.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();

तीसरा चरण. libusb-1.0-0-dev पैकेज को यहां बताए गए तरीके से इंस्टॉल करें. अगर यह पहले से है इंस्टॉल करने के बाद, अगले चरण पर जाएं.

# On the Linux
sudo apt-get install libusb-1.0-0-dev

# On the macOS
port install libusb
# or
brew install libusb

चरण 4. अपने बेज़ल कमांड में इन कॉन्फ़िगरेशन को कंपाइल करें:

# On the Linux
--define darwinn_portable=1 --linkopt=-lusb-1.0

# On the macOS, add '--linkopt=-lusb-1.0 --linkopt=-L/opt/local/lib/' if you are
# using MacPorts or '--linkopt=-lusb-1.0 --linkopt=-L/opt/homebrew/lib' if you
# are using Homebrew.
--define darwinn_portable=1 --linkopt=-L/opt/local/lib/ --linkopt=-lusb-1.0

# Windows is not supported yet.

टास्क लाइब्रेरी सीएलआई का डेमो आज़माएं टूल जानें. पहले से ट्रेन किए गए Edge TPU के बारे में ज़्यादा जानें मॉडल और ऐडवांस एज TPU सेटिंग का इस्तेमाल करें.

C++ में कोर एमएल डेलिगेट के इस्तेमाल का उदाहरण

इसका पूरा उदाहरण इमेज क्लासिफ़ायर कोर एमएल डेलिगेट पर देखा जा सकता है टेस्ट.

पहला चरण. यह अपने बेज़ल बिल्ड टारगेट में Core ML डेलिगेट प्लगिन पर निर्भर करता है, जैसे जैसे:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:coreml_plugin", # for Core ML Delegate
]

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

// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Core ML delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(::tflite::proto::Delegate::CORE_ML);
// Set DEVICES_ALL to enable Core ML delegation on any device (in contrast to
// DEVICES_WITH_NEURAL_ENGINE which creates Core ML delegate only on devices
// with Apple Neural Engine).
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->mutable_coreml_settings()->set_enabled_devices(::tflite::proto::CoreMLSettings::DEVICES_ALL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();

// Run inference on Core ML.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();