छवि क्लासिफायर को एकीकृत करें

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

अपने कस्टम इमेज क्लासिफायर या पूर्व-प्रशिक्षित लोगों को अपने मोबाइल ऐप्स में तैनात करने के लिए टास्क लाइब्रेरी ImageClassifier एपीआई का उपयोग करें।

ImageClassifier API की मुख्य विशेषताएं

  • इनपुट इमेज प्रोसेसिंग, जिसमें रोटेशन, आकार बदलना और रंग स्थान रूपांतरण शामिल है।

  • इनपुट छवि की रुचि का क्षेत्र.

  • मानचित्र स्थान को लेबल करें.

  • परिणामों को फ़िल्टर करने के लिए स्कोर सीमा।

  • टॉप-के वर्गीकरण परिणाम।

  • अनुमति सूची और अस्वीकरण सूची को लेबल करें।

समर्थित छवि वर्गीकरण मॉडल

निम्नलिखित मॉडलों को ImageClassifier API के साथ संगत होने की गारंटी दी गई है।

जावा में अनुमान चलाएँ

एंड्रॉइड ऐप में ImageClassifier का उपयोग कैसे करें के उदाहरण के लिए इमेज क्लासिफिकेशन संदर्भ ऐप देखें।

चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें

.tflite मॉडल फ़ाइल को एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में कॉपी करें जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और मॉड्यूल की 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 (NNAPI is included)
    implementation 'org.tensorflow:tensorflow-lite-task-vision'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

चरण 2: मॉडल का उपयोग करना

// 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);

ImageClassifier कॉन्फ़िगर करने के लिए अधिक विकल्पों के लिए स्रोत कोड और javadoc देखें।

आईओएस में अनुमान चलाएँ

चरण 1: निर्भरताएँ स्थापित करें

टास्क लाइब्रेरी CocoaPods का उपयोग करके इंस्टॉलेशन का समर्थन करती है। सुनिश्चित करें कि CocoaPods आपके सिस्टम पर स्थापित है। कृपया निर्देशों के लिए कोकोपोड्स इंस्टालेशन गाइड देखें।

Xcode प्रोजेक्ट में पॉड्स जोड़ने के विवरण के लिए कृपया CocoaPods गाइड देखें।

पॉडफाइल में TensorFlowLiteTaskVision पॉड जोड़ें।

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

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

चरण 2: मॉडल का उपयोग करना

तीव्र

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

उद्देश्य सी

// 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 कॉन्फ़िगर करने के लिए अधिक विकल्पों के लिए स्रोत कोड देखें।

पायथन में अनुमान चलाएँ

चरण 1: पिप पैकेज स्थापित करें

pip install tflite-support

चरण 2: मॉडल का उपयोग करना

# 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 मेटाडेटा राइटर API का उपयोग करके छवि क्लासिफायर के लिए मेटाडेटा बनाने के उदाहरण देखें।

संगत छवि वर्गीकरण मॉडल को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:

  • इनपुट छवि टेंसर (kTfLiteUInt8/kTfLiteFloat32)

    • आकार का छवि इनपुट [batch x height x width x channels]
    • बैच अनुमान समर्थित नहीं है ( batch 1 होना आवश्यक है)।
    • केवल RGB इनपुट समर्थित हैं ( channels 3 होना आवश्यक है)।
    • यदि प्रकार kTfLiteFloat32 है, तो इनपुट सामान्यीकरण के लिए मेटाडेटा में सामान्यीकरण विकल्प संलग्न करना आवश्यक है।
  • आउटपुट स्कोर टेंसर (kTfLiteUInt8/kTfLiteFloat32)

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