High-performance TensorFlow Lite library for React Native with GPU acceleration
High-performance TensorFlow Lite library for React Native with GPU acceleration
A high-performance TensorFlow Lite library for React Native, built with Nitro Modules.
If you are upgrading from v2, see the Migration Guide for breaking changes and upgrade steps.
yarn add react-native-fast-tflite react-native-nitro-modules
metro.config.js, add tflite as a supported asset extension:module.exports = {
// ...
resolver: {
assetExts: ['tflite', // ...
// ...
This allows you to drop .tflite files into your app and swap them out at runtime without rebuilding. yarn android / npx pod-install && yarn ios)Find a TensorFlow Lite (.tflite) model. There are thousands of public models on tfhub.dev.
Drag your model into your app's asset folder (e.g. src/assets/my-model.tflite)
Load the Model:
// Option A: Standalone Function
const model = await loadTensorflowModel(require('assets/my-model.tflite'), [])
// Option B: Hook in a Function Component
const plugin = useTensorflowModel(require('assets/my-model.tflite'), [])
Call the Model:
const inputData: ArrayBuffer = ...
const outputData = await model.run([inputData])
console.log(outputData)
Models can be loaded from the React Native bundle via require(..), or any URI/URL (http://.. or file://..):
// Asset from React Native Bundle
loadTensorflowModel(require('assets/my-model.tflite'), [])
// File on the local filesystem
loadTensorflowModel({ url: 'file:///var/mobile/.../my-model.tflite' }, [])
// Remote URL
loadTensorflowModel(
{ url: 'https://tfhub.dev/google/lite-model/object_detection_v1.tflite' },
[]
)
Loading a Model is asynchronous since buffers need to be allocated. Make sure to handle errors when loading.
TensorFlow uses tensors as input and output. Since TensorFlow Lite is optimized for fixed-size byte buffers, you are responsible for interpreting the raw data yourself.
Input and output values are passed as ArrayBuffer. To inspect tensor shapes, open your model in Netron.
For example, the object_detection_mobile_object_localizer_v1_1_default_1.tflite model on tfhub.dev has 1 input tensor and 4 output tensors:
In the description on tfhub.dev we can find the description of all tensors:
From that we know we need a 192 x 192 input image with 3 bytes per pixel (RGB).
If you're using this model with a VisionCamera Frame Processor, you need to convert the Frame to the model's expected input size. Use vision-camera-resizer to do the conversion:
…
[!NOTE] Unlike v4, VisionCamera v5 no longer requires boxing the model with
NitroModules.box(). Since v5 is built on Nitro Modules and uses react-native-worklets, worklets can access HybridObjects like the TFLite model directly.
GPU Delegates offer faster, GPU-accelerated computation. There are multiple delegates available:
Use the config plugin in your expo config (app.json, app.config.json or app.config.js):
{
"name": "my app",
"plugins": [
[
"react-native-fast-tflite",
{
"enableCoreMLDelegate": true
}
]
]
}
Set $EnableCoreMLDelegate to true in your Podfile:
$EnableCoreMLDelegate=true
# rest of your podfile...
Open your iOS project in Xcode and add the CoreML framework under General → Frameworks, Libraries and Embedded Content.
Re-install Pods and build:
cd ios && pod install && cd ..
yarn ios
Use the CoreML Delegate:
const model = await loadTensorflowModel(
require('assets/my-model.tflite'),
['core-ml']
)
[!NOTE] Not all model operations are supported on the CoreML delegate. Make sure your model is compatible.
To enable GPU or NNAPI on Android, you may need to include native libraries, especially on Android 12+.
Use the config plugin in your expo config (app.json, app.config.json or app.config.js) with enableAndroidGpuLibraries:
{
"name": "my app",
"plugins": [
[
"react-native-fast-tflite",
{
"enableAndroidGpuLibraries": true
}
]
]
}
By default, when enabled, libOpenCL.so will be included in your AndroidManifest.xml. You can also include more libraries by passing an array:
{
"name": "my app",
"plugins": [
[
"react-native-fast-tflite",
{
"enableAndroidGpuLibraries": ["libOpenCL-pixel.so", "libGLES_mali.so"]
}
]
]
}
[!NOTE] For Expo, remember to run prebuild if the library is not yet included in your
AndroidManifest.xml.
Add any needed entries to your AndroidManifest.xml:
Then use the delegate:
const model = await loadTensorflowModel(
require('assets/my-model.tflite'),
['android-gpu']
)
// or
const model = await loadTensorflowModel(
require('assets/my-model.tflite'),
['nnapi']
)
[!WARNING] NNAPI is deprecated on Android 15. GPU delegate is preferred.
[!NOTE] Android does not officially support OpenCL, but most GPU vendors do.
Join the Margelo Community Discord to chat about react-native-fast-tflite or other Margelo libraries.
This library is provided as is, I work on it in my free time.
If you're integrating react-native-fast-tflite in a production app, consider funding this project and contact me to receive premium enterprise support, help with issues, prioritize bugfixes, request features, and more.
gcc, cmake and python/python3. See the TensorFlow documentation on what you need exactly.yarn bootstrap and select y on all iOS and Android related questions.example/ios/TfliteExample.xcworkspaceexample/androidSee the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
No open issues yet, or sync has not completed.