ORT Mobile 模型匯出助手

提供了一系列工具,以幫助匯出和分析模型,從而在 ORT Mobile 中使用。

目錄

ORT Mobile 模型可用性檢查器

模型可用性檢查器提供有關模型在 ORT Mobile 上執行效果的資訊,包括其在 Android 上使用 NNAPI 和在 iOS 上使用 CoreML 的適用性。它還可以推薦執行特定工具來更新模型,使其更好地與 ORT Mobile 配合使用。

有關更多詳細資訊,請參閱此處

ONNX 模型 opset 更新器

如果您正在進行自定義構建,您可能希望將目標模型更新到相同的 ONNX opset 或多個 opset,以便自定義構建可以支援更少的 opset,從而減小二進位制檔案大小。大多數 ONNX 模型都可以使用此工具更新到較新的 ONNX opset。

用法

python -m onnxruntime.tools.update_onnx_opset --help
usage: update_onnx_opset.py:update_onnx_opset_helper [-h] [--opset OPSET] input_model output_model

Update the ONNX opset of the model. New opset must be later than the existing one. If not specified will update to opset 15.

positional arguments:
  input_model    Provide path to ONNX model to update.
  output_model   Provide path to write updated ONNX model to.

optional arguments:
  -h, --help     show this help message and exit
  --opset OPSET  ONNX opset to update to.

使用示例


python -m onnxruntime.tools.update_onnx_opset --opset 15 model.onnx model.opset15.onnx

ONNX 模型動態形狀修復器

如果模型可能與 NNAPI 或 CoreML 一起使用,則可能需要透過將任何動態維度大小設定為特定值來“固定”輸入形狀。

有關如何操作的資訊,請參閱關於 onnxruntime.tools.make_dynamic_shape_fixed 的文件。

QDQ 格式模型助手

根據 QDQ 格式模型的來源,可能需要對其某些方面進行最佳化,以確保在 ORT 中獲得最佳效能。可以使用 onnxruntime.tools.qdq_helpers.optimize_qdq_model 助手來完成此操作。

用法

python -m onnxruntime.tools.qdq_helpers.optimize_qdq_model --help
usage: optimize_qdq_model.py [-h] input_model output_model

Update a QDQ format ONNX model to ensure optimal performance when executed using ONNX Runtime.

positional arguments:
  input_model   Provide path to ONNX model to update.
  output_model  Provide path to write updated ONNX model to.

optional arguments:
  -h, --help    show this help message and exit

請注意,如果沒有最佳化,output_model 將與 input_model 相同,可以丟棄。

PyTorch 匯出助手

當使用 torch.onnx.exportPyTorch 匯出模型時,可以指定模型輸入的名稱,並且需要將模型輸入正確組合成元組。 infer_input_info 助手可用於自動發現 PyTorch 模型中使用的輸入名稱,並正確格式化輸入以用於 torch.onnx.export。

在以下示例中,我們提供了執行 torchvision mobilenet_v2 模型所需的輸入。返回的 input_names 和 inputs_as_tuple 可以直接在 torch.onnx.export 呼叫中使用。當模型有多個輸入,和/或這些輸入涉及更復雜的資料型別(例如字典)時,這提供了最大的益處。

import torch
import torchvision
from onnxruntime import tools

model = torchvision.models.mobilenet_v2(pretrained=True)
model.eval()

input0 = torch.zeros((1, 3, 224, 224), dtype=torch.float32)
input_names, inputs_as_tuple = tools.pytorch_export_helpers.infer_input_info(model, input0)

# input_names and inputs_as_tuple can be directly passed to torch.onnx.export
torch.onnx.export(model, inputs_as_tuple, "model.onnx", input_names=input_names, ...)