앱에서 커스텀 TensorFlow Lite 모델을 사용하는 경우 Firebase ML을 사용하여 모델을 배포할 수 있습니다. Firebase로 모델을 배포하면 앱의 초기 다운로드 크기를 줄일 수 있으며 앱의 새 버전을 출시하지 않고도 앱의 ML 모델을 업데이트할 수 있습니다. 또한 원격 구성 및 A/B 테스팅을 사용하면 다양한 사용자 집합에 서로 다른 모델을 동적으로 제공할 수 있습니다.
TensorFlow Lite 모델
TensorFlow Lite 모델은 휴대기기에서 실행되도록 최적화된 ML 모델입니다. TensorFlow Lite 모델을 가져오려면 다음 안내를 따르세요.
Flutter 프로젝트의 루트 디렉터리에서 다음 명령어를 실행하여 ML 모델 다운로더 플러그인을 설치합니다.
flutterpubaddfirebase_ml_model_downloader
프로젝트를 다시 빌드합니다.
flutterrun
1. 모델 배포
Firebase Console 또는 Firebase Admin Python 및 Node.js SDK를 사용하여 커스텀 TensorFlow 모델을 배포합니다. 커스텀 모델 배포 및 관리를 참조하세요.
Firebase 프로젝트에 커스텀 모델을 추가한 후 지정한 이름을 사용하여 앱에서 모델을 참조할 수 있습니다. 언제든지 새 TensorFlow Lite 모델을 배포하고 getModel()을 호출하여 새 모델을 사용자 기기에 다운로드할 수 있습니다(아래 참조).
2. 기기에 모델 다운로드 및 TensorFlow Lite 인터프리터 초기화
앱에서 TensorFlow Lite 모델을 사용하려면 우선 모델 다운로더를 사용하여 모델의 최신 버전을 기기에 다운로드합니다. 그런 다음 모델을 사용하여 TensorFlow Lite 인터프리터를 인스턴스화합니다.
모델 다운로드를 시작하려면 모델을 업로드할 때 할당한 이름, 항상 최신 모델을 다운로드할지 여부, 다운로드 허용 조건을 지정하여 모델 다운로더의 getModel() 메서드를 호출합니다.
다음 세 가지 다운로드 동작 중에서 선택할 수 있습니다.
다운로드 유형
설명
localModel
기기에서 로컬 모델을 가져옵니다.
사용할 수 있는 로컬 모델이 없으면 latestModel처럼 작동합니다. 모델 업데이트를 확인하지 않아도 된다면 이 다운로드 유형을 사용하세요. 예를 들어 원격 구성을 사용하여 모델 이름을 검색하고 항상 새 이름으로 모델을 업로드합니다(권장).
localModelUpdateInBackground
기기에서 로컬 모델을 가져와 백그라운드에서 모델 업데이트를 시작합니다.
사용할 수 있는 로컬 모델이 없으면 latestModel처럼 작동합니다.
latestModel
최신 모델을 가져옵니다. 로컬 모델이 최신 버전이면 로컬 모델을 반환합니다. 그렇지 않은 경우 최신 모델을 다운로드합니다. 이 동작은 최신 버전이 다운로드될 때까지 차단됩니다(권장하지 않음). 최신 버전이 명시적으로 필요한 경우에만 이 동작을 사용하세요.
모델 다운로드 여부가 확인될 때까지 모델 관련 기능 사용을 중지해야 합니다(예: UI 비활성화 또는 숨김).
FirebaseModelDownloader.instance.getModel("yourModelName",FirebaseModelDownloadType.localModel,FirebaseModelDownloadConditions(iosAllowsCellularAccess:true,iosAllowsBackgroundDownloading:false,androidChargingRequired:false,androidWifiRequired:false,androidDeviceIdleRequired:false,)).then((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.finallocalModelPath=customModel.file;// ...});
대부분의 앱은 초기화 코드에서 다운로드 작업을 시작하지만 모델 사용이 필요한 시점 이전에 언제든지 다운로드할 수 있습니다.
3. 입력 데이터에 대한 추론 수행
이제 모델 파일이 기기에 있으므로 TensorFlow Lite 인터프리터와 함께 사용하여 추론을 수행할 수 있습니다. Dart용 TensorFlow Lite 라이브러리가 유지되지 않는 경우 iOS 및 Android용 네이티브 TensorFlow Lite 라이브러리와 통합해야 합니다.
부록: 모델 보안
TensorFlow Lite 모델을 어떤 방식으로 Firebase ML에 제공하든 Firebase ML은 로컬 스토리지에 직렬화된 표준 프로토콜 버퍼(protobuf) 형식으로 모델을 저장합니다.
즉, 이론적으로는 누구나 모델을 복사할 수 있습니다. 하지만 실제로는 대부분의 모델이 애플리케이션별로 너무나 다르며 최적화를 통해 난독화되므로 위험도는 경쟁업체가 내 코드를 분해해서 재사용하는 것과 비슷한 수준입니다. 그러나 앱에서 커스텀 모델을 사용하기 전에 이러한 위험성을 알고 있어야 합니다.
[[["이해하기 쉬움","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-08-04(UTC)"],[],[],null,["# Use a custom TensorFlow Lite model with Flutter\n\n\u003cbr /\u003e\n\nIf your app uses custom\n[TensorFlow Lite](https://www.tensorflow.org/lite/) models, you can\nuse Firebase ML to deploy your models. By deploying models with Firebase, you\ncan reduce the initial download size of your app and update your app's ML models\nwithout releasing a new version of your app. And, with Remote Config and A/B\nTesting, you can dynamically serve different models to different sets of users.\n\nTensorFlow Lite models\n----------------------\n\nTensorFlow Lite models are ML models that are optimized to run on mobile\ndevices. To get a TensorFlow Lite model:\n\n- Use a pre-built model, such as one of the [official TensorFlow Lite models](https://www.tensorflow.org/lite/models)\n- [Convert a TensorFlow model, Keras model, or concrete function to TensorFlow Lite.](https://www.tensorflow.org/lite/convert)\n\nNote that in the absence of a maintained TensorFlow Lite library for Dart, you\nwill need to integrate with the native TensorFlow Lite library for your\nplatforms. This integration is not documented here.\n\nBefore you begin\n----------------\n\n1. [Install and initialize the Firebase SDKs for Flutter](/docs/flutter/setup)\n if you haven't already done so.\n\n2. From the root directory of your Flutter project, run the following\n command to install the ML model downloader plugin:\n\n flutter pub add firebase_ml_model_downloader\n\n3. Rebuild your project:\n\n flutter run\n\n1. Deploy your model\n--------------------\n\nDeploy your custom TensorFlow models using either the Firebase console or\nthe Firebase Admin Python and Node.js SDKs. See\n[Deploy and manage custom models](/docs/ml/manage-hosted-models).\n\nAfter you add a custom model to your Firebase project, you can reference the\nmodel in your apps using the name you specified. At any time, you can deploy a\nnew TensorFlow Lite model and download the new model onto users' devices by\ncalling `getModel()` (see below).\n\n2. Download the model to the device and initialize a TensorFlow Lite interpreter\n--------------------------------------------------------------------------------\n\nTo use your TensorFlow Lite model in your app, first use the model downloader\nto download the latest version of the model to the device. Then, instantiate a\nTensorFlow Lite interpreter with the model.\n\nTo start the model download, call the model downloader's `getModel()` method,\nspecifying the name you assigned the model when you uploaded it, whether you\nwant to always download the latest model, and the conditions under which you\nwant to allow downloading.\n\nYou can choose from three download behaviors:\n\n| Download type | Description |\n|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `localModel` | Get the local model from the device. If there is no local model available, this behaves like `latestModel`. Use this download type if you are not interested in checking for model updates. For example, you're using Remote Config to retrieve model names and you always upload models under new names (recommended). |\n| `localModelUpdateInBackground` | Get the local model from the device and start updating the model in the background. If there is no local model available, this behaves like `latestModel`. |\n| `latestModel` | Get the latest model. If the local model is the latest version, returns the local model. Otherwise, download the latest model. This behavior will block until the latest version is downloaded (not recommended). Use this behavior only in cases where you explicitly need the latest version. |\n\nYou should disable model-related functionality---for example, grey-out or\nhide part of your UI---until you confirm the model has been downloaded. \n\n FirebaseModelDownloader.instance\n .getModel(\n \"yourModelName\",\n FirebaseModelDownloadType.localModel,\n FirebaseModelDownloadConditions(\n iosAllowsCellularAccess: true,\n iosAllowsBackgroundDownloading: false,\n androidChargingRequired: false,\n androidWifiRequired: false,\n androidDeviceIdleRequired: false,\n )\n )\n .then((customModel) {\n // Download complete. Depending on your app, you could enable the ML\n // feature, or switch from the local model to the remote model, etc.\n\n // The CustomModel object contains the local path of the model file,\n // which you can use to instantiate a TensorFlow Lite interpreter.\n final localModelPath = customModel.file;\n\n // ...\n });\n\nMany apps start the download task in their initialization code, but you can do\nso at any point before you need to use the model.\n\n3. Perform inference on input data\n----------------------------------\n\nNow that you have your model file on the device you can use it with the\nTensorFlow Lite interpreter to perform inference. In the absence of a maintained\nTensorFlow Lite library for Dart, you will need to integrate with the\n[native TensorFlow Lite libraries](https://www.tensorflow.org/lite)\nfor iOS and Android.\n\nAppendix: Model security\n------------------------\n\nRegardless of how you make your TensorFlow Lite models available to\nFirebase ML, Firebase ML stores them in the standard serialized protobuf format in\nlocal storage.\n\nIn theory, this means that anybody can copy your model. However,\nin practice, most models are so application-specific and obfuscated by\noptimizations that the risk is similar to that of competitors disassembling and\nreusing your code. Nevertheless, you should be aware of this risk before you use\na custom model in your app."]]