TensorFlow Lite 작업 라이브러리

TensorFlow Lite Task Library에는 강력하고 사용하기 쉬운 앱 개발자가 TFLite로 ML 환경을 만들 수 있는 작업별 라이브러리를 제공합니다. 널리 사용되는 머신러닝에 최적화된 즉시 사용 가능한 모델 인터페이스를 제공합니다. 모델 학습 프로세스입니다. 각 태스크에 맞게 특별히 설계되어 성능 및 사용성을 높일 수 있습니다 작업 라이브러리는 크로스 플랫폼에서 작동하며 다음에서 지원됩니다. Java, C++, Swift를 지원합니다

작업 라이브러리에서 예상되는 사항

  • ML 전문가가 아닌 사용자도 사용할 수 있는 깔끔하고 잘 정의된 API
    코드 5줄만으로 추론을 수행할 수 있습니다. 강력한 성능의 이를 빌딩 블록으로 활용하여 작업을 쉽게 처리할 수 있습니다. TFLite로 휴대기기에서 ML을 개발하는 방법을 알아봅니다.

  • 복잡하지만 일반적인 데이터 처리
    일반적인 비전 및 자연어 처리 논리를 지원하여 데이터 형식 간의 관계를 이해하는 데 도움이 됩니다. 다음을 제공합니다. 학습 및 추론을 위한 공유 가능한 동일한 처리 논리를 제공합니다.

  • 높은 성능 향상
    데이터 처리는 몇 밀리초 이내에 완료되었으니 빠른 추론 경험을 제공합니다.

  • 확장성 및 맞춤설정
    작업 라이브러리 인프라가 제공하는 모든 이점을 활용할 수 있으며 자체 Android/iOS 추론 API를 쉽게 빌드할 수 있습니다.

지원되는 태스크

다음은 지원되는 작업 유형 목록입니다. 이 목록은 앞으로 계속해서 더 많은 사용 사례를 지원하고 있습니다.

위임으로 작업 라이브러리 실행

위임을 사용하면 다음 기기의 하드웨어 가속을 사용할 수 있습니다. TensorFlow Lite 모델에 GPUCoral Edge TPU. 활용 중 지연 시간 측면에서 막대한 이점을 제공하고 설계할 수 있습니다 예를 들어 GPU는 초당 최대 5배의 성능을 속도 향상 Coral Edge TPU 추론은 10배 더 높으며, CPU보다 빠릅니다.

작업 라이브러리는 간편한 구성 및 설정을 위한 대체 옵션을 제공합니다. 위임을 사용합니다 이제 Task API에서 다음 가속기가 지원됩니다.

  • Android의 경우 <ph type="x-smartling-placeholder">
      </ph>
    • GPU: Java / C++
  • Linux / Mac <ph type="x-smartling-placeholder">
  • iOS <ph type="x-smartling-placeholder">

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단계: 다음을 통해 작업 옵션에서 GPU 위임을 구성합니다. BaseOptions. 예를 들어 다음과 같이 ObjectDetector에서 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
]

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.

작업 라이브러리 CLI 데모 사용해 보기 도구 Coral Edge TPU 기기와 함께 사용할 수 있습니다 선행 학습된 Edge TPU 자세히 살펴보기 모델고급 Edge TPU 설정을 참고하세요.

C++에서 Core ML 대리자 사용 예

전체 예는 이미지 분류기 핵심 ML 위임에서 확인할 수 있습니다. 테스트를 참조하세요.

1단계: bazel 빌드 타겟의 Core ML 위임 플러그인에 종속됩니다. 예를 들면 다음과 같습니다. 방향:

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

2단계: 작업 옵션에서 Core ML 위임을 구성합니다. 예를 들어 ImageClassifier에서 Core ML 위임을 다음과 같이 업데이트합니다.

// 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();