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

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

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

สิ่งที่คาดหวังจากไลบรารีงาน

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

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

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

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

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

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

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

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

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

การรองรับการเร่งความเร็วใน 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 ใน ObjectDetecor ได้ดังนี้:

// 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
]

ตัวเลือกอื่นๆ ของผู้รับมอบสิทธิ์ ได้แก่:

"//tensorflow_lite_support/acceleration/configuration:nnapi_plugin", # for NNAPI
"//tensorflow_lite_support/acceleration/configuration:hexagon_plugin", # for Hexagon

ขั้นตอนที่ 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);

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

ตัวอย่างการใช้งาน 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 ขึ้นอยู่กับปลั๊กอินผู้รับมอบสิทธิ์ Coral Edge TPU ในเป้าหมายการสร้าง 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.

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

ตัวอย่างการใช้งาน Core ML Delegate ใน C++

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

ขั้นตอนที่ 1 ขึ้นอยู่กับปลั๊กอินผู้รับมอบสิทธิ์ Core ML ในเป้าหมายการสร้าง bazel ของคุณ เช่น:

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

ขั้นตอนที่ 2 กำหนดค่า Core ML Delegate ในตัวเลือกงาน ตัวอย่างเช่น คุณสามารถตั้งค่า Core ML Delegate ใน 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();