FrozenGene commented on a change in pull request #4297: [TOPI][OP] Support
Faster-RCNN Proposal OP on CPU
URL: https://github.com/apache/incubator-tvm/pull/4297#discussion_r345114018
##########
File path: topi/python/topi/vision/rcnn/proposal.py
##########
@@ -60,6 +61,261 @@ def reg_iou(x1, y1, x2, y2, dx1, dy1, dx2, dy2):
pred_y2 = y2 + dy2
return pred_x1, pred_y1, pred_x2, pred_y2
+def predict_bbox_ir(cls_prob_buf, bbox_pred_buf, im_info_buf, out_buf, scales,
ratios,
+ feature_stride, rpn_min_size, iou_loss):
+ """Predict bounding boxes based on anchors, scores and deltas.
+
+ Parameters
+ ----------
+ cls_prob_buf : tvm.schedule.Buffer
+ 4-D with shape [batch, 2 * num_anchors, height, width]
+
+ bbox_pred_buf : tvm.schedule.Buffer
+ 4-D with shape [batch, 4 * num_anchors, height, width]
+
+ im_info_buf : tvm.schedule.Buffer
+ 2-D with shape [batch, 3]
+
+ out_buf : tvm.schedule.Buffer
+ 3-D with shape [batch, num_bbox, 5]
+ The last dimension is in format of [w_start, h_start, w_end, h_end,
score]
+
+ scales : list/tuple of float
+ Scales of anchor windoes.
+
+ ratios : list/tuple of float
+ Ratios of anchor windoes.
+
+ feature_stride : int
+ The size of the receptive field each unit in the convolution layer of
the rpn, for example
+ the product of all stride's prior to this layer.
+
+ rpn_min_size : int
+ Minimum height or width in proposal.
+
+ iou_loss : bool
+ Usage of IoU loss.
+
+ Returns
+ -------
+ stmt : Stmt
+ The result IR statement.
+ """
+ batch, num_anchors, height, width = get_const_tuple(cls_prob_buf.shape)
+ num_anchors //= 2
+ ib = tvm.ir_builder.create()
+
+ p_score = ib.buffer_ptr(cls_prob_buf)
+ p_delta = ib.buffer_ptr(bbox_pred_buf)
+ p_im_info = ib.buffer_ptr(im_info_buf)
+ p_out = ib.buffer_ptr(out_buf)
+
+ idxm = tvm.indexmod
+ idxd = tvm.indexdiv
+
+ with ib.for_range(0, batch * height * width) as tid:
Review comment:
I tried to play Hybrid Script, but seems that we have some restriction so
that I can not complete Hybrid Script replacement. For example:
```python
@hybrid.script
def outer_product(a, b, x):
first_shape = a.shape[0]
first_shape //= 2
c = output_tensor((3, 3), 'float32')
for i in const_range(first_shape):
c[0, 0] = x[i // 2]
return c
a = tvm.placeholder((3, ), name='a')
b = tvm.placeholder((3, ), name='b')
x = [1, 2, 3]
c = outer_product(a, b, tvm.convert(x))
```
It will report ValueError: Const range should start from a const and iterate
const times
We should change into
```python
first_shape = a.shape[0] // 2
c = output_tensor((3, 3), 'float32')
for i in const_range(first_shape):
c[0, 0] = x[i // 2]
return c
```
This make code strange. Do we have any good ways to solve it?
Another thing is GPU / CPU relay on some common functions like
`generate_anchor` / `reg_bbox` and so on. Like in `generate_anchor`, we use
`math.floor`, hybrid script doesn't support. So if I have to move the
`generate_anchor` implementation into `predict_bbox_ir`, I meet one problem is
`floor`. When I add it `from tvm.intrin import floor`, I meet problem
`TypeError: 'FloatImm' object does not support indexing`, you could try this
simple reproduce case:
```python
@hybrid.script
def outer_product(a, b, x):
first_shape = a.shape[0]
#first_shape = first_shape // 2
c = output_tensor((3, 3), 'float32')
for i in const_range(first_shape):
floor(3.1 / 2.0)
return c
```
So must we use hybrid script? Seems hybrid script has some restriction.
Welcome feedback to above problems, if we could solve, I will try to use hybrid
script to continue.
----------------------------------------------------------------
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