ไลบรารีงาน TensorFlow Lite

Task Library ของ TensorFlow Lite มีชุดงานที่มีประสิทธิภาพและใช้งานง่าย ไลบรารีเฉพาะงานสำหรับนักพัฒนาแอปเพื่อสร้างประสบการณ์ ML ด้วย TFLite มอบอินเทอร์เฟซโมเดลที่พร้อมใช้งานทันทีซึ่งได้รับการเพิ่มประสิทธิภาพเพื่อแมชชีนเลิร์นนิงยอดนิยม เช่น การจัดประเภทรูปภาพ คำถามและคำตอบ เป็นต้น อินเทอร์เฟซออกแบบมาโดยเฉพาะสำหรับแต่ละงาน เพื่อให้ได้ ประสิทธิภาพและความสามารถในการใช้งาน ไลบรารีงานทำงานข้ามแพลตฟอร์มและได้รับการรองรับใน Java, C++ และ Swift

สิ่งที่จะได้รับจากไลบรารีงาน

  • API ที่สะอาดตาและกำหนดไว้เป็นอย่างดีซึ่งผู้เชี่ยวชาญที่ไม่ใช่ ML ใช้ได้
    การอนุมานสามารถทำได้ภายในโค้ดเพียง 5 บรรทัด ใช้กลยุทธ์และ API ที่ใช้งานง่ายในไลบรารีงานเป็นองค์ประกอบที่ใช้สร้างสรรค์เพื่อช่วยให้คุณ พัฒนา ML ด้วย TFLite บนอุปกรณ์เคลื่อนที่

  • การประมวลผลข้อมูลที่มีความซับซ้อนแต่เป็นที่นิยม
    รองรับการมองเห็นทั่วไปและตรรกะการประมวลผลภาษาธรรมชาติเพื่อแปลง ระหว่างข้อมูลของคุณและรูปแบบข้อมูลที่โมเดลต้องการ มี ตรรกะการประมวลผลเดียวกันที่แชร์ได้สำหรับการฝึกและการอนุมาน

  • ประสิทธิภาพที่เพิ่มขึ้นในระดับสูง
    การประมวลผลข้อมูลจะใช้เวลาไม่เกิน 2-3 มิลลิวินาที ประสบการณ์การอนุมานที่รวดเร็วโดยใช้ TensorFlow Lite

  • ความสามารถในการขยายและการปรับแต่ง
    คุณสามารถใช้ประโยชน์จากประโยชน์ทั้งหมดที่โครงสร้างพื้นฐานของไลบรารีงานมีให้ และ สร้าง API การอนุมาน Android/iOS ของคุณเองได้ง่ายๆ

งานที่รองรับ

ด้านล่างนี้คือรายการประเภทงานที่รองรับ ตัวเลขนี้คาดว่าจะเพิ่มขึ้นตาม เรายังคงเปิดใช้กรณีการใช้งานมากขึ้นเรื่อยๆ

เรียกใช้ไลบรารีงานด้วยผู้รับมอบสิทธิ์

ผู้รับมอบสิทธิ์เปิดใช้การเร่งฮาร์ดแวร์ของ โมเดล TensorFlow Lite โดยการใช้ประโยชน์จาก Accelerator ในอุปกรณ์ เช่น GPU และ Coral Edge TPU การใช้งาน สำหรับการปฏิบัติงานของเครือข่ายระบบประสาทเทียม จะให้ประโยชน์อย่างมากในแง่ของเวลาในการตอบสนอง และประสิทธิภาพกำลังไฟ ตัวอย่างเช่น GPU อาจได้สูงสุดถึง 5 เท่า เร่งความเร็ว เวลาในการตอบสนองบนอุปกรณ์เคลื่อนที่ และการอนุมาน TPU แบบ Coral Edge 10 เท่า เร็วกว่า CPU บนเดสก์ท็อป

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

การรองรับการเร่งความเร็วใน Task Swift / Web API จะพร้อมใช้งานเร็วๆ นี้

ตัวอย่างการใช้ GPU บน Android ใน Java

ขั้นตอนที่ 1 เพิ่มไลบรารีปลั๊กอินที่มอบสิทธิ์ GPU ลงใน build.gradle ของโมดูล ไฟล์:

dependencies {
    // Import Task Library dependency for vision, text, or audio.

    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

ขั้นตอนที่ 2 กำหนดค่าการมอบสิทธิ์ GPU ในตัวเลือกงานผ่าน BaseOptions. ตัวอย่างเช่น คุณสามารถตั้งค่า GPU ใน ObjectDetector ดังนี้

// Turn on GPU delegation.
BaseOptions baseOptions = BaseOptions.builder().useGpu().build();
// Configure other options in ObjectDetector
ObjectDetectorOptions options =
    ObjectDetectorOptions.builder()
        .setBaseOptions(baseOptions)
        .setMaxResults(1)
        .build();

// Create ObjectDetector from options.
ObjectDetector objectDetector =
    ObjectDetector.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<Detection> results = objectDetector.detect(image);

ตัวอย่างการใช้ GPU บน Android ใน C++

ขั้นตอนที่ 1 ขึ้นอยู่กับปลั๊กอินการมอบสิทธิ์ GPU ในเป้าหมายบิลด์ของ Bazel เช่น

deps = [
  "//tensorflow_lite_support/acceleration/configuration:gpu_plugin", # for GPU
]

ขั้นตอนที่ 2 กำหนดค่าการมอบสิทธิ์ GPU ในตัวเลือกงาน เช่น คุณสามารถ GPU ใน BertQuestionAnswerer ดังนี้

// Initialization
BertQuestionAnswererOptions options;
// Load the TFLite model.
auto base_options = options.mutable_base_options();
base_options->mutable_model_file()->set_file_name(model_file);
// Turn on GPU delegation.
auto tflite_settings = base_options->mutable_compute_settings()->mutable_tflite_settings();
tflite_settings->set_delegate(Delegate::GPU);
// (optional) Turn on automatical fallback to TFLite CPU path on delegation errors.
tflite_settings->mutable_fallback_settings()->set_allow_automatic_fallback_on_execution_error(true);

// Create QuestionAnswerer from options.
std::unique_ptr<QuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();

// Run inference on GPU.
std::vector<QaAnswer> results = answerer->Answer(context_of_question, question_to_ask);

สำรวจการตั้งค่า Accelerator ขั้นสูงเพิ่มเติม ที่นี่

ตัวอย่างการใช้ Coral Edge TPU ใน Python

กำหนดค่า Coral Edge TPU ในตัวเลือกฐานของงาน ตัวอย่างเช่น คุณสามารถ ตั้งค่า Coral Edge TPU ใน ImageClassifier ดังนี้

# Imports
from tflite_support.task import vision
from tflite_support.task import core

# Initialize options and turn on Coral Edge TPU delegation.
base_options = core.BaseOptions(file_name=model_path, use_coral=True)
options = vision.ImageClassifierOptions(base_options=base_options)

# Create ImageClassifier from options.
classifier = vision.ImageClassifier.create_from_options(options)

# Run inference on Coral Edge TPU.
image = vision.TensorImage.create_from_file(image_path)
classification_result = classifier.classify(image)

ตัวอย่างการใช้ Coral Edge TPU ใน C++

ขั้นตอนที่ 1 พึ่งพาปลั๊กอินการมอบสิทธิ์ TPU แบบ Coral Edge ในเป้าหมายบิลด์ bazel เช่น

deps = [
  "//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin", # for Coral Edge TPU
]

ขั้นตอนที่ 2 กำหนดค่า Coral Edge TPU ในตัวเลือกงาน ตัวอย่างเช่น คุณสามารถตั้ง เพิ่ม Coral Edge TPU ใน ImageClassifier ดังนี้

// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Coral Edge TPU delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(Delegate::EDGETPU_CORAL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();

// Run inference on Coral Edge TPU.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();

ขั้นตอนที่ 3 ติดตั้งแพ็กเกจ libusb-1.0-0-dev ตามรายการด้านล่าง หากมีอยู่แล้ว ติดตั้งแล้ว ให้ข้ามไปยังขั้นตอนถัดไป

# On the Linux
sudo apt-get install libusb-1.0-0-dev

# On the macOS
port install libusb
# or
brew install libusb

ขั้นตอนที่ 4 คอมไพล์ด้วยการกำหนดค่าต่อไปนี้ในคำสั่ง bazel

# On the Linux
--define darwinn_portable=1 --linkopt=-lusb-1.0

# On the macOS, add '--linkopt=-lusb-1.0 --linkopt=-L/opt/local/lib/' if you are
# using MacPorts or '--linkopt=-lusb-1.0 --linkopt=-L/opt/homebrew/lib' if you
# are using Homebrew.
--define darwinn_portable=1 --linkopt=-L/opt/local/lib/ --linkopt=-lusb-1.0

# Windows is not supported yet.

ลองใช้ การสาธิต CLI ในคลังงาน เครื่องมือ กับอุปกรณ์ Coral Edge TPU สำรวจเพิ่มเติมเกี่ยวกับ EDGE TPU ที่ฝึกไว้แล้วล่วงหน้า รุ่นและ Advanced Edge TPU การตั้งค่า

ตัวอย่างการใช้การมอบสิทธิ์ Core ML ใน C++

ดูตัวอย่างที่สมบูรณ์ได้ที่ Image Classifier Core ML Delegate ทดสอบ

ขั้นตอนที่ 1 ใช้ปลั๊กอินมอบสิทธิ์ Core ML ในเป้าหมายบิลด์ของ bazel เช่น เป็น:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:coreml_plugin", # for Core ML Delegate
]

ขั้นตอนที่ 2 กำหนดค่า Core ML Delegate ในตัวเลือกงาน ตัวอย่างเช่น คุณสามารถตั้ง ปรับการมอบสิทธิ์ Core ML ใน ImageClassifier ดังนี้

// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Core ML delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(::tflite::proto::Delegate::CORE_ML);
// Set DEVICES_ALL to enable Core ML delegation on any device (in contrast to
// DEVICES_WITH_NEURAL_ENGINE which creates Core ML delegate only on devices
// with Apple Neural Engine).
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->mutable_coreml_settings()->set_enabled_devices(::tflite::proto::CoreMLSettings::DEVICES_ALL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();

// Run inference on Core ML.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();