Skip to content

Instantly share code, notes, and snippets.

@mayjs
Created August 5, 2023 18:24
Show Gist options
  • Save mayjs/bfb327f90b9f4b140bf32b337d360d0a to your computer and use it in GitHub Desktop.
Save mayjs/bfb327f90b9f4b140bf32b337d360d0a to your computer and use it in GitHub Desktop.
Intermediate result inspection in ONNX models
from pathlib import Path
import onnx
model_path = Path("my_model.onnx")
extra_output = "/up1/Concat_3_output_0" # One good way to get this identifier for your node is to use https://netron.app/
extra_output_shape = [1, 1024, 55, 55]
new_model_path = model_path.with_stem(model_path.stem + "-" + extra_output.replace("/", "_").replace(".", "_").strip("_"))
model = onnx.load(model_path)
# Create a new output
intermediate_layer_value_info = onnx.helper.make_tensor_value_info("debug_out",
onnx.TensorProto.FLOAT,
extra_output_shape)
# Create an identity node to write the intermediate result to the new output
output_identity_node = onnx.helper.make_node("Identity", [extra_output], ["debug_out"])
# Add the output and the new node
model.graph.output.append(intermediate_layer_value_info)
model.graph.node.append(output_identity_node)
# Save the new model
onnx.save(model, new_model_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment