इमेज की कैटगरी तय करने वाले एल्गोरिदम को इंटिग्रेट करना

इमेज की कैटगरी तय करने की सुविधा, मशीन लर्निंग का एक सामान्य इस्तेमाल है. इसकी मदद से, यह पता लगाया जाता है कि इमेज दिखाता है. उदाहरण के लिए, हो सकता है कि हम यह जानना चाहें कि किस तरह का जानवर दिखता है दिए गए तस्वीर में दिखेगा. यह अनुमान लगाने के काम को कहते हैं कि कोई इमेज किस बारे में है इमेज क्लासिफ़िकेशन का इस्तेमाल करें. इमेज की कैटगरी तय करने वाले सिस्टम को, अलग-अलग इमेज की पहचान करने की ट्रेनिंग दी गई है इमेज की क्लास. उदाहरण के लिए, किसी मॉडल को फ़ोटो की पहचान करना ट्रेनिंग दी जा सकती है इमेज जिसमें तीन अलग-अलग तरह के जानवरों के बारे में बताया गया है: खरगोश, हैमस्टर, और कुत्ते. यहां जाएं: यह इमेज क्लासिफ़िकेशन का उदाहरण देखें.

अपनी पसंद की इमेज को डिप्लॉय करने के लिए, टास्क लाइब्रेरी ImageClassifier एपीआई का इस्तेमाल करें क्लासिफ़ायर या पहले से ट्रेन किए गए डेटा को आपके मोबाइल ऐप्लिकेशन में शामिल कर सकते हैं.

ImageClassifier एपीआई की मुख्य सुविधाएं

  • इनपुट इमेज प्रोसेसिंग, जिसमें रोटेशन, साइज़ बदलना, और कलर स्पेस शामिल हैं कन्वर्ज़न होता है.

  • इनपुट इमेज के लिए दिलचस्पी का क्षेत्र.

  • मैप स्थान-भाषा को लेबल करें.

  • नतीजों को फ़िल्टर करने के लिए, स्कोर थ्रेशोल्ड.

  • टॉप-k क्लासिफ़िकेशन के नतीजे.

  • अनुमति वाली सूची और ब्लॉकलिस्ट को लेबल करें.

इमेज की कैटगरी तय करने वाले काम करने वाले मॉडल

ये मॉडल, ImageClassifier के साथ काम करते हैं एपीआई.

Java में अनुमान चलाएं

ज़्यादा जानकारी के लिए, इमेज क्लासिफ़िकेशन के लिए रेफ़रंस ऐप्लिकेशन उदाहरण के लिए, जो Android ऐप्लिकेशन में ImageClassifier इस्तेमाल करने का तरीका है.

पहला चरण: Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना

.tflite मॉडल फ़ाइल को, Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. तय करें कि फ़ाइल कंप्रेस नहीं की जानी चाहिए, और मॉड्यूल की build.gradle फ़ाइल में TensorFlow Lite लाइब्रेरी जोड़ें:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }
}

dependencies {
    // Other dependencies

    // Import the Task Vision Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-vision'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

दूसरा चरण: मॉडल का इस्तेमाल करना

// Initialization
ImageClassifierOptions options =
    ImageClassifierOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setMaxResults(1)
        .build();
ImageClassifier imageClassifier =
    ImageClassifier.createFromFileAndOptions(
        context, modelFile, options);

// Run inference
List<Classifications> results = imageClassifier.classify(image);

ज़्यादा जानकारी के लिए, सोर्स कोड और javadoc ImageClassifier को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

iOS में अनुमान चलाएं

पहला चरण: डिपेंडेंसी इंस्टॉल करना

टास्क लाइब्रेरी में, CocoaPods का इस्तेमाल करके इंस्टॉल किए जा सकते हैं. पक्का करें कि CocoaPods आपके सिस्टम पर इंस्टॉल हो. कृपया CocoaPods को इंस्टॉल करने की गाइड देखें.

कृपया इनके लिए CocoaPods की गाइड Xcode प्रोजेक्ट में पॉड जोड़ने के बारे में जानकारी.

Podfile में TensorFlowLiteTaskVision पॉड जोड़ें.

target 'MyAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskVision'
end

पक्का करें कि अनुमान के लिए, जिस .tflite मॉडल का इस्तेमाल करना है वह इसमें मौजूद हो आपका ऐप्लिकेशन बंडल.

दूसरा चरण: मॉडल का इस्तेमाल करना

Swift

// Imports
import TensorFlowLiteTaskVision

// Initialization
guard let modelPath = Bundle.main.path(forResource: "birds_V1",
                                            ofType: "tflite") else { return }

let options = ImageClassifierOptions(modelPath: modelPath)

// Configure any additional options:
// options.classificationOptions.maxResults = 3

let classifier = try ImageClassifier.classifier(options: options)

// Convert the input image to MLImage.
// There are other sources for MLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
guard let image = UIImage (named: "sparrow.jpg"), let mlImage = MLImage(image: image) else { return }

// Run inference
let classificationResults = try classifier.classify(mlImage: mlImage)

Objective C

// Imports
#import <TensorFlowLiteTaskVision/TensorFlowLiteTaskVision.h>

// Initialization
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"birds_V1" ofType:@"tflite"];

TFLImageClassifierOptions *options =
    [[TFLImageClassifierOptions alloc] initWithModelPath:modelPath];

// Configure any additional options:
// options.classificationOptions.maxResults = 3;

TFLImageClassifier *classifier = [TFLImageClassifier imageClassifierWithOptions:options
                                                                          error:nil];

// Convert the input image to MLImage.
UIImage *image = [UIImage imageNamed:@"sparrow.jpg"];

// There are other sources for GMLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
GMLImage *gmlImage = [[GMLImage alloc] initWithImage:image];

// Run inference
TFLClassificationResult *classificationResult =
    [classifier classifyWithGMLImage:gmlImage error:nil];

ज़्यादा जानकारी के लिए, सोर्स कोड TFLImageClassifier को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

Python में इन्फ़रेंस चलाना

पहला चरण: पीआईपी पैकेज इंस्टॉल करना

pip install tflite-support

दूसरा चरण: मॉडल का इस्तेमाल करना

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

# Initialization
base_options = core.BaseOptions(file_name=model_path)
classification_options = processor.ClassificationOptions(max_results=2)
options = vision.ImageClassifierOptions(base_options=base_options, classification_options=classification_options)
classifier = vision.ImageClassifier.create_from_options(options)

# Alternatively, you can create an image classifier in the following manner:
# classifier = vision.ImageClassifier.create_from_file(model_path)

# Run inference
image = vision.TensorImage.create_from_file(image_path)
classification_result = classifier.classify(image)

ज़्यादा जानकारी के लिए, सोर्स कोड ImageClassifier को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

C++ में अनुमान चलाएं

// Initialization
ImageClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();

// Create input frame_buffer from your inputs, `image_data` and `image_dimension`.
// See more information here: tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h

std::unique_ptr<FrameBuffer> frame_buffer = CreateFromRgbRawBuffer(
      image_data, image_dimension);

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

ज़्यादा जानकारी के लिए, सोर्स कोड ImageClassifier को कॉन्फ़िगर करने के ज़्यादा विकल्पों के बारे में जानें.

परिणामों के उदाहरण

यहां एक उदाहरण के तौर पर, बर्ड क्लासिफ़ायर.

स्पैरो

Results:
  Rank #0:
   index       : 671
   score       : 0.91406
   class name  : /m/01bwb9
   display name: Passer domesticus
  Rank #1:
   index       : 670
   score       : 0.00391
   class name  : /m/01bwbt
   display name: Passer montanus
  Rank #2:
   index       : 495
   score       : 0.00391
   class name  : /m/0bwm6m
   display name: Passer italiae

आसान तरीके आज़माएं ImageClassifier के लिए सीएलआई डेमो टूल की मदद से कैसे डिज़ाइन किया गया है.

मॉडल के साथ काम करने से जुड़ी ज़रूरी शर्तें

ImageClassifier API के लिए, TFLite मॉडल ज़रूरी है. TFLite मॉडल का मेटाडेटा. इमेज की कैटगरी तय करने वाले टूल के लिए मेटाडेटा बनाने के उदाहरण देखें. इसके लिए, TensorFlow Lite Metadata Writer API.

साथ काम करने वाले इमेज क्लासिफ़ायर मॉडल को नीचे दी गई ज़रूरी शर्तें पूरी करनी होंगी:

  • इनपुट इमेज टेंसर (kTfLiteUInt8/kTfLiteFloat32)

    • [batch x height x width x channels] साइज़ का इमेज इनपुट.
    • बैच का अनुमान काम नहीं करता (batch को 1 होना ज़रूरी है).
    • सिर्फ़ आरजीबी इनपुट इस्तेमाल किए जा सकते हैं (channels का नंबर 3 होना ज़रूरी है).
    • अगर टाइप kTfLiteFloat32 है, तो NormalizationOptions को इनपुट नॉर्मलाइज़ेशन के लिए मेटाडेटा से जोड़ा गया है.
  • आउटपुट स्कोर टेंसर (kTfLiteUInt8/kTfLiteFloat32)

    • N क्लास के साथ और दो या चार डाइमेंशन, जैसे कि [1 x N] या [1 x 1 x 1 x N] के साथ
    • प्रकार के साथ AssociatedFile-s के रूप में वैकल्पिक (लेकिन सुझाया गया) लेबल मैप TENSOR_AXIS_LABEL, जिसमें हर लाइन में एक लेबल होना चाहिए. ज़्यादा जानकारी के लिए, उदाहरण के तौर पर लेबल फ़ाइल. label फ़ील्ड को भरने के लिए, ऐसी पहली AssociatedFile (अगर कोई है) का इस्तेमाल किया जाता है (C++ में class_name के नाम से) display_name फ़ील्ड को उस AssociatedFile (अगर कोई हो) से भरा जाता है जिसका स्थान ImageClassifierOptions में से display_names_locale फ़ील्ड का इस्तेमाल किया गया बनाने का समय (डिफ़ॉल्ट रूप से "hi"), जैसे कि अंग्रेज़ी. अगर इनमें से कोई भी उपलब्ध है, तो नतीजों का सिर्फ़ index फ़ील्ड भरा जाएगा.