Skip to main content

Simple OP Renamer for ONNX.

Project description

sor4onnx

Simple OP Renamer for ONNX.

https://github.com/PINTO0309/simple-onnx-processing-tools

Downloads GitHub PyPI CodeQL

Key concept

  • Performs a partial match search on the specified string and replaces all input and output names with the specified string.

1. Setup

1-1. HostPC

### option
$ echo export PATH="~/.local/bin:$PATH" >> ~/.bashrc \
&& source ~/.bashrc

### run
$ pip install -U onnx \
&& python3 -m pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com \
&& pip install -U sor4onnx

1-2. Docker

https://github.com/PINTO0309/simple-onnx-processing-tools#docker

2. CLI Usage

$ sor4onnx -h

usage:
  sor4onnx [-h]
  -if INPUT_ONNX_FILE_PATH
  -on OLD_NEW OLD_NEW
  -of OUTPUT_ONNX_FILE_PATH
  [-m {full,inputs,outputs}]
  [-sm {exact_match,partial_match,prefix_match,suffix_match}]
  [-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.

  -on OLD_NEW OLD_NEW, --old_new OLD_NEW OLD_NEW
      All occurrences of substring old replaced by new.
      e.g. --old_new "onnx::" ""

  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH
      Output onnx file path.

  -m {full,inputs,outputs}, --mode {full,inputs,outputs}
      Specifies the type of node to be replaced.
      full or inputs or outputs.
      full: Rename all nodes.
      inputs: Rename only the input node.
      outputs: Rename only the output node.
      Default: full

  -sm {exact_match,partial_match,prefix_match,suffix_match}, --search_mode {exact_match,partial_match,prefix_match,suffix_match}
      OP name search mode.
      exact_match or partial_match or prefix_match or suffix_match.
      exact_match: Exact match search for OP name.
      partial_match: Partial match search for OP name.
      prefix_match: Prefix match search for OP name.
      suffix_match: Suffix match search for OP name.
      Default: exact_match

  -n, --non_verbose
      Do not show all information logs. Only error logs are displayed.

3. In-script Usage

>>> from sor4onnx import rename
>>> help(rename)

Help on function rename in module sor4onnx.onnx_opname_renamer:

rename(
    old_new: List[str],
    input_onnx_file_path: Union[str, NoneType] = '',
    onnx_graph: Union[onnx.onnx_ml_pb2.ModelProto, NoneType] = None,
    output_onnx_file_path: Union[str, NoneType] = '',
    mode: Union[str, NoneType] = 'full',
    search_mode: Union[str, NoneType] = 'exact_match',
    non_verbose: Union[bool, NoneType] = False
) -> onnx.onnx_ml_pb2.ModelProto

    Parameters
    ----------
    old_new: List[str]
        All occurrences of substring old replaced by new.
        e.g.
        old_new = ["onnx::", ""]

    input_onnx_file_path: Optional[str]
        Input onnx file path.
        Either input_onnx_file_path or onnx_graph must be specified.
        Default: ''

    onnx_graph: Optional[onnx.ModelProto]
        onnx.ModelProto.
        Either input_onnx_file_path or onnx_graph must be specified.
        onnx_graph If specified, ignore input_onnx_file_path and process onnx_graph.

    output_onnx_file_path: Optional[str]
        Output onnx file path. If not specified, no ONNX file is output.
        Default: ''

    mode: Optional[str]
        Specifies the type of node to be replaced.
        full or inputs or outputs.
        full: Rename all nodes.
        inputs: Rename only the input node.
        outputs: Rename only the output node.
        Default: full

    search_mode: Optional[str]
        OP name search mode.
        exact_match or partial_match or prefix_match or suffix_match.
        exact_match: Exact match search for OP name.
        partial_match: Partial match search for OP name.
        prefix_match: Prefix match search for OP name.
        suffix_match: Suffix match search for OP name.
        Default: exact_match

    non_verbose: Optional[bool]
        Do not show all information logs. Only error logs are displayed.
        Default: False

    Returns
    -------
    renamed_graph: onnx.ModelProto
        Renamed onnx ModelProto.

4. CLI Execution

$ sor4onnx \
--input_onnx_file_path fusionnet_180x320.onnx \
--old_new "onnx::" "" \
--mode full \
--search_mode prefix_match \
--output_onnx_file_path fusionnet_180x320_renamed.onnx

5. In-script Execution

from sor4onnx import rename

onnx_graph = rename(
  old_new=["onnx::", ""],
  input_onnx_file_path="fusionnet_180x320.onnx",
  output_onnx_file_path="fusionnet_180x320_renamed.onnx",
  mode="full",
  search_mode="prefix_match",
)

# or

onnx_graph = rename(
  old_new=["onnx::", ""],
  onnx_graph=graph,
  mode="full",
  search_mode="prefix_match",
)

6. Sample

Before

image

After

image

7. Reference

  1. https://github.com/onnx/onnx/blob/main/docs/Operators.md
  2. https://docs.nvidia.com/deeplearning/tensorrt/onnx-graphsurgeon/docs/index.html
  3. https://github.com/NVIDIA/TensorRT/tree/main/tools/onnx-graphsurgeon
  4. https://github.com/PINTO0309/simple-onnx-processing-tools
  5. https://github.com/PINTO0309/PINTO_model_zoo

8. Issues

https://github.com/PINTO0309/simple-onnx-processing-tools/issues

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sor4onnx-1.0.6.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

sor4onnx-1.0.6-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file sor4onnx-1.0.6.tar.gz.

File metadata

  • Download URL: sor4onnx-1.0.6.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for sor4onnx-1.0.6.tar.gz
Algorithm Hash digest
SHA256 c46423e343ad218831b7227e67dc83f23cd1534ae69e177945db2f0cc1606cf4
MD5 c2205f6b0bdd9c3bca181f53f2c154d1
BLAKE2b-256 e3f5e904a7bfd1a36f9cabd8e09a04159ea81b216b8e3cea549bc9e5147b4686

See more details on using hashes here.

File details

Details for the file sor4onnx-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: sor4onnx-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for sor4onnx-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 38dd704a1733ddcebe22400dfa6fcba76a5cda2776f881689cbe02dc0d1ad979
MD5 69ce07865fe5a317009b3a5a4f770ed9
BLAKE2b-256 be126821c9b633ffbc1329a9edb4555a7b19623f7d194c60d012b80b45081695

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page