icemelon9 commented on a change in pull request #4312: [TOPI][Relay][OP] 
Dynamic NMS and strided_slice
URL: https://github.com/apache/incubator-tvm/pull/4312#discussion_r345405497
 
 

 ##########
 File path: topi/python/topi/vision/nms.py
 ##########
 @@ -289,8 +339,136 @@ def hybrid_nms(data, sorted_index, valid_count,
     return output, box_indices
 
 
[email protected]
+def hybrid_dynamic_nms(data, sorted_index, max_output_size, score_threshold,
+                       iou_threshold, score_index, zero, one):
+    """Hybrid routing for non-maximum suppression.
+
+    Parameters
+    ----------
+    data: tvm.Tensor or numpy NDArray
+        Bounding boxes with class and score. 3-D tensor with shape
+        [batch_size, num_anchors, 6] or [batch_size, num_anchors, 5].
+
+    sorted_index : tvm.Tensor or numpy NDArray
+        Bounding box indexes sorted by score, with shape
+        [batch_size, num_anchors].
+
+    max_output_size : tvm.const
+        Max number of output valid boxes for each instance.
+        By default all valid boxes are returned.
+
+    score_threshold : tvm.const
+        Lower limit of score for valid bounding boxes.
+
+    iou_threshold : tvm.const
+        Overlapping(IoU) threshold to suppress object with smaller score.
+
+    score_index: tvm.const
+        Index of the scores/confidence of boxes.
+
+    zero: tvm.const
+        Constant zero with the same dtype as data.
+
+    one: tvm.const
+        Constant one with the same dtype as data.
+
+    Returns
+    -------
+    box_indices: tvm.Tensor
+        2-D tensor with shape [batch_size, num_anchors].
+    """
+
+
+    batch_size = data.shape[0]
+    num_anchors = data.shape[1]
+    box_data_length = data.shape[2]
+
+    # box_indices is the expected value, similar to TF & ONNX
+    box_indices = output_tensor((batch_size, num_anchors), sorted_index.dtype)
+    output = output_tensor((batch_size,
+                            num_anchors,
+                            box_data_length,), data.dtype)
+
+    for i in range(batch_size):
+        if iou_threshold > 0:
+            # Reorder output
+            for j in parallel(num_anchors):
+                for k in range(box_data_length):
+                    output[i, j, k] = data[i, sorted_index[i, j], k]
+                if output[i, j, score_index] > score_threshold:
+                    box_indices[i, j] = sorted_index[i, j]
+                else:
+                    box_indices[i, j] = -1
+
+            # Apply nms
+            box_start_idx = 1
+            batch_idx = i
+
+            for j in range(num_anchors):
+                # index sorted
+                j_sorted = sorted_index[i, j]
+
+                box_a_idx = j
+                # l: left, t: top, r: right, b: bottom
+                a_l = min(output[batch_idx, box_a_idx, box_start_idx],
+                          output[batch_idx, box_a_idx, box_start_idx + 2])
+                a_t = min(output[batch_idx, box_a_idx, box_start_idx + 1],
+                          output[batch_idx, box_a_idx, box_start_idx + 3])
+                a_r = max(output[batch_idx, box_a_idx, box_start_idx],
+                          output[batch_idx, box_a_idx, box_start_idx + 2])
+                a_b = max(output[batch_idx, box_a_idx, box_start_idx + 1],
+                          output[batch_idx, box_a_idx, box_start_idx + 3])
+
+                for k in parallel(j + 1, num_anchors):
+                    k_sorted = sorted_index[i, k]
+                    box_b_idx = k
+                    # l: left, t: top, r: right, b: bottom
+                    b_l = min(output[batch_idx, box_b_idx, box_start_idx],
+                              output[batch_idx, box_b_idx, box_start_idx + 2])
+                    b_t = min(output[batch_idx, box_b_idx, box_start_idx + 1],
+                              output[batch_idx, box_b_idx, box_start_idx + 3])
+                    b_r = max(output[batch_idx, box_b_idx, box_start_idx],
+                              output[batch_idx, box_b_idx, box_start_idx + 2])
+                    b_b = max(output[batch_idx, box_b_idx, box_start_idx + 1],
+                              output[batch_idx, box_b_idx, box_start_idx + 3])
+
+                    # Overlapping width and height
+                    w = max(zero, min(a_r, b_r) - max(a_l, b_l))
+                    h = max(zero, min(a_b, b_b) - max(a_t, b_t))
+
+                    # Overlapping area
+                    area = h * w
+
+                    # total area of the figure formed by box a and box b 
except for overlapping area
+                    u = (a_r - a_l) * (a_b - a_t) + (b_r - b_l) * (b_b - b_t) 
- area
+
+                    # get the iou
+                    iou = area / u
+
+                    # output[i, k, sorted_index] = iou
+
+                    if iou >= score_threshold:
+                        box_indices[i, k] = -1
+
+        else:
+            for j in parallel(num_anchors):
+                box_indices[i, j] = sorted_index[i, j]
+
+        # Only return max_output_size valid boxes
+        num_valid_boxes = 0
+        if max_output_size > 0:
+            for j in parallel(num_anchors):
+                if num_valid_boxes == max_output_size:
+                    box_indices[i, j] = -1
+                else:
+                    num_valid_boxes += 1
+
+    return output, box_indices
+
+
 @tvm.target.generic_func
-def non_max_suppression(data, valid_count, max_output_size=-1,
+def non_max_suppression(data, valid_count, max_output_size=-1, 
score_threshold=0.0,
                         iou_threshold=0.5, force_suppress=False, top_k=-1,
                         coord_start=2, score_index=1, id_index=0,
                         return_indices=True, invalid_to_bottom=False):
 
 Review comment:
   Change the arg name `return_indeces` 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to