Skip to content

Instantly share code, notes, and snippets.

@antoinebrl
Last active May 18, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoinebrl/e0be20545d2d1c8e5020fffe25f65d20 to your computer and use it in GitHub Desktop.
Save antoinebrl/e0be20545d2d1c8e5020fffe25f65d20 to your computer and use it in GitHub Desktop.
ONNX Surgery example - Replacing `GlobalAveragePool` with `AveragePool`
import onnx
model = onnx.load("model.onnx")
graph = model.graph
# Identify relevant node
idx = -1
for i, n in enumerate(model.graph.node):
if n.name == "GlobalAveragePool":
idx = i
node = model.graph.node[idx]
print(node)
new_node = onnx.helper.make_node("AveragePool", node.input, node.output, kernel_shape=[12,4], pads=[0,0,0,0], strides=[12,4])
graph.node.remove(node)
graph.node.insert(idx, new_node)
onnx.checker.check_model(model)
onnx.save(model, "model-surgery.onnx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment