CLIP
This document describes how to run CLIP on the NPU.
Refer to Model Zoo Download for the example.
The CLIP example converts the image model and the text model separately, then computes image-text similarity on the board.
CLIP example directory structure:
$ tree ./
./
├── CMakeLists.txt
├── images_convert_model
├── text_convert_model
│ └── python
├── model
│ ├── clip-images_uint8_a733.nb
│ ├── clip-text_int16_a733.nb
│ ├── demo.png
│ ├── demo.txt
│ └── merges.txt
├── main.cpp
└── README.md
Model Conversion
Enter the container development environment first. See Create and Start Container in the Model Zoo download page.
Select the Docker image that matches the NPU:
- A733: ubuntu-npu:v2.0.10.2
- T527: ubuntu-npu:v1.8.13
Download the floating-point ONNX models from the Allwinner netdisk:
- clip-images: http://netstorage.allwinnertech.com:5000/sharing/DPFhNJzGl
- clip-text: http://netstorage.allwinnertech.com:5000/sharing/neeTHfl6P
Put clip-images.onnx in images_convert_model and clip-text.onnx in text_convert_model.
docker exec -it model-zoo /bin/bash
Convert clip-images
cd /workspace/examples/clip/images_convert_model/
./convert_model_env.sh
./pegasus_import.sh clip-images
./pegasus_quantize.sh clip-images uint8 10
- A733
- T527
./pegasus_export_ovx_nbg.sh clip-images uint8 a733
./pegasus_export_ovx_nbg.sh clip-images uint8 t527
Convert clip-text
cd /workspace/examples/clip/text_convert_model/
./convert_model_env.sh
./pegasus_import.sh clip-text
./pegasus_quantize.sh clip-text int16 10
- A733
- T527
./pegasus_export_ovx_nbg.sh clip-text int16 a733
./pegasus_export_ovx_nbg.sh clip-text int16 t527
The exported models are stored in the ../model directory.
Build the Example
Then compile the example. Exit the container first, then run the commands below.
Configure the third-party library and the cross-compilation toolchain first.
Skip this step if you have already configured them in another example.
cd ../../../3rdparty/opencv/
unzip opencv-4.9.0-aarch64-linux-sunxi-glibc.zip
cd ../../0-toolchains/
Download the toolchain from this link, put it in 0-toolchains/, then run:
tar -xvf gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu.tar.xz
cd ../examples/clip/
- A733
- T527
../build_linux.sh -t a733 -s debian11
../build_linux.sh -t t527 -s debian11
Model Deployment
After compilation, the example will be installed in the install directory. You can use scp to transfer it to the board.
Configure NPU Driver
You can skip this step if you have already configured NPU driver in other examples.
Transfer the driver library to the board's lib directory via scp.
- A733 corresponds to the common/npuruntime/lib_linux_aarch64/A733 directory
- T527 corresponds to the common/npuruntime/lib_linux_aarch64/T527 directory
Then execute the following command to export to environment variables.
echo 'export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
Run Example
After configuring the driver, you can run the example.
For T527 platform, you need to first enable NPU by referring to the A5E's "Enable NPU on Board" documentation, then use the following command to grant the current user permission to use /dev/vipcore.
sudo chmod 777 /dev/vipcore
- A733
- T527
cd clip_demo_linux_a733/
chmod +x ./clip_demo_a733
./clip_demo_a733 -ib model/clip-images_uint8_a733.nb -tb model/clip-text_int16_a733.nb -i model/demo.png -t model/demo.txt
The running result is as follows:
$ ./clip_demo_a733 -ib model/clip-images_uint8_a733.nb -tb model/clip-text_int16_a733.nb -i model/demo.png -t model/demo.txt
text_model_file=model/clip-text_int16_a733.nb, images_model_file=model/clip-images_uint8_a733.nb, input_images_file=model/demo.png, input_text_file=model/demo.txt, loop_count=1, malloc_mbyte=10
VIPLite driver software version 2.0.3.2-AW-2024-08-30
nbg name=model/clip-images_uint8_a733.nb, size: 63747488.
create network 0: 34340 us.
nbg name=model/clip-text_int16_a733.nb, size: 115039816.
create network 1: 60155 us.
prepare network: 2633 us.
prepare network: 1587 us.
images: model/demo.png
text : a photo of a motorcycle
score : 0.998
clip-text total infer time: 35368 us.
clip-images total infer time: 13695 us.
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner A733 | Vivante VIP9000 | clip-images | 224×224 | 34.3 ms | 2.6 ms | 13.7 ms | 50.6 ms | 73.0 FPS | |
| Allwinner A733 | Vivante VIP9000 | clip-text | 1×20 | 60.2 ms | 1.6 ms | 35.4 ms | 97.2 ms | 28.3 FPS |
cd clip_demo_linux_t527/
chmod +x ./clip_demo_t527
./clip_demo_t527 -ib model/clip-images_uint8_t527.nb -tb model/clip-text_int16_t527.nb -i model/demo.png -t model/demo.txt
The running result is as follows:
$ ./clip_demo_t527 -ib model/clip-images_uint8_t527.nb -tb model/clip-text_int16_t527.nb -i model/demo.png -t model/demo.txt
text_model_file=model/clip-text_int16_t527.nb, images_model_file=model/clip-images_uint8_t527.nb, input_images_file=model/demo.png, input_text_file=model/demo.txt, loop_count=1, malloc_mbyte=10
VIPLite driver software version 1.13.0.0-AW-2023-10-19
nbg name=model/clip-images_uint8_t527.nb, size: 64787840.
create network 0: 74736 us.
nbg name=model/clip-text_int16_t527.nb, size: 114667200.
create network 1: 131392 us.
prepare network: 8988 us.
prepare network: 12743 us.
clip-text total infer time: 113006 us.
images: model/demo.png
text : a photo of a motorcycle
score : 0.997
clip-images total infer time: 78735 us.
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner T527 | Vivante VIP9000 | clip-images | 224×224 | 74.7 ms | 9.0 ms | 78.7 ms | 162.4 ms | 12.7 FPS | |
| Allwinner T527 | Vivante VIP9000 | clip-text | 1×20 | 131.4 ms | 12.7 ms | 113.0 ms | 257.1 ms | 8.8 FPS |