{"id":15695850,"url":"https://github.com/pinto0309/sbi4onnx","last_synced_at":"2025-05-05T21:44:40.093Z","repository":{"id":40385384,"uuid":"487798767","full_name":"PINTO0309/sbi4onnx","owner":"PINTO0309","description":"A very simple script that only initializes the batch size of ONNX. Simple Batchsize Initialization for ONNX.","archived":false,"fork":false,"pushed_at":"2024-05-28T23:43:19.000Z","size":15725,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-16T13:36:38.891Z","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-02T10:12:56.000Z","updated_at":"2024-05-28T23:42:31.000Z","dependencies_parsed_at":"2024-01-09T00:35:07.753Z","dependency_job_id":"fbf3edcf-0046-4dc1-b090-ef6c992af1bc","html_url":"https://github.com/PINTO0309/sbi4onnx","commit_stats":{"total_commits":30,"total_committers":2,"mean_commits":15.0,"dds":"0.033333333333333326","last_synced_commit":"2316fa9fbdca2a963074c06496c7bc2b7b7ab1c5"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsbi4onnx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsbi4onnx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsbi4onnx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PINTO0309%2Fsbi4onnx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PINTO0309","download_url":"https://codeload.github.com/PINTO0309/sbi4onnx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252582246,"owners_count":21771635,"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-03T19:05:10.750Z","updated_at":"2025-05-05T21:44:40.058Z","avatar_url":"https://github.com/PINTO0309.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sbi4onnx\nA very simple script that only initializes the batch size of ONNX. **S**imple **B**atchsize **I**nitialization for **ONNX**.\n\nhttps://github.com/PINTO0309/simple-onnx-processing-tools\n\n[![Downloads](https://static.pepy.tech/personalized-badge/sbi4onnx?period=total\u0026units=none\u0026left_color=grey\u0026right_color=brightgreen\u0026left_text=Downloads)](https://pepy.tech/project/sbi4onnx) ![GitHub](https://img.shields.io/github/license/PINTO0309/sbi4onnx?color=2BAF2B) [![PyPI](https://img.shields.io/pypi/v/sbi4onnx?color=2BAF2B)](https://pypi.org/project/sbi4onnx/) [![CodeQL](https://github.com/PINTO0309/sbi4onnx/workflows/CodeQL/badge.svg)](https://github.com/PINTO0309/sbi4onnx/actions?query=workflow%3ACodeQL)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/33194443/170157713-78a1b84e-caf6-4abe-92e4-c9ea51bcaacd.png\" /\u003e\n\u003c/p\u003e\n\n# Key concept\n\n- [x] Initializes the ONNX batch size with the specified characters.\n- [x] This tool is not a panacea and may fail to initialize models with very complex structures. For example, there is an ONNX that contains a `Reshape` that involves a batch size, or a `Gemm` that contains a batch output other than 1 in the output result.\n- [x] A `Reshape` in a graph cannot contain more than two undefined shapes, such as `-1` or `N` or `None` or `unk_*`. Therefore, before initializing the batch size with this tool, make sure that the `Reshape` does not already contain one or more `-1` dimensions. If it already contains undefined dimensions, it may be possible to successfully initialize the batch size by pre-writing the undefined dimensions of the relevant `Reshape` to static values using **[sam4onnx](https://github.com/PINTO0309/sam4onnx)**.\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 --no-deps -U onnx-simplifier \\\n\u0026\u0026 pip install -U sbi4onnx\n```\n### 1-2. Docker\nhttps://github.com/PINTO0309/simple-onnx-processing-tools#docker\n\n## 2. CLI Usage\n```\n$ sbi4onnx -h\n\nusage:\n  sbi4onnx [-h]\n  -if INPUT_ONNX_FILE_PATH\n  -of OUTPUT_ONNX_FILE_PATH\n  -ics INITIALIZATION_CHARACTER_STRING\n  [-dos]\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  -of OUTPUT_ONNX_FILE_PATH, --output_onnx_file_path OUTPUT_ONNX_FILE_PATH\n      Output onnx file path.\n\n  -ics INITIALIZATION_CHARACTER_STRING, --initialization_character_string INITIALIZATION_CHARACTER_STRING\n      String to initialize batch size. \"-1\" or \"N\" or \"xxx\", etc...\n      Default: '-1'\n\n  -dos, --disable_onnxsim\n      Suppress the execution of onnxsim on the backend and dare to leave redundant processing.\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 sbi4onnx import initialize\n\u003e\u003e\u003e help(initialize)\n\nHelp on function initialize in module sbi4onnx.onnx_batchsize_initialize:\n\ninitialize(\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  initialization_character_string: Union[str, NoneType] = '-1',\n  non_verbose: Union[bool, NoneType] = False,\n  disable_onnxsim: Union[bool, NoneType] = False,\n) -\u003e onnx.onnx_ml_pb2.ModelProto\n\n    Parameters\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    initialization_character_string: Optional[str]\n        String to initialize batch size. \"-1\" or \"N\" or \"xxx\", etc...\n        Default: '-1'\n\n    disable_onnxsim: Optional[bool]\n        Suppress the execution of onnxsim on the backend and dare to leave redundant processing.\n        Default: False\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$ sbi4onnx \\\n--input_onnx_file_path whenet_224x224.onnx \\\n--output_onnx_file_path whenet_Nx224x224.onnx \\\n--initialization_character_string N\n\n$ sbi4onnx \\\n--input_onnx_file_path whenet_224x224.onnx \\\n--output_onnx_file_path whenet_Nx224x224.onnx \\\n--initialization_character_string -1\n\n$ sbi4onnx \\\n--input_onnx_file_path whenet_224x224.onnx \\\n--output_onnx_file_path whenet_Nx224x224.onnx \\\n--initialization_character_string abcdefg\n```\n\n## 5. In-script Execution\n```python\nfrom sbi4onnx import initialize\n\nonnx_graph = initialize(\n  input_onnx_file_path=\"whenet_224x224.onnx\",\n  output_onnx_file_path=\"whenet_Nx224x224.onnx\",\n  initialization_character_string=\"abcdefg\",\n)\n\n# or\n\nonnx_graph = initialize(\n  onnx_graph=graph,\n  initialization_character_string=\"abcdefg\",\n)\n```\n\n## 6. Sample\n### Before\n![image](https://user-images.githubusercontent.com/33194443/166225839-3b8d6378-e76f-4139-b5d1-db547ba16d16.png)\n\n### After\n![image](https://user-images.githubusercontent.com/33194443/166225927-cb39ea2f-85f6-4fdd-afbc-78a46a2475a1.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%2Fsbi4onnx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpinto0309%2Fsbi4onnx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpinto0309%2Fsbi4onnx/lists"}