-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relay][OP]NMS #1929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Relay][OP]NMS #1929
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
| from __future__ import absolute_import as _abs | ||
|
|
||
| from .multibox import * | ||
| from .nms import * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """Non-maximum suppression operations.""" | ||
| from __future__ import absolute_import as _abs | ||
| from . import _make | ||
|
|
||
| def nms(data, | ||
| valid_count, | ||
| overlap_threshold=0.5, | ||
| force_suppress=False, | ||
| topk=-1): | ||
| """Non-maximum suppression operator for object detection. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : relay.Expr | ||
| 3-D tensor with shape [batch_size, num_anchors, 6]. | ||
| The last dimension should be in format of | ||
| [class_id, score, box_left, box_top, box_right, box_bottom]. | ||
|
|
||
| valid_count : relay.Expr | ||
| 1-D tensor for valid number of boxes. | ||
|
|
||
| overlap_threshold : float, optional | ||
| Non-maximum suppression threshold. | ||
|
|
||
| force_suppress : bool, optional | ||
| Suppress all detections regardless of class_id. | ||
|
|
||
| topk : int, optional | ||
| Keep maximum top k detections before nms, -1 for no limit. | ||
|
|
||
| Returns | ||
| ------- | ||
| out : relay.Expr | ||
| 3-D tensor with shape [batch_size, num_anchors, 6]. | ||
| """ | ||
| return _make.nms(data, valid_count, overlap_threshold, force_suppress, topk) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /*! | ||
| * Copyright (c) 2018 by Contributors | ||
| * \file nms.cc | ||
| * \brief Non-maximum suppression operators | ||
| */ | ||
| #include <tvm/relay/op.h> | ||
| #include <tvm/relay/attrs/vision.h> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| TVM_REGISTER_NODE_TYPE(NMSAttrs); | ||
|
|
||
| bool NMSRel(const Array<Type>& types, | ||
| int num_inputs, | ||
| const Attrs& attrs, | ||
| const TypeReporter& reporter) { | ||
| CHECK_EQ(types.size(), 3); | ||
| const auto* data = types[0].as<TensorTypeNode>(); | ||
| const auto* valid_count = types[1].as<TensorTypeNode>(); | ||
| const auto& dshape = data->shape; | ||
| const auto& vshape = valid_count->shape; | ||
| CHECK_EQ(dshape.size(), 3) << "Input data should be 3-D."; | ||
| CHECK_EQ(vshape.size(), 1) << "Input valid count should be 1-D."; | ||
|
|
||
| // assign output type | ||
| reporter->Assign(types[2], TensorTypeNode::make(dshape, data->dtype)); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| Expr MakeNMS(Expr data, | ||
| Expr valid_count, | ||
| double overlap_threshold, | ||
| bool force_suppress, | ||
| int topk) { | ||
| auto attrs = make_node<NMSAttrs>(); | ||
| attrs->overlap_threshold = overlap_threshold; | ||
| attrs->force_suppress = force_suppress; | ||
| attrs->topk = topk; | ||
| static const Op& op = Op::Get("vision.nms"); | ||
| return CallNode::make(op, {data, valid_count}, Attrs(attrs), {}); | ||
| } | ||
|
|
||
|
|
||
| TVM_REGISTER_API("relay.op.vision._make.nms") | ||
| .set_body([](const TVMArgs& args, TVMRetValue* rv) { | ||
| runtime::detail::unpack_call<Expr, 5>(MakeNMS, args, rv); | ||
| }); | ||
|
|
||
|
|
||
| RELAY_REGISTER_OP("vision.nms") | ||
| .describe(R"doc("Non-maximum suppression." | ||
| )doc" TVM_ADD_FILELINE) | ||
| .set_num_inputs(2) | ||
| .add_argument("data", "Tensor", "Input data.") | ||
| .add_argument("valid_count", "Tensor", "Number of valid anchor boxes.") | ||
| .set_support_level(5) | ||
| .add_type_rel("NMS", NMSRel); | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is valid_count used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The methods multibox_detection and box_nms generating valid_cout are slightly different. This NMS is a general interface to support both cases. I put valid_count as an argument so that we don't need to add extra flag to differentiate multibox_detection and box_nms use cases.