TensorFlow Lite ในบริการ Google Play Java API

TensorFlow Lite ในบริการ Google Play ยังสามารถเข้าถึงได้โดยใช้ Java API นอกเหนือจาก Native API โดยเฉพาะอย่างยิ่ง TensorFlow Lite ในบริการ Google Play มีให้บริการผ่าน TensorFlow Lite Task API และ TensorFlow Lite Interpreter API ไลบรารีงานมอบอินเทอร์เฟซแบบจำลองที่พร้อมใช้งานทันทีที่ได้รับการปรับปรุงสำหรับงานการเรียนรู้ของเครื่องทั่วไปโดยใช้ข้อมูลภาพ เสียง และข้อความ TensorFlow Lite Interpreter API จัดทำโดยรันไทม์ TensorFlow มอบอินเทอร์เฟซที่ใช้งานทั่วไปมากขึ้นสำหรับการสร้างและใช้งานโมเดล ML

ส่วนต่อไปนี้ให้คำแนะนำเกี่ยวกับวิธีใช้ Interpreter และ Task Library API กับ TensorFlow Lite ในบริการ Google Play แม้ว่าจะเป็นไปได้ที่แอปจะใช้ทั้ง Interpreter API และ Task Library API แต่แอปส่วนใหญ่ควรใช้ API เพียงชุดเดียวเท่านั้น

การใช้ Task Library API

TensorFlow Lite Task API ล้อม Interpreter API และมอบอินเทอร์เฟซการเขียนโปรแกรมระดับสูงสำหรับงานแมชชีนเลิร์นนิงทั่วไปที่ใช้ข้อมูลภาพ เสียง และข้อความ คุณควรใช้ Task API หากแอปพลิเคชันของคุณต้องการ งานที่ได้รับการสนับสนุน อย่างใดอย่างหนึ่ง

1. เพิ่มการพึ่งพาโครงการ

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

  • ห้องสมุดวิสัยทัศน์: org.tensorflow:tensorflow-lite-task-vision-play-services
  • ไลบรารีเสียง: org.tensorflow:tensorflow-lite-task-audio-play-services
  • ไลบรารีข้อความ: org.tensorflow:tensorflow-lite-task-text-play-services

เพิ่มการอ้างอิงรายการใดรายการหนึ่งลงในโค้ดโปรเจ็กต์แอปของคุณเพื่อเข้าถึง Play services API สำหรับ TensorFlow Lite ตัวอย่างเช่น ใช้สิ่งต่อไปนี้เพื่อดำเนินงานด้านวิสัยทัศน์:

dependencies {
...
    implementation 'org.tensorflow:tensorflow-lite-task-vision-play-services:0.4.2'
...
}

2. เพิ่มการเริ่มต้นของ TensorFlow Lite

เริ่มต้นองค์ประกอบ TensorFlow Lite ของ Google Play services API ก่อนที่ จะใช้ TensorFlow Lite API ตัวอย่างต่อไปนี้เริ่มต้นไลบรารีการมองเห็น:

คอตลิน

init {
  TfLiteVision.initialize(context)
}

3. เรียกใช้การอนุมาน

หลังจากเริ่มต้นองค์ประกอบ TensorFlow Lite แล้ว ให้เรียกใช้เมธอด detect() เพื่อสร้างการอนุมาน รหัสที่แน่นอนภายใน detect() จะแตกต่างกันไปขึ้นอยู่กับไลบรารีและกรณีการใช้งาน ต่อไปนี้เป็นกรณีการใช้งานการตรวจจับวัตถุอย่างง่ายกับไลบรารี TfLiteVision :

คอตลิน

fun detect(...) {
  if (!TfLiteVision.isInitialized()) {
    Log.e(TAG, "detect: TfLiteVision is not initialized yet")
    return
  }

  if (objectDetector == null) {
    setupObjectDetector()
  }

  ...

}

ขึ้นอยู่กับรูปแบบข้อมูล คุณอาจต้องประมวลผลล่วงหน้าและแปลงข้อมูลของคุณภายในเมธอด detect() ก่อนที่จะสร้างการอนุมาน ตัวอย่างเช่น ข้อมูลรูปภาพสำหรับตัวตรวจจับวัตถุจำเป็นต้องมีสิ่งต่อไปนี้:

val imageProcessor = ImageProcessor.Builder().add(Rot90Op(-imageRotation / 90)).build()
val tensorImage = imageProcessor.process(TensorImage.fromBitmap(image))
val results = objectDetector?.detect(tensorImage)

การใช้ Interpreter API

Interpreter API ให้การควบคุมและความยืดหยุ่นมากกว่า Task Library API คุณควรใช้ Interpreter API หากไลบรารีงานไม่รองรับงาน Machine Learning ของคุณ หรือหากคุณต้องการอินเทอร์เฟซที่ใช้งานทั่วไปมากขึ้นสำหรับการสร้างและใช้งานโมเดล ML

1. เพิ่มการพึ่งพาโครงการ

เพิ่มการอ้างอิงต่อไปนี้ลงในโค้ดโปรเจ็กต์แอปของคุณเพื่อเข้าถึง Play services API สำหรับ TensorFlow Lite:

dependencies {
...
    // Tensorflow Lite dependencies for Google Play services
    implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
    // Optional: include Tensorflow Lite Support Library
    implementation 'com.google.android.gms:play-services-tflite-support:16.0.1'
...
}

2. เพิ่มการเริ่มต้นของ TensorFlow Lite

เริ่มต้นองค์ประกอบ TensorFlow Lite ของ Google Play services API ก่อนที่ จะใช้ TensorFlow Lite API:

คอตลิน

val initializeTask: Task<Void> by lazy { TfLite.initialize(this) }

ชวา

Task<Void> initializeTask = TfLite.initialize(context);

3. สร้างล่ามและตั้งค่าตัวเลือกรันไทม์

สร้างล่ามโดยใช้ InterpreterApi.create() และกำหนดค่าให้ใช้รันไทม์ของบริการ Google Play โดยการเรียก InterpreterApi.Options.setRuntime() ดังที่แสดงในโค้ดตัวอย่างต่อไปนี้:

คอตลิน

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private lateinit var interpreter: InterpreterApi
...
initializeTask.addOnSuccessListener {
  val interpreterOption =
    InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  interpreter = InterpreterApi.create(
    modelBuffer,
    interpreterOption
  )}
  .addOnFailureListener { e ->
    Log.e("Interpreter", "Cannot initialize interpreter", e)
  }

ชวา

import org.tensorflow.lite.InterpreterApi
import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime
...
private InterpreterApi interpreter;
...
initializeTask.addOnSuccessListener(a -> {
    interpreter = InterpreterApi.create(modelBuffer,
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY));
  })
  .addOnFailureListener(e -> {
    Log.e("Interpreter", String.format("Cannot initialize interpreter: %s",
          e.getMessage()));
  });

คุณควรใช้การดำเนินการข้างต้นเนื่องจากจะหลีกเลี่ยงการบล็อกเธรดอินเทอร์เฟซผู้ใช้ Android หากคุณต้องการจัดการการดำเนินการเธรดอย่างใกล้ชิดยิ่งขึ้น คุณสามารถเพิ่มการเรียก Tasks.await() เพื่อสร้างล่ามได้:

คอตลิน

import androidx.lifecycle.lifecycleScope
...
lifecycleScope.launchWhenStarted { // uses coroutine
  initializeTask.await()
}

ชวา

@BackgroundThread
InterpreterApi initializeInterpreter() {
    Tasks.await(initializeTask);
    return InterpreterApi.create(...);
}

4. เรียกใช้การอนุมาน

ใช้ interpreter ล่ามที่คุณสร้างขึ้น เรียกใช้เมธอด run() เพื่อสร้างการอนุมาน

คอตลิน

interpreter.run(inputBuffer, outputBuffer)

ชวา

interpreter.run(inputBuffer, outputBuffer);

การเร่งความเร็วด้วยฮาร์ดแวร์

TensorFlow Lite ช่วยให้คุณเร่งความเร็วประสิทธิภาพของโมเดลได้โดยใช้โปรเซสเซอร์ฮาร์ดแวร์พิเศษ เช่น หน่วยประมวลผลกราฟิก (GPU) คุณสามารถใช้ประโยชน์จากโปรเซสเซอร์พิเศษเหล่านี้ได้โดยใช้ไดรเวอร์ฮาร์ดแวร์ที่เรียกว่า ผู้รับมอบสิทธิ์ คุณสามารถใช้ผู้รับมอบสิทธิ์การเร่งความเร็วด้วยฮาร์ดแวร์ต่อไปนี้กับ TensorFlow Lite ในบริการ Google Play:

  • ผู้รับมอบสิทธิ์ GPU (แนะนำ) - ผู้รับมอบสิทธิ์นี้ให้บริการผ่านบริการ Google Play และโหลดแบบไดนามิก เช่นเดียวกับเวอร์ชันบริการ Play ของ Task API และ Interpreter API

  • ผู้รับมอบสิทธิ์ NNAPI - ผู้รับมอบสิทธิ์นี้พร้อมใช้งานในรูปแบบการพึ่งพาไลบรารีที่รวมอยู่ในโครงการพัฒนา Android ของคุณ และรวมอยู่ในแอปของคุณ

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการเร่งด้วยฮาร์ดแวร์ด้วย TensorFlow Lite โปรดดูที่หน้า ผู้ร่วมประชุม TensorFlow Lite

กำลังตรวจสอบความเข้ากันได้ของอุปกรณ์

อุปกรณ์บางชนิดไม่รองรับการเร่งด้วยฮาร์ดแวร์ GPU ด้วย TFLite เพื่อลดข้อผิดพลาดและการหยุดทำงานที่อาจเกิดขึ้น ให้ใช้วิธีการ TfLiteGpu.isGpuDelegateAvailable เพื่อตรวจสอบว่าอุปกรณ์เข้ากันได้กับผู้รับมอบสิทธิ์ GPU หรือไม่

ใช้วิธีนี้เพื่อยืนยันว่าอุปกรณ์เข้ากันได้กับ GPU หรือไม่ และใช้ CPU หรือตัวแทน NNAPI เป็นทางเลือกเมื่อไม่รองรับ GPU

useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

เมื่อคุณมีตัวแปรเช่น useGpuTask แล้ว คุณสามารถใช้ตัวแปรนั้นเพื่อพิจารณาว่าอุปกรณ์ใช้ตัวแทน GPU หรือไม่ ตัวอย่างต่อไปนี้แสดงวิธีการดำเนินการนี้กับทั้ง Task Library และ Interpreter API

ด้วย Task Api

คอตลิน

lateinit val optionsTask = useGpuTask.continueWith { task ->
  val baseOptionsBuilder = BaseOptions.builder()
  if (task.result) {
    baseOptionsBuilder.useGpu()
  }
 ObjectDetectorOptions.builder()
          .setBaseOptions(baseOptionsBuilder.build())
          .setMaxResults(1)
          .build()
}
    

ชวา

Task<ObjectDetectorOptions> optionsTask = useGpuTask.continueWith({ task ->
  BaseOptions baseOptionsBuilder = BaseOptions.builder();
  if (task.getResult()) {
    baseOptionsBuilder.useGpu();
  }
  return ObjectDetectorOptions.builder()
          .setBaseOptions(baseOptionsBuilder.build())
          .setMaxResults(1)
          .build()
});
    

ด้วยล่าม Api

คอตลิน

val interpreterTask = useGpuTask.continueWith { task ->
  val interpreterOptions = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
  if (task.result) {
      interpreterOptions.addDelegateFactory(GpuDelegateFactory())
  }
  InterpreterApi.create(FileUtil.loadMappedFile(context, MODEL_PATH), interpreterOptions)
}
    

ชวา

Task<InterpreterApi.Options> interpreterOptionsTask = useGpuTask.continueWith({ task ->
  InterpreterApi.Options options =
      new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY);
  if (task.getResult()) {
     options.addDelegateFactory(new GpuDelegateFactory());
  }
  return options;
});
    

GPU พร้อม Task Library API

วิธีใช้ตัวแทน GPU กับ Task API:

  1. อัปเดตการพึ่งพาโปรเจ็กต์เพื่อใช้ตัวแทน GPU จากบริการ Play:

    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
    
  2. เริ่มต้นผู้รับมอบสิทธิ์ GPU ด้วย setEnableGpuDelegateSupport ตัวอย่างเช่น คุณสามารถเริ่มต้นการมอบหมาย GPU สำหรับ TfLiteVision ได้ดังต่อไปนี้:

    คอตลิน

        TfLiteVision.initialize(context, TfLiteInitializationOptions.builder().setEnableGpuDelegateSupport(true).build())
        

    ชวา

        TfLiteVision.initialize(context, TfLiteInitializationOptions.builder().setEnableGpuDelegateSupport(true).build());
        
  3. เปิดใช้งานตัวเลือกผู้รับมอบสิทธิ์ GPU ด้วย BaseOptions :

    คอตลิน

        val baseOptions = BaseOptions.builder().useGpu().build()
        

    ชวา

        BaseOptions baseOptions = BaseOptions.builder().useGpu().build();
        
  4. กำหนดค่าตัวเลือกโดยใช้ .setBaseOptions ตัวอย่างเช่น คุณสามารถตั้งค่า GPU ใน ObjectDetector ได้ดังต่อไปนี้:

    คอตลิน

        val options =
            ObjectDetectorOptions.builder()
                .setBaseOptions(baseOptions)
                .setMaxResults(1)
                .build()
        

    ชวา

        ObjectDetectorOptions options =
            ObjectDetectorOptions.builder()
                .setBaseOptions(baseOptions)
                .setMaxResults(1)
                .build();
        

GPU พร้อม Interpreter API

หากต้องการใช้ตัวแทน GPU กับ Interpreter API:

  1. อัปเดตการพึ่งพาโปรเจ็กต์เพื่อใช้ตัวแทน GPU จากบริการ Play:

    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
    
  2. เปิดใช้งานตัวเลือกผู้รับมอบสิทธิ์ GPU ในการเริ่มต้น TFlite:

    คอตลิน

        TfLite.initialize(context,
          TfLiteInitializationOptions.builder()
           .setEnableGpuDelegateSupport(true)
           .build())
        

    ชวา

        TfLite.initialize(context,
          TfLiteInitializationOptions.builder()
           .setEnableGpuDelegateSupport(true)
           .build());
        
  3. เปิดใช้งานการมอบหมาย GPU ในตัวเลือกล่าม: ตั้งค่าโรงงานการมอบหมายเป็น GpuDelegateFactory โดยการเรียก addDelegateFactory() within InterpreterApi.Options()`:

    คอตลิน

        val interpreterOption = InterpreterApi.Options()
         .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
         .addDelegateFactory(GpuDelegateFactory())
        

    ชวา

        Options interpreterOption = InterpreterApi.Options()
          .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
          .addDelegateFactory(new GpuDelegateFactory());
        

การย้ายข้อมูลจาก TensorFlow Lite แบบสแตนด์อโลน

หากคุณวางแผนที่จะย้ายแอปของคุณจาก TensorFlow Lite แบบสแตนด์อโลนไปยัง Play services API โปรดอ่านคำแนะนำเพิ่มเติมต่อไปนี้สำหรับการอัปเดตโค้ดโปรเจ็กต์แอปของคุณ:

  1. ตรวจสอบส่วน ข้อจำกัด ของหน้านี้เพื่อให้แน่ใจว่ากรณีการใช้งานของคุณได้รับการสนับสนุน
  2. ก่อนที่จะอัปเดตโค้ด ให้ตรวจสอบประสิทธิภาพและความแม่นยำสำหรับโมเดลของคุณ โดยเฉพาะอย่างยิ่งหากคุณใช้ TensorFlow Lite เวอร์ชันเก่ากว่าเวอร์ชัน 2.1 เพื่อให้คุณมีข้อมูลพื้นฐานในการเปรียบเทียบกับการใช้งานใหม่
  3. หากคุณได้ย้ายโค้ดทั้งหมดของคุณไปใช้ Play services API สำหรับ TensorFlow Lite คุณควรลบการพึ่งพา ไลบรารีรันไทม์ TensorFlow Lite ที่มีอยู่ (รายการที่มี org.tensorflow: tensorflow-lite :* ) ออกจากไฟล์ build.gradle ของคุณเพื่อให้คุณ สามารถลดขนาดแอปของคุณได้
  4. ระบุเหตุการณ์ทั้งหมดของการสร้างอ็อบเจ็กต์ new Interpreter ในโค้ดของคุณ และแก้ไขแต่ละรายการเพื่อใช้การเรียก InterpreterApi.create() TfLite.initialize ใหม่เป็นแบบอะซิงโครนัส ซึ่งหมายความว่าในกรณีส่วนใหญ่ จะไม่ใช่การแทนที่แบบดรอปอิน: คุณต้องลงทะเบียน Listener เมื่อการโทรเสร็จสิ้น โปรดดูข้อมูลโค้ดในโค้ด ขั้นตอนที่ 3
  5. เพิ่ม import org.tensorflow.lite.InterpreterApi; และ import org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime; ไปยังไฟล์ต้นฉบับใด ๆ ที่ใช้คลาส org.tensorflow.lite.Interpreter หรือ org.tensorflow.lite.InterpreterApi
  6. หากผลลัพธ์การเรียก InterpreterApi.create() มีเพียงอาร์กิวเมนต์เดียว ให้เพิ่ม new InterpreterApi.Options() ต่อท้ายรายการอาร์กิวเมนต์
  7. ผนวก .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY) ต่อท้ายอาร์กิวเมนต์ของการเรียก InterpreterApi.create()
  8. แทนที่รายการอื่นๆ ทั้งหมดของคลาส org.tensorflow.lite.Interpreter ด้วย org.tensorflow.lite.InterpreterApi

หากคุณต้องการใช้ TensorFlow Lite แบบสแตนด์อโลนและ Play services API เคียงข้างกัน คุณต้องใช้ TensorFlow Lite 2.9 (หรือใหม่กว่า) TensorFlow Lite 2.8 และเวอร์ชันก่อนหน้าเข้ากันไม่ได้กับเวอร์ชัน Play services API