ประมวลผลข้อมูลอินพุตและเอาต์พุตด้วย TensorFlow Lite Support Library

นักพัฒนาแอปพลิเคชันบนมือถือมักจะโต้ตอบกับวัตถุที่พิมพ์ เช่น บิตแมปหรือค่าพื้นฐาน เช่น จำนวนเต็ม อย่างไรก็ตาม API ล่าม TensorFlow Lite ที่รันโมเดลการเรียนรู้ของเครื่องบนอุปกรณ์นั้นใช้เทนเซอร์ในรูปแบบของ ByteBuffer ซึ่งอาจเป็นการดีบักและจัดการได้ยาก TensorFlow Lite Android Support Library ได้รับการออกแบบมาเพื่อช่วยประมวลผลอินพุตและเอาต์พุตของรุ่น TensorFlow Lite และทำให้ล่าม TensorFlow Lite ใช้งานง่ายขึ้น

เริ่มต้น

นำเข้าการพึ่งพา Gradle และการตั้งค่าอื่นๆ

คัดลอกไฟล์โมเดล .tflite ไปยังไดเร็กทอรีทรัพย์สินของโมดูล Android ที่จะเรียกใช้โมเดล ระบุว่าไม่ควรบีบอัดไฟล์ และเพิ่มไลบรารี TensorFlow Lite ลงในไฟล์ build.gradle ของโมดูล:

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 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly-SNAPSHOT'
    implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly-SNAPSHOT'
}

สำรวจ ไลบรารีสนับสนุน TensorFlow Lite AAR ที่โฮสต์ที่ MavenCentral สำหรับเวอร์ชันต่างๆ ของไลบรารีสนับสนุน

การปรับแต่งและแปลงภาพเบื้องต้น

ไลบรารีสนับสนุน TensorFlow Lite มีชุดวิธีการปรับแต่งภาพพื้นฐาน เช่น การครอบตัดและการปรับขนาด หากต้องการใช้งาน ให้สร้าง ImagePreprocessor และเพิ่มการดำเนินการที่จำเป็น ในการแปลงรูปภาพให้อยู่ในรูปแบบเทนเซอร์ที่กำหนดโดยล่าม TensorFlow Lite ให้สร้าง 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 TensorFlow Lite interpreter needs.
TensorImage tensorImage = new TensorImage(DataType.UINT8);

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

DataType ของเทนเซอร์สามารถอ่านได้ผ่าน ไลบรารีตัวแยกข้อมูลเมตา รวมถึงข้อมูลโมเดลอื่นๆ

การประมวลผลข้อมูลเสียงพื้นฐาน

ไลบรารีสนับสนุน TensorFlow Lite ยังกำหนดคลาส 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()

สร้างวัตถุเอาต์พุตและเรียกใช้ model

ก่อนรันโมเดล เราต้องสร้างคอนเทนเนอร์อ็อบเจ็กต์ที่จะเก็บผลลัพธ์:

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();
}

ความครอบคลุมกรณีการใช้งานปัจจุบัน

เวอร์ชันปัจจุบันของไลบรารีสนับสนุน TensorFlow Lite ครอบคลุม:

  • ชนิดข้อมูลทั่วไป (float, uint8, รูปภาพ, เสียง และอาร์เรย์ของออบเจกต์เหล่านี้) เป็นอินพุตและเอาต์พุตของโมเดล tflite
  • การทำงานของรูปภาพพื้นฐาน (ครอบตัดรูปภาพ ปรับขนาด และหมุน)
  • การทำให้เป็นมาตรฐานและการหาปริมาณ
  • ไฟล์ utils

เวอร์ชันในอนาคตจะปรับปรุงการรองรับแอปพลิเคชันที่เกี่ยวข้องกับข้อความ

สถาปัตยกรรมตัวประมวลผลภาพ

การออกแบบ 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);

พารามิเตอร์การหาปริมาณของเทนเซอร์สามารถอ่านได้ผ่าน ไลบรารีตัวแยกข้อมูลเมตา