A very simple tool to rewrite parameters such as attributes and constants for OPs in ONNX models. Simple Attribute and Constant Modifier for ONNX.
Project description
sam4onnx
A very simple tool to rewrite parameters such as attributes and constants for OPs in ONNX models. Simple Attribute and Constant Modifier for ONNX.
https://github.com/PINTO0309/simple-onnx-processing-tools
Key concept
- Specify an arbitrary OP name and Constant type INPUT name or an arbitrary OP name and Attribute name, and pass the modified constants to rewrite the parameters of the relevant OP.
- Two types of input are accepted: .onnx file input and onnx.ModelProto format objects.
- To design the operation to be simple, only a single OP can be specified.
- Attributes and constants are forcibly rewritten, so the integrity of the entire graph is not checked in detail.
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 sam4onnx
1-2. Docker
### docker pull
$ docker pull pinto0309/sam4onnx:latest
### docker build
$ docker build -t pinto0309/sam4onnx:latest .
### docker run
$ docker run --rm -it -v `pwd`:/workdir pinto0309/sam4onnx:latest
$ cd /workdir
2. CLI Usage
$ sam4onnx -h
usage:
sam4onnx [-h]
--input_onnx_file_path INPUT_ONNX_FILE_PATH
--output_onnx_file_path OUTPUT_ONNX_FILE_PATH
[--op_name OP_NAME]
[--attributes NAME DTYPE VALUE]
[--input_constants NAME DTYPE VALUE]
[--non_verbose]
optional arguments:
-h, --help
show this help message and exit
--input_onnx_file_path INPUT_ONNX_FILE_PATH
Input onnx file path.
--output_onnx_file_path OUTPUT_ONNX_FILE_PATH
Output onnx file path.
--op_name OP_NAME
OP name of the attributes to be changed.
When --attributes is specified, --op_name must always be specified.
e.g. --op_name aaa
--attributes NAME DTYPE VALUE
Parameter to change the attribute of the OP specified in --op_name.
If the OP specified in --op_name has no attributes,
it is ignored. attributes can be specified multiple times.
--attributes name dtype value dtype is one of
"float32" or "float64" or "int32" or "int64" or "str".
https://github.com/onnx/onnx/blob/main/docs/Operators.md
e.g.
--attributes alpha float32 [[1.0]]
--attributes beta float32 [1.0]
--attributes transA int64 0
--attributes transB int64 0
--input_constants NAME DTYPE VALUE
Specifies the name of the constant to be changed.
If you want to change only the constant,
you do not need to specify --op_name and --attributes.
input_constants can be specified multiple times.
--input_constants constant_name numpy.dtype value
e.g.
--input_constants constant_name1 int64 0
--input_constants constant_name2 float32 [[1.0,2.0,3.0],[4.0,5.0,6.0]]
--non_verbose
Do not show all information logs. Only error logs are displayed.
3. In-script Usage
$ python
>>> from sam4onnx import modify
>>> help(modify)
Help on function modify in module sam4onnx.onnx_attr_const_modify:
modify(
input_onnx_file_path: Union[str, NoneType] = '',
output_onnx_file_path: Union[str, NoneType] = '',
onnx_graph: Union[onnx.onnx_ml_pb2.ModelProto, NoneType] = None,
op_name: Union[str, NoneType] = '',
attributes: Union[dict, NoneType] = None,
input_constants: Union[dict, NoneType] = None,
non_verbose: Union[bool, NoneType] = False
) -> onnx.onnx_ml_pb2.ModelProto
Parameters
----------
input_onnx_file_path: Optional[str]
Input onnx file path.
Either input_onnx_file_path or onnx_graph must be specified.
output_onnx_file_path: Optional[str]
Output onnx file path.
If output_onnx_file_path is not specified, no .onnx file is output.
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.
op_name: Optional[str]
OP name of the attributes to be changed.
When --attributes is specified, --op_name must always be specified.
Default: ''
https://github.com/onnx/onnx/blob/main/docs/Operators.md
attributes: Optional[dict]
Specify output attributes for the OP to be generated.
See below for the attributes that can be specified.
{"attr_name1": numpy.ndarray, "attr_name2": numpy.ndarray, ...}
e.g. attributes =
{
"alpha": np.asarray(1.0, dtype=np.float32),
"beta": np.asarray(1.0, dtype=np.float32),
"transA": np.asarray(0, dtype=np.int64),
"transB": np.asarray(0, dtype=np.int64)
}
Default: None
https://github.com/onnx/onnx/blob/main/docs/Operators.md
input_constants: Optional[dict]
Specifies the name of the constant to be changed.
If you want to change only the constant,
you do not need to specify --op_name and --attributes.
{"constant_name1": numpy.ndarray, "constant_name2": numpy.ndarray, ...}
e.g.
input_constants =
{
"constant_name1": np.asarray(0, dtype=np.int64),
"constant_name2": np.asarray([[1.0,2.0,3.0],[4.0,5.0,6.0]], dtype=np.float32)
}
Default: None
https://github.com/onnx/onnx/blob/main/docs/Operators.md
non_verbose: Optional[bool]
Do not show all information logs. Only error logs are displayed.
Default: False
Returns
-------
modified_graph: onnx.ModelProto
Mddified onnx ModelProto
4. CLI Execution
$ sam4onnx \
--op_name Transpose_17 \
--input_onnx_file_path input.onnx \
--output_onnx_file_path output.onnx \
--attributes perm int64 [0,1]
5. In-script Execution
from sam4onnx import modify
modified_graph = modify(
onnx_graph=graph,
input_constants={"241": np.asarray([1], dtype=np.int64)},
non_verbose=True,
)
6. Sample
6-1. Transpose - update perm
$ sam4onnx \
--op_name Transpose_17 \
--input_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt.onnx \
--output_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt_mod.onnx \
--attributes perm int64 [0,1]
6-2. Mul - update Constant (170)
- From: 2
, To: 1
$ sam4onnx \
--input_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt.onnx \
--output_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt_mod.onnx \
--input_constants 170 float32 1
6-3. Reshape - update Constant (241)
- From: [-1]
, To: [1]
$ sam4onnx \
--input_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt.onnx \
--output_onnx_file_path hitnet_sf_finalpass_720x1280_nonopt_mod.onnx \
--input_constants 241 int64 [1]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sam4onnx-1.0.1.tar.gz
(9.4 kB
view details)
Built Distribution
File details
Details for the file sam4onnx-1.0.1.tar.gz
.
File metadata
- Download URL: sam4onnx-1.0.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d5c44ede43fb891dd7fed63488f1ec00655e8e7881e47084ed25f8d2197473b |
|
MD5 | 141c0ace4820b22b5ea429c97c6efeb2 |
|
BLAKE2b-256 | 52ed3272c80c0d1e744e8f05881f702de550e2f09cabf450f80c4f51696816f9 |
File details
Details for the file sam4onnx-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: sam4onnx-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17930fa96e3f7ab4661f0b01353b1e42ab82a3c7a37da79b8fad32bd2697bfb9 |
|
MD5 | 85b1d5a6c511b4b47f4bfb9b790901f6 |
|
BLAKE2b-256 | af505bba7dd3fd58230ad5eba856e64cabd9896e5006b701596d5e558d4fb42e |