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

# 使用 AI Hub 优化模型

> 使用 Qualcomm AI Hub 下载预优化模型，或编译和优化您自己的模型，以便在 Qualcomm Dragonwing IoT 平台上部署。

要在 Qualcomm AI 硬件上快速原型化模型，AI Hub
提供了一种方法，可针对视觉、音频和语音场景，
在设备端优化、验证和部署机器学习模型。

<img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/ai-hub_QLI.png" alt="Qualcomm AI Hub 工作流概览" />

## 设置您的环境

<Steps>
  <Step title="设置 Python 环境">
    在您的主机上安装 [miniconda](https://docs.conda.io/projects/miniconda/en/latest/miniconda-install.html)。

    为 AI Hub 设置 Python 虚拟环境：

    ```shell theme={null}
    conda activate 
    ```

    ```shell theme={null}
    conda create python=3.10 -n qai_hub 
    ```

    ```shell theme={null}
    conda activate qai_hub
    ```
  </Step>

  <Step title="安装 git">
    ```shell theme={null}
    sudo apt-get install git
    ```
  </Step>

  <Step title="安装 AI Hub Python 客户端">
    ```shell theme={null}
    pip3 install qai-hub
    ```

    ```shell theme={null}
    pip3 install "qai-hub[torch]"
    ```
  </Step>

  <Step title="登录 AI Hub">
    访问 [AI Hub](https://aihub.qualcomm.com/)，使用您的
    Qualcomm ID 登录，以查看您创建的作业相关信息。

    登录后，前往 **Account > Settings > API Token** 获取用于配置客户端的 API 令牌。
  </Step>

  <Step title="使用 API 令牌配置客户端">
    ```shell theme={null}
    qai-hub configure --api_token <INSERT_API_TOKEN>
    ```
  </Step>
</Steps>

## 选择 AI Hub 工作流

### 尝试预优化模型

<Steps>
  <Step title="浏览 AI Hub Model Zoo">
    访问 [AI Hub Model Zoo](https://aihub.qualcomm.com/iot/models)，获取适用于 Qualcomm 评估套件的预优化模型。
  </Step>

  <Step title="按 EVK 筛选模型">
    在左侧窗格中选择匹配的芯片组。例如，
    为 Qualcomm Dragonwing™ RB3 Gen 2 选择 **Qualcomm QCS6490**。
  </Step>

  <Step title="选择模型">
    从筛选后的视图中选择模型以进入模型页面。
  </Step>

  <Step title="选择运行时和精度">
    在模型页面上，选择运行时和精度。
  </Step>

  <Step title="下载模型">
    选择 **Download** 下载模型。下载的模型已预优化，可用于部署。有关更多信息，请参阅[运行推理](../map/use-available-frameworks-and-runtimes)。
  </Step>
</Steps>

<img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/ai-hub-download.png" alt="AI Hub 模型下载页面" />

### 自带模型

<Steps>
  <Step title="选择预训练模型">
    选择 PyTorch 或 ONNX 格式的预训练模型。
  </Step>

  <Step title="提交模型以进行编译或优化">
    使用 Python API 向 AI Hub 提交模型以进行编译或优化。

    提交编译作业时，为您的 EVK 选择设备或芯片组以及目标运行时。对于 Qualcomm Dragonwing™ RB3 Gen 2，支持 LiteRT 运行时。

    | **芯片组**                        | **运行时** | **CPU**        | **GPU**   | **HTP**    |
    | ------------------------------ | ------- | -------------- | --------- | ---------- |
    | Qualcomm Dragonwing™ RB3 Gen 2 | LiteRT  | INT8、FP16、FP32 | FP16、FP32 | INT8、INT16 |

    提交后，AI Hub 会为该作业生成一个唯一 ID。您可以
    使用此作业 ID 查看作业详细信息。
  </Step>

  <Step title="AI Hub 优化模型">
    AI Hub 会根据您选择的设备和运行时对模型进行优化。

    * 或者，您可以提交作业，在从设备农场分配的
      真实设备上（使用 Python API）对优化后的模型进行分析或
      运行推理。

      * **分析（Profiling）**：在已分配的设备上对模型进行基准测试，
        并提供统计数据，包括每层级的平均推理时间、
        运行时配置等。

      * **推理（Inference）**：在已分配的设备上运行模型，
        使用作为推理作业一部分提交的数据，
        通过优化后的模型执行推理。
  </Step>

  <Step title="查看作业并下载优化后的模型">
    提交的每个作业都可以在 AI Hub 门户中查看。已完成的编译作业会提供可下载的优化模型链接，之后即可将其部署到本地开发设备上，例如 Qualcomm Dragonwing™ RB3 Gen 2。
  </Step>
</Steps>

以下示例摘自 [AI Hub 文档](https://workbench.aihub.qualcomm.com/docs/)，将预训练的 MobileNet V2 模型从 PyTorch 上传到 AI Hub，并将其编译为适用于 Qualcomm Dragonwing™ RB3 Gen 2 的优化 LiteRT 模型。

```python theme={null}
import torch
import torchvision

import qai_hub as hub

client = hub.Client()

# Using pre-trained MobileNet
torch_model = torchvision.models.mobilenet_v2(pretrained=True)
torch_model.eval()

# Trace model
input_shape: tuple[int, ...] = (1, 3, 224, 224)
example_input = torch.rand(input_shape)
with torch.no_grad():
    pt2_model = torch.export.export(torch_model, (example_input,))

# Compile model on a specific device
compile_job = client.submit_compile_job(
    pt2_model,
    name="MobileNet_V2",
    device=hub.Device("Dragonwing RB3 Gen 2 Vision Kit"),
    input_specs=dict(image=input_shape),
)

# Download the optimized compiled model
compile_job.download_target_model("MobileNet_V2.tflite")
```

<Note>
  要停用之前激活的 `qai_hub` 环境，请使用以下命令。

  ```shell theme={null}
  conda deactivate
  ```
</Note>

模型下载完成后，即可部署。有关后续步骤，请参阅[运行推理](../map/use-available-frameworks-and-runtimes)。

有关 AI Hub 工作流和 API 的更多详细信息，请参阅 [AI Hub 文档](https://workbench.aihub.qualcomm.com/docs/hub/index.html#examples)，
浏览 [AI Hub 教程视频](https://www.youtube.com/watch?v=V1CDWYZ7Shw\&list=PLxeazpXYyqtOowtUdvigvAgMV5_K1KIrh)，
或观看以下有关如何在 AI Hub 中分析模型的视频。

<iframe width="100%" height="400" src="https://www.youtube.com/embed/V1CDWYZ7Shw" title="AI Hub - Profile Models" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />
