Package: sam4onnx
sam4onnxA very simple tool to rewrite parameters such as attributes and constants for OPs in ONNX models.SimpleAttribute and ConstantModifier forONNX.https://github.com/PINTO0309/simple-onnx-processing-toolsKey conceptSpecify 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.Support for recursive search of subgraphs ofIfOP.1. Setup1-1. HostPC### option$echoexportPATH="~/.local/bin:$PATH">>~/.bashrc\&&source~/.bashrc### run$pipinstall-Uonnx\&&python3-mpipinstall-Uonnx_graphsurgeon--index-urlhttps://pypi.ngc.nvidia.com\&&pipinstall-Usam4onnx1-2. Dockerhttps://github.com/PINTO0309/simple-onnx-processing-tools#docker2. CLI Usage$ sam4onnx -h

usage:
    sam4onnx [-h]
    -if INPUT_ONNX_FILE_PATH
    -of OUTPUT_ONNX_FILE_PATH
    [-on OP_NAME]
    [-a NAME DTYPE VALUE]
    [-da DELETE_ATTRIBUTES [DELETE_ATTRIBUTES ...]]
    [-ic NAME DTYPE VALUE]
    [-os OUTPUT_NAME OUTPUT_SHAPE]
    [-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.

  -on OP_NAME, --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

  -a ATTRIBUTES ATTRIBUTES ATTRIBUTES, --attributes ATTRIBUTES ATTRIBUTES ATTRIBUTES
    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

  -da DELETE_ATTRIBUTES [DELETE_ATTRIBUTES ...], --delete_attributes DELETE_ATTRIBUTES [DELETE_ATTRIBUTES ...]
    Parameter to delete the attribute of the OP specified in --op_name.
    If the OP specified in --op_name has no attributes,
    it is ignored. delete_attributes can be specified multiple times.
    --delete_attributes name1 name2 name3
    https://github.com/onnx/onnx/blob/main/docs/Operators.md

    e.g. --delete_attributes alpha beta

  -ic INPUT_CONSTANTS INPUT_CONSTANTS INPUT_CONSTANTS, --input_constants INPUT_CONSTANTS INPUT_CONSTANTS INPUT_CONSTANTS
    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]]
    --input_constants constant_name3 float32 [\'-Infinity\']

  -os OUTPUT_SHAPES OUTPUT_SHAPES, --output_shapes OUTPUT_SHAPES OUTPUT_SHAPES
    Specifies the name of the output to be changed. output_shapes can be specified multiple times.
    --output_shapes output_name1 shape1
    --output_shapes output_name2 shape2

    e.g.
    --output_shapes output_name1 [1]
    --output_shapes output_name2 [1,3,224,224]

  -n, --non_verbose
    Do not show all information logs. Only error logs are displayed.3. In-script Usage$python>>>fromsam4onnximportmodify>>>help(modify)Helponfunctionmodifyinmodulesam4onnx.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,delete_attributes:Union[List[str],NoneType]=None,input_constants:Union[dict,NoneType]=None,output_shapes:Optional[List]=None,non_verbose:Union[bool,NoneType]=False)->onnx.onnx_ml_pb2.ModelProtoParameters----------input_onnx_file_path:Optional[str]Inputonnxfilepath.Eitherinput_onnx_file_pathoronnx_graphmustbespecified.output_onnx_file_path:Optional[str]Outputonnxfilepath.Ifoutput_onnx_file_pathisnotspecified,no.onnxfileisoutput.onnx_graph:Optional[onnx.ModelProto]onnx.ModelProto.Eitherinput_onnx_file_pathoronnx_graphmustbespecified.onnx_graphIfspecified,ignoreinput_onnx_file_pathandprocessonnx_graph.op_name:Optional[str]OPnameoftheattributestobechanged.When--attributesisspecified,--op_namemustalwaysbespecified.Default:''https://github.com/onnx/onnx/blob/main/docs/Operators.mdattributes:Optional[dict]SpecifyoutputattributesfortheOPtobegenerated.Seebelowfortheattributesthatcanbespecified.{"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:Nonehttps://github.com/onnx/onnx/blob/main/docs/Operators.mddelete_attributes:Optional[List[str]]ParametertodeletetheattributeoftheOPspecifiedin--op_name.IftheOPspecifiedin--op_namehasnoattributes,itisignored.delete_attributescanbespecifiedmultipletimes.--delete_attributesname1name2name3https://github.com/onnx/onnx/blob/main/docs/Operators.mde.g.--delete_attributesalphabetainput_constants:Optional[dict]Specifiesthenameoftheconstanttobechanged.Ifyouwanttochangeonlytheconstant,youdonotneedtospecify--op_nameand--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),"constant_name3":np.asarray([-np.inf],dtype=np.float32),}Default:Nonehttps://github.com/onnx/onnx/blob/main/docs/Operators.mdoutput_shapes:Optional[List[int]]Specifiesthenameoftheoutput_shapestobechanged.output_shapescanbespecifiedmultipletimes.output_shapes=[['output_name1',shape1],['output_name2',shape2],:]e.g.output_shapes=[['aaa',[1]],['bbb',[1,3,224,224]],]non_verbose:Optional[bool]Donotshowallinformationlogs.Onlyerrorlogsaredisplayed.Default:FalseReturns-------modified_graph:onnx.ModelProtoMddifiedonnxModelProto4. CLI Execution$sam4onnx\--input_onnx_file_pathinput.onnx\--output_onnx_file_pathoutput.onnx\--op_nameTranspose_17\--attributespermint64[0,1]5. In-script Executionfromsam4onnximportmodifymodified_graph=modify(onnx_graph=graph,op_name="Reshape_17",input_constants={"241":np.asarray([1],dtype=np.int64)},non_verbose=True,)6. Sample6-1. Transpose - updatepermandoutput_shapes$sam4onnx\--input_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt.onnx\--output_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt_mod.onnx\--op_nameTranspose_17\--attributespermint64[0,1]\--output_shapes[128,256]6-2. Mul - updateConstant (170)- From:2, To:1$sam4onnx\--input_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt.onnx\--output_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt_mod.onnx\--op_nameMul_5\--input_constants170float3216-3. Reshape - updateConstant (241)- From:[-1], To:[1]$sam4onnx\--input_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt.onnx\--output_onnx_file_pathhitnet_sf_finalpass_720x1280_nonopt_mod.onnx\--op_nameReshape_34\--input_constants241int64[1]7. Issueshttps://github.com/PINTO0309/simple-onnx-processing-tools/issues