Android पर, अपनी पसंद के मुताबिक TensorFlow Lite मॉडल का इस्तेमाल करना

अगर आपका ऐप्लिकेशन, पसंद के मुताबिक बनाए गए TensorFlow Lite मॉडल का इस्तेमाल करता है, तो अपने मॉडल डिप्लॉय करने के लिए Firebase ML का इस्तेमाल किया जा सकता है. Firebase की मदद से मॉडल डिप्लॉय करके, ऐप्लिकेशन के शुरुआती डाउनलोड साइज़ को कम किया जा सकता है. साथ ही, ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, उसके एमएल मॉडल अपडेट किए जा सकते हैं. इसके अलावा, Remote Config और A/B Testing की मदद से, उपयोगकर्ताओं के अलग-अलग सेट को डाइनैमिक तौर पर अलग-अलग मॉडल दिखाए जा सकते हैं.

TensorFlow Lite मॉडल

TensorFlow Lite मॉडल, ऐसे एमएल मॉडल होते हैं जिन्हें मोबाइल डिवाइसों पर चलाने के लिए ऑप्टिमाइज़ किया जाता है. TensorFlow लाइट मॉडल पाने के लिए:

शुरू करने से पहले

  1. अगर आपने पहले से ही A/B टेस्टिंग नहीं बनाई है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
  2. मॉड्यूल (ऐप्लिकेशन-लेवल) की 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:35.1.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 लाइब्रेरी डिपेंडेंसी जोड़ें

    अगर आपको 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.1.1")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }
  3. अपने ऐप्लिकेशन के मेनिफ़ेस्ट में, एलान करें कि इंटरनेट की अनुमति ज़रूरी है:
    <uses-permission android:name="android.permission.INTERNET" />

1. अपना मॉडल डिप्लॉय करना

Firebase कंसोल या Firebase Admin Python और Node.js SDK टूल का इस्तेमाल करके, अपने कस्टम TensorFlow मॉडल डिप्लॉय करें. कस्टम मॉडल डिप्लॉय और मैनेज करना लेख पढ़ें.

अपने Firebase प्रोजेक्ट में कस्टम मॉडल जोड़ने के बाद, अपने ऐप्लिकेशन में मॉडल को रेफ़रंस के तौर पर इस्तेमाल किया जा सकता है. इसके लिए, आपको उस नाम का इस्तेमाल करना होगा जो आपने तय किया है. किसी भी समय, नया TensorFlow लाइट मॉडल डिप्लॉय किया जा सकता है. साथ ही, getModel() को कॉल करके, उपयोगकर्ताओं के डिवाइसों पर नया मॉडल डाउनलोड किया जा सकता है (नीचे देखें).

2. मॉडल को डिवाइस पर डाउनलोड करें और TensorFlow लाइट इंटरप्रेटर को शुरू करें

अपने ऐप्लिकेशन में TensorFlow Lite मॉडल का इस्तेमाल करने के लिए, पहले Firebase ML SDK टूल का इस्तेमाल करके, डिवाइस पर मॉडल का नया वर्शन डाउनलोड करें. इसके बाद, मॉडल के साथ 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?
}

अपेंडिक्स: मॉडल की सुरक्षा

इससे कोई फ़र्क़ नहीं पड़ता कि आपने Firebase ML के लिए, TensorFlow लाइट मॉडल कैसे उपलब्ध कराए हैं. Firebase ML उन्हें लोकल स्टोरेज में, स्टैंडर्ड सीरियलाइज़्ड प्रोटोबफ़ फ़ॉर्मैट में सेव करता है.

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

Android एपीआई लेवल 21 (Lollipop) और इसके बाद के वर्शन पर, मॉडल को ऐसी डायरेक्ट्री में डाउनलोड किया जाता है जिसे अपने-आप बैकअप लेने की सुविधा से बाहर रखा गया है.

Android एपीआई लेवल 20 और इससे पहले के वर्शन पर, मॉडल को ऐप्लिकेशन के निजी इंटरनल स्टोरेज में com.google.firebase.ml.custom.models नाम की डायरेक्ट्री में डाउनलोड किया जाता है. अगर आपने BackupAgent का इस्तेमाल करके फ़ाइल बैकअप की सुविधा चालू की है, तो आपके पास इस डायरेक्ट्री को बाहर रखने का विकल्प है.