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

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

TensorFlow Lite के मॉडल

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

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

  1. अगर आपने अब तक ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
  2. आपके मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल में (आम तौर पर <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle), Android के लिए, Firebase एमएल मॉडल डाउनलोडर लाइब्रेरी के लिए डिपेंडेंसी जोड़ें. हमारा सुझाव है कि Firebase Android बीओएम लाइब्रेरी के वर्शन को कंट्रोल करने के लिए.

    इसके अलावा, Firebase ML मॉडल डाउनलोडर सेट अप करने के दौरान, आपको TensorFlow Lite SDK टूल का इस्तेमाल करें.

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
    
        // 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:25.0.0")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }
    अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
    क्या आपको Kotlin से जुड़े लाइब्रेरी मॉड्यूल की तलाश है? इतने समय में शुरू होगा अक्टूबर 2023 (Firebase BoM 32.5.0) में, Kotlin और Java डेवलपर, दोनों मुख्य लाइब्रेरी मॉड्यूल पर निर्भर करते हैं (जानकारी के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले सवाल).
  3. अपने ऐप्लिकेशन के मेनिफ़ेस्ट में, यह बताएं कि इंटरनेट की अनुमति ज़रूरी है:
    <uses-permission android:name="android.permission.INTERNET" />
    अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है

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

Firebase कंसोल या Firebase एडमिन Python और Node.js SDK टूल. यहां जाएं: कस्टम मॉडल डिप्लॉय और मैनेज करना.

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

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

अपने ऐप्लिकेशन में TensorFlow Lite मॉडल का इस्तेमाल करने के लिए, पहले Firebase ML SDK टूल का इस्तेमाल करें डिवाइस पर मॉडल का नया वर्शन डाउनलोड करने के लिए. फिर, इंस्टैंशिएट करके एक मॉडल के साथ TensorFlow Lite का अनुवादक.

मॉडल डाउनलोड करने के लिए, मॉडल डाउनलोड करने वाले के getModel() तरीके को कॉल करें, उस नाम को दर्ज करते हुए जो आपने मॉडल को अपलोड करते समय असाइन किया था, चाहे आप हमेशा नया मॉडल डाउनलोड करना चाहते हैं और उन स्थितियों को को डाउनलोड करने की अनुमति देनी है.

डाउनलोड करने के इन तीन तरीकों में से कोई एक चुनें:

डाउनलोड का टाइप ब्यौरा
LOCAL_MODEL डिवाइस से लोकल मॉडल डाउनलोड करें. अगर कोई लोकल मॉडल उपलब्ध नहीं है, तो यह LATEST_MODEL की तरह काम करता है. इसका इस्तेमाल करें अगर आपकी दिलचस्पी नहीं है, तो डाउनलोड का टाइप मॉडल के अपडेट देखे जा रहे हैं. उदाहरण के लिए, तो आप अपने डेटा को वापस पाने के लिए रिमोट कॉन्फ़िगरेशन का इस्तेमाल कर रहे हैं मॉडल के नाम और आप हमेशा मॉडल अपलोड करते हैं नए नामों के अंतर्गत (सुझाया गया).
LOCAL_MODEL_UPDATE_IN_BACKGROUND डिवाइस से लोकल मॉडल डाउनलोड करें और बैकग्राउंड में मॉडल अपडेट करना शुरू करें. अगर कोई लोकल मॉडल उपलब्ध नहीं है, तो यह LATEST_MODEL की तरह काम करता है.
LATEST_MODEL सबसे नया मॉडल पाएं. अगर लोकल मॉडल सबसे नया वर्शन, model. अगर आपको ऐसा नहीं करना है, तो नया वीडियो डाउनलोड करें model. यह व्यवहार तब तक ब्लॉक रहेगा, जब तक सबसे नया वर्शन डाउनलोड किया गया है (नहीं ). इस व्यवहार का इस्तेमाल सिर्फ़ ऐसे मामले, जहां आपको स्पष्ट रूप से नवीनतम वर्शन है.

आपको मॉडल से जुड़ी सुविधाएं बंद करनी चाहिए. उदाहरण के लिए, ग्रे-आउट या अपने यूज़र इंटरफ़ेस (यूआई) का कुछ हिस्सा छिपाएं—जब तक आप यह पुष्टि नहीं कर लेते कि मॉडल डाउनलोड हो गया है.

Kotlin+KTX

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] फ़्लोटिंग-पॉइंट वैल्यू, इनपुट ByteBuffer जनरेट की जा सकती है Bitmap ऑब्जेक्ट से, जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है:

Kotlin+KTX

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+KTX

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+KTX

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 उन्हें इस तरह संग्रहित करता है कि वे मानक सीरियल वाले प्रोटोबफ़ फ़ॉर्मैट में लोकल स्टोरेज.

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

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

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