> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.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-prod/WwC9kmcnKl9Ef7de/Key-Documents/AI-Developer-Workflow/_images/node-output0.png?fit=max&auto=format&n=WwC9kmcnKl9Ef7de&q=85&s=b5a57333db94f3a3c9484c0f230ee051" 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=/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.
   ```
