Skip to main content

ONNX 开始

环境#

基础#

conda create -n onnx python=3.8 -yconda activate onnx
# ONNX#  https://github.com/onnx/onnxconda install -c conda-forge onnx -ypython -c "import onnx; print(onnx.__version__)"
import onnxmodel = onnx.load("model.onnx")

简化#

# ONNX Simplifier#  https://github.com/daquexian/onnx-simplifierpip install onnx-simplifierpython -m onnxsim -h
import onnxsimmodel_simp, check = onnxsim.simplify(model, perform_optimization=False)assert check, "Simplified ONNX model could not be validated"

使用#

给出些 ONNX 模型使用的示例方法。

提取子模型#

import onnx
input_path = "path/to/the/original/model.onnx"output_path = "path/to/save/the/extracted/model.onnx"input_names = ["input_0", "input_1", "input_2"]output_names = ["output_0", "output_1"]
onnx.utils.extract_model(input_path, output_path, input_names, output_names)

修改输入输出名称#

def _onnx_rename(model, names, names_new):  for node in model.graph.node:    for i, n in enumerate(node.input):      if n in names:        node.input[i] = names_new[names.index(n)]    for i, n in enumerate(node.output):      if n in names:        node.output[i] = names_new[names.index(n)]  for node in model.graph.input:    if node.name in names:      node.name = names_new[names.index(node.name)]  # print(model.graph.input)  for node in model.graph.output:    if node.name in names:      node.name = names_new[names.index(node.name)]  # print(model.graph.output)
_onnx_rename(model, ["input", "output"], ["input_new", "output_new"])

修改输入输出维度#

此为修改模型的。如果要修改某节点的,见参考 onnx_cut.py_onnx_specify_shapes()

from onnx.tools import update_model_dims
update_model_dims.update_inputs_outputs_dims(model,  {"input": [1, 3, 512, 512]},  {"scores": [100, 1], "boxes": [100, 4]})

推理模型节点维度#

指明模型输入维度后,可自动推理后续节点的维度。

model_infer = onnx.shape_inference.infer_shapes(model)

获取图属性名称索引#

辅助找出指定名称的图属性。

def _onnx_graph_name_map(graph_prop_list):  m = {}  for n in graph_prop_list:    m[n.name] = n  return m
node_map = _onnx_graph_name_map(graph.node)initializer_map = _onnx_graph_name_map(graph.initializer)input_map = _onnx_graph_name_map(graph.input)output_map = _onnx_graph_name_map(graph.output)value_info_map = _onnx_graph_name_map(graph.value_info)

获取节点输入名称索引#

辅助找出指定输入名称的节点列表。输出同样。

def _onnx_node_input_map(node_list):  m = {}  for n in node_list:    for n_input in n.input:      if n_input in m:        m[n_input].append(n)      else:        m[n_input] = [n]  return m
node_input_map = _onnx_node_input_map(graph.node)

获取图属性位置#

辅助找出图某属性所在列表位置。

def _onnx_graph_index(graph_prop_list, prop, by_name=False):  for i, n in enumerate(graph_prop_list):    if by_name:      if n.name == prop.name:        return i    else:      if n == prop:        return i  return -1
node_i = _onnx_graph_index(graph.node, node)

获取某区间的节点#

辅助找出某区间的节点字典。

def _onnx_node_between(node_beg, node_end, node_input_map):  nodes = {}  def _between(beg, end):    if beg.name == end.name:      return    for n_output in beg.output:      for n in node_input_map[n_output]:        if n.name == end.name or n.name in nodes:          continue        nodes[n.name] = n        _between(n, end)  _between(node_beg, node_end)  return nodes

替换某个节点#

替换或修改某个节点的过程。

from onnx import helper
node = graph.node[100]node_i = _onnx_graph_index(graph.node, node)
graph.node.remove(node)
node_new = helper.make_node(  'Pad',                  # name  ['X', 'pads', 'value'], # inputs  ['Y'],                  # outputs  mode='constant',        # attributes)graph.node.insert(node_i, node_new)

模型运行推理#

模型运行推理,得到输出的过程。

import cv2 as cvimport numpy as npimport onnxruntime as nxrun
onnx_session = nxrun.InferenceSession("path/to/model.onnx")
img = cv.imread("path/to/image.png", cv.IMREAD_COLOR)# img = img[...,::-1]  # BGR > RGB# _, _, h, w = input_node.shape  # BCHW# img = cv.resize(src=img, dsize=(w, h), interpolation=cv.INTER_LINEAR_EXACT)input_data = np.moveaxis(img, -1, 0)  # HWC > CHWinput_data = input_data[np.newaxis, :].astype(np.float32)
def _get_output_names(onnx_session):  names = []  for node in onnx_session.get_outputs():    names.append(node.name)  return names
output_names = _get_output_names(onnx_session)
outputs = onnx_session.run(  output_names, input_feed={"input": input_data})

参考#