Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:vim4:npu:demos:yolov7-tiny

YOLOv7-tiny VIM4 Demo - 1

Train the model

Download the YOLOv7 official code WongKinYiu/yolov7

$ git clone https://github.com/WongKinYiu/yolov7

Refer README.md to create and train a YOLOv7 tiny model.

Convert the model

Build a virtual environment

Follow Docker official documentation to install Docker: Install Docker Engine on Ubuntu.

Then fetch the prebuilt NPU Docker container and run it.

$ docker pull yanwyb/npu:v1
$ docker run -it --name vim4-npu1 -v $(pwd):/home/khadas/npu \
				-v /etc/localtime:/etc/localtime:ro \
				-v /etc/timezone:/etc/timezone:ro \
				yanwyb/npu:v1

Get the conversion tool

Download The conversion tool from khadas/vim4_npu_sdk.

$ git clone https://gitlab.com/khadas/vim4_npu_sdk

Convert

After training the model, modify yolov7/models/yolo.py as follows.

diff --git a/models/yolo.py b/models/yolo.py
index 95a019c..98ae95c 100644
--- a/models/yolo.py
+++ b/models/yolo.py
@@ -144,7 +144,10 @@ class IDetect(nn.Module):
         for i in range(self.nl):
             x[i] = self.m[i](x[i])  # conv
             bs, _, ny, nx = x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)
-            x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
 
             if not self.training:  # inference
                 if self.grid[i].shape[2:4] != x[i].shape[2:4]:

Then, run export.py to convert the model to ONNX.

$ python export.py

Enter vim4_npu_sdk/demo and modify convert_adla.sh as follows.

convert_adla.sh
#!/bin/bash
 
ACUITY_PATH=../bin/
#ACUITY_PATH=../python/tvm/
adla_convert=${ACUITY_PATH}adla_convert
 
 
if [ ! -e "$adla_convert" ]; then
    adla_convert=${ACUITY_PATH}adla_convert.py
fi
 
$adla_convert --model-type onnx \
        --model ./model_source/yolov7_tiny/yolov7_tiny.onnx \
        --inputs "images" \
        --input-shapes  "3,640,640"  \
        --dtypes "float32" \
        --quantize-dtype int8 --outdir onnx_output  \
        --channel-mean-value "0,0,0,255"  \
        --source-file dataset.txt  \
        --batch-size 1 --target-platform PRODUCT_PID0XA003

Run convert_adla.sh to generate the VIM4 model. The converted model is xxx.adla in onnx_output.

$ bash convert_adla.sh

Run inference on the NPU

Get source code

Clone the source code khadas/vim4_npu_applications.

$ git clone https://github.com/khadas/vim4_npu_applications

Install dependencies

$ sudo apt update
$ sudo apt install libopencv-dev python3-opencv cmake

Compile and run

Picture input demo

Put yolov7_tiny_int8.adla in vim4_npu_applications/yolov7_tiny/data/.

# Compile
$ cd vim4_npu_applications/yolov7_tiny
$ mkdir build
$ cd build
$ cmake ..
$ make
 
# Run
$ sudo ./yolov7_tiny -m ../data/yolov7_tiny_int8.adla -p ../data/horses.jpg

Camera input demo

Put yolov7_tiny_int8.adla in vim4_npu_applications/yolov7_tiny_cap/data/.

# Compile
$ cd vim4_npu_applications/yolov7_tiny_cap
$ mkdir build
$ cd build
$ cmake ..
$ make
 
# Run
$ sudo ./yolov7_tiny_cap -m ../data/yolov7_tiny_int8.adla -d 0 -w 1920 -h 1080

0 is the camera device index.

If your YOLOv7-tiny model classes are not the same as COCO, please change data/coco_80_labels_list.txt and the OBJ_CLASS_NUM in include/postprocess.h.

Last modified: 2024/01/04 21:10 by nick