메타데이터를 사용하여 모델 인터페이스 생성

LiteRT 메타데이터를 사용하여 개발자는 래퍼 코드를 사용하여 Android에서 통합을 사용 설정합니다. 대부분의 개발자는 Android 스튜디오 ML 모델 결합의 그래픽 인터페이스는 가장 사용이 간편합니다. 추가 맞춤설정이 필요하거나 명령줄을 사용 중인 경우 LiteRT 코드 생성 도구도 사용할 수 있습니다.

Android 스튜디오 ML 모델 결합 사용

메타데이터로 향상된 LiteRT 모델의 경우 개발자는 Android 스튜디오 ML 모델 결합을 사용하여 프로젝트에 대한 설정을 지정하고 모델에 따라 래퍼 클래스를 생성합니다. 메타데이터로 변환할 수 있습니다. 래퍼 코드는 ByteBuffer 대신 개발자는 BitmapRect와 같은 유형이 지정된 객체 사용

Android 스튜디오에서 LiteRT 모델 가져오기

  1. TFLite 모델을 사용할 모듈을 마우스 오른쪽 버튼으로 클릭하거나 File 그리고 New > Other > LiteRT Model

  2. TFLite 파일의 위치를 선택합니다. 도구를 사용하면 사용자를 대신하여 ML 모델 결합을 통해 모듈의 종속 항목을 구성하고 모든 종속 항목이 Android 모듈의 build.gradle 파일.

    선택사항: 다음 경우에 TensorFlow GPU를 가져오기 위한 두 번째 체크박스를 선택합니다. GPU 가속을 사용해야 합니다

  3. Finish 아이콘을 클릭합니다.

  4. 가져오기가 완료되면 다음 화면이 표시됩니다. 시작 모델을 사용하여 Kotlin 또는 Java를 선택한 다음 코드를 복사하여 Sample Code 섹션. 이 화면으로 돌아오려면 Android 스튜디오의 ml 디렉터리에 있는 TFLite 모델

모델 추론 가속화

ML 모델 바인딩은 개발자가 위임 사용 및 스레드 수.

1단계: 다음이 포함된 모듈 build.gradle 파일을 확인합니다. 종속됩니다.

    dependencies {
        ...
        // TFLite GPU delegate 2.3.0 or above is required.
        implementation 'com.google.ai.edge.litert:litert-gpu:2.3.0'
    }

2단계: 기기에서 실행 중인 GPU가 TensorFlow GPU와 호환되는지 감지 여러 CPU 스레드를 사용하여 모델을 실행하지 않는 경우:

Kotlin

    import org.tensorflow.lite.gpu.CompatibilityList
    import org.tensorflow.lite.gpu.GpuDelegate

    val compatList = CompatibilityList()

    val options = if(compatList.isDelegateSupportedOnThisDevice) {
        // if the device has a supported GPU, add the GPU delegate
        Model.Options.Builder().setDevice(Model.Device.GPU).build()
    } else {
        // if the GPU is not supported, run on 4 threads
        Model.Options.Builder().setNumThreads(4).build()
    }

    // Initialize the model as usual feeding in the options object
    val myModel = MyModel.newInstance(context, options)

    // Run inference per sample code
      

자바

    import org.tensorflow.lite.support.model.Model
    import org.tensorflow.lite.gpu.CompatibilityList;
    import org.tensorflow.lite.gpu.GpuDelegate;

    // Initialize interpreter with GPU delegate
    Model.Options options;
    CompatibilityList compatList = CompatibilityList();

    if(compatList.isDelegateSupportedOnThisDevice()){
        // if the device has a supported GPU, add the GPU delegate
        options = Model.Options.Builder().setDevice(Model.Device.GPU).build();
    } else {
        // if the GPU is not supported, run on 4 threads
        options = Model.Options.Builder().setNumThreads(4).build();
    }

    MyModel myModel = new MyModel.newInstance(context, options);

    // Run inference per sample code
      

LiteRT 코드 생성기로 모델 인터페이스 생성

메타데이터로 향상된 LiteRT 모델의 경우 개발자는 LiteRT Android 래퍼 코드 생성기를 사용하여 래퍼 코드를 찾습니다. 래퍼 코드는 ByteBuffer를 사용하여 직접 가져올 수도 있습니다. 대신 개발자가 TensorFlow 유형이 지정된 객체(예: Bitmap, Rect)가 있는 라이트 모델

코드 생성기의 유용성은 LiteRT 모델의 메타데이터 항목입니다. <Codegen usage> 섹션 참고 Google Cloud 콘솔의 metadata_schema.fbs, 각 필드를 파싱하는 방법을 확인할 수 있습니다.

래퍼 코드 생성

터미널에 다음 도구를 설치해야 합니다.

pip install tflite-support

완료되면 다음 문법을 사용하여 코드 생성기를 사용할 수 있습니다.

tflite_codegen --model=./model_with_metadata/mobilenet_v1_0.75_160_quantized.tflite \
    --package_name=org.tensorflow.lite.classify \
    --model_class_name=MyClassifierModel \
    --destination=./classify_wrapper

결과 코드는 대상 디렉터리에 있습니다. 만약 Google Colab 또는 기타 리모컨을 사용하여 결과를 압축 파일로 압축하는 것이 더 쉬울 것입니다. Android 스튜디오 프로젝트에 추가합니다.

# Zip up the generated code
!zip -r classify_wrapper.zip classify_wrapper/

# Download the archive
from google.colab import files
files.download('classify_wrapper.zip')

생성된 코드 사용

1단계: 생성된 코드 가져오기

필요한 경우 생성된 코드를 디렉터리 구조에 압축 해제합니다. 루트 생성된 코드는 SRC_ROOT인 것으로 가정합니다.

LiteRT를 사용할 Android 스튜디오 프로젝트를 엽니다. 다음 방법으로 생성된 모듈을 가져옵니다. 및 파일 -> 신규 -> 모듈 가져오기 -> SRC_ROOT 선택

위의 예를 사용하여 디렉터리와 가져온 모듈은 다음과 같이 classify_wrapper

2단계: 앱의 build.gradle 파일 업데이트

생성된 라이브러리 모듈을 사용할 앱 모듈에서 다음을 실행합니다.

Android 섹션에서 다음을 추가합니다.

aaptOptions {
   noCompress "tflite"
}

종속 항목 섹션에서 다음을 추가합니다.

implementation project(":classify_wrapper")

3단계: 모델 사용

// 1. Initialize the model
MyClassifierModel myImageClassifier = null;

try {
    myImageClassifier = new MyClassifierModel(this);
} catch (IOException io){
    // Error reading the model
}

if(null != myImageClassifier) {

    // 2. Set the input with a Bitmap called inputBitmap
    MyClassifierModel.Inputs inputs = myImageClassifier.createInputs();
    inputs.loadImage(inputBitmap));

    // 3. Run the model
    MyClassifierModel.Outputs outputs = myImageClassifier.run(inputs);

    // 4. Retrieve the result
    Map<String, Float> labeledProbability = outputs.getProbability();
}

모델 추론 가속화

생성된 코드는 개발자가 코드를 가속화하는 방법을 제공합니다. 대리인을 통해 스레드가 필요합니다. 이는 모델 객체를 초기화할 때 설정할 수 있으며, 매개변수:

  • Context: Android 활동 또는 서비스의 컨텍스트
  • (선택사항) Device: TFLite 가속 위임. 예를 들면 다음과 같습니다. GPUDelegate
  • (선택사항) numThreads: 모델을 실행하는 데 사용되는 스레드 수입니다. 기본값은 1입니다.

예를 들어 GPU 대리자 하나와 최대 세 개의 스레드를 사용하려면 모델은 다음과 같습니다.

try {
    myImageClassifier = new MyClassifierModel(this, Model.Device.GPU, 3);
} catch (IOException io){
    // Error reading the model
}

문제 해결

'java.io.FileNotFoundException: 이 파일은 파일 설명자 아마도 압축되어 오류가 있으면 다음 줄을 삽입합니다. 를 추가합니다.

aaptOptions {
   noCompress "tflite"
}