中文 | English
The Nuclei AI Models repository collects dozens of TFLite models. Combined with nuclei-sdk, users can quickly deploy and run these models on Nuclei Cores or Nuclei QEMU, and observe their performance.
- Model training and quantization are not included. Users must either train and quantize models themselves, or download existing TFLite models.
- Currently, only int8 and int16 TFLite models are supported.
- Image Classification (image_classification)
- Object Detection (object_detection)
- License Plate Recognition (LPRNet)
- Pose Estimation (Gesture Detection)
- Keyword Recognition (audio)
- Instance Segmentation (instance_segmentation)
- Semantic Segmentation (semantic_segmentation)
...
Follow the steps below:
$ git clone https://github.com/Nuclei-Software/nuclei-ai-modelsRun the setup script to automatically download required dependencies and configure the toolchain path:
$ cd nuclei-ai-models
$ source setup.shThis script performs the following tasks:
- Downloads nuclei_studio and nuclei-sdk into the
downloadsfolder. - Downloads tflm into
downloads/nuclei-sdk/Components. - Sets the toolchain environment path.
Take the MNIST model as an example, running it on Nuclei QEMU:
$ cd modelzoo/image_classification/mnist/src
$ make SOC=evalsoc CORE=n300fd clean
$ make SOC=evalsoc CORE=n300fd run_qemuSample log output (Predict num is 7, indicating successful execution):
Run program mnist.elf on qemu-system-riscv32
qemu-system-riscv32 -M nuclei_evalsoc,download=ddr -cpu nuclei-n300fd,ext= -smp 1 -icount shift=0 -nodefaults -nographic -serial stdio -kernel mnist.elf
Nuclei SDK Build Time: Aug 13 2025, 11:04:46
Download Mode: DDR
CPU Frequency 1000005632 Hz
CPU HartID: 0
Predict result is 7, precision: 0.988281
Users can train and quantize a new int8/int16 TFLite model and deploy it in this repository. Follow the steps below:
For example, using the MNIST model, you have trained and quantized a model named: mnist_int8.tflite.
Alternatively, download a pre-quantized MNIST model from the web.
Following the structure of existing examples, create a new MNIST demo under the modelzoo directory.
The directory structure should be:
pretrained_models # Store TFLite models
script # Python inference scripts
src # Embedded/MCU-side inference code
README.md # Description of the use casePlace the mnist_int8.tflite model into the pretrained_models folder. Write a Python script in the script folder to run inference using TensorFlow, so you can verify the model's performance on the host side.
First, install required dependencies:
$ cd nuclei-ai-models
$ python -m pip install -r requirement.txtRun inference on the host:
$ cd mnist/script
$ python tflite_prediction.pyHost-side log output:
img.shape is (28, 28)
x_test.shape is (1, 28, 28, 1)
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
The result is: 7 Probability: 0.98828125
Use the Linux xxd tool to convert the model into a hexadecimal C array:
$ xxd -i mnist_int8.tflite > mnist_int8.ccThis generates a C array like:
unsigned char ___pretrained_models_mnist_int8_tflite[] = {
0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x14, 0x00, 0x20, 0x00,
0x1c, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x08, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x58, 0x2a, 0x00, 0x00,
0x68, 0x2a, 0x00, 0x00, 0x10, 0x4d, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
...
};
unsigned int ___pretrained_models_mnist_int8_tflite_len = 19944;Similarly, serialize the test image for inference:
$ cd mnist/script
$ python im2array.pyThis generates test_image_provider.cc.
Copy the generated mnist_int8.cc and test_image_provider.cc into the src folder. Write the inference code by referring to existing examples.
To register required operators, use the provided Python script for assistance:
$ python /path/to/nuclei-ai-models/common/scripts/generate_op_resolver_code.py mnist_int8.tfliteThe script generates code like:
Operator number is 7
if (op_resolver.AddQuantize() != kTfLiteOk) {
printf("Add Quantize fail!\n");
return;
}
if (op_resolver.AddConv2D() != kTfLiteOk) {
printf("Add Conv2D fail!\n");
return;
}
if (op_resolver.AddDepthwiseConv2D() != kTfLiteOk) {
printf("Add DepthwiseConv2D fail!\n");
return;
}
if (op_resolver.AddMean() != kTfLiteOk) {
printf("Add Mean fail!\n");
return;
}
if (op_resolver.AddFullyConnected() != kTfLiteOk) {
printf("Add FullyConnected fail!\n");
return;
}
if (op_resolver.AddSoftmax() != kTfLiteOk) {
printf("Add Softmax fail!\n");
return;
}
if (op_resolver.AddDequantize() != kTfLiteOk) {
printf("Add Dequantize fail!\n");
return;
}You can copy and paste this into your inference code.
After writing the inference code, create a Makefile. You can refer to existing examples.
Example Makefile:
TARGET = mnist
AI_MODELS_ROOT := ../../../../
# REBUILD_TFLM = 0: Use pre-built TFLM static library (supported for n300fd and nx900fd)
# REBUILD_TFLM = 1: Compile TFLM from source (slower)
REBUILD_TFLM ?= 1
CORE ?= n300fd
ARCH_EXT ?= v
DOWNLOAD ?= ddr
SRCDIRS = .
INCDIRS = .
NMSIS_LIB := nmsis_nn
STDCLIB ?= newlib_small
COMMON_FLAGS := -O3
include $(AI_MODELS_ROOT)/modelzoo/Makefile.commonNote:
Since compiling TFLM from source is time-consuming, theREBUILD_TFLMflag allows you to choose between using a static library or building from source.
IfCOREis set ton300fdornx900fdand no additional extensions are enabled (i.e.,ARCH_EXTis empty), you can setREBUILD_TFLM ?= 0to use the static library and reduce build time. Otherwise, setREBUILD_TFLM ?= 1.
You can run the demo on a Nuclei Core or Nuclei QEMU. Here is an example using QEMU:
$ cd modelzoo/image_classification/mnist/src
$ make SOC=evalsoc CORE=n300fd clean
$ make SOC=evalsoc CORE=n300fd run_qemu