Google Play 서비스의 LiteRT는 네이티브 API 외에도 Java 또는 Kotlin 코드에서 사용할 수 있는 Java API를 사용하여 액세스할 수도 있습니다. 특히 Google Play 서비스의 LiteRT는
LiteRT Interpreter API를 통해 사용할 수
있습니다.
Interpreter API 사용
TensorFlow 런타임에서 제공하는 LiteRT Interpreter API는 ML 모델을 빌드하고 실행하기 위한 범용 인터페이스를 제공합니다. 다음 단계에 따라 Google Play 서비스 런타임에서 TensorFlow Lite를 사용하여 인터프리터 API로 추론을 실행합니다.
1. 프로젝트 종속 항목 추가
LiteRT용 Play 서비스 API에 액세스하려면 앱 프로젝트 코드에 다음 종속 항목을 추가합니다.
dependencies{...// LiteRT dependencies for Google Play servicesimplementation'com.google.android.gms:play-services-tflite-java:16.1.0'// Optional: include LiteRT Support Libraryimplementation'com.google.android.gms:play-services-tflite-support:16.1.0'...}
2. LiteRT 초기화 추가
LiteRT API를 사용하기 전에 Google Play 서비스 API의 LiteRT 구성요소를 초기화합니다.
코드를 업데이트하기 전에 모델의 성능 및 정확성 검사를 수행하는 것이 좋습니다. 특히 버전 2.1보다 이전 버전의 LiteRT(TF Lite)를 사용하는 경우 새 구현과 비교할 기준을 마련할 수 있습니다.
LiteRT용 Play 서비스 API를 사용하도록 모든 코드를 이전한 경우 앱 크기를 줄이기 위해 build.gradle 파일에서 기존 LiteRT 런타임 라이브러리 종속 항목(org.tensorflow:tensorflow-lite:*가 있는 항목)을 삭제해야 합니다.
코드에서 new Interpreter 객체 생성이 발생한 모든 항목을 식별하고 InterpreterApi.create() 호출을 사용하도록 각 객체를 수정합니다. 새 TfLite.initialize는 비동기식입니다. 즉, 대부분의 경우 드롭인 대체가 아닙니다. 호출이 완료될 때 리스너를 등록해야 합니다. 3단계 코드의 코드 스니펫을 참고하세요.
org.tensorflow.lite.Interpreter 또는 org.tensorflow.lite.InterpreterApi 클래스를 사용하여 소스 파일에 import org.tensorflow.lite.InterpreterApi; 및 import
org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime;를 추가합니다.
결과로 도출되는 InterpreterApi.create() 호출에 인수가 하나만 있는 경우 인수 목록에 new InterpreterApi.Options()를 추가합니다.
InterpreterApi.create() 호출의 마지막 인수에 .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)를 추가합니다.
org.tensorflow.lite.Interpreter 클래스의 다른 모든 항목을 org.tensorflow.lite.InterpreterApi로 바꿉니다.
독립형 LiteRT와 Play 서비스 API를 함께 사용하려면 LiteRT (TF Lite) 버전 2.9 이상을 사용해야 합니다.
LiteRT(TF Lite) 버전 2.8 이하 버전은 Play 서비스 API 버전과 호환되지 않습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-24(UTC)"],[],[],null,["# LiteRT in Google Play services Java (and Kotlin) API\n\nLiteRT in Google Play services can also be accessed using Java APIs,\nwhich can be used from Java or Kotlin code, in\naddition to the Native API. In particular, LiteRT in Google Play\nservices is available through the [LiteRT Interpreter\nAPI](../../api/tflite/java/org/tensorflow/lite/InterpreterApi).\n\nUsing the Interpreter APIs\n--------------------------\n\nThe LiteRT Interpreter API, provided by the TensorFlow runtime,\nprovides a general-purpose interface for building and running ML models. Use the\nfollowing steps to run inferences with the Interpreter API using the TensorFlow\nLite in Google Play services runtime.\n\n### 1. Add project dependencies\n\n| **Note:** LiteRT in Google Play services uses the `play-services-tflite` package.\n\nAdd the following dependencies to your app project code to access the Play\nservices API for LiteRT: \n\n dependencies {\n ...\n // LiteRT dependencies for Google Play services\n implementation 'com.google.android.gms:play-services-tflite-java:16.1.0'\n // Optional: include LiteRT Support Library\n implementation 'com.google.android.gms:play-services-tflite-support:16.1.0'\n ...\n }\n\n### 2. Add initialization of LiteRT\n\nInitialize the LiteRT component of the Google Play services API\n*before* using the LiteRT APIs: \n\n### Kotlin\n\n```kotlin\nval initializeTask: Task\u003cVoid\u003e by lazy { TfLite.initialize(this) }\n```\n\n### Java\n\n```java\nTask\u003cVoid\u003e initializeTask = TfLite.initialize(context);\n```\n| **Note:** Make sure the `TfLite.initialize` task completes before executing code that accesses LiteRT APIs. Use the `addOnSuccessListener()` method, as shown in the next section.\n\n### 3. Create an Interpreter and set runtime option\n\nCreate an interpreter using `InterpreterApi.create()` and configure it to use\nGoogle Play services runtime, by calling `InterpreterApi.Options.setRuntime()`,\nas shown in the following example code: \n\n### Kotlin\n\n```kotlin\nimport org.tensorflow.lite.InterpreterApi\nimport org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime\n...\nprivate lateinit var interpreter: InterpreterApi\n...\ninitializeTask.addOnSuccessListener {\n val interpreterOption =\n InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n interpreter = InterpreterApi.create(\n modelBuffer,\n interpreterOption\n )}\n .addOnFailureListener { e -\u003e\n Log.e(\"Interpreter\", \"Cannot initialize interpreter\", e)\n }\n```\n\n### Java\n\n```java\nimport org.tensorflow.lite.InterpreterApi\nimport org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime\n...\nprivate InterpreterApi interpreter;\n...\ninitializeTask.addOnSuccessListener(a -\u003e {\n interpreter = InterpreterApi.create(modelBuffer,\n new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY));\n })\n .addOnFailureListener(e -\u003e {\n Log.e(\"Interpreter\", String.format(\"Cannot initialize interpreter: %s\",\n e.getMessage()));\n });\n```\n\nYou should use the implementation above because it avoids blocking the Android\nuser interface thread. If you need to manage thread execution more closely, you\ncan add a `Tasks.await()` call to interpreter creation: \n\n### Kotlin\n\n```kotlin\nimport androidx.lifecycle.lifecycleScope\n...\nlifecycleScope.launchWhenStarted { // uses coroutine\n initializeTask.await()\n}\n```\n\n### Java\n\n```java\n@BackgroundThread\nInterpreterApi initializeInterpreter() {\n Tasks.await(initializeTask);\n return InterpreterApi.create(...);\n}\n```\n| **Warning:** Do not call `.await()` on the foreground user interface thread because it interrupts display of user interface elements and creates a poor user experience.\n\n### 4. Run inferences\n\nUsing the `interpreter` object you created, call the `run()` method to generate\nan inference. \n\n### Kotlin\n\n```kotlin\ninterpreter.run(inputBuffer, outputBuffer)\n```\n\n### Java\n\n```java\ninterpreter.run(inputBuffer, outputBuffer);\n```\n\nHardware acceleration\n---------------------\n\nLiteRT allows you to accelerate the performance of your model using\nspecialized hardware processors, such as graphics processing units (GPUs). You\ncan take advantage of these specialized processors using hardware drivers called\n[*delegates*](../performance/delegates).\n\nThe [GPU delegate](../performance/gpu) is provided through Google Play services\nand is dynamically loaded, just like the Play services versions of the\nInterpreter API.\n\n### Checking device compatibility\n\nNot all devices support GPU hardware acceleration with TFLite. In order to\nmitigate errors and potential crashes, use the\n`TfLiteGpu.isGpuDelegateAvailable` method to check whether a device is\ncompatible with the GPU delegate.\n\nUse this method to confirm whether a device is compatible with GPU, and use CPU\nas a fallback for when GPU is not supported. \n\n useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)\n\nOnce you have a variable like `useGpuTask`, you can use it to determine whether\ndevices use the GPU delegate. \n\n### Kotlin\n\n```kotlin\nval interpreterTask = useGpuTask.continueWith { task -\u003e\n val interpreterOptions = InterpreterApi.Options()\n .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n if (task.result) {\n interpreterOptions.addDelegateFactory(GpuDelegateFactory())\n }\n InterpreterApi.create(FileUtil.loadMappedFile(context, MODEL_PATH), interpreterOptions)\n}\n \n```\n\n### Java\n\n```java\nTask\u003cInterpreterApi.Options\u003e interpreterOptionsTask = useGpuTask.continueWith({ task -\u003e\n InterpreterApi.Options options =\n new InterpreterApi.Options().setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY);\n if (task.getResult()) {\n options.addDelegateFactory(new GpuDelegateFactory());\n }\n return options;\n});\n \n```\n\n### GPU with Interpreter APIs\n\nTo use the GPU delegate with the Interpreter APIs:\n\n1. Update the project dependencies to use the GPU delegate from Play services:\n\n implementation 'com.google.android.gms:play-services-tflite-gpu:16.2.0'\n\n2. Enable the GPU delegate option in the TFlite initialization:\n\n ### Kotlin\n\n ```kotlin\n TfLite.initialize(context,\n TfLiteInitializationOptions.builder()\n .setEnableGpuDelegateSupport(true)\n .build())\n ```\n\n ### Java\n\n ```java\n TfLite.initialize(context,\n TfLiteInitializationOptions.builder()\n .setEnableGpuDelegateSupport(true)\n .build());\n ```\n3. Enable GPU delegate in the interpreter options: set the delegate factory to\n GpuDelegateFactory by calling `addDelegateFactory()\n within`InterpreterApi.Options()\\`:\n\n ### Kotlin\n\n ```kotlin\n val interpreterOption = InterpreterApi.Options()\n .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n .addDelegateFactory(GpuDelegateFactory())\n ```\n\n ### Java\n\n ```java\n Options interpreterOption = InterpreterApi.Options()\n .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n .addDelegateFactory(new GpuDelegateFactory());\n ```\n\nMigrating from stand-alone LiteRT\n---------------------------------\n\nIf you are planning to migrate your app from stand-alone LiteRT to the\nPlay services API, review the following additional guidance for updating your\napp project code:\n\n1. Review the [Limitations](#limitations) section of this page to ensure your use case is supported.\n2. Prior to updating your code, we recommend doing performance and accuracy checks for your models, particularly if you are using versions of LiteRT (TF Lite) earlier than version 2.1, so you have a baseline to compare against the new implementation.\n3. If you have migrated all of your code to use the Play services API for LiteRT, you should remove the existing LiteRT *runtime\n library* dependencies (entries with `org.tensorflow:`**tensorflow-lite**`:*`) from your build.gradle file so that you can reduce your app size.\n4. Identify all occurrences of `new Interpreter` object creation in your code, and modify each one so that it uses the `InterpreterApi.create()` call. The new `TfLite.initialize` is asynchronous, which means in most cases it's not a drop-in replacement: you must register a listener for when the call completes. Refer to the code snippet in [Step 3](#step_3_interpreter) code.\n5. Add `import org.tensorflow.lite.InterpreterApi;` and `import\n org.tensorflow.lite.InterpreterApi.Options.TfLiteRuntime;` to any source files using the `org.tensorflow.lite.Interpreter` or `org.tensorflow.lite.InterpreterApi` classes.\n6. If any of the resulting calls to `InterpreterApi.create()` have only a single argument, append `new InterpreterApi.Options()` to the argument list.\n7. Append `.setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)` to the last argument of any calls to `InterpreterApi.create()`.\n8. Replace all other occurrences of the `org.tensorflow.lite.Interpreter` class with `org.tensorflow.lite.InterpreterApi`.\n\nIf you want to use stand-alone LiteRT and the Play services API\nside-by-side, you must use LiteRT (TF Lite) version 2.9 or later.\nLiteRT (TF Lite) version 2.8 and earlier versions are not compatible with the\nPlay services API version."]]