For the complete documentation index, see llms.txt. This page is also available as Markdown.

TensorFlow Lite

TensorFlow Lite 2.8 inference for mobile and edge deployment

TensorFlow Lite

The nd4j-tensorflow-lite module integrates TensorFlow Lite (TFLite) 2.8 for inference on mobile and edge devices. It accepts .tflite flatbuffer models and runs them via the TFLite C++ interpreter, accessed from Java through JavaCPP bindings.

TFLite models are optimized for constrained environments: they are smaller, faster, and use less memory than full TF models. Android deployment and ARM-based edge hardware are primary use cases.


When to Use TFLite

Scenario
Recommended

Android deployment

Yes

Edge device (Raspberry Pi, Jetson Nano)

Yes

Quantized model (INT8, FLOAT16)

Yes

Server-side inference with large models

No — use ORT or SameDiff

Model inspection or training

No — use SameDiff


Maven Dependency

<dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-tensorflow-lite</artifactId>
    <version>${dl4j.version}</version>
</dependency>

The module includes TFLite 2.8 native libraries for Linux x86_64 and ARM64, and Android ARM64/ARM32.


Converting a Model to TFLite

From Keras / TF SavedModel

With INT8 Quantization

From a Frozen Graph


Loading and Running a TFLite Model

Use TFLiteRunner to load a .tflite model and run inference:


Finding Tensor Names

TFLite models expose tensor metadata. Inspect in Python before import:


Quantized Models

TFLite supports INT8 quantized models for faster inference on hardware with integer accelerators. When using a quantized model, ensure your input data is quantized appropriately:

For FLOAT16 quantized models, no special handling is needed — the TFLite interpreter dequantizes internally.


Complete Example: MobileNet V2 on Android

This example targets Android deployment. The Maven dependency is replaced with the Android-specific AAR in a Gradle build file, but the Java API is identical.


Troubleshooting

Unsupported op: TFLite has a restricted op set. If conversion fails or inference throws an error about an unsupported op, use converter.target_spec.supported_ops to include TFLITE_BUILTINS and SELECT_TF_OPS for a broader op set during Python conversion.

Model not found: on Android, place .tflite files in src/main/assets/. Access them via context.getAssets().open("model.tflite").

Wrong data type: quantized INT8 models require INT8 inputs. Float models require FLOAT32. Mismatches cause runtime errors.

Inference speed: if inference is slow on a device with a Neural Processing Unit (NPU), ensure the TFLite delegate for that hardware is enabled (e.g., NNAPI delegate on Android, GPU delegate). The nd4j-tensorflow-lite module uses the CPU delegate by default.

Last updated

Was this helpful?