LiteRT की सहायता लाइब्रेरी की मदद से, इनपुट और आउटपुट डेटा को प्रोसेस करना

मोबाइल ऐप्लिकेशन डेवलपर आम तौर पर टाइप की गई वस्तुओं जैसे बिटमैप या प्रिमिटिव, जैसे कि पूर्णांक. हालांकि, LiteRT इंटरप्रेटर उपयोगकर्ता के डिवाइस पर मशीन लर्निंग मॉडल को चलाने वाला एपीआई, टेंसर का इस्तेमाल इस तरह करता है: बाइटबफ़र, जिसे डीबग और हेर-फेर करना मुश्किल हो सकता है. कॉन्टेंट बनाने LiteRT Android सपोर्ट लाइब्रेरी इसे LiteRT मॉडल के इनपुट और आउटपुट को प्रोसेस करने में मदद करने के लिए डिज़ाइन किया गया है. LiteRT इंटरप्रेटर को इस्तेमाल करना आसान बनाएं.

शुरू करें

Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना

.tflite मॉडल फ़ाइल को, Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. तय करें कि फ़ाइल कंप्रेस नहीं की जानी चाहिए, और मॉड्यूल की build.gradle फ़ाइल में LiteRT लाइब्रेरी जोड़ें:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import tflite dependencies
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
    // The GPU delegate library is optional. Depend on it as needed.
    implementation 'com.google.ai.edge.litert:litert-gpu:0.0.0-nightly-SNAPSHOT'
    implementation 'com.google.ai.edge.litert:litert-support:0.0.0-nightly-SNAPSHOT'
}

एक्सप्लोर करें MavenCentral पर होस्ट की गई LiteRT सपोर्ट लाइब्रेरी एएआर के अलग-अलग वर्शन के लिए उपलब्ध है.

बुनियादी इमेज में बदलाव करना और उसे बदलना

LiteRT की सहायता लाइब्रेरी में, इमेज में बदलाव करने के लिए बुनियादी सुविधाओं का एक सुइट मौजूद है काटें और साइज़ बदलें. इसका इस्तेमाल करने के लिए, ImagePreprocessor बनाएं और ज़रूरी कार्रवाइयां जोड़ें. इमेज को टेंसर फ़ॉर्मैट में बदलने के लिए LiteRT अनुवादक के लिए ज़रूरी है. इस्तेमाल करने के लिए TensorImage बनाएं इनपुट के रूप में:

import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;

// Initialization code
// Create an ImageProcessor with all ops required. For more ops, please
// refer to the ImageProcessor Architecture section in this README.
ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR))
        .build();

// Create a TensorImage object. This creates the tensor of the corresponding
// tensor type (uint8 in this case) that the LiteRT interpreter needs.
TensorImage tensorImage = new TensorImage(DataType.UINT8);

// Analysis code for every frame
// Preprocess the image
tensorImage.load(bitmap);
tensorImage = imageProcessor.process(tensorImage);

टेंसर के DataType को मेटाडेटा एक्सट्रैक्टर लाइब्रेरी अन्य मॉडल जानकारी.

सामान्य ऑडियो डेटा प्रोसेसिंग

LiteRT की सपोर्ट लाइब्रेरी में, TensorAudio क्लास रैपिंग के बारे में भी बताया गया है ऑडियो डेटा प्रोसेस करने के कुछ बुनियादी तरीकों का इस्तेमाल करना चाहिए. यह ज़्यादातर इसके साथ इस्तेमाल किया जाता है AudioRecord और एक रिंग बफ़र में ऑडियो के नमूने कैप्चर करता है.

import android.media.AudioRecord;
import org.tensorflow.lite.support.audio.TensorAudio;

// Create an `AudioRecord` instance.
AudioRecord record = AudioRecord(...)

// Create a `TensorAudio` object from Android AudioFormat.
TensorAudio tensorAudio = new TensorAudio(record.getFormat(), size)

// Load all audio samples available in the AudioRecord without blocking.
tensorAudio.load(record)

// Get the `TensorBuffer` for inference.
TensorBuffer buffer = tensorAudio.getTensorBuffer()

आउटपुट ऑब्जेक्ट बनाएं और मॉडल को चलाएं

मॉडल चलाने से पहले, हमें ऐसे कंटेनर ऑब्जेक्ट बनाने होंगे जो परिणाम संग्रहित करें:

import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

// Create a container for the result and specify that this is a quantized model.
// Hence, the 'DataType' is defined as UINT8 (8-bit unsigned integer)
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

मॉडल और मौजूदा अनुमान लोड करना:

import java.nio.MappedByteBuffer;
import org.tensorflow.lite.InterpreterFactory;
import org.tensorflow.lite.InterpreterApi;

// Initialise the model
try{
    MappedByteBuffer tfliteModel
        = FileUtil.loadMappedFile(activity,
            "mobilenet_v1_1.0_224_quant.tflite");
    InterpreterApi tflite = new InterpreterFactory().create(
        tfliteModel, new InterpreterApi.Options());
} catch (IOException e){
    Log.e("tfliteSupport", "Error reading model", e);
}

// Running inference
if(null != tflite) {
    tflite.run(tImage.getBuffer(), probabilityBuffer.getBuffer());
}

नतीजे ऐक्सेस करना

डेवलपर इसके ज़रिए सीधे आउटपुट ऐक्सेस कर सकते हैं probabilityBuffer.getFloatArray(). अगर मॉडल संख्या में आउटपुट देता है, तो परिणाम को रूपांतरित करना याद रखें. MobileNet के क्वांटाइज़ किए गए मॉडल के लिए, डेवलपर प्रत्येक आउटपुट मान को 255 से विभाजित करना होगा, ताकि हर कैटगरी के लिए 0 (कम से कम) से 1 (सबसे ज़्यादा संभावना) तक.

ज़रूरी नहीं: नतीजों को लेबल के हिसाब से मैप करना

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

import org.tensorflow.lite.support.common.FileUtil;

final String ASSOCIATED_AXIS_LABELS = "labels.txt";
List<String> associatedAxisLabels = null;

try {
    associatedAxisLabels = FileUtil.loadLabels(this, ASSOCIATED_AXIS_LABELS);
} catch (IOException e) {
    Log.e("tfliteSupport", "Error reading label file", e);
}

नीचे दिया गया स्निपेट बताता है कि प्रायिकताओं को श्रेणी लेबल:

import java.util.Map;
import org.tensorflow.lite.support.common.TensorProcessor;
import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.label.TensorLabel;

// Post-processor which dequantize the result
TensorProcessor probabilityProcessor =
    new TensorProcessor.Builder().add(new NormalizeOp(0, 255)).build();

if (null != associatedAxisLabels) {
    // Map of labels and their corresponding probability
    TensorLabel labels = new TensorLabel(associatedAxisLabels,
        probabilityProcessor.process(probabilityBuffer));

    // Create a map to access the result based on label
    Map<String, Float> floatMap = labels.getMapWithFloatValue();
}

इस्तेमाल के उदाहरण का मौजूदा कवरेज

LiteRT सपोर्ट लाइब्रेरी के मौजूदा वर्शन में ये चीज़ें शामिल हैं:

  • सामान्य डेटा टाइप (float, uint8, इमेज, ऑडियो, और इन ऑब्जेक्ट का अरे) tflite मॉडल के इनपुट और आउटपुट के तौर पर इस्तेमाल किया जा सकता है.
  • बुनियादी इमेज ऑपरेशन (इमेज काटें, उसका साइज़ बदलें, और उसे घुमाएं).
  • नॉर्मलाइज़ेशन और क्वांटाइज़ेशन
  • फ़ाइल यूटिल्स

आने वाले समय में, टेक्स्ट से जुड़े ऐप्लिकेशन बेहतर तरीके से काम कर सकेंगे.

इमेज प्रोसेसर आर्किटेक्चर

ImageProcessor के डिज़ाइन की मदद से, इमेज में बदलाव करने वाले ऑपरेशन को अनुमति मिली उन्हें बिल्ड प्रोसेस के दौरान बेहतरीन तरीके से तय और ऑप्टिमाइज़ किया जाना चाहिए. ImageProcessor फ़िलहाल, तीन बुनियादी प्री-प्रोसेसिंग ऑपरेशन को पूरा करता है, जैसा कि नीचे कोड स्निपेट में तीन टिप्पणियां दी गई हैं:

import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.common.ops.QuantizeOp;
import org.tensorflow.lite.support.image.ops.ResizeOp;
import org.tensorflow.lite.support.image.ops.ResizeWithCropOrPadOp;
import org.tensorflow.lite.support.image.ops.Rot90Op;

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int size = height > width ? width : height;

ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        // Center crop the image to the largest square possible
        .add(new ResizeWithCropOrPadOp(size, size))
        // Resize using Bilinear or Nearest neighbour
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR));
        // Rotation counter-clockwise in 90 degree increments
        .add(new Rot90Op(rotateDegrees / 90))
        .add(new NormalizeOp(127.5, 127.5))
        .add(new QuantizeOp(128.0, 1/128.0))
        .build();

ज़्यादा जानकारी देखें इसके बारे में यहां नॉर्मलाइज़ेशन और क्वांटाइज़ेशन.

सहायता लाइब्रेरी का मुख्य लक्ष्य, सभी लोगों की मदद करना है tf.image बदलाव लाने के लिए किया जा सकता है. इसका मतलब है कि चैनल में किया गया बदलाव, TensorFlow जैसा ही होगा और इन्हें लागू करना ऑपरेटिंग सिस्टम पर निर्भर नहीं होगा.

डेवलपर, कस्टम प्रोसेसर बना सकते हैं. यह ज़रूरी है इन केस को ट्रेनिंग प्रोसेस के साथ अलाइन करना होगा - जैसे कि बढ़ाने के लिए, प्री-प्रोसेसिंग को ट्रेनिंग और अनुमान, दोनों पर लागू किया जाना चाहिए रीप्रॉड्यूसिबिलिटी.

क्वांटाइज़ेशन

TensorImage या TensorBuffer जैसे इनपुट या आउटपुट ऑब्जेक्ट शुरू करते समय आपको उनके प्रकार DataType.UINT8 या DataType.FLOAT32 बनाने होंगे.

TensorImage tensorImage = new TensorImage(DataType.UINT8);
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

TensorProcessor का इस्तेमाल, इनपुट टेंसर का हिसाब लगाने या आउटपुट की वैल्यू कम करने के लिए किया जा सकता है टेंसर. उदाहरण के लिए, किसी संख्यात्मक आउटपुट TensorBuffer को प्रोसेस करते समय, डेवलपर, फ़्लोटिंग पॉइंट के तौर पर नतीजे की संख्या तय करने के लिए, DequantizeOp का इस्तेमाल कर सकता है 0 और 1 के बीच की संभावना:

import org.tensorflow.lite.support.common.TensorProcessor;

// Post-processor which dequantize the result
TensorProcessor probabilityProcessor =
    new TensorProcessor.Builder().add(new DequantizeOp(0, 1/255.0)).build();
TensorBuffer dequantizedBuffer = probabilityProcessor.process(probabilityBuffer);

टेंसर के क्वांटाइज़ेशन पैरामीटर को मेटाडेटा एक्सट्रैक्टर लाइब्रेरी पर क्लिक करें.