Windows 机器学习(ML)包括 ONNX 运行时的共享副本,包括其 API。 这意味着通过 Windows 应用 SDK 安装 Windows ML 时,你的应用将有权访问完整的 ONNX API 图面。
若要查看特定 Windows ML 版本中包含的 ONNX 运行时版本,请参阅 Windows ML 中提供的 ONNX 运行时版本。
本页介绍如何使用 Windows ML 中包含的 ONNX API。
先决条件
Windows ML 中 ONNX API 的命名空间/标头如下所示:
在 C# 中,ONNX API 的命名空间与直接使用 ONNX 运行时时相同。
using Microsoft.ML.OnnxRuntime;
在C++中,ONNX 运行时标头包含在目录中 winml/ ,以避免与其他版本的 ONNX 运行时冲突。
#include <winml/onnxruntime_cxx_api.h>
如果你有使用 ONNX 标头且不想更新包含的现有代码,可以通过在项目中设置 winml/ 属性来告知 WinML 公开没有前缀的标头:
<PropertyGroup>
<WinMLEnableDefaultOrtHeaderIncludePath>true</WinMLEnableDefaultOrtHeaderIncludePath>
</PropertyGroup>
然后,标头将与独立的 ONNX 运行时相同:
#include <onnxruntime_cxx_api.h>
注释
在 Windows ML 的预 GA 版本中,C++ ONNX 运行时标头的前缀为 win_,而不是位于子目录 winml/ 中,并且 WinMLEnableDefaultOrtHeaderIncludePath 属性不存在。
在 Python 中,ONNX 运行时模块的名称与直接使用 ONNX 运行时时的名称相同。
import onnxruntime as ort
应用程序接口
ONNX API 与直接使用 ONNX 运行时时相同。 例如,若要创建推理会话:
// Create inference session using compiled model
using InferenceSession session = new(compiledModelPath, sessionOptions);
// Create inference session using compiled model
Ort::Session session(env, compiledModelPath.c_str(), sessionOptions);
# Create inference session using compiled model
session = ort.InferenceSession(output_model_path, sess_options=options)
建议阅读 ONNX 运行时文档 ,了解有关如何在 Windows ML 中使用 ONNX 运行时 API 的详细信息。
另请参阅