This shows you the differences between two versions of the page.
— |
products:sbc:vim3:npu:ksnn:demos:yolov8n-pose [2025/06/06 05:47] (current) louis created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~tag> NPU YOLO KSNN VIM3 ~~ | ||
+ | ====== YOLOv8n-Pose KSNN Demo - 8 ====== | ||
+ | |||
+ | {{indexmenu_n> | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | YOLOv8n-Pose inherits the powerful object detection backbone and neck architecture of YOLOv8n. It extends the standard YOLOv8n object detection model by integrating dedicated pose estimation layers onto its head. This allows it to not only detect people (bboxes) but also simultaneously predict the spatial positions (keypoints) of their anatomical joints (e.g., shoulders, elbows, knees, ankles). | ||
+ | |||
+ | Inference results on VIM3. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | **Inference speed test**: USB camera about **182ms** per frame. MIPI camera about **156ms** per frame. | ||
+ | |||
+ | ===== Train the model ===== | ||
+ | |||
+ | Download the YOLOv8 official code. [[gh> | ||
+ | |||
+ | ```shell | ||
+ | $ git clone https:// | ||
+ | ``` | ||
+ | |||
+ | Refer '' | ||
+ | |||
+ | ===== Convert the model ===== | ||
+ | |||
+ | ==== Get the conversion tool ==== | ||
+ | |||
+ | ```shell | ||
+ | $ git lfs install | ||
+ | $ git lfs clone https:// | ||
+ | ``` | ||
+ | |||
+ | The KSNN conversion tool is under '' | ||
+ | |||
+ | ```shell | ||
+ | $ cd aml_npu_sdk/ | ||
+ | $ convert | ||
+ | ``` | ||
+ | |||
+ | ==== Convert ==== | ||
+ | |||
+ | After training model, modify **Class Detect** and **Class Pose** in '' | ||
+ | |||
+ | ```diff head.py | ||
+ | diff --git a/ | ||
+ | index 0b02eb3..0a6e43a 100644 | ||
+ | --- a/ | ||
+ | +++ b/ | ||
+ | @@ -42,6 +42,9 @@ class Detect(nn.Module): | ||
+ | |||
+ | def forward(self, | ||
+ | """ | ||
+ | + if torch.onnx.is_in_onnx_export(): | ||
+ | + return self.forward_export(x) | ||
+ | + | ||
+ | shape = x[0].shape | ||
+ | for i in range(self.nl): | ||
+ | x[i] = torch.cat((self.cv2[i](x[i]), | ||
+ | @@ -80,6 +83,15 @@ class Detect(nn.Module): | ||
+ | | ||
+ | | ||
+ | |||
+ | + def forward_export(self, | ||
+ | + results = [] | ||
+ | + for i in range(self.nl): | ||
+ | + dfl = self.cv2[i](x[i]).contiguous() | ||
+ | + cls = self.cv3[i](x[i]).contiguous() | ||
+ | + results.append(torch.cat([cls, | ||
+ | + return tuple(results) | ||
+ | + | ||
+ | |||
+ | @@ -255,6 +283,16 @@ class Pose(Detect): | ||
+ | def forward(self, | ||
+ | """ | ||
+ | bs = x[0].shape[0] | ||
+ | - kpt = torch.cat([self.cv4[i](x[i]).view(bs, | ||
+ | + if torch.onnx.is_in_onnx_export(): | ||
+ | + kpt = [self.cv4[i](x[i]) for i in range(self.nl)] | ||
+ | + else: | ||
+ | + kpt = torch.cat([self.cv4[i](x[i]).view(bs, | ||
+ | x = self.detect(self, | ||
+ | + | ||
+ | + if torch.onnx.is_in_onnx_export(): | ||
+ | + output = [] | ||
+ | + for i in range(self.nl): | ||
+ | + output.append((torch.cat([x[i], | ||
+ | + return output | ||
+ | ``` | ||
+ | |||
+ | <WRAP important> | ||
+ | If you pip-installed ultralytics package, you should modify in package. | ||
+ | </ | ||
+ | |||
+ | Create a python file written as follows to export ONNX model. | ||
+ | |||
+ | ```python export.py | ||
+ | from ultralytics import YOLO | ||
+ | model = YOLO(" | ||
+ | results = model.export(format=" | ||
+ | ``` | ||
+ | |||
+ | ```shell | ||
+ | $ python export.py | ||
+ | ``` | ||
+ | |||
+ | <WRAP important> | ||
+ | Use [[https:// | ||
+ | |||
+ | {{: | ||
+ | </ | ||
+ | |||
+ | Enter '' | ||
+ | |||
+ | ```shell | ||
+ | # uint8 | ||
+ | $ ./convert --model-name yolov8n_pose \ | ||
+ | --platform onnx \ | ||
+ | --model yolov8n_pose.onnx \ | ||
+ | --mean-values '0 0 0 0.00392156' | ||
+ | --quantized-dtype asymmetric_affine \ | ||
+ | --source-files ./ | ||
+ | --batch-size 1 \ | ||
+ | --iterations 1 \ | ||
+ | --kboard VIM3 --print-level 0 | ||
+ | ``` | ||
+ | |||
+ | <WRAP important> | ||
+ | Now KSNN only supports '' | ||
+ | </ | ||
+ | |||
+ | If you want to use more quantified images, please modify '' | ||
+ | |||
+ | If you use '' | ||
+ | |||
+ | If run succeed, converted model and library will generate in '' | ||
+ | |||
+ | <WRAP important> | ||
+ | If your YOLOv8n-Pose model perform bad on board, please try quanfity model in int8 or int16. | ||
+ | ```shell | ||
+ | # int8 | ||
+ | $ ./convert --model-name yolov8n_pose \ | ||
+ | --platform onnx \ | ||
+ | --model yolov8n_pose.onnx \ | ||
+ | --mean-values '0 0 0 0.00392156' | ||
+ | --quantized-dtype dynamic_fixed_point \ | ||
+ | --qtype int8 \ | ||
+ | --source-files ./ | ||
+ | --batch-size 1 \ | ||
+ | --iterations 1 \ | ||
+ | --kboard VIM3 --print-level 0 | ||
+ | |||
+ | # int16 | ||
+ | $ ./convert --model-name yolov8n_pose \ | ||
+ | --platform onnx \ | ||
+ | --model yolov8n_pose.onnx \ | ||
+ | --mean-values '0 0 0 0.00392156' | ||
+ | --quantized-dtype dynamic_fixed_point \ | ||
+ | --qtype int16 \ | ||
+ | --source-files ./ | ||
+ | --batch-size 1 \ | ||
+ | --iterations 1 \ | ||
+ | --kboard VIM3 --print-level 0 | ||
+ | ``` | ||
+ | </ | ||
+ | |||
+ | ===== Run inference on the NPU by KSNN ===== | ||
+ | |||
+ | ==== Install KSNN ==== | ||
+ | |||
+ | Download KSNN library and demo code. [[gh> | ||
+ | |||
+ | ```shell | ||
+ | $ git clone --recursive https:// | ||
+ | $ cd ksnn/ksnn | ||
+ | $ pip3 install ksnn-1.3-py3-none-any.whl | ||
+ | ``` | ||
+ | |||
+ | If your kernel version is 5.15, use '' | ||
+ | |||
+ | ==== Install dependencies ==== | ||
+ | |||
+ | ```shell | ||
+ | $ pip3 install matplotlib | ||
+ | ``` | ||
+ | |||
+ | Put '' | ||
+ | |||
+ | ==== Picture input demo ==== | ||
+ | |||
+ | ```shell | ||
+ | $ cd ksnn/ | ||
+ | $ python3 yolov8n-pose-picture.py --model ./ | ||
+ | ``` | ||
+ | |||
+ | === Camera input demo === | ||
+ | |||
+ | For USB camera. | ||
+ | |||
+ | ```shell | ||
+ | # usb | ||
+ | $ cd ksnn/ | ||
+ | $ python3 yolov8n-pose-cap.py --model ./ | ||
+ | ``` | ||
+ | |||
+ | For MIPI camera, OpenCV do not support GSTREAMER by **pip install**. So you need to install OpenCV by **sudo apt install**. | ||
+ | |||
+ | ```shell | ||
+ | # mipi | ||
+ | $ pip3 uninstall opencv-python numpy | ||
+ | $ sudo apt install python3-opencv | ||
+ | $ pip3 install numpy==1.23 | ||
+ | $ cd ksnn/ | ||
+ | $ python3 yolov8n-pose-cap.py --model ./ | ||
+ | ``` | ||
+ | |||
+ | '' |