question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add a node to the end of a graph

See original GitHub issue

I am using PyTorch to export a model, but the exporter does not export NMS. So one solution is for me to export before NMS and then add NMS node = onnx.helper.make_node('NonMaxSuppression') manually after loading the exported model.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
avasbrcommented, Aug 3, 2020

Got it. Thanks again for all the help @dashesy

As an aside, I dug a bit more into the onnx docs and found this to be quite useful: https://github.com/onnx/onnx/blob/master/docs/IR.md

It doesn’t cover specific examples of how to do things, but it clarified a number of concepts for me. In the end, I was able to attach a NonMaxSuppression op with score_threshold, iou_threshold, and max_output_boxes_per_class as constant values, and pass the onnx checker as well. For whoever it might help in the future, here’s the full code that worked for me, when going from an onnx model that currently outputs decoded bboxes and scores:

onnx version: 1.7.0 pytorch version: 1.4.0

onnx_fpath = [Path to .onnx model]
onnx_model = onnx.load(onnx_fpath)

# make constant tensors
score_threshold = onnx.helper.make_tensor("score_threshold", TensorProto.FLOAT, [1], [0.9])
iou_threshold = onnx.helper.make_tensor("iou_threshold", TensorProto.FLOAT, [1], [0.3])
max_output_boxes_per_class = onnx.helper.make_tensor("max_output_boxes_per_class", TensorProto.INT64, [1], [200])

# create the NMS node
inputs=['bboxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold',]
outputs = ["selected_indices"]
nms_node = onnx.helper.make_node(
    'NonMaxSuppression',
    inputs,
    ["selected_indices"],
)

# add to the list of graph nodes
graph.node.append(nms_node)

# append to the output (now the outputs would be scores, bboxes, selected_indices)
output_value_info = onnx.helper.make_tensor_value_info("selected_indices", TensorProto.INT64, shape=[])
graph.output.append(output_value_info)

# add to initializers - without this, onnx will not know where these came from, and complain that 
# they're neither outputs of other nodes, nor inputs. As initializers, however, they are treated 
# as constants needed for the NMS op
graph.initializer.append(score_threshold)
graph.initializer.append(iou_threshold)
graph.initializer.append(max_output_boxes_per_class)

# check that it works and re-save
onnx.checker.check_model(onnx_model)
onnx.save(onnx_model, onnx_fpath)
4reactions
fumihwhcommented, Aug 9, 2019

First way: If you want to add a node to the end of a graph, use onnx.helper to make a node and append to model.graph.node is right way. Don’t forget to modify graph.output also. Second way: modify your code, add support to export nms to exporter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

inserting a node at the end of a linked list in c - Log2Base2
1. Declare head pointer and make it as NULL. · 2. Create a new node with the given data. And make the new...
Read more >
Adding and deleting nodes in a graph | Gephi Cookbook
Go to the Data Laboratory mode in Gephi. · Click on the Add Node button in the top panel. This opens the Add...
Read more >
How to add new nodes to an existing graph with fixed ...
Click -> select a node · Click again -> unselect · Click no other node -> create new node between · New node...
Read more >
Insertion and deletion of nodes and edges in a graph using ...
press 1 to insert a node 2.press 2 to delete a node 3.press 3 to insert an edge 4.press 4 to delete an...
Read more >
Modify Nodes and Edges of Existing Graph - MathWorks
Create a graph with four nodes and four edges. The corresponding elements in s and t specify the end nodes of each graph...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found