> ## 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)。

在主机上运行以下命令。

<Steps>
  <Step title="激活您的虚拟环境">
    ```shell theme={null}
    source <venv_path>/bin/activate
    ```
  </Step>

  <Step title="安装 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
    ```
  </Step>
</Steps>

### 步骤

<Steps>
  <Step title="将 ONNX 模型转换为 DLC">
    ```shell theme={null}
    snpe-onnx-to-dlc -i yolov8s.onnx
    ```
  </Step>

  <Step title="生成量化 DLC">
    <Steps>
      <Step title="准备校准数据集" />

      <Step title="收集 5-10 张训练时使用的图像,并将这些图像保存到输入目录" />

      <Step title="使用 preprocess.py 脚本将 .jpg 图像转换为量化所需的 RAW 文件">
        <Note>
          在本示例中,模型使用 320x320 的输入尺寸。
        </Note>

        <Steps>
          <Step title="下载脚本">
            ```shell theme={null}
            wget https://raw.githubusercontent.com/qualcomm/sample-apps-for-qualcomm-linux/refs/heads/main/qualcomm-linux/scripts/preprocess.py
            ```
          </Step>

          <Step title="使用以下选项运行脚本">
            ```shell theme={null}
            python preprocess.py <INPUT PATH> <OUTPUT PATH> 1 0
            ```

            * `<INPUT PATH>`:包含原始图像的文件夹
            * `<OUTPUT PATH>`:用于生成 RAW 文件的文件夹
          </Step>
        </Steps>
      </Step>

      <Step title="创建一个 input.txt 文件,其中包含所有已生成 RAW 文件的路径">
        量化过程需要此文件。
      </Step>
    </Steps>
  </Step>

  <Step title="量化模型">
    使用 `snpe-dlc-quantize` 将模型转换为量化 DLC。

    ```shell theme={null}
    snpe-dlc-quantize --input_dlc yolov8s.dlc --input_list input.txt
    ```
  </Step>
</Steps>

## 运行演示

<Steps>
  <Step title="下载标签文件">
    请参阅[下载模型和标签文件](../topic/classify-objects-with-default-model#download-model-and-label-files)。
  </Step>

  <Step title="在主机上,设置用户环境变量">
    ```shell theme={null}
    export USER=ubuntu
    ```
  </Step>

  <Step title="将测试视频文件推送到设备的 /etc/media">
    ```shell theme={null}
    scp <video file> $USER@<IP address of target device>:/home/ubuntu/
    ssh ubuntu<ip-address>
    cp /home/ubuntu/<video file> /etc/media/
    exit
    ```
  </Step>

  <Step title="将量化后的 YoloV8 模型推送到设备">
    ```shell theme={null}
    scp <model file> $USER@<IP_ADDRESS_OF_TARGET_DEVICE>:/home/ubuntu/
    ssh ubuntu<ip-address>
    cp /home/ubuntu/<model file> /etc/models/
    exit
    ```
  </Step>

  <Step title="获取模型的输出张量,例如 output0">
    <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/node-output0.png" alt="Netron 图查看器中的模型输出张量名称" />
  </Step>

  <Step title="使用 SSH 登录设备">
    ```shell theme={null}
    ssh $USER@<IP_ADDRESS_OF_TARGET_DEVICE>
    ```
  </Step>

  <Step title="在新 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>
  </Step>
</Steps>
