از یک مدل سفارشی TensorFlow Lite با Flutter استفاده کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اگر برنامه شما از مدلهای سفارشی TensorFlow Lite استفاده میکند، میتوانید از Firebase ML برای استقرار مدلهای خود استفاده کنید. با استقرار مدلها با Firebase، میتوانید حجم دانلود اولیه برنامه خود را کاهش دهید و مدلهای ML برنامه خود را بدون انتشار نسخه جدیدی از برنامه خود بهروزرسانی کنید. و با Remote Config و A/B Testing، می توانید مدل های مختلف را به صورت پویا به مجموعه های مختلف کاربران ارائه دهید.
مدل های TensorFlow Lite
مدل های TensorFlow Lite مدل های ML هستند که برای اجرا در دستگاه های تلفن همراه بهینه شده اند. برای دریافت یک مدل TensorFlow Lite:
توجه داشته باشید که در صورت عدم وجود کتابخانه TensorFlow Lite برای Dart، باید با کتابخانه بومی TensorFlow Lite برای پلتفرمهای خود ادغام شوید. این ادغام در اینجا مستند نشده است.
از دایرکتوری ریشه پروژه Flutter خود، دستور زیر را برای نصب افزونه دانلودر مدل ML اجرا کنید:
flutterpubaddfirebase_ml_model_downloader
پروژه خود را بازسازی کنید:
flutterrun
1. مدل خود را مستقر کنید
مدلهای TensorFlow سفارشی خود را با استفاده از کنسول Firebase یا Firebase Admin Python و Node.js SDK اجرا کنید. به استقرار و مدیریت مدلهای سفارشی مراجعه کنید.
پس از اینکه یک مدل سفارشی را به پروژه Firebase خود اضافه کردید، میتوانید با استفاده از نامی که مشخص کردهاید به مدل در برنامههای خود ارجاع دهید. در هر زمان، میتوانید یک مدل جدید TensorFlow Lite را اجرا کنید و با فراخوانی getModel() مدل جدید را در دستگاههای کاربران دانلود کنید (به زیر مراجعه کنید).
2. مدل را در دستگاه دانلود کنید و یک مفسر TensorFlow Lite را مقداردهی اولیه کنید
برای استفاده از مدل TensorFlow Lite در برنامه خود، ابتدا از دانلودر مدل برای دانلود آخرین نسخه مدل در دستگاه استفاده کنید. سپس، یک مفسر TensorFlow Lite را با مدل نمونهسازی کنید.
برای شروع دانلود مدل، متد getModel() دانلود کننده مدل را فراخوانی کنید، نامی را که به مدل اختصاص داده اید هنگام آپلود کردن، مشخص کنید که آیا می خواهید همیشه آخرین مدل را دانلود کنید، و شرایطی که می خواهید اجازه دانلود را بدهید.
شما می توانید از بین سه رفتار دانلود انتخاب کنید:
نوع دانلود
توضیحات
localModel
مدل محلی را از دستگاه دریافت کنید. اگر هیچ مدل محلی در دسترس نباشد، مانند latestModel عمل می کند. اگر علاقه ای به بررسی به روز رسانی مدل ندارید از این نوع دانلود استفاده کنید. برای مثال، شما از Remote Config برای بازیابی نام مدل ها استفاده می کنید و همیشه مدل ها را با نام های جدید آپلود می کنید (توصیه می شود).
localModelUpdateInBackground
مدل محلی را از دستگاه دریافت کنید و به روز رسانی مدل را در پس زمینه شروع کنید. اگر هیچ مدل محلی در دسترس نباشد، مانند latestModel عمل می کند.
latestModel
آخرین مدل را دریافت کنید. اگر مدل محلی آخرین نسخه باشد، مدل محلی را برمی گرداند. در غیر این صورت آخرین مدل را دانلود کنید. این رفتار تا زمانی که آخرین نسخه دانلود نشود مسدود می شود (توصیه نمی شود). از این رفتار فقط در مواردی استفاده کنید که به صراحت به آخرین نسخه نیاز دارید.
باید عملکردهای مربوط به مدل را غیرفعال کنید - برای مثال، خاکستری کردن یا پنهان کردن بخشی از رابط کاربری خود - تا زمانی که تأیید کنید مدل دانلود شده است.
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 برای انجام استنتاج استفاده کنید. در صورت عدم وجود کتابخانه TensorFlow Lite برای Dart، باید با کتابخانههای بومی TensorFlow Lite برای iOS و Android یکپارچه شوید.
ضمیمه: امنیت مدل
صرف نظر از اینکه چگونه مدلهای TensorFlow Lite خود را در دسترس Firebase ML قرار میدهید، Firebase ML آنها را در قالب استاندارد پروتوباف سریالی در حافظه محلی ذخیره میکند.
در تئوری، این بدان معنی است که هر کسی می تواند مدل شما را کپی کند. با این حال، در عمل، بیشتر مدلها به قدری برنامههای کاربردی خاص هستند و بهوسیله بهینهسازیها مبهم هستند که خطر آن مشابه خطر جداسازی و استفاده مجدد کد شما توسط رقبا است. با این وجود، قبل از استفاده از یک مدل سفارشی در برنامه خود، باید از این خطر آگاه باشید.
تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-25 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]