react-native-fast-tflite

margeloMIT活跃维护

在 React Native 里跑 TensorFlow Lite 模型的高性能绑定库,支持 GPU 加速,适合在手机上做目标检测、人脸识别等端侧推理。

🧬 High-performance TensorFlow Lite library for React Native with GPU acceleration

v3.0.1发布于 2026-04-21 10:01 UTC1,242star92fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
yarn add react-native-fast-tflite react-native-nitro-modules
在 metro.config.js 的 resolver.assetExts 中加入 'tflite'
硬件要求按 README 说明

README 未说明具体显存或 GPU 型号要求;GPU 加速需在 iOS 启用 CoreML delegate,Android 需在 AndroidManifest 声明 libOpenCL.so 等原生库,且并非所有模型算子都受 delegate 支持。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

在 React Native 里跑 TensorFlow Lite 模型的高性能库,支持 GPU 加速

在 React Native 里跑 TensorFlow Lite 模型的高性能绑定库,支持 GPU 加速,适合在手机上做目标检测、人脸识别等端侧推理。

核心能力

  • 基于 Nitro Modules 构建,使用零拷贝 ArrayBuffer 传递张量
  • 直接调用 TFLite C/C++ 核心 API 访问内存
  • 支持运行时热替换 .tflite 模型文件
  • 可启用 CoreML、Android GPU、NNAPI 等加速 delegate
  • 与 VisionCamera 帧处理器集成做实时推理

容易踩的坑

必须同时安装 react-native-nitro-modules,否则无法运行;需自行准备 .tflite 模型文件并拖入 assets;GPU delegate 需额外配置(Podfile 的 $EnableCoreMLDelegate 或 Expo 插件、Android 原生库声明),且模型算子可能不兼容;NNAPI 在 Android 15 已废弃,优先用 GPU delegate。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v3.0.1
  1. v3.0.1Release 3.0.1最新正式版2026-04-21 · 2026-04-21 10:01 UTC

    3.0.1 (2026-04-21)

    Bug Fixes

    • return created GPU delegate from getAndroidGPUDelegate (#184) (0a53098)
  2. v3.0.0Release 3.0.02026-04-14 · 2026-04-14 14:35 UTC
    查看更新说明

    3.0.0 (2026-04-14)

    In react-native-fast-tflite v3.0.0 we migrated the entire module to a Nitro Module! 🥳 While this brings two main benefits:

    • Support for new arch (+bridgeless), and old arch
    • Better performance and stability It mainly improves the reliability of the module. Mainly, ArrayBuffer is now a first-class supported parameter type thanks to Nitro, and we no longer have to rely on a manual JSI bindings - which honestly were kind of tedious and sometimes even crashed the app.

    Bug Fixes

    • Fix SIGSEV crashes from stale jsi::ArrayBuffer cache by migrating to Nitro ArrayBuffer (#172) (6f86153)
    • Support Bridgeless mode using RCTTurboModuleWithJSIBindings (#167) (f3eb957)

    Features

  3. v3.0.0-beta.0Release 3.0.0-beta.0pre-release2026-04-14 · 2026-04-14 11:13 UTC
    查看更新说明

    3.0.0-beta.0 (2026-04-14)

    Bug Fixes

    • support Bridgeless mode using RCTTurboModuleWithJSIBindings (#167) (f3eb957)

    Features

  4. v2.0.0Release 2.0.02026-01-13 · 2026-01-13 10:50 UTC
    查看更新说明

    2.0.0 (2026-01-13)

    Bug Fixes

    • Add 16KB page alignment and upgrade LiteRT to 1.4.0 (#163) (2959de9)
    • Move spec into src (15663b6)

    Features

    • Add support for Android 16KB page sizes (#145) (eec86aa)
  5. v1.6.1Release 1.6.12025-04-08 · 2025-04-08 08:31 UTC
    查看更新说明

    1.6.1 (2025-04-08)

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    A high-performance TensorFlow Lite library for React Native, built with Nitro Modules.

    • ⚡ Powered by Nitro Modules
    • 💨 Zero-copy ArrayBuffers
    • 🔧 Uses the low-level C/C++ TensorFlow Lite core API for direct memory access
    • 🔄 Supports swapping out TensorFlow Models at runtime
    • 🖥️ Supports GPU-accelerated delegates (CoreML/Metal/OpenGL)
    • 📸 Easy VisionCamera integration

    Migrating from v2

    If you are upgrading from v2, see the Migration Guide for breaking changes and upgrade steps.

    Installation

    1. Add the npm packages:
      yarn add react-native-fast-tflite react-native-nitro-modules
      
    2. In 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. 🔥
    3. (Optional) To enable GPU delegates, see Using GPU Delegates below.
    4. Run your app (yarn android / npx pod-install && yarn ios)

    Usage

    1. Find a TensorFlow Lite (.tflite) model. There are thousands of public models on tfhub.dev.

    2. Drag your model into your app's asset folder (e.g. src/assets/my-model.tflite)

    3. 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'), [])
      
    4. Call the Model:

      const inputData: ArrayBuffer = ...
      const outputData = await model.run([inputData])
      console.log(outputData)
      

    Loading Models

    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.

    Input and Output data

    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:

    Screenshot of netron.app inspecting the model

    In the description on tfhub.dev we can find the description of all tensors:

    Screenshot of tfhub.dev inspecting the model

    From that we know we need a 192 x 192 input image with 3 bytes per pixel (RGB).

    Usage (VisionCamera)

    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:

    import { Camera, useFrameOutput } from 'react-native-vision-camera'
    import { useResizer } from 'react-native-vision-camera-resizer'
    import { useTensorflowModel } from 'react-native-fast-tflite'
    
    const objectDetection = useTensorflowModel(require('object_detection.tflite'), [])
    
    // 1. Create a resizer that converts Frames to 192x192x3 (RGB, uint8)
    const { resizer } = useResizer({
      width: 192,
      height: 192,
      channelOrder: 'rgb',
      dataType: 'uint8',
    })
    
    const frameOutput = useFrameOutput({
      pixelFormat: 'yuv',
      onFrame(frame) {
        'worklet'
        if (objectDetection.state !== 'loaded' || resizer == null) {
          frame.dispose()
          return
        }
    
        // 2. Resize the Frame to the model's input size
        const resized = resizer.resize(frame)
        frame.dispose()
        const data = new Uint8Array(resized.getPixelBuffer())
        resized.dispose()
    
        // 3. Extract the exact slice of the underlying ArrayBuffer
        const inputBuffer = data.buffer.slice(
          data.byteOffset,
          data.byteOffset + data.byteLength
        )
    
        // 4. Run model with given input buffer synchronously
        const outputs = objectDetection.model.runSync([inputBuffer])
    
        // 5. Interpret outputs accordingly
        const detection_boxes = new Float32Array(outputs[0]!)
        const detection_classes = new Float32Array(outputs[1]!)
        const detection_scores = new Float32Array(outputs[2]!)
        const num_detections = new Float32Array(outputs[3]!)
        console.log(`Detected ${num_detections[0]} objects!`)
      },
    })
    
    return <Camera device="back" isActive={true} outputs={[frameOutput]} {...otherProps} />
    

    [!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.

    Using GPU Delegates

    GPU Delegates offer faster, GPU-accelerated computation. There are multiple delegates available:

    CoreML (iOS)
    Expo

    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
          }
        ]
      ]
    }
    
    Bare React Native
    1. Set $EnableCoreMLDelegate to true in your Podfile:

      $EnableCoreMLDelegate=true
      
      # rest of your podfile...
      
    2. Open your iOS project in Xcode and add the CoreML framework under General → Frameworks, Libraries and Embedded Content.

    3. Re-install Pods and build:

      cd ios && pod install && cd ..
      yarn ios
      
    4. 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.

    Android GPU/NNAPI (Android)

    To enable GPU or NNAPI on Android, you may need to include native libraries, especially on Android 12+.

    Expo

    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.

    Bare React Native

    Add any needed entries to your AndroidManifest.xml:

    <uses-native-library android:name="libOpenCL.so" android:required="false" />
    <uses-native-library android:name="libOpenCL-pixel.so" android:required="false" />
    <uses-native-library android:name="libGLES_mali.so" android:required="false" />
    <uses-native-library android:name="libPVROCL.so" android:required="false" />
    

    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.

    Community Discord

    Join the Margelo Community Discord to chat about react-native-fast-tflite or other Margelo libraries.

    Adopting at scale

    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.

    Contributing

    1. Clone the repo
    2. Make sure you have installed Xcode CLI tools such as gcc, cmake and python/python3. See the TensorFlow documentation on what you need exactly.
    3. Run yarn bootstrap and select y on all iOS and Android related questions.
    4. Open the example app and start developing
      • iOS: example/ios/TfliteExample.xcworkspace
      • Android: example/android

    See the contributing guide to learn how to contribute to the repository and the development workflow.

    License

    MIT