Skip to content

janicevidal/yizhu-onnx-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yizhu-onnx-tool

yizhu-onnx-tool

onnx batchsize

onnx_batchsize_convert.py

功能概述:

转换 onnx 模型的 batchsize,初始 batch 为任意字符

功能点:

  • dynamic batchsize 转换为 static batchsize,设置 initialization_character_string 为 int 数值;
  • static batchsize 转换为 dynamic batchsize,设置 initialization_character_string 为-1、N 或者其他字符;
  • 应对复杂模型时,可能会失败。如 onnx 模型中 Reshape 算子包含 batch size 情况。Reshape 算子,不能包含两种以上未定义的 shape size,如-1、N、None 或 unk_*。

In-script Usage

usage: onnx_batchsize_convert.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH -ics INITIALIZATION_CHARACTER_STRING [-n]
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.
  -ics INITIALIZATION_CHARACTER_STRING, --initialization_character_string INITIALIZATION_CHARACTER_STRING
                        String to initialize batch size. "-1" or "N" or "xxx", etc... Default: '-1'
  -n, --non_verbose     Do not show all information logs. Only error logs are displayed.

onnx compare

onnx_compare.py

功能概述:

比较两个 onnx 模型在结构上的区别

功能点:

  • onnx 模型先转换为文本格式,再调用第三方库 deepdiff 进行文本比较;

  • 如果对比结果不同,将会给出下面对应的返回:

    • type_changes:类型改变的 key
    • values_changed:值发生变化的 key
    • dictionary_item_added:字典 key 添加
    • dictionary_item_removed:字段 key 删除

In-script Usage

usage: onnx_compare.py [-h] -if INPUT_ONNX_FILE_PATH -icf INPUT_COMPARE_ONNX_FILE_PATH

optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -icf INPUT_COMPARE_ONNX_FILE_PATH, --input_compare_onnx_file_path INPUT_COMPARE_ONNX_FILE_PATH
                        Input compare onnx file path.

onnx extract

onnx_isolate_subgraph.py

功能概述:

按照给定的输入输出 tensor 提取 onnx 模型的子图

功能点:

  • 按照给定的输入输出 tensor 提取 onnx 模型的子图,支持静态输入和动态输入模型的子图提取;
  • strart 和 end 节点的 shape 和 dtype 可指定,end 节点的 shape 必须严格指定,不然固定模型 shape 时会报错。

In-script Usage

usage: onnx_isolate_subgraph.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH -is ISOLATE_START -ie ISOLATE_END [-isd ISOLATE_START_DTYPE] [-ied ISOLATE_END_DTYPE]
                                [-iss ISOLATE_START_SHAPE [ISOLATE_START_SHAPE ...]] [-ies ISOLATE_END_SHAPE [ISOLATE_END_SHAPE ...]]

optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.
  -is ISOLATE_START, --isolate_start ISOLATE_START
                        Subgraph start node.
  -ie ISOLATE_END, --isolate_end ISOLATE_END
                        Subgraph end node.
  -isd ISOLATE_START_DTYPE, --isolate_start_dtype ISOLATE_START_DTYPE
                        Subgraph start node dtype initalization.
  -ied ISOLATE_END_DTYPE, --isolate_end_dtype ISOLATE_END_DTYPE
                        Subgraph end node dtype initalization.
  -iss ISOLATE_START_SHAPE [ISOLATE_START_SHAPE ...], --isolate_start_shape ISOLATE_START_SHAPE [ISOLATE_START_SHAPE ...]
                        Subgraph start node shape initalization.
  -ies ISOLATE_END_SHAPE [ISOLATE_END_SHAPE ...], --isolate_end_shape ISOLATE_END_SHAPE [ISOLATE_END_SHAPE ...]

CLI Execution

python onnx_isolate_subgraph.py -if resnet50_v1.5_pth.onnx -of r50_subgraph_test.onnx -is input -ie /layer1/layer1.0/conv2/Conv_output_0 -iss 1 3 224 224 -ies 1 64 56 56 -isd float32

onnx fold_constant

onnx_fold_constant.py

功能概述:

onnx 模型常量折叠

功能点:

  • onnx 模型常量折叠,来源于 onnx-graphsurgeon 中的常量折叠方法,onnx-simplifier 中同样有实现。

In-script Usage

usage: onnx_fold_constant.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.

onnx infer_shape

onnx_shape_inference.py

功能概述:

给 ONNX 文件添加 tensor 的 shape 信息,支持三种模式 onnx, ort_symbolic, ort_run

功能点:

  • 使用 onnx.shape_inference 形状推理(默认);
  • 使用 onnxruntime 框架中提供的 符号计算 形状推理 方式;
  • 使用 onnxruntime 实时推理,收集 runtime 过程中的 shape 信息,但这将会导致部分动态维度变为静态。

In-script Usage

usage: onnx_shape_inference.py [-h] -if INPUT_ONNX_FILE_PATH [-of OUTPUT_ONNX_FILE_PATH] [-mode SHAPE_INFER_MODE] [-n] [--auto_merge] [--guess_output_rank]
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.
  -mode SHAPE_INFER_MODE, --shape_infer_mode SHAPE_INFER_MODE
                        Shape inference mode (onnx, ort_symbolic, ort_run).
  -n, --non_verbose     Do not show all information logs. Only error logs are displayed.
  --auto_merge          Automatically merge symbolic dims when confliction happens
  --guess_output_rank   guess output rank to be the same as input 0 for unknown ops

onnx info

onnx_structure_info.py

功能概述:

导出 onnx 模型的信息

功能点:

  • 检查 onnx 模型的运行,导出 onnx 模型的信息,输出 txt 文件

In-script Usage

usage: onnx_structure_info.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_INFO_FILE_PATH
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_INFO_FILE_PATH, --output_info_file_path OUTPUT_INFO_FILE_PATH
                        Output info file path (*.txt)

onnx modify

onnx_to_json.py

onnx_from_json.py

功能概述:

提供 onnx 文件和 json 格式互相转换功能,以文本形式对模型进行修改

功能点:

  • onnx 文件和 json 格式的互相转换,分为了两个脚本处理

In-script Usage

usage: onnx_to_json.py [-h] -if INPUT_ONNX_FILE_PATH -oj OUTPUT_JSON_PATH [-i JSON_INDENT]
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input ONNX model path. (*.onnx)
  -oj OUTPUT_JSON_PATH, --output_json_path OUTPUT_JSON_PATH
                        Output JSON file path (*.json)
  -i JSON_INDENT, --json_indent JSON_INDENT
                        Number of indentations in JSON. (default=2)
usage: onnx_from_json.py [-h] -ij INPUT_JSON_PATH -of OUTPUT_ONNX_FILE_PATH

optional arguments:
  -h, --help            show this help message and exit
  -ij INPUT_JSON_PATH, --input_json_path INPUT_JSON_PATH
                        Input JSON file path (*.json)
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output ONNX model path (*.onnx)

onnx opset

onnx_opset_change.py

功能概述:

转化 onnx 模型的 opset

功能点:

  • 转化 onnx 模型的 opset

In-script Usage

usage: onnx_opset_change.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH -os OPSET [-n]
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.
  -os OPSET, --opset OPSET
                        opset number to be changed. e.g. --opset 11
  -n, --non_verbose     Do not show all information logs. Only error logs are displayed

onnx to fp16

onnx_fp16_convert.py

功能概述:

转化 onnx 模型的数据类型为 fp16

功能点:

  • 转化 onnx 模型的数据类型为 fp16,来源于 onnxconverter_common

In-script Usage

usage: onnx_fp16_convert.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.

onnx cleanup

onnx_cleanup.py

功能概述:

清除无用节点

功能点:

  • 清除 onnx 中无用节点,来源于 onnx-graphsurgeon

In-script Usage

usage: onnx_cleanup.py [-h] -if INPUT_ONNX_FILE_PATH -of OUTPUT_ONNX_FILE_PATH
optional arguments:
  -h, --help            show this help message and exit
  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH
                        Input onnx file path.
  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
                        Output onnx file path.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages