{"id":15935022,"url":"https://github.com/pinto0309/sna4onnx","last_synced_at":"2025-03-24T21:32:42.993Z","repository":{"id":38192100,"uuid":"485202182","full_name":"PINTO0309/sna4onnx","owner":"PINTO0309","description":"Simple node addition tool for onnx. Simple Node Addition for ONNX.","archived":false,"fork":false,"pushed_at":"2022-09-09T16:46:39.000Z","size":113,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-08T03:22:04.763Z","etag":null,"topics":["cli","model-converter","models","onnx","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PINTO0309.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-25T02:50:51.000Z","updated_at":"2024-01-04T17:08:04.000Z","dependencies_parsed_at":"2022-08-19T05:31:18.283Z","dependency_job_id":null,"html_url":"https://github.com/PINTO0309/sna4onnx","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsna4onnx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsna4onnx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsna4onnx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsna4onnx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PINTO0309","download_url":"https://codeload.github.com/PINTO0309/sna4onnx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222015638,"owners_count":16916623,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","model-converter","models","onnx","python"],"created_at":"2024-10-07T03:22:14.782Z","updated_at":"2024-10-29T08:21:20.200Z","avatar_url":"https://github.com/PINTO0309.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sna4onnx\nSimple node addition tool for onnx. **S**imple **N**ode **A**ddition for **ONNX**.\n\nhttps://github.com/PINTO0309/simple-onnx-processing-tools\n\n[![Downloads](https://static.pepy.tech/personalized-badge/sna4onnx?period=total\u0026units=none\u0026left_color=grey\u0026right_color=brightgreen\u0026left_text=Downloads)](https://pepy.tech/project/sna4onnx) ![GitHub](https://img.shields.io/github/license/PINTO0309/sna4onnx?color=2BAF2B) [![PyPI](https://img.shields.io/pypi/v/sna4onnx?color=2BAF2B)](https://pypi.org/project/sna4onnx/) [![CodeQL](https://github.com/PINTO0309/sna4onnx/workflows/CodeQL/badge.svg)](https://github.com/PINTO0309/sna4onnx/actions?query=workflow%3ACodeQL)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/33194443/170157317-1d96079c-0b46-4bec-bdec-4cc9feb0d553.png\" /\u003e\n\u003c/p\u003e\n\n# Key concept\n- [x] Combines the OP generated by **[sog4onnx](https://github.com/PINTO0309/sog4onnx)** with the specified output and input OPs.\n- [x] Use a good combination with the ONNX merge tool **[snc4onnx](https://github.com/PINTO0309/snc4onnx)** while merging OP parts.\n- [x] Only one OP can be extrapolated at a time.\n- [x] After OP extrapolation, the entire model is checked, and even if there is a consistency problem, only a warning is displayed and the ONNX file is output as is.\n- [x] Add unconnected input and output variables to the input/output OP of a graph.\n\n## 1. Setup\n### 1-1. HostPC\n```bash\n### option\n$ echo export PATH=\"~/.local/bin:$PATH\" \u003e\u003e ~/.bashrc \\\n\u0026\u0026 source ~/.bashrc\n\n### run\n$ pip install -U onnx \\\n\u0026\u0026 python3 -m pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com \\\n\u0026\u0026 pip install -U sna4onnx\n```\n### 1-2. Docker\nhttps://github.com/PINTO0309/simple-onnx-processing-tools#docker\n\n## 2. CLI Usage\n```\n$ sna4onnx -h\n\nusage:\n  sna4onnx [-h]\n  -if INPUT_ONNX_FILE_PATH\n  -aot ADD_OP_TYPE\n  -aon ADD_OP_NAME\n  [-aoiv NAME TYPE VALUE]\n  [-aoov NAME TYPE VALUE]\n  [-aoa NAME DTYPE VALUE]\n  [-csoon SRCOP_NAME SRCOP_OUTPUT_NAME ADDOP_NAME ADDOP_INPUT_NAME]\n  -cdoin ADDOP_NAME ADDOP_OUTPUT_NAME DESTOP_NAME DESTOP_INPUT_NAME\n  [-of OUTPUT_ONNX_FILE_PATH]\n  [-n]\n\noptional arguments:\n  -h, --help\n      show this help message and exit\n\n  -if INPUT_ONNX_FILE_PATH, --input_onnx_file_path INPUT_ONNX_FILE_PATH\n      Input onnx file path.\n\n  -aot ADD_OP_TYPE, --add_op_type ADD_OP_TYPE\n      ONNX OP type.\n      https://github.com/onnx/onnx/blob/main/docs/Operators.md\n\n  -aon ADD_OP_NAME, --add_op_name ADD_OP_NAME\n      Name of OP to be added.\n      e.g.\n      --add_op_name AddOP1\n\n  -aoiv ADD_OP_INPUT_VARIABLES ADD_OP_INPUT_VARIABLES ADD_OP_INPUT_VARIABLES, --add_op_input_variables ADD_OP_INPUT_VARIABLES ADD_OP_INPUT_VARIABLES ADD_OP_INPUT_VARIABLES\n      input_variables can be specified multiple times.\n      --add_op_input_variables variable_name numpy.dtype shape\n      https://github.com/onnx/onnx/blob/main/docs/Operators.md\n      e.g.\n      --add_op_input_variables inpname1 float32 [1,3,5,5]\n      --add_op_input_variables inpname2 int32 [1]\n      --add_op_input_variables inpname3 float64 [1,3,224,224]\n\n  -aoov ADD_OP_OUTPUT_VARIABLES ADD_OP_OUTPUT_VARIABLES ADD_OP_OUTPUT_VARIABLES, --add_op_output_variables ADD_OP_OUTPUT_VARIABLES ADD_OP_OUTPUT_VARIABLES ADD_OP_OUTPUT_VARIABLES\n      output_variables can be specified multiple times.\n      --add_op_output_variables variable_name numpy.dtype shape\n      https://github.com/onnx/onnx/blob/main/docs/Operators.md\n      e.g.\n      --add_op_output_variables outname1 float32 [1,3,5,5]\n      --add_op_output_variables outname2 int32 [1]\n      --add_op_output_variables outname3 float64 [1,3,224,224]\n\n  -aoa ADD_OP_ATTRIBUTES ADD_OP_ATTRIBUTES ADD_OP_ATTRIBUTES, --add_op_attributes ADD_OP_ATTRIBUTES ADD_OP_ATTRIBUTES ADD_OP_ATTRIBUTES\n      attributes can be specified multiple times.\n      --add_op_attributes name dtype value\n      dtype is one of \"float32\" or \"float64\" or \"int32\" or \"int64\" or \"str\".\n      https://github.com/onnx/onnx/blob/main/docs/Operators.md\n      e.g.\n      --add_op_attributes alpha float32 1.0\n      --add_op_attributes beta float32 1.0\n      --add_op_attributes transA int64 0\n      --add_op_attributes transB int64 0\n\n  -csoon CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES, --connection_src_op_output_names CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES CONNECTION_SRC_OP_OUTPUT_NAMES\n      Specify the name of the output name from which to connect.\n      e.g.\n      -Before-\n        [OpA]oname1 - iname1[OpB]oname1\n        [OpC]oname1\n\n      -After-\n        [OpA]oname1 - iname1[AddOP1]oname1 - iname1[OpB]oname1\n        [OpC]oname1 - iname2[AddOP1]\n\n      When extrapolating a new OP between OpA and OpB.\n      --connection_src_op_output_names OpA oname1 AddOP1 iname1\n      --connection_src_op_output_names OpC oname1 AddOP1 iname2\n      This need not be specified only when the type of the newly added OP is Constant.\n\n  -cdoin CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES, --connection_dest_op_input_names CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES CONNECTION_DEST_OP_INPUT_NAMES\n      Specify the name of the input name from which to connect.\n      e.g.\n      -Before-\n        [OpA]oname1 - iname1[OpB]oname1\n        [OpC]oname1\n\n      -After-\n        [OpA]oname1 - iname1[AddOP1]oname1 - iname1[OpB]oname1\n        [OpC]oname1 - iname2[AddOP1]\n\n      When extrapolating a new OP between OpA and OpB.\n      --connection_dest_op_input_names AddOP1 oname1 OpB iname1\n\n  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH\n      Output onnx file path.\n      If not specified, a file with \"_mod\" appended to the end of input_onnx_file_path is output.\n      e.g.\n      aaa.onnx -\u003e aaa_mod.onnx\n\n  -n, --non_verbose\n      Do not show all information logs. Only error logs are displayed.\n```\n\n## 3. In-script Usage\n```python\n\u003e\u003e\u003e from sna4onnx import add\n\u003e\u003e\u003e help(add)\n\nHelp on function add in module sna4onnx.onnx_operation_adder:\n\nadd(\n  connection_src_op_output_names: List,\n  connection_dest_op_input_names: List,\n  add_op_type: str, add_op_name: str,\n  add_op_input_variables: Union[dict, NoneType] = None,\n  add_op_output_variables: Union[dict, NoneType] = None,\n  add_op_attributes: Union[dict, NoneType] = None,\n  input_onnx_file_path: Union[str, NoneType] = '',\n  onnx_graph: Union[onnx.onnx_ml_pb2.ModelProto, NoneType] = None,\n  output_onnx_file_path: Union[str, NoneType] = '',\n  non_verbose: Union[bool, NoneType] = False\n) -\u003e onnx.onnx_ml_pb2.ModelProto\n\n    Parameters\n    ----------\n    connection_src_op_output_names: List\n        Specify the name of the output name from which to connect.\n\n        e.g.\n        -Before-\n            [OpA] outnameA - inpnameB1 [OpB] outnameB\n            [OpC] outnameC\n        -After-\n            [OpA] outnameA - inpname1 [AddOP1] outname1 - inpnameB1 [OpB] outnameB\n            [OpC] outnameC - inpname2 [AddOP1]\n        When extrapolating a new OP between OpA and OpB.\n        connection_src_op_output_names = [\n            [\"OpA\", \"outnameA\", \"AddOP1\", \"inpname1\",],\n            [\"OpC\", \"outnameC\", \"AddOP1\", \"inpname2\",],\n        ]\n\n        This need not be specified only when the type of the newly added OP is Constant.\n\n    connection_dest_op_input_names: List\n        Specify the name of the input name from which to connect.\n\n        e.g.\n        -Before-\n            [OpA] outnameA - inpnameB1 [OpB] outnameB\n            [OpC] outnameC\n        -After-\n            [OpA] outnameA - inpname1 [AddOP1] outname1 - inpnameB1 [OpB] outnameB\n            [OpC] outnameC - inpname2 [AddOP1]\n        When extrapolating a new OP between OpA and OpB.\n        connection_dest_op_input_names = [\n            [\"AddOP1\", \"outname1\", \"OpB\", \"inpnameB1\"],\n        ]\n\n    add_op_type: str\n        ONNX op type.\n        See below for the types of OPs that can be specified.\n\n        e.g. \"Add\", \"Div\", \"Gemm\", ...\n        https://github.com/onnx/onnx/blob/main/docs/Operators.md\n\n    add_op_name: str\n        Name of OP to be added.\n\n        e.g. --add_op_name AddOP1\n\n    add_op_input_variables: Optional[dict]\n        Specify input variables for the OP to be generated.\n        See below for the variables that can be specified.\n\n        {\n            \"input_var_name1\": [numpy.dtype, shape],\n            \"input_var_name2\": [numpy.dtype, shape],\n            ...\n        }\n\n        e.g.\n        add_op_input_variables = {\n            \"inpname1\": [np.float32, [1,224,224,3]],\n            \"inpname2\": [np.bool_, [0]],\n            ...\n        }\n        https://github.com/onnx/onnx/blob/main/docs/Operators.md\n\n    add_op_output_variables: Optional[dict]\n        Specify output variables for the OP to be generated.\n        See below for the variables that can be specified.\n\n        {\n            \"output_var_name1\": [numpy.dtype, shape],\n            \"output_var_name2\": [numpy.dtype, shape],\n            ...\n        }\n\n        e.g.\n        add_op_output_variables = {\n            \"outname1\": [np.float32, [1,224,224,3]],\n            \"outname2\": [np.bool_, [0]],\n            ...\n        }\n        https://github.com/onnx/onnx/blob/main/docs/Operators.md\n\n    add_op_attributes: Optional[dict]\n        Specify output add_op_attributes for the OP to be generated.\n        See below for the add_op_attributes that can be specified.\n\n        {\n            \"attr_name1\": value1,\n            \"attr_name2\": value2,\n            \"attr_name3\": value3,\n            ...\n        }\n\n        e.g.\n        add_op_attributes = {\n            \"alpha\": 1.0,\n            \"beta\": 1.0,\n            \"transA\": 0,\n            \"transB\": 0,\n        }\n        Default: None\n        https://github.com/onnx/onnx/blob/main/docs/Operators.md\n\n    input_onnx_file_path: Optional[str]\n        Input onnx file path.\n        Either input_onnx_file_path or onnx_graph must be specified.\n        Default: ''\n\n    onnx_graph: Optional[onnx.ModelProto]\n        onnx.ModelProto.\n        Either input_onnx_file_path or onnx_graph must be specified.\n        onnx_graph If specified, ignore input_onnx_file_path and process onnx_graph.\n\n    output_onnx_file_path: Optional[str]\n        Output onnx file path. If not specified, no ONNX file is output.\n        Default: ''\n\n    non_verbose: Optional[bool]\n        Do not show all information logs. Only error logs are displayed.\n        Default: False\n\n    Returns\n    -------\n    changed_graph: onnx.ModelProto\n        Changed onnx ModelProto.\n```\n\n## 4. CLI Execution\n```bash\n$ sna4onnx \\\n--input_onnx_file_path crestereo_init_iter2_120x160.onnx \\\n--connection_src_op_output_name Sub_451 onnx::Pow_603 dummy_mul inp1 \\\n--connection_dest_op_input_name dummy_mul out1 Div_458 onnx::Pow_603 \\\n--add_op_type Mul \\\n--add_op_name dummy_mul \\\n--add_op_input_variables inp1 float32 [1,70,256] \\\n--add_op_input_variables inp2_const float32 [1] \\\n--add_op_output_variables out1 float32 [1,70,256] \\\n--output_onnx_file_path test.onnx\n```\n\n## 5. In-script Execution\n```python\nfrom sna4onnx import add\n\nonnx_graph = add(\n  input_onnx_file_path=\"crestereo_init_iter2_120x160.onnx\",\n  connection_src_op_output_names=[\n    [\"Sub_451\", \"onnx::Pow_603\", \"dummy_mul\", \"inp1\"],\n  ],\n  connection_dest_op_input_names=[\n    [\"dummy_mul\",\"out1\", \"Div_458\", \"onnx::Pow_603\"],\n  ],\n  add_op_type=\"Mul\",\n  add_op_name=\"dummy_mul\",\n  add_op_input_variables={\n    \"inp1\": [np.float32, [1,70,256]],\n    \"inp2_const\": [np.float32, [1]],\n  },\n  add_op_output_variables={\n    \"out1\": [np.float32, [1,70,256]],\n  },\n)\n\n# or\n\nonnx_graph = add(\n  onnx_graph=graph,\n  connection_src_op_output_names=[\n    [\"Sub_451\", \"onnx::Pow_603\", \"dummy_mul\", \"inp1\"],\n  ],\n  connection_dest_op_input_names=[\n    [\"dummy_mul\",\"out1\", \"Div_458\", \"onnx::Pow_603\"],\n  ],\n  add_op_type=\"Mul\",\n  add_op_name=\"dummy_mul\",\n  add_op_input_variables={\n    \"inp1\": [np.float32, [1,70,256]],\n    \"inp2_const\": [np.float32, [1]],\n  },\n  add_op_output_variables={\n    \"out1\": [np.float32, [1,70,256]],\n  },\n)\n```\n\n## 6. Sample\n### Before\n![20220426234631](https://user-images.githubusercontent.com/33194443/165336928-066542b7-89ad-4c51-9e10-6bb2e8e6344a.png)\n\n### After\n![image](https://user-images.githubusercontent.com/33194443/165440616-d9947caa-ba7d-4f83-9d97-d71be0d3a579.png)\n\n## 7. Reference\n1. https://github.com/onnx/onnx/blob/main/docs/Operators.md\n2. https://docs.nvidia.com/deeplearning/tensorrt/onnx-graphsurgeon/docs/index.html\n3. https://github.com/NVIDIA/TensorRT/tree/main/tools/onnx-graphsurgeon\n4. https://github.com/PINTO0309/simple-onnx-processing-tools\n5. https://github.com/PINTO0309/PINTO_model_zoo\n\n## 8. Issues\nhttps://github.com/PINTO0309/simple-onnx-processing-tools/issues\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpinto0309%2Fsna4onnx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpinto0309%2Fsna4onnx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpinto0309%2Fsna4onnx/lists"}