छवि वर्गीकरण मशीन सीखने का एक सामान्य उपयोग है जो यह पहचानने के लिए है कि छवि क्या दर्शाती है। उदाहरण के लिए, हम जानना चाह सकते हैं कि किसी दिए गए चित्र में किस प्रकार का जानवर दिखाई देता है। एक छवि जो दर्शाती है उसकी भविष्यवाणी करने का कार्य छवि वर्गीकरण कहलाता है। छवियों के विभिन्न वर्गों को पहचानने के लिए एक छवि क्लासिफायरियर को प्रशिक्षित किया जाता है। उदाहरण के लिए, एक मॉडल को तीन अलग-अलग प्रकार के जानवरों का प्रतिनिधित्व करने वाली तस्वीरों को पहचानने के लिए प्रशिक्षित किया जा सकता है: खरगोश, हम्सटर और कुत्ते। छवि वर्गीकरण के बारे में अधिक जानकारी के लिए छवि वर्गीकरण अवलोकन देखें।
अपने मोबाइल ऐप में अपने कस्टम इमेज क्लासिफायर या पूर्व-प्रशिक्षित लोगों को तैनात करने के लिए टास्क लाइब्रेरी ImageClassifier
एपीआई का उपयोग करें।
ImageClassifier API की मुख्य विशेषताएं
इनपुट इमेज प्रोसेसिंग, जिसमें रोटेशन, आकार बदलना और रंग स्थान रूपांतरण शामिल हैं।
इनपुट छवि के हित का क्षेत्र।
लेबल मानचित्र स्थान।
परिणामों को फ़िल्टर करने के लिए स्कोर थ्रेशोल्ड।
टॉप-के वर्गीकरण परिणाम।
लेबल अनुमत सूची और अस्वीकृत सूची।
समर्थित छवि वर्गीकरण मॉडल
निम्नलिखित मॉडलों को ImageClassifier
API के साथ संगत होने की गारंटी है।
छवि वर्गीकरण के लिए TensorFlow Lite Model Maker द्वारा बनाए गए मॉडल।
AutoML विज़न एज इमेज क्लासिफिकेशन द्वारा बनाए गए मॉडल।
कस्टम मॉडल जो मॉडल संगतता आवश्यकताओं को पूरा करते हैं।
जावा में अनुमान चलाएँ
Android ऐप में ImageClassifier
का उपयोग करने के तरीके के उदाहरण के लिए छवि वर्गीकरण संदर्भ ऐप देखें।
चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें
एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में .tflite
मॉडल फ़ाइल की प्रतिलिपि बनाएँ जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और TensorFlow Lite लाइब्रेरी को मॉड्यूल की build.gradle
फ़ाइल में जोड़ें:
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 स्थापित है। निर्देशों के लिए कृपया 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
को कॉन्फ़िगर करने के अधिक विकल्पों के लिए स्रोत कोड देखें।
सी ++ में अनुमान चलाएं
// 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 के लिए सरल CLI डेमो टूल आज़माएं।
मॉडल संगतता आवश्यकताएँ
ImageClassifier
API अनिवार्य TFLite मॉडल मेटाडेटा के साथ TFlite मॉडल की अपेक्षा करता है। TensorFlow Lite Metadata Writer API का उपयोग करके इमेज क्लासिफायरियर के लिए मेटाडेटा बनाने के उदाहरण देखें।
संगत छवि क्लासिफायरियर मॉडल को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:
इनपुट इमेज टेंसर (kTfLiteUInt8/kTfLiteFloat32)
- आकार का छवि इनपुट
[batch x height x width x channels]
। - बैच अनुमान समर्थित नहीं है (
batch
1 होना आवश्यक है)। - केवल आरजीबी इनपुट समर्थित हैं (
channels
3 होना आवश्यक है)। - यदि प्रकार kTfLiteFloat32 है, तो सामान्यीकरण विकल्प को इनपुट सामान्यीकरण के लिए मेटाडेटा से संलग्न करना आवश्यक है।
- आकार का छवि इनपुट
आउटपुट स्कोर टेंसर (kTfLiteUInt8/kTfLiteFloat32)
-
N
वर्गों और या तो 2 या 4 आयामों के साथ, अर्थात[1 x N]
या[1 x 1 x 1 x N]
- TENSOR_AXIS_LABELS प्रकार के साथ AssociatedFile-s के रूप में वैकल्पिक (लेकिन अनुशंसित) लेबल मानचित्र, जिसमें प्रति पंक्ति एक लेबल है। उदाहरण लेबल फ़ाइल देखें। इस तरह की पहली एसोसिएटेडफाइल (यदि कोई हो) का उपयोग परिणामों के
label
फ़ील्ड (C++ मेंclass_name
के रूप में नामित) को भरने के लिए किया जाता है।display_name
फ़ील्ड एसोसिएटेडफाइल (यदि कोई हो) से भरी जाती है जिसका लोकेलdisplay_names_locale
केImageClassifierOptions
फ़ील्ड से मेल खाता है जो निर्माण के समय ("एन") डिफ़ॉल्ट रूप से, यानी अंग्रेजी में उपयोग किया जाता है। यदि इनमें से कोई भी उपलब्ध नहीं है, तो केवल परिणामों कीindex
फ़ील्ड भरी जाएगी।
-
छवि वर्गीकरण मशीन सीखने का एक सामान्य उपयोग है जो यह पहचानने के लिए है कि छवि क्या दर्शाती है। उदाहरण के लिए, हम जानना चाह सकते हैं कि किसी दिए गए चित्र में किस प्रकार का जानवर दिखाई देता है। एक छवि जो दर्शाती है उसकी भविष्यवाणी करने का कार्य छवि वर्गीकरण कहलाता है। छवियों के विभिन्न वर्गों को पहचानने के लिए एक छवि क्लासिफायरियर को प्रशिक्षित किया जाता है। उदाहरण के लिए, एक मॉडल को तीन अलग-अलग प्रकार के जानवरों का प्रतिनिधित्व करने वाली तस्वीरों को पहचानने के लिए प्रशिक्षित किया जा सकता है: खरगोश, हम्सटर और कुत्ते। छवि वर्गीकरण के बारे में अधिक जानकारी के लिए छवि वर्गीकरण अवलोकन देखें।
अपने मोबाइल ऐप में अपने कस्टम इमेज क्लासिफायर या पूर्व-प्रशिक्षित लोगों को तैनात करने के लिए टास्क लाइब्रेरी ImageClassifier
एपीआई का उपयोग करें।
ImageClassifier API की मुख्य विशेषताएं
इनपुट इमेज प्रोसेसिंग, जिसमें रोटेशन, आकार बदलना और रंग स्थान रूपांतरण शामिल हैं।
इनपुट छवि के हित का क्षेत्र।
लेबल मानचित्र स्थान।
परिणामों को फ़िल्टर करने के लिए स्कोर थ्रेशोल्ड।
टॉप-के वर्गीकरण परिणाम।
लेबल अनुमत सूची और अस्वीकृत सूची।
समर्थित छवि वर्गीकरण मॉडल
निम्नलिखित मॉडलों को ImageClassifier
API के साथ संगत होने की गारंटी है।
छवि वर्गीकरण के लिए TensorFlow Lite Model Maker द्वारा बनाए गए मॉडल।
AutoML विज़न एज इमेज क्लासिफिकेशन द्वारा बनाए गए मॉडल।
कस्टम मॉडल जो मॉडल संगतता आवश्यकताओं को पूरा करते हैं।
जावा में अनुमान चलाएँ
Android ऐप में ImageClassifier
का उपयोग करने के तरीके के उदाहरण के लिए छवि वर्गीकरण संदर्भ ऐप देखें।
चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें
एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में .tflite
मॉडल फ़ाइल की प्रतिलिपि बनाएँ जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और TensorFlow Lite लाइब्रेरी को मॉड्यूल की build.gradle
फ़ाइल में जोड़ें:
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 स्थापित है। निर्देशों के लिए कृपया 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
को कॉन्फ़िगर करने के अधिक विकल्पों के लिए स्रोत कोड देखें।
सी ++ में अनुमान चलाएं
// 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 के लिए सरल CLI डेमो टूल आज़माएं।
मॉडल संगतता आवश्यकताएँ
ImageClassifier
API अनिवार्य TFLite मॉडल मेटाडेटा के साथ TFlite मॉडल की अपेक्षा करता है। TensorFlow Lite Metadata Writer API का उपयोग करके इमेज क्लासिफायरियर के लिए मेटाडेटा बनाने के उदाहरण देखें।
संगत छवि क्लासिफायरियर मॉडल को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:
इनपुट इमेज टेंसर (kTfLiteUInt8/kTfLiteFloat32)
- आकार का छवि इनपुट
[batch x height x width x channels]
। - बैच अनुमान समर्थित नहीं है (
batch
1 होना आवश्यक है)। - केवल आरजीबी इनपुट समर्थित हैं (
channels
3 होना आवश्यक है)। - यदि प्रकार kTfLiteFloat32 है, तो सामान्यीकरण विकल्प को इनपुट सामान्यीकरण के लिए मेटाडेटा से संलग्न करना आवश्यक है।
- आकार का छवि इनपुट
आउटपुट स्कोर टेंसर (kTfLiteUInt8/kTfLiteFloat32)
-
N
वर्गों और या तो 2 या 4 आयामों के साथ, अर्थात[1 x N]
या[1 x 1 x 1 x N]
- TENSOR_AXIS_LABELS प्रकार के साथ AssociatedFile-s के रूप में वैकल्पिक (लेकिन अनुशंसित) लेबल मानचित्र, जिसमें प्रति पंक्ति एक लेबल है। उदाहरण लेबल फ़ाइल देखें। इस तरह की पहली एसोसिएटेडफाइल (यदि कोई हो) का उपयोग परिणामों के
label
फ़ील्ड (C++ मेंclass_name
के रूप में नामित) को भरने के लिए किया जाता है।display_name
फ़ील्ड एसोसिएटेडफाइल (यदि कोई हो) से भरी जाती है जिसका लोकेलdisplay_names_locale
केImageClassifierOptions
फ़ील्ड से मेल खाता है जो निर्माण के समय ("एन") डिफ़ॉल्ट रूप से, यानी अंग्रेजी में उपयोग किया जाता है। यदि इनमें से कोई भी उपलब्ध नहीं है, तो केवल परिणामों कीindex
फ़ील्ड भरी जाएगी।
-