Skip to content

Commit

Permalink
skip addition of mean and scale nodes to onnx model if mean=0 and sca…
Browse files Browse the repository at this point in the history
…le=1
  • Loading branch information
ptoupas committed Nov 8, 2024
1 parent 93c3e94 commit 4033408
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions modelconverter/utils/onnx_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ def onnx_attach_normalization_to_inputs(
new_nodes.append(concat_node)
last_output = f"normalized_{input_name}"

# 2. Subtract (mean) if mean_values is not None
if not reverse_only and cfg.mean_values is not None:
# 2. Subtract (mean) if mean_values is not None and not all 0
if (
not reverse_only
and cfg.mean_values is not None
and any(v != 0 for v in cfg.mean_values)
):
sub_out = f"sub_out_{input_name}"
sub_node = helper.make_node(
"Sub",
Expand All @@ -110,8 +114,12 @@ def onnx_attach_normalization_to_inputs(
)
new_initializers.append(mean_tensor)

# 3. Divide (scale) if scale_values is not None
if not reverse_only and cfg.scale_values is not None:
# 3. Divide (scale) if scale_values is not None and not all 1
if (
not reverse_only
and cfg.scale_values is not None
and any(v != 1 for v in cfg.scale_values)
):
div_out = f"div_out_{input_name}"
div_node = helper.make_node(
"Mul",
Expand Down

0 comments on commit 4033408

Please sign in to comment.