> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 模型移植最佳实践

> 使用 Qualcomm AI Runtime (QAIRT) SDK 导出并量化自定义 YOLOv8 模型,并在 Qualcomm 评估套件上运行它。

## 使用 QAIRT SDK 导出自定义 YOLOv8 模型

### 前提条件

在装有 `Python >= 3.10` 和 `PyTorch >=1.8` 的主机上安装 Qualcomm AI Runtime SDK。

有关更多详细信息,请参阅[安装 Qualcomm AI Runtime SDK](../topic/qairt-install)。

在主机上运行以下命令。

1. 激活您的虚拟环境。

   ```shell theme={null}
   source <venv_path>/bin/activate
   ```

2. 安装 Ultralytics 软件包并导出 ONNX 模型。
   ```shell theme={null}
   pip install ultralytics
   ```
   ```shell theme={null}
   yolo export model=yolov8s.pt imgsz=320 format=onnx opset=11 optimize=True simplify=true
   ```

### 步骤

1. 将 ONNX 模型转换为 DLC。
   ```shell theme={null}
   snpe-onnx-to-dlc -i yolov8s.onnx
   ```

2. 生成量化 DLC。

   1. 准备校准数据集。
   2. 收集 5-10 张训练时使用的图像,并将这些图像保存到输入目录。
   3. 使用 `preprocess.py` 脚本将 `.jpg` 图像转换为量化所需的 RAW 文件。

      <Note>
        在本示例中,模型使用 320x320 的输入尺寸。
      </Note>

      1. 按如下方式下载脚本:
         ```shell theme={null}
         wget https://raw.githubusercontent.com/qualcomm/sample-apps-for-qualcomm-linux/refs/heads/main/qualcomm-linux/scripts/preprocess.py
         ```
      2. 使用以下选项运行脚本:
         ```shell theme={null}
         python preprocess.py <INPUT PATH> <OUTPUT PATH> 1 0
         ```
         * `<INPUT PATH>`: 包含原始图像的文件夹
         * `<OUTPUT PATH>`: 用于生成 RAW 文件的文件夹
   4. 创建一个 `input.txt` 文件,其中包含所有已生成 RAW 文件的路径。

      量化过程需要此文件。

3. 量化模型,使用 `snpe-dlc-quantize` 将模型转换为量化 DLC。
   ```shell theme={null}
   snpe-dlc-quantize --input_dlc yolov8s.dlc --input_list input.txt
   ```

## 运行演示

1. 下载标签文件。请参阅[下载模型和标签文件](../topic/classify-objects-with-default-model#download-model-and-label-files)。

2. 在主机上,设置用户环境变量:
   ```shell theme={null}
   export USER=root
   ```

3. 将测试视频文件推送到设备的 `/etc/media`。
   ```shell theme={null}
   scp <video file> $USER@<IP address of target device>:/etc/media/
   ```

4. 将量化后的 YoloV8 模型推送到设备。
   ```shell theme={null}
   scp <model file> $USER@<IP address of target device>:/etc/models/
   ```

5. 获取模型的输出张量,例如 `output0`。
   <img src="https://mintcdn.com/qualcomm-staging/uWQLnBcS9sxlZhgB/Key-Documents/AI-Developer-Workflow/_images/node-output0.png?fit=max&auto=format&n=uWQLnBcS9sxlZhgB&q=85&s=5bfc317efa008e2616dd2ffe815bae73" alt="Netron 图查看器中的模型输出张量名称" width="1511" height="802" data-path="Key-Documents/AI-Developer-Workflow/_images/node-output0.png" />

6. 使用 SSH 登录设备:
   ```shell theme={null}
   ssh $USER@<IP_ADDRESS_OF_TARGET_DEVICE>
   ```

7. 在新 shell 中,运行以下命令:

   ```shell theme={null}
   gst-launch-1.0 -e filesrc location=<video file path> ! qtdemux ! queue ! h264parse ! v4l2h264dec capture-io-mode=4 output-io-mode=4 ! video/x-raw,format=NV12 ! queue ! tee name=split split. ! queue ! qtivcomposer name=mixer ! queue ! waylandsink fullscreen=true sync=true split. ! queue ! qtimlvconverter ! queue ! qtimlsnpe delegate=dsp model=<model file path> tensors="<output0>" ! queue ! qtimlpostprocess settings="{\"confidence\": 51.0}" results=10 module=yolov8 labels=<label file path> ! video/x-raw,width=640,height=360 ! queue ! mixer.
   ```

   <Note>
     将以下占位符替换为相应的文件路径:<br />
     `<video file path>`: 输入视频文件的路径<br />
     `<model file path>`: YOLOv8 DLC 模型文件的路径<br />
     `<label file path>`: YOLOv8 标签文件的路径<br /><br />
     例如:

     ```shell theme={null}
     gst-launch-1.0 -e filesrc location=/etc/media/video.mp4 ! qtdemux ! queue ! h264parse ! v4l2h264dec capture-io-mode=4 output-io-mode=4 ! video/x-raw,format=NV12 ! queue ! tee name=split split. ! queue ! qtivcomposer name=mixer ! queue ! waylandsink fullscreen=true sync=true split. ! queue ! qtimlvconverter ! queue ! qtimlsnpe delegate=dsp model=/etc/models/yolov8s_quantized.dlc tensors="<output0>" ! queue ! qtimlpostprocess settings="{\"confidence\": 51.0}" results=10 module=yolov8 labels=/etc/labels/yolov8.json ! video/x-raw,width=640,height=360 ! queue ! mixer.
     ```
   </Note>
