अगर आपका ऐप्लिकेशन, कस्टम TensorFlow Lite मॉडल का इस्तेमाल करता है, तो मॉडल डिप्लॉय करने के लिए Firebase ML का इस्तेमाल किया जा सकता है. Firebase की मदद से मॉडल डिप्लॉय करके, अपने ऐप्लिकेशन का शुरुआती डाउनलोड साइज़ कम किया जा सकता है. साथ ही, ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, उसके एमएल मॉडल अपडेट किए जा सकते हैं. इसके अलावा, Remote Config और A/B Testing की मदद से, उपयोगकर्ताओं के अलग-अलग ग्रुप को अलग-अलग मॉडल दिखाए जा सकते हैं.
TensorFlow Lite मॉडल
TensorFlow लाइट मॉडल, एमएल मॉडल होते हैं. इन्हें मोबाइल डिवाइसों पर काम करने के लिए ऑप्टिमाइज़ किया जाता है. TensorFlow लाइट मॉडल पाने के लिए:
- पहले से बने मॉडल का इस्तेमाल करें. जैसे, TensorFlow Lite के आधिकारिक मॉडल.
- TensorFlow मॉडल, Keras मॉडल या कॉन्क्रीट फ़ंक्शन को TensorFlow Lite में बदलें.
शुरू करने से पहले
- अगर आपने पहले से ही, अपने Android प्रोजेक्ट में Firebase नहीं जोड़ा है.
-
अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल
(आम तौर पर,
<project>/<app-module>/build.gradle.ktsया<project>/<app-module>/build.gradle) में, Android के लिए Firebase ML मॉडल डाउनलोडर लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.इसके अलावा, Firebase ML मॉडल डाउनलोडर सेट अप करने के लिए, आपको अपने ऐप्लिकेशन में TensorFlow लाइट SDK टूल जोड़ना होगा.
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.15.0")) // Add the dependency for the Firebase ML model downloader library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ml-modeldownloader")
// Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0") }Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.
(दूसरा तरीका) BoM का इस्तेमाल किए बिना Firebase लाइब्रेरी की डिपेंडेंसी जोड़नाBoM
अगर Firebase BoM का इस्तेमाल नहीं किया जाता है, तो आपको हर Firebase लाइब्रेरी के वर्शन को उसकी डिपेंडेंसी लाइन में बताना होगा.
ध्यान दें कि अगर आपके ऐप्लिकेशन में एक से ज़्यादा Firebase लाइब्रेरी का इस्तेमाल किया जाता है, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए BoM का इस्तेमाल करें. इससे यह पक्का होता है कि सभी वर्शन एक-दूसरे के साथ काम करते हैं.
dependencies { // Add the dependency for the Firebase ML model downloader library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ml-modeldownloader:26.0.2")
// Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0") } - अपने ऐप्लिकेशन के मेनिफ़ेस्ट में, यह एलान करें कि INTERNET की अनुमति ज़रूरी है:
<uses-permission android:name="android.permission.INTERNET" />
1. अपना मॉडल डिप्लॉय करना
Firebase Firebase कंसोल या Firebase Admin Python और Node.js SDK टूल का इस्तेमाल करके, अपने कस्टम TensorFlow मॉडल डिप्लॉय करें. कस्टम मॉडल डिप्लॉय और मैनेज करना लेख पढ़ें .
अपने Firebase प्रोजेक्ट में कस्टम मॉडल जोड़ने के बाद, बताए गए नाम का इस्तेमाल करके अपने ऐप्लिकेशन में
मॉडल का रेफ़रंस दिया जा सकता है. किसी भी समय, नया TensorFlow Lite मॉडल डिप्लॉय किया जा सकता है. साथ ही,
को कॉल करके, उपयोगकर्ताओं के डिवाइसों पर नया मॉडल डाउनलोड किया जा सकता है. इसके बारे में नीचे बताया गया है.getModel()
2. मॉडल को डिवाइस पर डाउनलोड करना और TensorFlow लाइट इंटरप्रेटर को शुरू करना
अपने ऐप्लिकेशन में TensorFlow लाइट मॉडल का इस्तेमाल करने के लिए, सबसे पहले Firebase ML एसडीके का इस्तेमाल करके, मॉडल का नया वर्शन डिवाइस पर डाउनलोड करें. इसके बाद, मॉडल के साथ TensorFlow लाइट इंटरप्रेटर को इंस्टैंशिएट करें.मॉडल डाउनलोड करने के लिए, मॉडल डाउनलोडर के getModel() तरीके को कॉल करें. इसमें, मॉडल अपलोड करते समय दिया गया नाम, यह जानकारी कि आपको हमेशा नया मॉडल डाउनलोड करना है या नहीं, और वे शर्तें बताएं जिनके तहत डाउनलोड करने की अनुमति देनी है.
डाउनलोड करने के तीन तरीके चुने जा सकते हैं:
| डाउनलोड करने का तरीका | ब्यौरा |
|---|---|
| LOCAL_MODEL | डिवाइस से लोकल मॉडल पाएं.
अगर कोई लोकल मॉडल उपलब्ध नहीं है, तो यह LATEST_MODEL की तरह काम करता है. अगर आपको मॉडल के अपडेट की जांच करने में दिलचस्पी नहीं है, तो डाउनलोड करने के इस तरीके का इस्तेमाल करें. उदाहरण के लिए, अगर मॉडल के नाम पाने के लिए रिमोट कॉन्फ़िगरेशन का इस्तेमाल किया जा रहा है और हमेशा नए नामों से मॉडल अपलोड किए जा रहे हैं (सुझाया जाता है). |
| LOCAL_MODEL_UPDATE_IN_BACKGROUND | डिवाइस से लोकल मॉडल पाएं और बैकग्राउंड में मॉडल को अपडेट करना शुरू करें.
अगर कोई लोकल मॉडल उपलब्ध नहीं है, तो यह LATEST_MODEL की तरह काम करता है. |
| LATEST_MODEL | नया मॉडल पाएं. अगर लोकल मॉडल, नया वर्शन है, तो लोकल मॉडल दिखाता है. वरना, नया मॉडल डाउनलोड करें. नया वर्शन डाउनलोड होने तक, यह तरीका काम नहीं करेगा. हमारा सुझाव है कि इसका इस्तेमाल न करें. इस तरीके का इस्तेमाल सिर्फ़ उन मामलों में करें जहां आपको साफ़ तौर पर नए वर्शन की ज़रूरत हो. |
मॉडल डाउनलोड होने की पुष्टि होने तक, मॉडल से जुड़ी सुविधाओं को बंद रखें. उदाहरण के लिए, अपने यूज़र इंटरफ़ेस (यूआई) के किसी हिस्से को धुंधला करें या छिपाएं.
Kotlin
val conditions = CustomModelDownloadConditions.Builder()
.requireWifi() // Also possible: .requireCharging() and .requireDeviceIdle()
.build()
FirebaseModelDownloader.getInstance()
.getModel("your_model", DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND,
conditions)
.addOnSuccessListener { model: CustomModel? ->
// Download complete. Depending on your app, you could enable the ML
// feature, or switch from the local model to the remote model, etc.
// The CustomModel object contains the local path of the model file,
// which you can use to instantiate a TensorFlow Lite interpreter.
val modelFile = model?.file
if (modelFile != null) {
interpreter = Interpreter(modelFile)
}
}
Java
CustomModelDownloadConditions conditions = new CustomModelDownloadConditions.Builder()
.requireWifi() // Also possible: .requireCharging() and .requireDeviceIdle()
.build();
FirebaseModelDownloader.getInstance()
.getModel("your_model", DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND, conditions)
.addOnSuccessListener(new OnSuccessListener<CustomModel>() {
@Override
public void onSuccess(CustomModel model) {
// Download complete. Depending on your app, you could enable the ML
// feature, or switch from the local model to the remote model, etc.
// The CustomModel object contains the local path of the model file,
// which you can use to instantiate a TensorFlow Lite interpreter.
File modelFile = model.getFile();
if (modelFile != null) {
interpreter = new Interpreter(modelFile);
}
}
});
कई ऐप्लिकेशन, अपने इनिशियलाइज़ेशन कोड में डाउनलोड करने का टास्क शुरू करते हैं. हालांकि, मॉडल का इस्तेमाल करने से पहले, किसी भी समय ऐसा किया जा सकता है.
3. इनपुट डेटा पर अनुमान लगाना
अपने मॉडल के इनपुट और आउटपुट के आकार पाना
TensorFlow Lite मॉडल इंटरप्रेटर, इनपुट के तौर पर एक या उससे ज़्यादा मल्टीडाइमेंशनल ऐरे लेता है और आउटपुट के तौर पर उन्हें जनरेट करता है. इन ऐरे में या तो
byte, int, long, या float
वैल्यू होती हैं. किसी मॉडल को डेटा पास करने या उसके नतीजे का इस्तेमाल करने से पहले, आपको यह पता होना चाहिए
कि आपके मॉडल में इस्तेमाल होने वाले ऐरे की संख्या और डाइमेंशन ("आकार") क्या हैं.
अगर आपने मॉडल खुद बनाया है या मॉडल के इनपुट और आउटपुट फ़ॉर्मैट के बारे में दस्तावेज़ मौजूद है, तो आपके पास यह जानकारी पहले से हो सकती है. अगर आपको अपने मॉडल के इनपुट और आउटपुट के आकार और डेटा टाइप के बारे में नहीं पता है, तो अपने मॉडल की जांच करने के लिए, TensorFlow Lite इंटरप्रेटर का इस्तेमाल किया जा सकता है. उदाहरण के लिए:
Python
import tensorflow as tf interpreter = tf.lite.Interpreter(model_path="your_model.tflite") interpreter.allocate_tensors() # Print input shape and type inputs = interpreter.get_input_details() print('{} input(s):'.format(len(inputs))) for i in range(0, len(inputs)): print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype'])) # Print output shape and type outputs = interpreter.get_output_details() print('\n{} output(s):'.format(len(outputs))) for i in range(0, len(outputs)): print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))
आउटपुट का उदाहरण:
1 input(s): [ 1 224 224 3] <class 'numpy.float32'> 1 output(s): [1 1000] <class 'numpy.float32'>
इंटरप्रेटर चलाना
अपने मॉडल के इनपुट और आउटपुट का फ़ॉर्मैट तय करने के बाद, अपना इनपुट डेटा पाएं. साथ ही, डेटा में ज़रूरी बदलाव करें, ताकि आपके मॉडल के लिए सही आकार का इनपुट मिल सके.उदाहरण के लिए, अगर आपके पास [1 224 224 3] फ़्लोटिंग-पॉइंट वैल्यू वाला इमेज क्लासिफ़िकेशन मॉडल है, तो Bitmap ऑब्जेक्ट से इनपुट ByteBuffer जनरेट किया जा सकता है. इसके लिए, यहां दिया गया उदाहरण देखें:
Kotlin
val bitmap = Bitmap.createScaledBitmap(yourInputImage, 224, 224, true)
val input = ByteBuffer.allocateDirect(224*224*3*4).order(ByteOrder.nativeOrder())
for (y in 0 until 224) {
for (x in 0 until 224) {
val px = bitmap.getPixel(x, y)
// Get channel values from the pixel value.
val r = Color.red(px)
val g = Color.green(px)
val b = Color.blue(px)
// Normalize channel values to [-1.0, 1.0]. This requirement depends on the model.
// For example, some models might require values to be normalized to the range
// [0.0, 1.0] instead.
val rf = (r - 127) / 255f
val gf = (g - 127) / 255f
val bf = (b - 127) / 255f
input.putFloat(rf)
input.putFloat(gf)
input.putFloat(bf)
}
}
Java
Bitmap bitmap = Bitmap.createScaledBitmap(yourInputImage, 224, 224, true);
ByteBuffer input = ByteBuffer.allocateDirect(224 * 224 * 3 * 4).order(ByteOrder.nativeOrder());
for (int y = 0; y < 224; y++) {
for (int x = 0; x < 224; x++) {
int px = bitmap.getPixel(x, y);
// Get channel values from the pixel value.
int r = Color.red(px);
int g = Color.green(px);
int b = Color.blue(px);
// Normalize channel values to [-1.0, 1.0]. This requirement depends
// on the model. For example, some models might require values to be
// normalized to the range [0.0, 1.0] instead.
float rf = (r - 127) / 255.0f;
float gf = (g - 127) / 255.0f;
float bf = (b - 127) / 255.0f;
input.putFloat(rf);
input.putFloat(gf);
input.putFloat(bf);
}
}
इसके बाद, मॉडल के आउटपुट को शामिल करने के लिए, बड़ा ByteBuffer असाइन करें. साथ ही, TensorFlow Lite इंटरप्रेटर के run() तरीके में, इनपुट बफ़र और आउटपुट बफ़र पास करें. उदाहरण के लिए, [1 1000] फ़्लोटिंग-पॉइंट वैल्यू वाले आउटपुट आकार के लिए:
Kotlin
val bufferSize = 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE
val modelOutput = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder())
interpreter?.run(input, modelOutput)
Java
int bufferSize = 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE;
ByteBuffer modelOutput = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder());
interpreter.run(input, modelOutput);
आउटपुट का इस्तेमाल करने का तरीका, इस्तेमाल किए जा रहे मॉडल पर निर्भर करता है.
उदाहरण के लिए, अगर क्लासिफ़िकेशन किया जा रहा है, तो अगले चरण में, नतीजे के इंडेक्स को उन लेबल से मैप किया जा सकता है जो उन्हें दिखाते हैं:
Kotlin
modelOutput.rewind()
val probabilities = modelOutput.asFloatBuffer()
try {
val reader = BufferedReader(
InputStreamReader(assets.open("custom_labels.txt")))
for (i in probabilities.capacity()) {
val label: String = reader.readLine()
val probability = probabilities.get(i)
println("$label: $probability")
}
} catch (e: IOException) {
// File not found?
}
Java
modelOutput.rewind();
FloatBuffer probabilities = modelOutput.asFloatBuffer();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("custom_labels.txt")));
for (int i = 0; i < probabilities.capacity(); i++) {
String label = reader.readLine();
float probability = probabilities.get(i);
Log.i(TAG, String.format("%s: %1.4f", label, probability));
}
} catch (IOException e) {
// File not found?
}
अपेंडिक्स: मॉडल की सुरक्षा
TensorFlow Lite मॉडल को Firebase ML के लिए उपलब्ध कराने के तरीके से कोई फ़र्क़ नहीं पड़ता. Firebase ML उन्हें लोकल स्टोरेज में, स्टैंडर्ड सीरियलाइज़्ड protobuf फ़ॉर्मैट में सेव करता है.
सैद्धांतिक तौर पर, इसका मतलब है कि कोई भी व्यक्ति आपके मॉडल को कॉपी कर सकता है. हालांकि, असल में, ज़्यादातर मॉडल, ऐप्लिकेशन के हिसाब से बने होते हैं. साथ ही, ऑप्टिमाइज़ेशन की वजह से उन्हें इस तरह से बनाया जाता है कि उन्हें आसानी से समझा न जा सके. इसलिए, यह खतरा, प्रतिस्पर्धियों के आपके कोड को अलग-अलग हिस्सों में बांटकर फिर से इस्तेमाल करने के खतरे जैसा ही होता है. इसके बावजूद, अपने ऐप्लिकेशन में कस्टम मॉडल का इस्तेमाल करने से पहले, आपको इस खतरे के बारे में पता होना चाहिए.
Android एपीआई लेवल 21 (Lollipop) और इसके बाद के वर्शन पर, मॉडल को ऐसे डायरेक्ट्री में डाउनलोड किया जाता है जिसे अपने-आप बैकअप होने से बाहर रखा जाता है.
Android API लेवल 20 और इससे पहले के वर्शन पर, मॉडल को ऐप्लिकेशन के प्राइवेट
इंटरनल स्टोरेज में, com.google.firebase.ml.custom.models नाम की डायरेक्ट्री में डाउनलोड किया जाता है. BackupAgent का इस्तेमाल करके, फ़ाइल बैकअप की सुविधा चालू करने पर, इस डायरेक्ट्री को बैकअप से बाहर रखा जा सकता है.