TensorFlow Lite Task Library ประกอบด้วยชุดของไลบรารีเฉพาะงานที่มีประสิทธิภาพและใช้งานง่ายสำหรับนักพัฒนาแอปเพื่อสร้างประสบการณ์ ML ด้วย TFLite มีอินเทอร์เฟซโมเดลสำเร็จรูปที่ปรับให้เหมาะสมสำหรับงานแมชชีนเลิร์นนิงยอดนิยม เช่น การจัดประเภทรูปภาพ คำถามและคำตอบ เป็นต้น อินเทอร์เฟซโมเดลได้รับการออกแบบมาโดยเฉพาะสำหรับแต่ละงานเพื่อให้ได้ประสิทธิภาพและการใช้งานที่ดีที่สุด Task Library ทำงานข้ามแพลตฟอร์มและรองรับ Java, C++ และ Swift
สิ่งที่คาดหวังจากไลบรารีงาน
API ที่ชัดเจนและชัดเจนใช้งานได้โดยผู้ที่ไม่ใช่ผู้เชี่ยวชาญด้าน ML
การอนุมานสามารถทำได้ภายในโค้ดเพียง 5 บรรทัด ใช้ API ที่มีประสิทธิภาพและใช้งานง่ายในไลบรารีงานเป็นหน่วยการสร้างเพื่อช่วยให้คุณพัฒนา ML ด้วย TFLite บนอุปกรณ์พกพาได้อย่างง่ายดายการประมวลผลข้อมูลที่ซับซ้อนแต่ใช้ร่วมกัน
รองรับการมองเห็นทั่วไปและตรรกะการประมวลผลภาษาธรรมชาติเพื่อแปลงระหว่างข้อมูลของคุณและรูปแบบข้อมูลที่โมเดลต้องการ ให้ตรรกะการประมวลผลที่ใช้ร่วมกันได้เหมือนกันสำหรับการฝึกอบรมและการอนุมานได้รับประสิทธิภาพสูง
การประมวลผลข้อมูลจะใช้เวลาไม่เกินสองสามมิลลิวินาที ทำให้มั่นใจได้ถึงประสบการณ์การอนุมานที่รวดเร็วโดยใช้ TensorFlow Liteความสามารถในการขยายและการปรับแต่ง
คุณสามารถใช้ประโยชน์จากโครงสร้างพื้นฐานของ Task Library และสร้าง API การอนุมาน Android/iOS ของคุณเองได้อย่างง่ายดาย
งานที่รองรับ
ด้านล่างนี้คือรายการประเภทงานที่รองรับ รายการคาดว่าจะเพิ่มขึ้นในขณะที่เรายังคงเปิดใช้กรณีการใช้งานมากขึ้นเรื่อยๆ
Vision API
API ภาษาธรรมชาติ (NL)
API เสียง
API ที่กำหนดเอง
- ขยายโครงสร้างพื้นฐานของ Task API และสร้าง API แบบกำหนดเอง
เรียกใช้ไลบรารีงานกับผู้รับมอบสิทธิ์
ผู้รับมอบสิทธิ์ เปิดใช้งานการเร่งด้วยฮาร์ดแวร์ของรุ่น TensorFlow Lite โดยใช้ประโยชน์จากตัวเร่งความเร็วบนอุปกรณ์ เช่น GPU และ Coral Edge TPU การใช้อุปกรณ์เหล่านี้เพื่อการทำงานของโครงข่ายประสาทเทียมจะให้ประโยชน์อย่างมากในแง่ของเวลาแฝงและประสิทธิภาพการใช้พลังงาน ตัวอย่างเช่น GPU สามารถให้ ความเร็วสูงสุด 5 เท่า ในเวลาแฝงบนอุปกรณ์มือถือ และการอนุมาน Coral Edge TPU เร็วกว่า CPU เดสก์ท็อป 10 เท่า
ไลบรารีงานให้การกำหนดค่าที่ง่ายดายและตัวเลือกสำรองเพื่อให้คุณตั้งค่าและใช้ผู้รับมอบสิทธิ์ ตัวเร่งความเร็วต่อไปนี้ได้รับการสนับสนุนใน Task API แล้ว:
- แอนดรอยด์
- ลินุกซ์/แมค
- ขอบปะการัง TPU : C++
- iOS
- ผู้รับมอบสิทธิ์ Core ML : C++
รองรับการเร่งความเร็วใน 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
]
ตัวเลือกผู้รับมอบสิทธิ์อื่นๆ ได้แก่:
"//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();