Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

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

Doc for version ddk-3.4.7.7

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.

Follow the script below to get Docker image:

docker pull numbqq/npu-vim4

Get the conversion tool

Download The conversion tool from khadas/vim4_npu_sdk.

$ git lfs install
$ git lfs clone https://gitlab.com/khadas/vim4_npu_sdk.git
$ cd vim4_npu_sdk
$ ls
adla-toolkit-binary  adla-toolkit-binary-3.1.7.4  convert-in-docker.sh  Dockerfile  docs  README.md
  • adla-toolkit-binary/docs - SDK documentations
  • adla-toolkit-binary/bin - SDK tools required for model conversion
  • adla-toolkit-binary/demo - Conversion examples

If your kernel is older than 241129, please use version before tag ddk-3.4.7.7.

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]:

yolo.py has many forward. Right place is class IDetect function fuseforward.

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"  \
        --inference-input-type "float32" \
        --inference-output-type "float32" \
        --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

If your kernel is older than 241129, please use version before tag ddk-3.4.7.7.

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
$ ./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
$ ./yolov7_tiny_cap -m ../data/yolov7_tiny_int8.adla -d 0

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/12/03 02:32 by louis