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

# 使用 qrb_ros_nn_inference 执行 AI 模型推理

[qrb\_ros\_nn\_inference](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) 是一个用于执行神经网络模型的 ROS2 软件包，为机器人应用提供基于 AI 的感知能力。

**它提供以下功能：**

* 支持三种模型格式的模型推理 API：
  * `.tflite`
  * `.so`
  * `.bin`
* 基于 Qualcomm 平台的模型推理加速

## `qrb_ros_nn_inference` 的管线流程

<img src="https://mintcdn.com/qualcomm-prod/IP0o0G5fE3MFmCJ8/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/nn-inference-pipeline.svg?fit=max&auto=format&n=IP0o0G5fE3MFmCJ8&q=85&s=a380f719946ebb2e68450dc8862846de" alt="pipeline" style={{ display: 'block', margin: '0 auto' }} width="982" height="243" data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/nn-inference-pipeline.svg" />

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：qrb\_ros\_nn\_inference 的管线
</p>

## `qrb_ros_nn_inference ` 管线中使用的 ROS 话题

<table>
  <thead>
    <tr>
      <th style={{ width: '30%' }}>ROS 话题</th>
      <th style={{ width: '20%' }}>消息类型</th>
      <th style={{ width: '20%' }}>发布/订阅者</th>
      <th style={{ width: '20%' }}>说明</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`/qrb_inference_input_tensor`</td>
      <td>`qrb_ros_tensor_list_msgs::msg::TensorList`</td>
      <td>由 `qrb_ros_nn_inference` 订阅</td>
      <td>订阅用于模型推理的输入张量。</td>
    </tr>

    <tr>
      <td>`/qrb_inference_output_tensor`</td>
      <td>`qrb_ros_tensor_list_msgs::msg::TensorList`</td>
      <td>由 `qrb_ros_nn_inference` 发布</td>
      <td>发布模型的输出张量。</td>
    </tr>
  </tbody>
</table>

## `qrb_ros_nn_inference ` 管线中使用的 ROS 节点参数

<table>
  <thead>
    <tr>
      <th style={{ width: '20%' }}>节点参数</th>
      <th style={{ width: '25%' }}>类型</th>
      <th style={{ width: '25%' }}>默认值</th>
      <th style={{ width: '30%' }}>说明</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`backend_option`</td>
      <td style={{textAlign: 'center'}}>`string`</td>
      <td style={{textAlign: 'center'}}>`""`</td>
      <td>模型推理的硬件加速选项。有效值请参阅 [qrb\_ros\_nn\_inference 文档](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference/blob/main/qrb_inference_manager/Documentation.md#2.1)。</td>
    </tr>

    <tr>
      <td>`model_path`</td>
      <td style={{textAlign: 'center'}}>`string`</td>
      <td style={{textAlign: 'center'}}>`""`</td>
      <td>表示模型文件的路径。</td>
    </tr>
  </tbody>
</table>

## 前提条件

* 您已根据[为运行示例应用设置环境](./set-up-env-for-sample-app)完成了**设置设备**。
* 您已从 [Qualcomm AI hub](https://aihub.qualcomm.com/) 下载模型。
* 您已根据 [qrb\_ros\_nn\_inference 示例](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference/tree/main/test)准备好预处理节点和后处理节点。

## 运行开箱即用的 `qrb_ros_nn_inference`

1. 启动一个终端，并运行以下命令在设备上设置 QIR SDK 和 ROS2 环境。

   `ROS_DOMAIN_ID` 的取值范围：\[0, 232]

   ```cpp theme={null}
   ssh root@[ip-addr]
   (ssh) source /usr/share/qirp-setup.sh
   (ssh) export ROS_DOMAIN_ID=xx
   ```
2. 启动三个节点：预处理节点、`qrb_ros_nn_inference` 节点和后处理节点。
   ```undefined theme={null}
   (ssh) ros2 launch model_inference.launch.py
   ```

## 构建并运行 `qrb_ros_nn_inference`

1. 在主机上构建 QIR SDK 提供的 `qrb_ros_nn_inference`。
   ```powershell theme={null}
   cd <qirp_decompressed_workspace>
   source setup.sh
   cd qirp-samples/platform/qrb_ros_nn_inference
   colcon build --merge-install --cmake-args ${CMAKE_ARGS}
   ```
2. 将 `qrb_ros_nn_inference` 推送到设备。

   <Note>
     **注意**

     确保设备上已安装 QIR SDK。
   </Note>

   ```powershell theme={null}
   cd qirp-samples/platform/qrb_ros_nn_inference/install
   tar czvf qrb_ros_nn_inference.tar.gz include lib share
   scp qrb_ros_nn_inference.tar.gz root@[ip-addr]:/opt/
   ssh root@[ip-addr]
   (ssh) tar --no-overwrite-dir --no-same-owner -zxf /opt/qrb_ros_nn_inference.tar.gz -C /usr/ros/jazzy/
   ```
3. 启动一个终端，并运行以下命令在设备上设置 QIR SDK 和 ROS2 环境。
   `ROS_DOMAIN_ID` 的取值范围：\[0, 232]
   ```cpp theme={null}
   ssh root@[ip-addr]
   (ssh) source /usr/share/qirp-setup.sh
   (ssh) export ROS_DOMAIN_ID=xx
   ```
4. 启动三个节点：预处理节点、`qrb_ros_nn_inference` 节点和后处理节点。
   ```undefined theme={null}
   (ssh) ros2 launch model_inference.launch.py
   ```

## 限制

有关最新的限制，请参阅 GitHub 上的 [qrb\_ros\_nn\_inference](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference)。
