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.
- Multi-platform Support for Android, iOS, Windows, Mac, Linux.
- Flexibility to use any TFLite Model.
- Acceleration using multi-threading and delegate support.
- Similar structure as TensorFlow Lite Java API.
- Inference speeds close to native Android Apps built using the Java API.
- You can choose to use any TensorFlow version by building binaries locally.
- Run inference in different isolates to prevent jank in UI thread.
tflite_flutter:
git:
url: https://github.com/hoomanmmd/tflite_flutter.git
ref: master
To run on Simulator, Update end of your PodFile
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'i386 arm64'
end
end
end
A dedicated library with simple architecture for processing and manipulating input and output of TFLite Models. API design and documentation is identical to the TensorFlow Lite Android Support Library. Strongly recommended to be used with tflite_flutter_plugin
. Learn more.
Title | Code | Demo | Blog |
---|---|---|---|
Text Classification App | Code | ![]() |
Blog/Tutorial |
Image Classification App | Code | ![]() |
- |
Object Detection App | Code | ![]() |
Blog/Tutorial |
Reinforcement Learning App | Code | ![]() |
Blog/Tutorial |
import 'package:tflite_flutter/tflite_flutter.dart';
-
From asset
Place
your_model.tflite
inassets
directory. Make sure to include assets inpubspec.yaml
.final interpreter = await tfl.Interpreter.fromAsset('your_model.tflite');
Refer to the documentation for info on creating interpreter from buffer or file.
See TFLite Flutter Helper Library for easy processing of input and output.
-
For single input and output
Use
void run(Object input, Object output)
.// For ex: if input tensor shape [1,5] and type is float32 var input = [[1.23, 6.54, 7.81. 3.21, 2.22]]; // if output tensor shape [1,2] and type is float32 var output = List.filled(1*2, 0).reshape([1,2]); // inference interpreter.run(input, output); // print the output print(output);
-
For multiple inputs and outputs
Use
void runForMultipleInputs(List<Object> inputs, Map<int, Object> outputs)
.var input0 = [1.23]; var input1 = [2.43]; // input: List<Object> var inputs = [input0, input1, input0, input1]; var output0 = List<double>.filled(1, 0); var output1 = List<double>.filled(1, 0); // output: Map<int, Object> var outputs = {0: output0, 1: output1}; // inference interpreter.runForMultipleInputs(inputs, outputs); // print outputs print(outputs)
interpreter.close();
Note: This feature is under testing and could be unstable with some builds and on some devices.
-
NNAPI delegate for Android
var interpreterOptions = InterpreterOptions()..useNnApiForAndroid = true; final interpreter = await Interpreter.fromAsset('your_model.tflite', options: interpreterOptions);
or
var interpreterOptions = InterpreterOptions()..addDelegate(NnApiDelegate()); final interpreter = await Interpreter.fromAsset('your_model.tflite', options: interpreterOptions);
-
GPU delegate for Android and iOS
-
Android GpuDelegateV2
final gpuDelegateV2 = GpuDelegateV2( options: GpuDelegateOptionsV2( false, TfLiteGpuInferenceUsage.fastSingleAnswer, TfLiteGpuInferencePriority.minLatency, TfLiteGpuInferencePriority.auto, TfLiteGpuInferencePriority.auto, )); var interpreterOptions = InterpreterOptions()..addDelegate(gpuDelegateV2); final interpreter = await Interpreter.fromAsset('your_model.tflite', options: interpreterOptions);
-
iOS Metal Delegate (GpuDelegate)
final gpuDelegate = GpuDelegate( options: GpuDelegateOptions(true, TFLGpuDelegateWaitType.active), ); var interpreterOptions = InterpreterOptions()..addDelegate(gpuDelegate); final interpreter = await Interpreter.fromAsset('your_model.tflite', options: interpreterOptions);
-
Refer Tests to see more example code for each method.
- Tian LIN, Jared Duke, Andrew Selle, YoungSeok Yoon, Shuangfeng Li from the TensorFlow Lite Team for their invaluable guidance.
- Authors of dart-lang/tflite_native.