TensorFlow Lite 작업 라이브러리에는 앱 개발자가 TFLite로 ML 경험을 생성할 수 있는 강력하고 사용하기 쉬운 작업별 라이브러리 세트가 포함되어 있습니다. 이미지 분류, 질문 및 답변 등과 같은 인기 있는 기계 학습 작업에 최적화된 기본 모델 인터페이스를 제공합니다. 모델 인터페이스는 최상의 성능과 사용성을 달성하기 위해 각 작업에 대해 특별히 설계되었습니다. 작업 라이브러리는 플랫폼 간 작동하며 Java, C++ 및 Swift에서 지원됩니다.
작업 라이브러리에서 기대할 수 있는 것
비 ML 전문가가 사용할 수 있는 명확하고 잘 정의된 API
추론은 단 5줄의 코드 내에서 수행할 수 있습니다. 작업 라이브러리의 강력하고 사용하기 쉬운 API를 빌딩 블록으로 사용하여 모바일 장치에서 TFLite로 ML을 쉽게 개발할 수 있습니다.복잡하지만 일반적인 데이터 처리
데이터와 모델에 필요한 데이터 형식 간 변환을 위한 공통 비전 및 자연어 처리 논리를 지원합니다. 교육 및 추론을 위해 공유 가능한 동일한 처리 논리를 제공합니다.고성능 이득
데이터 처리는 몇 밀리초 이상 걸리지 않으므로 TensorFlow Lite를 사용하여 빠른 추론 경험을 보장합니다.확장성 및 사용자 정의
작업 라이브러리 인프라가 제공하는 모든 이점을 활용하고 자체 Android/iOS 추론 API를 쉽게 구축할 수 있습니다.
지원되는 작업
다음은 지원되는 작업 유형 목록입니다. 우리가 계속해서 더 많은 사용 사례를 가능하게 함에 따라 목록이 늘어날 것으로 예상됩니다.
비전 API
자연어(NL) API
오디오 API
커스텀 API
- Task API 인프라를 확장하고 맞춤형 API 를 구축합니다.
대리인과 함께 작업 라이브러리 실행
대리자 는 GPU 및 Coral Edge TPU 와 같은 온디바이스 가속기를 활용하여 TensorFlow Lite 모델의 하드웨어 가속을 활성화합니다. 이를 신경망 운영에 활용하면 대기 시간과 전력 효율성 측면에서 엄청난 이점을 얻을 수 있습니다. 예를 들어 GPU는 모바일 장치에서 최대 5배의 지연 속도 향상 을 제공할 수 있으며 Coral Edge TPU는 데스크톱 CPU보다 10배 더 빠르게 추론합니다.
작업 라이브러리는 대리자를 설정하고 사용할 수 있는 간편한 구성 및 대체 옵션을 제공합니다. 이제 Task API에서 다음 액셀러레이터가 지원됩니다.
Task Swift/Web API의 가속 지원이 곧 제공될 예정입니다.
Java에서 Android의 GPU 사용 예
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단계. BaseOptions 를 통해 작업 옵션에서 GPU 대리자를 구성합니다. 예를 들어 다음과 같이 ObjectDetecor
에서 GPU를 설정할 수 있습니다.
// 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);
C++에서 Android의 GPU 사용 예
1단계. 다음과 같이 bazel 빌드 대상의 GPU 위임 플러그인에 의존합니다.
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 대리자를 구성합니다. 예를 들어 다음과 같이 BertQuestionAnswerer
에서 GPU를 설정할 수 있습니다.
// 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);
여기에서 고급 가속기 설정을 살펴보십시오.
Python에서 Coral Edge TPU 사용 예
작업의 기본 옵션에서 Coral Edge TPU를 구성합니다. 예를 들어 다음과 같이 ImageClassifier
에서 Coral Edge TPU를 설정할 수 있습니다.
# 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)
C++에서 Coral Edge TPU 사용 예
1단계. 다음과 같이 bazel 빌드 대상의 Coral Edge TPU 대리자 플러그인에 의존합니다.
deps = [
"//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin", # for Coral Edge TPU
]
2단계. 작업 옵션에서 Coral Edge TPU를 구성합니다. 예를 들어 다음과 같이 ImageClassifier
에서 Coral Edge TPU를 설정할 수 있습니다.
// 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.
Coral Edge TPU 장치와 함께 작업 라이브러리 CLI 데모 도구 를 사용해 보십시오. 사전 훈련된 Edge TPU 모델 및 고급 Edge TPU 설정 에 대해 자세히 알아보세요.
C++에서 Core ML Delegate 사용 예
전체 예제는 Image Classifier Core ML Delegate Test 에서 찾을 수 있습니다.
1단계. 다음과 같이 bazel 빌드 대상의 Core ML 위임 플러그인에 의존합니다.
deps = [
"//tensorflow_lite_support/acceleration/configuration:coreml_plugin", # for Core ML Delegate
]
2단계. 작업 옵션에서 Core ML Delegate를 구성합니다. 예를 들어 다음과 같이 ImageClassifier
에서 Core ML Delegate를 설정할 수 있습니다.
// 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();