> ## 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.

# 使用 sample_resnet101 对图像进行分类

`sample_resnet101` 示例应用使用 `ResNet-101` 模型和 ROS 2 节点对图像进行分类。它使用 Qualcomm® AI Engine Direct SDK (QNN) 进行模型推理。

`resnet101` 是一个可以对 ImageNet 数据集图像进行分类的机器学习模型。它也可以作为构建针对特定用例定制的更高级模型的基础。

<Note>
  **注意**

  有关更多信息，请参见 GitHub 上的 [sample\_resnet101](https://github.com/qualcomm-qrb-ros/qrb_ros_samples/tree/main/ai_vision/sample_resnet101)。
</Note>

<video
  autoPlay
  muted
  loop
  playsInline
  width={500}
  height={521}
  src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/resnet101-output.mp4"
  style={{
width: "70%",
display: "block",
margin: "0 auto"}}
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`sample_resnet101` 的示例效果
</p>

## `sample_resnet101` 的流水线流程

<img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/resnet101-pipeline-regenerate.svg" style={{width: "100%"}} />

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`sample_resnet101` 的流水线流程
</p>

## `sample_resnet101` 中使用的 ROS 节点

| **ROS 节点**                                                                          | **说明**                                                                                                                      |
| :---------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------- |
| [qrb\_ros\_camera](https://github.com/qualcomm-qrb-ros/qrb_ros_camera)              | Qualcomm ROS 2 包，按参数采集图像并将其发布到 ROS topic。以 `camera_node` 节点运行。                                                              |
| `qrb_ros_resnet101`                                                                 | 来自 `sample_resnet101` 包的预处理节点（`resnet101_node`）。订阅图像 topic，将其转换并调整为 224x224x3，然后将 tensor 发布到 `/qrb_inference_input_tensor`。 |
| [qrb\_ros\_nn\_inference](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) | 加载 `ResNet101_w8a8.bin` 模型，接收预处理后的 tensor，使用 QNN HTP 后端运行推理，并发布输出 tensor。以 `nn_inference_node` 节点运行。                        |
| `qrb_ros_resnet101_posprocess`                                                      | 来自 `sample_resnet101` 包的后处理节点（`resnet101_postprocess_node`）。将输出 tensor 映射到 ImageNet 标签并发布分类结果。                              |
| [image\_publisher](https://github.com/ros-perception/image_pipeline)                | ROS 2 Jazzy 包，使用本地路径发布图像 ROS topic。当使用 `launch_with_image_publisher.py` 启动示例时替代 `qrb_ros_camera`。                           |

## `sample_resnet101` 中使用的 ROS topic

| **ROS topic**                  |                   **类型**                  |            **发布者**           |
| :----------------------------- | :---------------------------------------: | :--------------------------: |
| `/cam0_stream1`                |          `sensor_msgs.msg.Image`          |         `camera_node`        |
| `/image_raw`                   |          `sensor_msgs.msg.Image`          |       `image_publisher`      |
| `/qrb_inference_input_tensor`  | `qrb_ros_tensor_list_msgs.msg.TensorList` |       `resnet101_node`       |
| `/qrb_inference_output_tensor` | `qrb_ros_tensor_list_msgs.msg.TensorList` |      `nn_inference_node`     |
| `/resnet101_output`            |           `std_msgs.msg.String`           | `resnet101_postprocess_node` |

<Note>
  **注意**

  预处理节点订阅 `image_raw`。使用 `launch_with_qrb_ros_camera.py` 时，该订阅被重映射到 `/cam0_stream1`；使用 `launch_with_image_publisher.py` 时，则保持默认的 `/image_raw`。
</Note>

## 前提条件

你已完成 [设置运行示例应用的环境](./set-up-env-for-sample-app) 中的设置。

## 开箱即用运行 `sample_resnet101`

<Accordion title="试一试">
  <Steps>
    <Step title="在开发套件上运行示例">
      ```bash title="SSH Session" theme={null}
      # Set up the runtime environment
      source /usr/share/qirp-setup.sh -m
      # Set the ROS_DOMAIN_ID
      export ROS_DOMAIN_ID=123
      ros2 launch sample_resnet101 launch_with_qrb_ros_camera.py
      ```
    </Step>

    <Step title="在主机计算机上检查分类结果">
      ```bash title="SSH Session" theme={null}
      export ROS_DOMAIN_ID=123
      ros2 topic echo /resnet101_output
      ```
    </Step>
  </Steps>
</Accordion>

## 构建并运行 `sample_resnet101`

以下步骤以 Dragonwing IQ-9075 Evaluation Kit 为例构建 `sample_resnet101` 包。

<Accordion title="试一试">
  <Steps>
    <Step title="在主机计算机上构建并打包">
      1. 构建示例应用项目。
         ```bash theme={null}
         cd <decompressed_workspace>/images/iq-9075-evk/qirpsdk_artifacts/iq-9075-evk/
         tar -zxvf qirp-sdk_<qirp_version>.tar.gz
         cd qirp-sdk
         source setup.sh
         # build sample
         cd <qirp_decompressed_path>/qirp-samples/ai_vision/sample_resnet101
         colcon build
         ```
      2. 打包并将示例应用推送到设备。
         ```bash theme={null}
         # package and push build result of sample
         cd <qirp_decompressed_path>/qirp-samples/ai_vision/sample_resnet101/install/sample_resnet101
         tar -czvf sample_resnet101.tar.gz lib share
         scp sample_resnet101.tar.gz root@[ip-addr]:/opt/
         ```
    </Step>

    <Step title="在开发套件上安装并运行">
      1. 安装示例应用。
         ```bash title="SSH Session" theme={null}
         # Install sample package
         tar --no-overwrite-dir --no-same-owner -zxf /opt/sample_resnet101.tar.gz -C /usr/ros/jazzy/
         ```
      2. 按照 [开箱即用运行 sample\_resnet101](#run-out-of-the-box-sample_resnet101) 中的步骤运行示例应用。
    </Step>
  </Steps>
</Accordion>
