sima_utils.onnx.model_builder

Attributes

OnnxNode

Classes

OnnxModelBuilder

Helper class to build onnx model.

Functions

get_onnx_node_output_name(β†’Β str)

Gets the output name of the given node with only one output.

Module Contents

sima_utils.onnx.model_builder.OnnxNode
sima_utils.onnx.model_builder.get_onnx_node_output_name(node: OnnxNode) str

Gets the output name of the given node with only one output.

Args: node: A node in ONNX model.

Returns:

A string corresponding to the node’s single output.

class sima_utils.onnx.model_builder.OnnxModelBuilder

Helper class to build onnx model.

Usage:

# Initialization builder = OnnxModelBuilder()

# Create arbitrary number of model inputs input_node = builder.create_input_node(β€œinput”, input_shape, input_dtype)

# Create nodes; use helper functions provided for each operation type. conv_node = builder.make_conv_node(input_node, np_weights, np_bias, **conv_attrs) …

# Create model outputs; use get_onnx_node_output_name to get the names from output nodes builder.create_output_node(get_onnx_node_output_name(conv_node), output_shape, output_dtype)

# Finalize model and save to a file builder.create_and_save_model(Path(onnx_file_path))

IR_VERSION

IR version of the onnx model.

OPSET_ID

Operator set id of the onnx model.

_input_nodes

Input nodes of the onnx model.

_output_nodes

Output nodes of the onnx model.

_initializer_list

List of initializers (constants) used in the model.

_node_list

List of nodes created for a model.

_op_count

Dictionary keeping track of quantity of created nodes per op_type.

IR_VERSION: ClassVar[int] = 8
OPSET_ID: ClassVar[int] = 17
create_model(model_name: str) onnx.ModelProto

Creates the model.

Parameters:

model_name – The name of the model.

Returns:

An ONNX ModelProto object defining the ONNX model.

create_and_save_model(onnx_file_name: pathlib.Path)

Creates and saves the model, performing also shape inference in the process.

Parameters:

onnx_file_name – A path to the file in which the ONNX model will be saved.

create_input_node(name: str, shape: collections.abc.Sequence[int], dtype: type = np.float32) OnnxNode

Creates an input node with the provided name, shape and data type.

Parameters:
  • name – Input’s name.

  • shape – Input’s shape.

  • dtype – Input’s data type.

Returns:

The OnnxNode object (onnx.ValueInfoProto) for the created input node.

create_output_node(name: str, shape: collections.abc.Sequence[int], dtype: type = np.float32)

Creates an output node with the provided name, shape and data type.

Parameters:
  • name – Output’s name.

  • shape – Output’s shape.

  • dtype – Output’s data type.

create_initializer(name: str | None, value: int | float | numpy.ndarray) OnnxNode

Creates an initializer with the name.

Parameters:
  • name – Name of the initializer. If name is None, use β€œConstant_{idx}”.

  • value – Value of the initializer.

Returns:

An OnnxNode object (onnx.TensorProto) created for the initializer.

build_op(input_nodes: collections.abc.Sequence[OnnxNode], op_type: str, **kwargs) OnnxNode

Builds an ONNX node.

Parameters:
  • input_nodes – A list of input nodes.

  • op_type – Name of the operator type.

  • **kwargs – Operator attributes.

Returns:

Created OnnxNode (onnx.NodeProto) object.

make_conv_node(input_node: OnnxNode, weight: numpy.ndarray, bias: numpy.ndarray | None = None, auto_pad: str | None = None, dilations: collections.abc.Sequence[int] | None = None, strides: collections.abc.Sequence[int] | None = None, group: int | None = None, kernel_shape: collections.abc.Sequence[int] | None = None, pads: collections.abc.Sequence[int] | None = None) OnnxNode

Create an ONNX Conv operator node. https://onnx.ai/onnx/operators/onnx__Conv.html#conv-11

Parameters:
  • input_node – Input node to Conv operator node.

  • weight – Weight tensor numpy array.

  • bias – Optional 1D bias tensor numpy array.

  • auto_pad – Optional string value for Conv β€˜auto_pad’ attribute. Valid values are NOTSET, SAME_UPPER, SAME_LOWER and VALID. If None, the β€˜auto_pad’ attribute will take the default (NOTSET) value.

  • dilations – Optional sequence of integers for Conv β€˜dilations’ attribute. If None, the β€˜dilations’ attribute will be set to 1 along each spatial axis.

  • strides – Stride along each spatial axis. If not present, the stride defaults is 1 along each spatial axis.

  • group – Optional integer value for Conv β€˜groups’ attribute. If None, the β€˜groups’ attribute will be set to 1.

  • kernel_shape – Optional sequence of integers for Conv β€˜kernel_shape’ attribute. If None, the β€˜kernel_shape’ attribute will be inferred from weight shape.

  • pads – Optional sequence of integers for Conv β€˜pads’ attribute. If None, the β€˜pads’ attribute will be set to 0 along start and end of each spatial axis.

Returns:

Onnx NodeProto instance containing the Conv operation.

make_conv_transpose_node(input_node: OnnxNode, weight: numpy.ndarray, bias: numpy.ndarray | None = None, auto_pad: str | None = None, dilations: collections.abc.Sequence[int] | None = None, strides: collections.abc.Sequence[int] | None = None, group: int | None = None, kernel_shape: collections.abc.Sequence[int] | None = None, pads: collections.abc.Sequence[int] | None = None, output_padding: collections.abc.Sequence[int] | None = None, output_shape: collections.abc.Sequence[int] | None = None) OnnxNode

Create an ONNX ConvTranspose operator node. https://onnx.ai/onnx/operators/onnx__ConvTranspose.html#convtranspose-11

Parameters:
  • input_node – Input node to ConvTranspose operator node.

  • weight – Weight tensor numpy array.

  • bias – Optional 1D bias tensor numpy array.

  • auto_pad – Optional string value for Conv β€˜auto_pad’ attribute. Valid values are NOTSET, SAME_UPPER, SAME_LOWER and VALID. If None, the β€˜auto_pad’ attribute will take the default (NOTSET) value.

  • dilations – Optional sequence of integers for Conv β€˜dilations’ attribute. If None, the β€˜dilations’ attribute will be set to 1 along each spatial axis.

  • strides – Stride along each spatial axis. If not present, the stride defaults is 1 along each spatial axis.

  • group – Optional integer value for Conv β€˜groups’ attribute. If None, the β€˜groups’ attribute will be set to 1.

  • kernel_shape – Optional sequence of integers for Conv β€˜kernel_shape’ attribute. If None, the β€˜kernel_shape’ attribute will be inferred from weight shape.

  • pads – Optional sequence of integers for Conv β€˜pads’ attribute. If None, the β€˜pads’ attribute will be set to 0 along start and end of each spatial axis.

  • output_padding – Optional sequence of ints to add output shape padding (per spatial axis). Defaults to 0 per spatial axis if None. Typically not used together with output_shape.

  • output_shape – Optional sequence of ints to explicitly set the output spatial shape.

Returns:

Onnx NodeProto instance containing the ConvTranspose operation.

make_avg_pool_node(input_node: OnnxNode, kernel_shape: collections.abc.Sequence[int], strides: collections.abc.Sequence[int] | None = None, pads: collections.abc.Sequence[int] | None = None, ceil_mode: bool = False, count_include_pad: bool = False, auto_pad: str = 'NOTSET') OnnxNode

Create an ONNX AveragePool operator node. https://onnx.ai/onnx/operators/onnx__AveragePool.html#averagepool-11

Parameters:
  • input_node – Input node to the AveragePool operator node.

  • kernel_shape – The size of the kernel along each axis.

  • strides – Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis.

  • pads – Padding for the beginning and ending along each spatial axis. If None, pads default to 0 along start and end of each spatial axis.

  • ceil_mode – Whether to use ceil or floor (default) to compute the output shape.

  • count_include_pad – Whether include pad pixels when calculating values for the edges. Default is 0, doesn’t count include pad.

  • auto_pad – Valid values are NOTSET, SAME_UPPER, SAME_LOWER, and VALID. Default is β€˜NOTSET’.

Returns:

Onnx NodeProto instance containing the AveragePool operation.

make_global_avg_pool_node(input_node: OnnxNode) OnnxNode

Create an ONNX GlobalAveragePool operator node. https://onnx.ai/onnx/operators/onnx__GlobalAveragePool.html#globalaveragepool-1

Parameters:

input_node – Input node to the GlobalAveragePool operator node.

Returns:

Onnx NodeProto instance containing the GlobalAveragePool operation.

make_global_max_pool_node(input_node: OnnxNode) OnnxNode

Create an ONNX GlobalMaxPool operator node. https://onnx.ai/onnx/operators/onnx__GlobalMaxPool.html#globalmaxpool-1

Parameters:

input_node – Input node to the GlobalMaxPool operator node.

Returns:

Onnx NodeProto instance containing the GlobalMaxPool operation.

make_max_pool_node(input_node: OnnxNode, kernel_shape: collections.abc.Sequence[int], dilations: collections.abc.Sequence[int] | None = None, strides: collections.abc.Sequence[int] | None = None, pads: collections.abc.Sequence[int] | None = None, ceil_mode: bool = False, storage_order: int = 0, auto_pad: str = 'NOTSET') OnnxNode

Create an ONNX MaxPool operator node. https://onnx.ai/onnx/operators/onnx__MaxPool.html#maxpool-12

Parameters:
  • input_node – Input node to the MaxPool operator node.

  • kernel_shape – The size of the kernel along each axis.

  • dilations – Dilation value along each spatial axis of filter. If not present, the dilation defaults to 1 along each spatial axis.

  • strides – Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis.

  • pads – Padding for the beginning and ending along each spatial axis. If None, pads default to 0 along start and end of each spatial axis.

  • ceil_mode – Whether to use ceil or floor (default) to compute the output shape.

  • storage_order – The storage order of the tensor. 0 is row major, and 1 is column major.

  • auto_pad – Valid values are NOTSET, SAME_UPPER, SAME_LOWER, and VALID. Default is β€˜NOTSET’.

Returns:

Onnx NodeProto instance containing the MaxPool operation.

make_batch_normalization_node(input_node: OnnxNode, scale: OnnxNode | numpy.ndarray, bias: OnnxNode | numpy.ndarray, input_mean: OnnxNode | numpy.ndarray, input_var: OnnxNode | numpy.ndarray, epsilon: float = 1e-05, momentum: float = 0.9) OnnxNode

Create an ONNX BatchNormalization operator node. https://onnx.ai/onnx/operators/onnx__BatchNormalization.html#batchnormalization-15

Parameters:
  • input_node – Input node to the BatchNormalization operator node.

  • scale – Scale tensor of shape C. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • bias – Bias tensor of shape C. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • input_mean – Mean tensor of shape C. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • input_var – Variance tensor of shape C. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • epsilon – The epsilon value to use to avoid division by zero. Default is 1e-05.

  • momentum – Float value for the momentum used in running mean and variance computation.

Returns:

Onnx NodeProto instance containing the BatchNormalization operation.

make_clip_node(input_node: OnnxNode, min: numpy.ndarray | None = None, max: numpy.ndarray | None = None) OnnxNode

Create an ONNX Clip operator node. https://onnx.ai/onnx/operators/onnx__Clip.html#clip-13

Parameters:
  • input_node – Input node to the Clip operator node.

  • min – Minimum value for clipping. If None, no lower bound is applied.

  • max – Maximum value for clipping. If None, no upper bound is applied.

Returns:

Onnx NodeProto instance containing the Clip operation.

make_concat_node(input_nodes: list[OnnxNode], axis: int) OnnxNode

Create an ONNX Concat operator node. https://onnx.ai/onnx/operators/onnx__Concat.html#concat-13

Parameters:
  • input_nodes – List of input nodes to be concatenated.

  • axis – Which axis to concat on.

Returns:

Onnx NodeProto instance containing the Concat operation.

make_einsum_node(input_nodes: list[OnnxNode], equation: str) OnnxNode

Create an ONNX Einsum operator node. https://onnx.ai/onnx/operators/onnx__Einsum.html#einsum-12

Parameters:
  • input_nodes – List of input nodes for the Einsum operation.

  • equation – Einsum expression string.

Returns:

Onnx NodeProto instance containing the Einsum operation.

make_erf_node(input_node: OnnxNode) OnnxNode

Create an ONNX Erf operator node. https://onnx.ai/onnx/operators/onnx__Erf.html#erf-13

Parameters:

input_node – Input node to the Erf operator node.

Returns:

Onnx NodeProto instance containing the Erf operation.

make_flatten_node(input_node: OnnxNode, axis: int = 1) OnnxNode

Create an ONNX Flatten operator node. https://onnx.ai/onnx/operators/onnx__Flatten.html#flatten-13

Parameters:
  • input_node – Input node to the Flatten operator node.

  • axis – Integer specifying the axis from which to flatten the input tensor. All dimensions up to (but not including) this axis are preserved, and all following dimensions are flattened into one. Default is 1.

Returns:

Onnx NodeProto instance containing the Flatten operation.

make_gemm_node(input_a: OnnxNode, input_b: OnnxNode, input_c: OnnxNode | None = None, alpha: float = 1.0, beta: float = 1.0, transA: int = 0, transB: int = 0) OnnxNode

Create an ONNX Gemm (General Matrix Multiplication) operator node. https://onnx.ai/onnx/operators/onnx__Gemm.html#gemm-13

Parameters:
  • input_a – First input matrix (A).

  • input_b – Second input matrix (B).

  • input_c – Optional bias matrix (C). If None, no bias is added.

  • alpha – Scalar multiplier for A * B. Default is 1.0.

  • beta – Scalar multiplier for C. Default is 1.0.

  • transA – Whether A should be transposed.

  • transB – Whether B should be transposed.

Returns:

Onnx NodeProto instance containing the Gemm operation.

make_reduce_mean_node(input_node: OnnxNode, axes: collections.abc.Sequence[int] | None = None, keepdims: int = 1) OnnxNode

Create an ONNX ReduceMean operator node. https://onnx.ai/onnx/operators/onnx__ReduceMean.html#reducemean-13

Parameters:
  • input_node – Input node to the ReduceMean operator node.

  • axes – A list of integers, along which to reduce. If None, reduction is performed over all dimensions.

  • keepdims – Keep the reduced dimension or not, default 1 means keep reduced dimension.

Returns:

Onnx NodeProto instance containing the ReduceMean operation.

make_reduce_sum_node(input_node: OnnxNode, axes: numpy.ndarray | None = None, keepdims: int = 1, noop_with_empty_axes: int = 0) OnnxNode

Create an ONNX ReduceSum operator node. https://onnx.ai/onnx/operators/onnx__ReduceSum.html#reducesum-13

Parameters:
  • input_node – Input node to the ReduceSum operator node.

  • axes – Optional numpy array specifying the axes along which to reduce. If None, reduction is performed over all dimensions.

  • keepdims – Keep the reduced dimension or not, default 1 means keep reduced dimension.

  • noop_with_empty_axes – Defines behavior when axes is not provided or is empty. Integer flag (0 or 1). If 1 and axes is empty, input is unchanged and reduce sum behaves like no-op. If 0, reduction happens over all dimensions.

Returns:

Onnx NodeProto instance containing the ReduceSum operation.

make_reshape_node(input_node: OnnxNode, shape: numpy.ndarray, allowzero: int = 0) OnnxNode

Create an ONNX Reshape operator node. https://onnx.ai/onnx/operators/onnx__Reshape.html#reshape-14

Parameters:
  • input_node – Input node to the Reshape operator node.

  • shape – 1D numpy array specifying the new shape for the input tensor.

  • allowzero – By default, when any value in the β€˜shape’ input is equal to zero the corresponding dimension value is copied from the input tensor dynamically. allowzero=1 indicates that if any value in the β€˜shape’ input is set to zero, the zero value is honored, similar to NumPy.

Returns:

Onnx NodeProto instance containing the Reshape operation.

make_resize_node(input_node: OnnxNode, roi: numpy.ndarray | None = None, scales: numpy.ndarray | None = None, sizes: numpy.ndarray | None = None, coordinate_transformation_mode: str = 'half_pixel', cubic_coeff_a: float = -0.75, exclude_outside: int = 0, extrapolation_value: float = 0.0, mode: str = 'nearest', nearest_mode: str = 'round_prefer_floor') OnnxNode

Create an ONNX Resize operator node. https://onnx.ai/onnx/operators/onnx__Resize.html#resize-13

Parameters:
  • input_node – Input node to the Resize operator node.

  • roi – Optional numpy array specifying the region of interest for resizing.

  • scales – Optional numpy array specifying scale factors for each dimension.

  • sizes – Optional numpy array specifying the target size for each dimension.

  • coordinate_transformation_mode – Specifies how to transform coordinates. Common values: β€˜half_pixel’, β€˜align_corners’, β€˜asymmetric’. Default is β€˜half_pixel’.

  • cubic_coeff_a – Coefficient for cubic interpolation. Default is -0.75.

  • exclude_outside – Integer flag (0 or 1). If 1, exclude values outside the input boundaries. Default is 0.

  • extrapolation_value – Value used for extrapolation when sampling outside input boundaries. Default is 0.0.

  • mode – Interpolation mode. Valid values: β€˜nearest’, β€˜linear’, β€˜cubic’. Default is β€˜nearest’.

  • nearest_mode – Specifies rounding mode for nearest interpolation. Default is β€˜round_prefer_floor’.

Returns:

Onnx NodeProto instance containing the Resize operation.

make_matmul_node(lhs: OnnxNode | numpy.ndarray, rhs: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX MatMul operator node. https://onnx.ai/onnx/operators/onnx__MatMul.html#matmul-13

At least one of the inputs (lhs, rhs) must be a variable (onnx.NodeProto object).

Parameters:
  • lhs – Input lhs node or numpy array to MatMul operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • rhs – Input rhs node or numpy array to MatMul operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

Returns:

Onnx NodeProto instance containing the MatMul operation.

make_pad_node(input_node: OnnxNode, pads: numpy.ndarray, constant_value: int | str | bool = 0, mode: str = 'constant') OnnxNode

Create an ONNX Pad operator node. https://onnx.ai/onnx/operators/onnx__Pad.html#pad-13

Parameters:
  • input_node – The input tensor node to pad.

  • pads – Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis.

  • constant_value – A scalar value to be used if the mode chosen is constant (by default it is 0, empty string or False).

  • mode – Padding mode. Can be β€˜constant’ (default), β€˜reflect’, or β€˜edge’.

Returns:

Onnx NodeProto instance containing the Pad operation.

make_slice_node(data_node: OnnxNode, starts: numpy.ndarray, ends: numpy.ndarray, axes: numpy.ndarray | None = None, steps: numpy.ndarray | None = None) OnnxNode

Create an ONNX Slice operator node. https://onnx.ai/onnx/operators/onnx__Slice.html#slice-13

Parameters:
  • data_node – The input tensor node to slice.

  • starts – 1D numpy array of int64 specifying start indices.

  • ends – 1D numpy array of int64 specifying end indices.

  • axes – (Optional) 1D numpy array of int64 specifying axes.

  • steps – (Optional) 1D numpy array of int64 specifying steps.

Returns:

Onnx NodeProto instance containing the Slice operation.

make_split_node(input_node: OnnxNode, output_names: list[str], axis: int = 0, split: numpy.ndarray | None = None) OnnxNode

Create an ONNX Split operator node. https://onnx.ai/onnx/operators/onnx__Split.html#split-13

Parameters:
  • input_node – The input tensor node to split.

  • output_names – List of output names.

  • axis – Axis along which to split. Default is 0.

  • split – (Optional) 1D numpy array of int64 specifying sizes for each output. Values should be >= 0. Sum of the values must be equal to the dim value at β€˜axis’ specified.

Returns:

Onnx NodeProto instance containing the Split operation.

make_sqrt_node(input_node: OnnxNode) OnnxNode

Create an ONNX Sqrt operator node. https://onnx.ai/onnx/operators/onnx__Sqrt.html#sqrt-13

Parameters:

input_node – The input tensor node.

Returns:

Onnx NodeProto instance containing the Sqrt operation.

make_tanh_node(input_node: OnnxNode) OnnxNode

Create an ONNX Tanh operator node. https://onnx.ai/onnx/operators/onnx__Tanh.html#tanh-13

Parameters:

input_node – The input tensor node.

Returns:

Onnx NodeProto instance containing the Sqrt operation.

make_add_node(lhs: OnnxNode | numpy.ndarray, rhs: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX Add operator node. https://onnx.ai/onnx/operators/onnx__Add.html#add-14

At least one of the inputs (lhs, rhs) must be a variable (onnx.NodeProto object).

Parameters:
  • lhs – Input lhs node or numpy array to Add operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • rhs – Input rhs node or numpy array to Add operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

Returns:

Onnx NodeProto instance containing the Add operation.

make_sub_node(lhs: OnnxNode | numpy.ndarray, rhs: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX Sub operator node. https://onnx.ai/onnx/operators/onnx__Sub.html#sub-14

At least one of the inputs (lhs, rhs) must be a variable (onnx.NodeProto object).

Parameters:
  • lhs – Input lhs node or numpy array to Sub operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • rhs – Input rhs node or numpy array to Sub operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

Returns:

Onnx NodeProto instance containing the Sub operation.

make_mul_node(lhs: OnnxNode | numpy.ndarray, rhs: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX Mul operator node. https://onnx.ai/onnx/operators/onnx__Mul.html#mul-14

At least one of the inputs (lhs, rhs) must be a variable (onnx.NodeProto object).

Parameters:
  • lhs – Input lhs node or numpy array to Mul operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • rhs – Input rhs node or numpy array to Mul operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

Returns:

Onnx NodeProto instance containing the Mul operation.

make_div_node(lhs: OnnxNode | numpy.ndarray, rhs: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX Div operator node. https://onnx.ai/onnx/operators/onnx__Div.html#div-14

At least one of the inputs (lhs, rhs) must be a variable (onnx.NodeProto object).

Parameters:
  • lhs – Input lhs node or numpy array to Div operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • rhs – Input rhs node or numpy array to Div operator node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

Returns:

Onnx NodeProto instance containing the Div operation.

make_elu_node(input_node: OnnxNode, alpha: float = 1.0) OnnxNode

Create an ONNX Elu operator node. https://onnx.ai/onnx/operators/onnx__Elu.html

Parameters:
  • input_node – Input node of Elu operator.

  • alpha – Coefficient of ELU.

Returns:

Onnx NodeProto instance containing the Elu operation.

make_hardsigmoid_node(input_node: OnnxNode, alpha: float = 0.2, beta: float = 0.5) OnnxNode

Create an ONNX HardSigmoid operator node. https://onnx.ai/onnx/operators/onnx__HardSigmoid.html

Parameters:
  • input_node – Input node of HardSigmoid operator.

  • alpha – Value of alpha inside hardsigmoid equation.

  • beta – Value of beta inside hardsigmoid equation.

Returns:

Onnx NodeProto instance containing the HardSigmoid operation.

make_hardswish_node(input_node: OnnxNode) OnnxNode

Create an ONNX HardSwish operator node. https://onnx.ai/onnx/operators/onnx__HardSwish.html

Parameters:

input_node – Input node of hardswish operator.

Returns:

Onnx NodeProto instance containing the HardSwish operation.

make_leakyrelu_node(input_node: OnnxNode, alpha: float = 0.01) OnnxNode

Create an ONNX LeakyRelu operator node. https://onnx.ai/onnx/operators/onnx__LeakyRelu.html

Parameters:
  • input_node – Input node of leakyrelu operator.

  • alpha – Defines the coefficient of leakage.

Returns:

Onnx NodeProto instance containing the LeakyRelu operation.

make_relu_node(input_node: OnnxNode) OnnxNode

Create an ONNX Relu operator node. https://onnx.ai/onnx/operators/onnx__Relu.html

Parameters:

input_node – Input node of relu operator.

Returns:

Onnx NodeProto instance containing the Relu operation.

make_prelu_node(input_node: OnnxNode, slope: OnnxNode | numpy.ndarray) OnnxNode

Create an ONNX PRelu operator node. https://onnx.ai/onnx/operators/onnx__PRelu.html

Slope parameter must be unidirectionally broadcastable to input_node.

Parameters:
  • input_node – Input node of prelu operator.

  • slope – Input slope node or numpy array to PRelu operator node. In the case of a numpy

  • array

  • list. (the onnx.TensorProto object would be created and added to initializer)

Returns:

Onnx NodeProto instance containing the PRelu operation.

make_sigmoid_node(input_node: OnnxNode) OnnxNode

Create an ONNX Sigmoid operator node. https://onnx.ai/onnx/operators/onnx__Sigmoid.html

Parameters:

input_node – Input node of sigmoid operator.

Returns:

Onnx NodeProto instance containing the Sigmoid operation.

make_softmax_node(input_node: OnnxNode, axis: int = -1) OnnxNode

Create an ONNX Softmax operator node. https://onnx.ai/onnx/operators/onnx__Softmax.html

Parameters:
  • input_node – Input node of softmax operator.

  • axis – Defines the dimension operator will be applied on.

Returns:

Onnx NodeProto instance containing the Softmax operation.

make_instance_normalization_node(input_node: OnnxNode, scale: OnnxNode | numpy.ndarray, bias: OnnxNode | numpy.ndarray, epsilon: float = 1e-05) OnnxNode

Create an ONNX InstanceNormalization operator node. https://onnx.ai/onnx/operators/onnx__InstanceNormalization.html

Parameters:
  • input_node – Input node of InstanceNormalization operator.

  • scale – The 1D input scale tensor of the same size as the channel dim of the input_node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • bias – The 1D input bias tensor of the same size as the channel dim of the input_node. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • epsilon – The epsilon value to use to avoid division by zero.

Returns:

Onnx NodeProto instance containing the InstanceNormalization operation.

make_layer_normalization_node(input_node: OnnxNode, scale: OnnxNode | numpy.ndarray, bias: OnnxNode | numpy.ndarray | None = None, axis: int = -1, epsilon: float = 1e-05, stash_type: int = 1) OnnxNode

Create an ONNX LayerNormalization operator node. https://onnx.ai/onnx/operators/onnx__LayerNormalization.html

Scale and bias (if present) parameters must be unidirectionally broadcastable to input_node.

Parameters:
  • input_node – Input node of LayerNormalization operator.

  • scale – Input scale tensor. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list.

  • bias – Optional input bias tensor. In the case of a numpy array, the onnx.TensorProto object would be created and added to initializer list. If None, bias would not be used in the layer_norm equation.

  • axis – The first normalization dimension. If rank(input_node) is r, axis’ allowed range is [-r, r). Negative value means counting dimensions from the back.

  • epsilon – The epsilon value to use to avoid division by zero.

  • stash_type – Type of Mean and InvStdDev. This also specifies stage one’s computation precision.

Returns:

Onnx NodeProto instance containing the LayerNormalization operation.

make_squeeze_node(input_node: OnnxNode, axes: collections.abc.Sequence[int] | None = None) OnnxNode

Create an ONNX Squeeze operator node. https://onnx.ai/onnx/operators/onnx__Squeeze.html

Parameters:
  • input_node – Input node of Squeeze operator.

  • axes – List of integers indicating the dimensions to squeeze. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input_node).

Returns:

Onnx NodeProto instance containing the Squeeze operation.

make_transpose_node(input_node: OnnxNode, perm: collections.abc.Sequence[int] | None = None) OnnxNode

Create an ONNX Transpose operator node. https://onnx.ai/onnx/operators/onnx__Transpose.html

Parameters:
  • input_node – Input node of Transpose operator.

  • perm – A list of integers. By default, reverse the dimensions, otherwise permute the axes

  • given. (according to the values)

Returns:

Onnx NodeProto instance containing the Transpose operation.

make_unsqueeze_node(input_node: OnnxNode, axes: collections.abc.Sequence[int]) OnnxNode

Create an ONNX Unsqueeze operator node. https://onnx.ai/onnx/operators/onnx__Unsqueeze.html

Parameters:
  • input_node – Input node of Unsqueeze operator.

  • axes – List of integers indicating the dimensions to be inserted. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(expanded_node).

Returns:

Onnx NodeProto instance containing the Unsqueeze operation.