This is an old revision of the document!
Download the yolov7 official code WongKinYiu/yolov7
$ git clone https://github.com/WongKinYiu/yolov7.git
Refer README.md
to create and train a yolov7_tiny model.
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
Download The conversion tool from khadas/vim4_npu_sdk.
$ git clone https://gitlab.com/khadas/vim4_npu_sdk
After training 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 model to onnx.
$ python export.py
Enter vim4_npu_sdk/demo
and modify convert_adla.sh
as follows.
#!/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 VIM4 model. The converted model is xxx.adla
in onnx_output
.
$ bash convert_adla.sh
Clone the source code khadas/vim4_npu_applications.
$ git clone https://github.com/khadas/vim4_npu_applications
$ sudo apt update $ sudo apt install libopencv-dev python3-opencv cmake
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
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 camera device index.
If your yolov7_tiny model classes is not the same as coco, please change data/coco_80_labels_list.txt
and the OBJ_CLASS_NUM
in include/postprocess.h
.