~~tag> NPU YOLO OpenCV VIM4 ~~ ====== YOLOv7-tiny VIM4 Demo - 1 ====== {{indexmenu_n>1}} ===== Train the model ===== Download the YOLOv7 official code [[gh>WongKinYiu/yolov7]] ```shell $ 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: [[https://docs.docker.com/engine/install/ubuntu/|Install Docker Engine on Ubuntu]]. Then fetch the prebuilt NPU Docker container and run it. ```shell $ 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 [[gl>khadas/vim4_npu_sdk]]. ```shell $ git clone https://gitlab.com/khadas/vim4_npu_sdk ``` ==== Convert ==== After training the model, modify ''yolov7/models/yolo.py'' as follows. ```diff 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. ```shell $ python export.py ``` Enter ''vim4_npu_sdk/demo'' and modify ''convert_adla.sh'' as follows. ```bash 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''. ```shell $ bash convert_adla.sh ``` ===== Run inference on the NPU ===== ==== Get source code ==== Clone the source code [[gh>khadas/vim4_npu_applications]]. ```shell $ git clone https://github.com/khadas/vim4_npu_applications ``` ==== Install dependencies ==== ```shell $ 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/''. ```shell # 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/''. ```shell # 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''.