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!
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.
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: 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:
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.
import 'package:tflite_flutter/tflite_flutter.dart';
In the dependency section of pubspec.yaml
file, add tflite_flutter: ^0.10.1
(adjust the version accordingly based on the latest release)
From asset
Place your_model.tflite
in assets
directory. Make sure to include assets in pubspec.yaml
.
final interpreter = await tfl.Interpreter.fromAsset('assets/your_model.tflite');
Refer to the documentation for info on creating interpreter from buffer or file.
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();
IsolateInterpreter
To utilize asynchronous inference, first create your Interpreter
and then wrap it with IsolateInterpreter
.
final interpreter = await Interpreter.fromAsset('assets/your_model.tflite');
final isolateInterpreter =
await IsolateInterpreter.create(address: interpreter.address);
Both run
and runForMultipleInputs
methods of isolateInterpreter
are asynchronous:
await isolateInterpreter.run(input, output);
await isolateInterpreter.runForMultipleInputs(inputs, outputs);
By using IsolateInterpreter
, the inference runs in a separate isolate. This ensures that the main isolate, responsible for UI tasks, remains unblocked and responsive.
Run this command:
With Flutter:
$ flutter pub add tflite_flutter
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
tflite_flutter: ^0.10.1
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:tflite_flutter/tflite_flutter.dart';
Author: tensorflow
Source: https://github.com/tensorflow/flutter-tflite
License: Apache-2.0 license