ย้ายข้อมูลจาก API รูปแบบที่กำหนดเองเดิม

ไลบรารี firebase-ml-model-interpreter เวอร์ชัน 22.0.2 เปิดตัววิธี getLatestModelFile() ใหม่ ซึ่งจะรับตําแหน่งในอุปกรณ์ของโมเดลที่กําหนดเอง คุณสามารถใช้เมธอดนี้เพื่อสร้างอินสแตนซ์ของออบเจ็กต์ TensorFlow LiteInterpreter โดยตรง ซึ่งจะใช้แทน WrapperFirebaseModelInterpreter ได้

นับจากนี้ไป เราขอแนะนำให้ใช้แนวทางนี้ เนื่องจากเวอร์ชันโปรแกรมล่าม TensorFlow Lite ไม่ได้เชื่อมโยงกับเวอร์ชันไลบรารี Firebase อีกต่อไป คุณจึงมีความยืดหยุ่นมากขึ้นในการอัปเกรดเป็น TensorFlow Lite เวอร์ชันใหม่เมื่อต้องการ หรือใช้บิลด์ TensorFlow Lite ที่กําหนดเองได้ง่ายขึ้น

หน้านี้จะแสดงวิธีย้ายข้อมูลจากการใช้ FirebaseModelInterpreter ไปใช้ TensorFlow Lite Interpreter

1. อัปเดตทรัพยากร Dependency ของโปรเจ็กต์

อัปเดตทรัพยากร Dependency ของโปรเจ็กต์ให้รวมไลบรารี firebase-ml-model-interpreter เวอร์ชัน 22.0.2 (หรือใหม่กว่า) และไลบรารี tensorflow-lite ดังนี้

ก่อน

implementation("com.google.firebase:firebase-ml-model-interpreter:22.0.1")

หลัง

implementation("com.google.firebase:firebase-ml-model-interpreter:22.0.2")
implementation("org.tensorflow:tensorflow-lite:2.0.0")

2. สร้างตัวแ interpreter ของ TensorFlow Lite แทน FirebaseModelInterpreter

แทนที่จะสร้าง FirebaseModelInterpreter ให้รับตําแหน่งของโมเดลในอุปกรณ์ด้วย getLatestModelFile() และใช้เพื่อสร้าง TensorFlow Lite Interpreter

ก่อน

KotlinJava
val remoteModel = FirebaseCustomRemoteModel.Builder("your_model").build()
val options = FirebaseModelInterpreterOptions.Builder(remoteModel).build()
val interpreter = FirebaseModelInterpreter.getInstance(options)
FirebaseCustomRemoteModel remoteModel =
        new FirebaseCustomRemoteModel.Builder("your_model").build();
FirebaseModelInterpreterOptions options =
        new FirebaseModelInterpreterOptions.Builder(remoteModel).build();
FirebaseModelInterpreter interpreter = FirebaseModelInterpreter.getInstance(options);

หลัง

KotlinJava
val remoteModel = FirebaseCustomRemoteModel.Builder("your_model").build()
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
    .addOnCompleteListener { task ->
        val modelFile = task.getResult()
        if (modelFile != null) {
            // Instantiate an org.tensorflow.lite.Interpreter object.
            interpreter = Interpreter(modelFile)
        }
    }
FirebaseCustomRemoteModel remoteModel =
        new FirebaseCustomRemoteModel.Builder("your_model").build();
FirebaseModelManager.getInstance().getLatestModelFile(remoteModel)
        .addOnCompleteListener(new OnCompleteListener<File>() {
            @Override
            public void onComplete(@NonNull Task<File> task) {
                File modelFile = task.getResult();
                if (modelFile != null) {
                    // Instantiate an org.tensorflow.lite.Interpreter object.
                    Interpreter interpreter = new Interpreter(modelFile);
                }
            }
        });

3. อัปเดตโค้ดการเตรียมอินพุตและเอาต์พุต

เมื่อใช้ FirebaseModelInterpreter คุณจะระบุรูปร่างอินพุตและเอาต์พุตของโมเดลได้ด้วยการส่งออบเจ็กต์ FirebaseModelInputOutputOptions ไปยังโปรแกรมแปลเมื่อเรียกใช้

สําหรับโปรแกรมตีความ TensorFlow Lite คุณจะจัดสรรออบเจ็กต์ ByteBuffer ที่มีขนาดเหมาะสมสําหรับอินพุตและเอาต์พุตของโมเดลแทน

เช่น หากโมเดลของคุณมีรูปร่างอินพุตเป็นค่า float [1 224 224 3] และรูปร่างเอาต์พุตเป็นค่า float [1 1000] ให้ทําการเปลี่ยนแปลงต่อไปนี้

ก่อน

KotlinJava
val inputOutputOptions = FirebaseModelInputOutputOptions.Builder()
    .setInputFormat(0, FirebaseModelDataType.FLOAT32, intArrayOf(1, 224, 224, 3))
    .setOutputFormat(0, FirebaseModelDataType.FLOAT32, intArrayOf(1, 1000))
    .build()

val input = ByteBuffer.allocateDirect(224*224*3*4).order(ByteOrder.nativeOrder())
// Then populate with input data.

val inputs = FirebaseModelInputs.Builder()
    .add(input)
    .build()

interpreter.run(inputs, inputOutputOptions)
    .addOnSuccessListener { outputs ->
        // ...
    }
    .addOnFailureListener {
        // Task failed with an exception.
        // ...
    }
FirebaseModelInputOutputOptions inputOutputOptions =
        new FirebaseModelInputOutputOptions.Builder()
                .setInputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 224, 224, 3})
                .setOutputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 1000})
                .build();

float[][][][] input = new float[1][224][224][3];
// Then populate with input data.

FirebaseModelInputs inputs = new FirebaseModelInputs.Builder()
        .add(input)
        .build();

interpreter.run(inputs, inputOutputOptions)
        .addOnSuccessListener(
                new OnSuccessListener<FirebaseModelOutputs>() {
                    @Override
                    public void onSuccess(FirebaseModelOutputs result) {
                        // ...
                    }
                })
        .addOnFailureListener(
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        // Task failed with an exception
                        // ...
                    }
                });

หลัง

KotlinJava
val inBufferSize = 1 * 224 * 224 * 3 * java.lang.Float.SIZE / java.lang.Byte.SIZE
val inputBuffer = ByteBuffer.allocateDirect(inBufferSize).order(ByteOrder.nativeOrder())
// Then populate with input data.

val outBufferSize = 1 * 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE
val outputBuffer = ByteBuffer.allocateDirect(outBufferSize).order(ByteOrder.nativeOrder())

interpreter.run(inputBuffer, outputBuffer)
int inBufferSize = 1 * 224 * 224 * 3 * java.lang.Float.SIZE / java.lang.Byte.SIZE;
ByteBuffer inputBuffer =
        ByteBuffer.allocateDirect(inBufferSize).order(ByteOrder.nativeOrder());
// Then populate with input data.

int outBufferSize = 1 * 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE;
ByteBuffer outputBuffer =
        ByteBuffer.allocateDirect(outBufferSize).order(ByteOrder.nativeOrder());

interpreter.run(inputBuffer, outputBuffer);

4. อัปเดตโค้ดการจัดการเอาต์พุต

สุดท้าย ให้แปลงเอาต์พุต ByteBuffer ให้เป็นโครงสร้างที่สะดวกสําหรับกรณีการใช้งานแทนการเรียกใช้เอาต์พุตของโมเดลด้วยเมธอด getOutput() ของออบเจ็กต์ FirebaseModelOutputs

เช่น หากทําการจัดประเภท คุณอาจทําการเปลี่ยนแปลงต่อไปนี้

ก่อน

KotlinJava
val output = result.getOutput(0)
val probabilities = output[0]
try {
    val reader = BufferedReader(InputStreamReader(assets.open("custom_labels.txt")))
    for (probability in probabilities) {
        val label: String = reader.readLine()
        println("$label: $probability")
    }
} catch (e: IOException) {
    // File not found?
}
float[][] output = result.getOutput(0);
float[] probabilities = output[0];
try {
    BufferedReader reader = new BufferedReader(
          new InputStreamReader(getAssets().open("custom_labels.txt")));
    for (float probability : probabilities) {
        String label = reader.readLine();
        Log.i(TAG, String.format("%s: %1.4f", label, probability));
    }
} catch (IOException e) {
    // File not found?
}

หลัง

KotlinJava
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?
}
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?
}