tflite_flutter 패키지는 Android, iOS, macOS, Windows 및 Linux에서 Flutter 애플리케이션에 TensorFlow Lite 모델을 원활하게 통합할 수 있도록 합니다. 이미지 분류, 객체 감지, 음성 인식과 같은 기기 내 머신러닝 작업을 수행하기 위한 간단한 Dart API를 제공합니다. 성능과 유연성을 고려하여 설계되었으며, 최소 지연 시간으로 모델 로딩, 추론 및 실시간 처리를 지원합니다. 클라우드 서비스에 의존하지 않고 로컬 AI 기능을 갖춘 스마트 앱을 개발하는 개발자에게 이상적입니다.
사용 사례
모바일 앱에서의 이미지 분류
실시간 객체 탐지
기기 내 음성 인식
로컬 모델을 사용한 OCR
제스처 또는 움직임 분석
주요 기능
크로스 플랫폼 지원
저지연 추론
간단한 Dart API
모델 양자화 지원
라이브 모델 로딩
적합한 대상
모바일 및 데스크톱 ML 앱
오프라인 AI 기능
개인정보 보호 중심 애플리케이션
고성능 추론
TensorFlow Lite를 사용하는 개발자
고려 사항
TFLite 모델 파일이 필요합니다
지원되는 모델 유형에 제한됨
메모리 사용량은 모델 크기에 따라 달라집니다
내장 모델 학습 없음
플랫폼별 최적화가 필요할 수 있습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Announcement
Update: 26 April, 2023
This repo is a TensorFlow managed fork of the tflite_flutter_plugin project by the amazing Amish Garg. The goal of this project is to support our Flutter community in creating machine-learning backed apps with the TensorFlow Lite framework.
This project is currently a work-in-progress as we update it to create a working plugin that meets the latest and greatest Flutter and TensorFlow Lite standards. That said, pull requests and contributions are more than welcome and will be reviewed by TensorFlow or Flutter team members. We thank you for your understanding as we make progress on this update.
Feel free to reach out to us by posting in the issues or discussion areas.
Thanks!
PaulTR
Overview
TensorFlow Lite Flutter plugin provides a flexible and fast solution for accessing TensorFlow Lite interpreter and performing inference. The API is similar to the TFLite Java and Swift APIs. It directly binds to TFLite C API making it efficient (low-latency). Offers acceleration support using NNAPI, GPU delegates on Android, Metal and CoreML delegates on iOS, and XNNPack delegate on Desktop platforms.
Key Features
Multi-platform Support for Android and iOS
Flexibility to use any TFLite Model.
Acceleration using multi-threading.
Similar structure as TensorFlow Lite Java API.
Inference speeds close to native Android Apps built using the Java API.
Run inference in different isolates to prevent jank in UI thread.
(Important) Initial setup : Add dynamic libraries to your app
Android & iOS
Examples and support now support dynamic library downloads! iOS samples can be run with the commands
flutter build ios & flutter install ios from their respective iOS folders.
Android can be run with the commands
flutter build android & flutter install android
while devices are plugged in.
Note: This requires a device with a minimum API level of 26.
Note: TFLite may not work in the iOS simulator. It's recommended that you test with a physical device.
When creating a release archive (IPA), the symbols are stripped by Xcode, so the command flutter build ipa may throw a Failed to lookup symbol ... symbol not found error. To work around this:
In Xcode, go to Target Runner > Build Settings > Strip Style
Change from All Symbols to Non-Global Symbols
MacOS
For MacOS a TensorFlow Lite dynamic library needs to be added to the project manually.
For this, first a .dylib needs to be built. You can follow the Bazel build guide or the CMake build guide to build the libraries.
CMake Note:
cross compiling in CMake can be achieved using:
-DCMAKE_OSX_ARCHITECTURES=x86_64|arm64
bundling two architectures (arm / x86) using lipo:
lipo -create arm64/libtensorflowlite_c.dylib x86/libtensorflowlite_c.dylib -output libtensorflowlite_c.dylib
For Linux a TensorFlow Lite dynamic library needs to be added to the project manually.
For this, first a .so needs to be built. You can follow the Bazel build guide or the CMake build guide to build the libraries.
As a second step, the library needs to be added to your application's project. This is a simple procedure
Create a folder called blobs in the top level of your project
Copy the libtensorflowlite_c-linux.so to this folder
Append following lines to your linux/CMakeLists.txt
...
# get tf lite binaries
install(
FILES ${PROJECT_BUILD_DIR}/../blobs/libtensorflowlite_c-linux.so
DESTINATION ${INSTALL_BUNDLE_DATA_DIR}/../blobs/
)
Windows
For Windows a TensorFlow Lite dynamic library needs to be added to the project manually.
For this, first a .dll needs to be built. You can follow the Bazel build guide or the CMake build guide to build the libraries.
As a second step, the library needs to be added to your application's project. This is a simple procedure
Create a folder called blobs in the top level of your project
Copy the libtensorflowlite_c-win.dll to this folder
Append following lines to your windows/CMakeLists.txt
...
# get tf lite binaries
install(
FILES ${PROJECT_BUILD_DIR}/../blobs/libtensorflowlite_c-win.dll
DESTINATION ${INSTALL_BUNDLE_DATA_DIR}/../blobs/
)
TFLite Flutter Helper Library
The helper library has been deprecated. New development underway for a replacement at https://github.com/google/flutter-mediapipe. Current timeline is to have wide support by the end of August, 2023.
In the dependency section of pubspec.yaml file, add tflite_flutter: ^0.10.1 (adjust the version accordingly based on the latest release)
Creating the Interpreter
From asset
Place your_model.tflite in assets directory. Make sure to include assets in pubspec.yaml.
final interpreter = awaitInterpreter.fromAsset('assets/your_model.tflite');
Refer to the documentation for info on creating interpreter from buffer or file.
Performing inference
For single input and output
Use void run(Object input, Object output).
// For ex: if input tensor shape [1,5] and type is float32var input = [[1.23, 6.54, 7.81, 3.21, 2.22]];
// if output tensor shape [1,2] and type is float32var output = List.filled(1*2, 0).reshape([1,2]);
// inference
interpreter.run(input, output);
// print the outputprint(output);
For multiple inputs and outputs
Use void runForMultipleInputs(List<Object> inputs, Map<int, Object> outputs).
To utilize asynchronous inference, first create your Interpreter and then wrap it with IsolateInterpreter.
final interpreter = awaitInterpreter.fromAsset('assets/your_model.tflite');
final isolateInterpreter =
awaitIsolateInterpreter.create(address: interpreter.address);
Both run and runForMultipleInputs methods of isolateInterpreter are asynchronous:
By using IsolateInterpreter, the inference runs in a separate isolate. This ensures that the main isolate, responsible for UI tasks, remains unblocked and responsive.
Contribute to this package
This package is managed using melos. Before starting to work on the project, make sure to run the bootstrap command.
dart pub global activate melos # Install or activate melos globally
melos bootstrap # Initialize the workspace and bootstrap the package
Generated code
This package uses ffigen to generate FFI bindings. To run code generation, you can use the following melos command: