इमेज सर्च की सुविधा की मदद से, इमेज के डेटाबेस में मिलती-जुलती इमेज खोजी जा सकती हैं. यह, खोज क्वेरी को एक हाई-डाइमेंशनल वेक्टर में एम्बेड करके काम करता है. यह वेक्टर, क्वेरी के सिमैंटिक मतलब को दिखाता है. इसके बाद, ScaNN (स्केलेबल नियरेस्ट नेबर) का इस्तेमाल करके, पहले से तय किए गए कस्टम इंडेक्स में मिलती-जुलती खोज की जाती है.
इमेज क्लासिफ़िकेशन के उलट, पहचाने जा सकने वाले आइटम की संख्या बढ़ाने के लिए, पूरे मॉडल को फिर से ट्रेन करने की ज़रूरत नहीं होती. इंडेक्स को फिर से बनाकर, नए आइटम जोड़े जा सकते हैं. इससे, इमेज के बड़े डेटाबेस (100 हज़ार से ज़्यादा आइटम) के साथ काम किया जा सकता है.
अपने मोबाइल ऐप्लिकेशन में कस्टम इमेज सर्च करने की सुविधा को डिप्लॉय करने के लिए, Task Library ImageSearcher API का इस्तेमाल करें.
ImageSearcher API की मुख्य सुविधाएं
यह नोड, एक इमेज को इनपुट के तौर पर लेता है. साथ ही, इंडेक्स में एम्बेडिंग एक्सट्रैक्शन और सबसे मिलते-जुलते पड़ोसी की खोज करता है.
इनपुट इमेज को प्रोसेस करना. इसमें इमेज को घुमाना, उसका साइज़ बदलना, और कलर स्पेस बदलना शामिल है.
इनपुट इमेज में दिलचस्पी वाला क्षेत्र.
ज़रूरी शर्तें
ImageSearcher एपीआई का इस्तेमाल करने से पहले, खोज के लिए इमेज के कस्टम कॉर्पस के आधार पर इंडेक्स बनाना ज़रूरी है. इसके लिए, Model Maker Searcher API का इस्तेमाल किया जा सकता है. इसके लिए, ट्यूटोरियल को फ़ॉलो करें और उसमें दिए गए निर्देशों के मुताबिक बदलाव करें.
इसके लिए, आपको इन चीज़ों की ज़रूरत होगी:
- TFLite इमेज एम्बेडर मॉडल, जैसे कि mobilenet
v3.
Kaggle Models पर Google Image Modules collection से, पहले से ट्रेन किए गए ज़्यादा एम्बेडर मॉडल (इन्हें फ़ीचर वेक्टर मॉडल भी कहा जाता है) देखें.
- इमेज के कॉर्पस को बेहतर बनाने में मदद मिलती है.
इस चरण के बाद, आपके पास एक स्टैंडअलोन TFLite सर्च करने वाला मॉडल (जैसे कि mobilenet_v3_searcher.tflite) होना चाहिए.यह ओरिजनल इमेज एम्बेडर मॉडल होता है. इसमें इंडेक्स को TFLite मॉडल मेटाडेटा में अटैच किया जाता है.
Java में अनुमान लगाने की सुविधा इस्तेमाल करना
पहला चरण: Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना
.tflite खोज करने वाले मॉडल की फ़ाइल को Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. यह तय करें कि फ़ाइल को कंप्रेस नहीं किया जाना चाहिए. साथ ही, मॉड्यूल की build.gradle फ़ाइल में TensorFlow Lite लाइब्रेरी जोड़ें:
android {
// Other settings
// Specify tflite index 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:0.4.4'
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.4'
}
दूसरा चरण: मॉडल का इस्तेमाल करना
// Initialization
ImageSearcherOptions options =
ImageSearcherOptions.builder()
.setBaseOptions(BaseOptions.builder().useGpu().build())
.setSearcherOptions(
SearcherOptions.builder().setL2Normalize(true).build())
.build();
ImageSearcher imageSearcher =
ImageSearcher.createFromFileAndOptions(context, modelFile, options);
// Run inference
List<NearestNeighbor> results = imageSearcher.search(image);
ImageSearcher को कॉन्फ़िगर करने के ज़्यादा विकल्पों के लिए, सोर्स कोड और javadoc देखें.
C++ में अनुमान चलाने की सुविधा
// Initialization
ImageSearcherOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
options.mutable_embedding_options()->set_l2_normalize(true);
std::unique_ptr<ImageSearcher> image_searcher = ImageSearcher::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 SearchResult result = image_searcher->Search(*frame_buffer).value();
ImageSearcher को कॉन्फ़िगर करने के ज़्यादा विकल्पों के लिए, सोर्स कोड देखें.
Python में अनुमान लगाने की सुविधा का इस्तेमाल करना
पहला चरण: TensorFlow Lite Support Pypi पैकेज इंस्टॉल करें.
TensorFlow Lite Support Pypi पैकेज को इंस्टॉल करने के लिए, इस निर्देश का इस्तेमाल करें:
pip install tflite-support
दूसरा चरण: मॉडल का इस्तेमाल करना
from tflite_support.task import vision
# Initialization
image_searcher = vision.ImageSearcher.create_from_file(model_path)
# Run inference
image = vision.TensorImage.create_from_file(image_file)
result = image_searcher.search(image)
ImageSearcher को कॉन्फ़िगर करने के ज़्यादा विकल्पों के लिए, सोर्स कोड देखें.
परिणामों के उदाहरण
Results:
Rank#0:
metadata: burger
distance: 0.13452
Rank#1:
metadata: car
distance: 1.81935
Rank#2:
metadata: bird
distance: 1.96617
Rank#3:
metadata: dog
distance: 2.05610
Rank#4:
metadata: cat
distance: 2.06347
अपने मॉडल और टेस्ट डेटा के साथ, ImageSearcher के लिए उपलब्ध, सीएलआई डेमो टूल आज़माएं.