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

ไลบรารีงาน TensorFlow Lite ประกอบด้วยชุดไลบรารีเฉพาะงานที่มีประสิทธิภาพและใช้งานง่ายสำหรับนักพัฒนาแอปเพื่อสร้างประสบการณ์ ML ด้วย TFLite โดยมอบอินเทอร์เฟซโมเดลสำเร็จรูปที่ได้รับการปรับปรุงให้เหมาะสมสำหรับงานแมชชีนเลิร์นนิงยอดนิยม เช่น การจัดหมวดหมู่รูปภาพ คำถามและคำตอบ ฯลฯ อินเทอร์เฟซโมเดลได้รับการออกแบบมาโดยเฉพาะสำหรับแต่ละงานเพื่อให้ได้ประสิทธิภาพและการใช้งานที่ดีที่สุด Task Library ทำงานข้ามแพลตฟอร์มและรองรับบน 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 ใน 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();