Android에서 커스텀 TensorFlow Lite 모델 사용

앱에서 커스텀 TensorFlow Lite 모델을 사용하는 경우 Firebase ML을 사용하여 모델을 배포할 수 있습니다. Firebase로 모델을 배포하면 앱의 초기 다운로드 크기를 줄일 수 있으며 앱의 새 버전을 출시하지 않고도 앱의 ML 모델을 업데이트할 수 있습니다. 또한 원격 구성 및 A/B 테스팅을 사용하면 다양한 사용자 집합에 서로 다른 모델을 동적으로 제공할 수 있습니다.

TensorFlow Lite 모델

TensorFlow Lite 모델은 휴대기기에서 실행되도록 최적화된 ML 모델입니다. TensorFlow Lite 모델을 가져오려면 다음 안내를 따르세요.

시작하기 전에

  1. 아직 추가하지 않았으면 Android 프로젝트에 Firebase를 추가합니다.
  2. 모듈(앱 수준) Gradle 파일(일반적으로 <project>/<app-module>/build.gradle.kts 또는 <project>/<app-module>/build.gradle)에서 Firebase ML 모델 다운로더 Android 라이브러리의 종속 항목을 추가합니다. 라이브러리 버전 관리 제어에는 Firebase Android BoM을 사용하는 것이 좋습니다.

    또한 Firebase ML 모델 다운로더를 설정하는 과정에서 앱에 TensorFlow Lite SDK를 추가해야 합니다.

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.5.0"))
    
        // Add the dependency for the Firebase ML model downloader library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-ml-modeldownloader-ktx")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }

    Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전만 사용합니다.

    (대안) BoM을 사용하지 않고 Firebase 라이브러리 종속 항목을 추가합니다.

    Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

    앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

    dependencies {
        // Add the dependency for the Firebase ML model downloader library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-ml-modeldownloader-ktx:24.2.1")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.5.0"))
    
        // Add the dependency for the Firebase ML model downloader library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-ml-modeldownloader")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }

    Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전만 사용합니다.

    (대안) BoM을 사용하지 않고 Firebase 라이브러리 종속 항목을 추가합니다.

    Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

    앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

    dependencies {
        // Add the dependency for the Firebase ML model downloader library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-ml-modeldownloader:24.2.1")
    // Also add the dependency for the TensorFlow Lite library and specify its version implementation("org.tensorflow:tensorflow-lite:2.3.0")
    }
  3. 앱의 매니페스트에서 INTERNET 권한이 필요하다고 선언합니다.
    <uses-permission android:name="android.permission.INTERNET" />

1. 모델 배포

Firebase Console 또는 Firebase Admin Python 및 Node.js SDK를 사용하여 커스텀 TensorFlow 모델을 배포합니다. 커스텀 모델 배포 및 관리를 참조하세요.

Firebase 프로젝트에 커스텀 모델을 추가한 후 지정한 이름을 사용하여 앱에서 모델을 참조할 수 있습니다. 언제든지 새 TensorFlow Lite 모델을 배포하고 getModel()을 호출하여 새 모델을 사용자 기기에 다운로드할 수 있습니다(아래 참조).

2. 기기에 모델 다운로드 및 TensorFlow Lite 인터프리터 초기화

앱에서 TensorFlow Lite 모델을 사용하려면 우선 Firebase ML SDK를 사용하여 모델의 최신 버전을 기기에 다운로드합니다. 그런 다음 모델을 사용하여 TensorFlow Lite 인터프리터를 인스턴스화합니다.

모델 다운로드를 시작하려면 모델을 업로드할 때 할당한 이름, 항상 최신 모델을 다운로드할지 여부, 다운로드 허용 조건을 지정하여 모델 다운로더의 getModel() 메서드를 호출합니다.

다음 세 가지 다운로드 동작 중에서 선택할 수 있습니다.

다운로드 유형 설명
LOCAL_MODEL 기기에서 로컬 모델을 가져옵니다. 사용할 수 있는 로컬 모델이 없으면 LATEST_MODEL처럼 작동합니다. 모델 업데이트를 확인하지 않아도 된다면 이 다운로드 유형을 사용하세요. 예를 들어 원격 구성을 사용하여 모델 이름을 검색하고 항상 새 이름으로 모델을 업로드하는 경우가 해당됩니다(권장).
LOCAL_MODEL_UPDATE_IN_BACKGROUND 기기에서 로컬 모델을 가져와 백그라운드에서 모델 업데이트를 시작합니다. 사용할 수 있는 로컬 모델이 없으면 LATEST_MODEL처럼 작동합니다.
LATEST_MODEL 최신 모델을 가져옵니다. 로컬 모델이 최신 버전이면 로컬 모델을 반환합니다. 그렇지 않은 경우 최신 모델을 다운로드합니다. 이 동작은 최신 버전이 다운로드될 때까지 차단됩니다(권장하지 않음). 최신 버전이 명시적으로 필요한 경우에만 이 동작을 사용하세요.

모델 다운로드 여부가 확인될 때까지 모델 관련 기능 사용을 중지해야 합니다(예: UI 비활성화 또는 숨김).

Kotlin+KTX

val conditions = CustomModelDownloadConditions.Builder()
        .requireWifi()  // Also possible: .requireCharging() and .requireDeviceIdle()
        .build()
FirebaseModelDownloader.getInstance()
        .getModel("your_model", DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND,
            conditions)
        .addOnSuccessListener { model: CustomModel? ->
            // Download complete. Depending on your app, you could enable the ML
            // feature, or switch from the local model to the remote model, etc.

            // The CustomModel object contains the local path of the model file,
            // which you can use to instantiate a TensorFlow Lite interpreter.
            val modelFile = model?.file
            if (modelFile != null) {
                interpreter = Interpreter(modelFile)
            }
        }

Java

CustomModelDownloadConditions conditions = new CustomModelDownloadConditions.Builder()
    .requireWifi()  // Also possible: .requireCharging() and .requireDeviceIdle()
    .build();
FirebaseModelDownloader.getInstance()
    .getModel("your_model", DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND, conditions)
    .addOnSuccessListener(new OnSuccessListener<CustomModel>() {
      @Override
      public void onSuccess(CustomModel model) {
        // Download complete. Depending on your app, you could enable the ML
        // feature, or switch from the local model to the remote model, etc.

        // The CustomModel object contains the local path of the model file,
        // which you can use to instantiate a TensorFlow Lite interpreter.
        File modelFile = model.getFile();
        if (modelFile != null) {
            interpreter = new Interpreter(modelFile);
        }
      }
    });

대부분의 앱은 초기화 코드에서 다운로드 작업을 시작하지만 모델 사용이 필요한 시점 이전에 언제든지 다운로드할 수 있습니다.

3. 입력 데이터에 대한 추론 수행

모델의 입력 및 출력 모양 가져오기

TensorFlow Lite 모델 인터프리터는 하나 이상의 다차원 배열을 입력으로 받아 출력합니다. 이러한 배열은 byte, int, long, float 값 중 하나를 포함합니다. 모델에 데이터를 전달하거나 결과를 사용하려면 먼저 모델에서 사용하는 배열의 수와 차원('모양')을 알아야 합니다.

모델을 직접 빌드했거나 모델의 입력 및 출력 형식이 문서화된 경우 이 정보를 이미 알고 있을 수 있습니다. 모델의 입출력 모양과 데이터 유형을 모르는 경우 TensorFlow Lite 인터프리터를 사용하여 모델을 검사할 수 있습니다. 예를 들면 다음과 같습니다.

Python

import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path="your_model.tflite")
interpreter.allocate_tensors()

# Print input shape and type
inputs = interpreter.get_input_details()
print('{} input(s):'.format(len(inputs)))
for i in range(0, len(inputs)):
    print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype']))

# Print output shape and type
outputs = interpreter.get_output_details()
print('\n{} output(s):'.format(len(outputs)))
for i in range(0, len(outputs)):
    print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))

출력 예시:

1 input(s):
[  1 224 224   3] <class 'numpy.float32'>

1 output(s):
[1 1000] <class 'numpy.float32'>

인터프리터 실행

모델의 입력과 출력 형식을 확인했으면 입력 데이터를 가져와 모델에 적합한 입력 모양을 만드는 데 필요한 데이터 변환을 수행합니다.

예를 들어 입력 모양이 [1 224 224 3] 부동 소수점 값인 이미지 분류 모델이 있는 경우 다음 예시와 같이 Bitmap 객체에서 입력 ByteBuffer를 생성할 수 있습니다.

Kotlin+KTX

val bitmap = Bitmap.createScaledBitmap(yourInputImage, 224, 224, true)
val input = ByteBuffer.allocateDirect(224*224*3*4).order(ByteOrder.nativeOrder())
for (y in 0 until 224) {
    for (x in 0 until 224) {
        val px = bitmap.getPixel(x, y)

        // Get channel values from the pixel value.
        val r = Color.red(px)
        val g = Color.green(px)
        val b = Color.blue(px)

        // Normalize channel values to [-1.0, 1.0]. This requirement depends on the model.
        // For example, some models might require values to be normalized to the range
        // [0.0, 1.0] instead.
        val rf = (r - 127) / 255f
        val gf = (g - 127) / 255f
        val bf = (b - 127) / 255f

        input.putFloat(rf)
        input.putFloat(gf)
        input.putFloat(bf)
    }
}

Java

Bitmap bitmap = Bitmap.createScaledBitmap(yourInputImage, 224, 224, true);
ByteBuffer input = ByteBuffer.allocateDirect(224 * 224 * 3 * 4).order(ByteOrder.nativeOrder());
for (int y = 0; y < 224; y++) {
    for (int x = 0; x < 224; x++) {
        int px = bitmap.getPixel(x, y);

        // Get channel values from the pixel value.
        int r = Color.red(px);
        int g = Color.green(px);
        int b = Color.blue(px);

        // Normalize channel values to [-1.0, 1.0]. This requirement depends
        // on the model. For example, some models might require values to be
        // normalized to the range [0.0, 1.0] instead.
        float rf = (r - 127) / 255.0f;
        float gf = (g - 127) / 255.0f;
        float bf = (b - 127) / 255.0f;

        input.putFloat(rf);
        input.putFloat(gf);
        input.putFloat(bf);
    }
}

그런 다음 모델의 출력을 포함할 수 있을 만큼 충분히 큰 ByteBuffer를 할당하고 입력 버퍼와 출력 버퍼를 TensorFlow Lite 인터프리터의 run() 메서드에 전달합니다. 예를 들어 출력 모양이 [1 1000] 부동 소수점 값인 경우는 다음과 같습니다.

Kotlin+KTX

val bufferSize = 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE
val modelOutput = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder())
interpreter?.run(input, modelOutput)

Java

int bufferSize = 1000 * java.lang.Float.SIZE / java.lang.Byte.SIZE;
ByteBuffer modelOutput = ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder());
interpreter.run(input, modelOutput);

출력을 사용하는 방법은 사용 중인 모델에 따라 다릅니다.

예를 들어 분류를 수행하는 경우 그 다음 단계로 결과의 색인을 색인이 나타내는 라벨에 매핑할 수 있습니다.

Kotlin+KTX

modelOutput.rewind()
val probabilities = modelOutput.asFloatBuffer()
try {
    val reader = BufferedReader(
            InputStreamReader(assets.open("custom_labels.txt")))
    for (i in probabilities.capacity()) {
        val label: String = reader.readLine()
        val probability = probabilities.get(i)
        println("$label: $probability")
    }
} catch (e: IOException) {
    // File not found?
}

Java

modelOutput.rewind();
FloatBuffer probabilities = modelOutput.asFloatBuffer();
try {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(getAssets().open("custom_labels.txt")));
    for (int i = 0; i < probabilities.capacity(); i++) {
        String label = reader.readLine();
        float probability = probabilities.get(i);
        Log.i(TAG, String.format("%s: %1.4f", label, probability));
    }
} catch (IOException e) {
    // File not found?
}

부록: 모델 보안

TensorFlow Lite 모델을 어떤 방식으로 Firebase ML에 제공하든 Firebase ML은 로컬 스토리지에 직렬화된 표준 프로토콜 버퍼(protobuf) 형식으로 모델을 저장합니다.

즉, 이론적으로는 누구나 모델을 복사할 수 있습니다. 하지만 실제로는 대부분의 모델이 애플리케이션별로 너무나 다르며 최적화를 통해 난독화되므로 위험도는 경쟁업체가 내 코드를 분해해서 재사용하는 것과 비슷한 수준입니다. 그렇지만 앱에서 커스텀 모델을 사용하기 전에 이러한 위험성을 알고 있어야 합니다.

Android API 수준 21(Lollipop) 이상에서는 자동 백업에서 제외된 디렉터리에 모델이 다운로드됩니다.

Android API 수준 20 이하에서는 앱의 비공개 내부 저장소에 있는 com.google.firebase.ml.custom.models라는 디렉터리에 모델이 다운로드됩니다. BackupAgent를 사용하여 파일 백업을 사용 설정했다면 이 디렉터리를 제외하도록 선택할 수 있습니다.