> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# ONNX Runtime 简介
- URL: https://www.hubwiz.com/blog/onnx-runtime-intro/
- Published: 2026-01-20T17:16:45.000Z
- Updated: 2026-01-20T17:16:45.000Z
- Description: 有大量的不同深度学习模型可用——对象检ONNX Runtime 是一个强大的推理引擎，它解决了这个问题；您只需要将模型导出到ONNX格式（.onnx），就可以运行数千种不同的模型用于任何深度学习任务。
- Author: admin
- Tags: LIBRARY, ONNX, #Import 2026-10-14 14:41

有大量的不同深度学习模型可用——**对象检测**（YOLO、Faster R-CNN）、**图像分类**（ResNet、MobileNet）、图像分割（**Mask R-CNN**）、LLM（**OpenAI GPT**）等。如果您查看这些模型的实现仓库，您会看到**每个都有不同的依赖**。有些需要PyTorch，有些需要TensorFlow，甚至不同版本。您不能创建一个PyTorch环境并期望运行任何用PyTorch实现的模型，因为**依赖项不相同**。

ONNX Runtime 是一个强大的推理引擎，它解决了这个问题；您**只需要将模型导出到ONNX格式（.onnx），就可以运行数千种不同的模型用于任何深度学习任务**。

在这篇文章中，我将简要介绍ONNX Runtime，如何运行像YOLO和Faster R-CNN这样的对象检测模型，以及具有不同骨干（我使用了ResNet-50）的图像分类模型。您不必局限于这些模型；过程是相同的：

1. **训练模型或使用预训练模型**
2. **将模型导出到ONNX格式**
3. **使用ONNX Runtime 对图像和视频进行推理**

## 1、ONNX 模型动物园

您可以从[ONNX 模型动物园](https://onnx.ai/models/?ref=hubwiz.com)检查不同的模型，这些模型已经是ONNX格式，无需修改任何东西。您可以直接从这里下载模型，或将您的模型导出到ONNX格式并继续。

![](https://www.hubwiz.com/blog/content/images/2026/01/image-633.png)

有不同类别，例如您可以看到具有不同骨干的不同计算机视觉模型，如**ResNet**、**MobileNet**。

![](https://www.hubwiz.com/blog/content/images/2026/01/image-634.png)

**ONNX格式的计算机视觉模型（ONNX 模型动物园）**

这里是NLP模型。

![](https://www.hubwiz.com/blog/content/images/2026/01/image-635.png)

**ONNX格式的NLP模型（ONNX 模型动物园）**

如果您不想花时间将模型导出到ONNX格式，您可以使用这些模型。但如果您想运行自定义模型，您可以按照我的[文章](https://visionbrick.com/run-any-deep-learning-model-with-onnx-runtime-in-python/?ref=hubwiz.com)查看如何将模型导出到ONNX格式。

例如，您可以训练一个自定义**YOLO模型**（YOLOv5、YOLOv8、YOLOv10、YOLOv11…）并使用**Ultralytics**库将其导出到ONNX格式。

![](https://www.hubwiz.com/blog/content/images/2026/01/image-636.png)

对于**Faster R-CNN**类似；您可以使用**PyTorch**。如前所述，您可以在我的其他博客文章中找到完整解释；现在我只在这里分享代码片段。

```
# Export the model
torch.onnx.export(model, dummy, "faster-rcnn-custom.onnx",
                  opset_version=11,
                  input_names=["images"],
                  output_names=["boxes", "labels", "scores"])

```

![](https://www.hubwiz.com/blog/content/images/2026/01/image-637.png)

推理部分取决于模型；对于不同模型，它不是相同的。但逻辑是相同的：加载模型，预处理图像，在处理过的图像上运行模型，并后处理输出。

例如，您可以像这样加载模型（它们必须是ONNX格式）：

```
# Load Faster R-CNN object detection model model
session = ort.InferenceSession("models/fasterrcnn-detection.onnx", providers=['CUDAExecutionProvider'])

```

![](https://www.hubwiz.com/blog/content/images/2026/01/image-638.png)

您通常需要调整图像大小以**匹配模型输入尺寸**，归一化图像，并添加批次维度。

![](https://www.hubwiz.com/blog/content/images/2026/01/image-639.png)

您可以基于置信度值过滤一些检测，或应用\*\*NMS（非最大抑制）\*\*来移除重叠检测。

---

原文链接：[Introduction to ONNX Runtime: Run Any Object Detection, Image Classification, Segmentation, NLP…](https://medium.com/@siromermer/introduction-to-onnx-runtime-run-any-object-detection-image-classification-segmentation-nlp-7d5e0720ec6d?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处