Skip to content

Commit

Permalink
Add get_valid_counts relay test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang committed Jan 13, 2019
1 parent ece6b8d commit 81bb789
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion nnvm/tests/python/compiler/test_top_level4.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def test_nms():
[1, 0.5, 100, 60, 70, 110]]]).astype("float32")
np_valid_count = np.array([4]).astype("int32")
np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
[0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79],
[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]]])

target = "llvm"
Expand Down
4 changes: 2 additions & 2 deletions nnvm/tests/python/frontend/mxnet/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,5 @@ def test_forward_l2_normalize():
test_forward_argmax()
test_forward_argmin()
test_forward_box_nms()
#test_forward_slice_axis()
#test_forward_l2_normalize()
test_forward_slice_axis()
test_forward_l2_normalize()
56 changes: 47 additions & 9 deletions tests/python/relay/test_op_level5.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,47 @@ def verify_multibox_prior(x, dshape, ref_res, sizes=(1.0,),
verify_multibox_prior(x, dshape, ref_res, clip=False, check_type_only=True)


def test_get_valid_counts():
def verify_get_valid_counts(dshape, score_threshold):
dtype = "float32"
batch_size, num_anchor, elem_length = dshape
np_data = np.random.uniform(size=dshape).astype(dtype)
np_out1 = np.zeros(shape=(batch_size,))
np_out2 = np.zeros(shape=dshape).astype(dtype)
for i in range(batch_size):
np_out1[i] = 0
inter_idx = 0
for j in range(num_anchor):
score = np_data[i, j, 1]
if score >= score_threshold:
for k in range(elem_length):
np_out2[i, inter_idx, k] = np_data[i, j, k]
np_out1[i] += 1
inter_idx += 1
if j >= np_out1[i]:
for k in range(elem_length):
np_out2[i, j, k] = -1

x = relay.var("x", relay.ty.TensorType(dshape, dtype))
z = relay.vision.get_valid_counts(x, score_threshold)
assert "score_threshold" in z.astext()
func = relay.Function([x], z.astuple())
func = relay.ir_pass.infer_type(func)
ctx_list = [("llvm", tvm.cpu(0))]
for target, ctx in ctx_list:
intrp = relay.create_executor("debug", ctx=ctx, target=target)
out = intrp.evaluate(func)(np_data)
tvm.testing.assert_allclose(out[0].asnumpy(), np_out1, rtol=1e-3)
tvm.testing.assert_allclose(out[1].asnumpy(), np_out2, rtol=1e-3)

verify_get_valid_counts((1, 2500, 6), 0)
verify_get_valid_counts((1, 2500, 6), -1)
verify_get_valid_counts((3, 1000, 6), 0.55)
verify_get_valid_counts((16, 500, 6), 0.95)


def test_nms():
def verify_nms(x0_data, x1_data, dshape, ref_res, valid_count,
def verify_nms(x0_data, x1_data, dshape, ref_res,
overlap_threshold=0.5, force_suppress=False, topk=-1,
check_type_only=False):
x0 = relay.var("x0", relay.ty.TensorType(dshape, "float32"))
Expand Down Expand Up @@ -166,26 +205,24 @@ def verify_nms(x0_data, x1_data, dshape, ref_res, valid_count,
[1, 0.5, 100, 60, 70, 110]]]).astype("float32")
np_valid_count = np.array([4]).astype("int32")
np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
[0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79],
[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]]])
num_anchors = 5

dshape = (tvm.var("n"), num_anchors, 6)
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
verify_nms(np_data, np_valid_count, dshape, np_result,
force_suppress=True, topk=2, check_type_only=True)
dshape = (1, num_anchors, 6)
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
verify_nms(np_data, np_valid_count, dshape, np_result,
force_suppress=True, topk=2, check_type_only=False)

np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
[1, 0.7, 30, 60, 50, 80], [-1, 0.9, 35, 61, 52, 79],
[1, 0.7, 30, 60, 50, 80], [-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]]])
dshape = (tvm.var("n"), num_anchors, 6)
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
check_type_only=True)
verify_nms(np_data, np_valid_count, dshape, np_result, check_type_only=True)
dshape = (1, num_anchors, 6)
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
topk=3)
verify_nms(np_data, np_valid_count, dshape, np_result, topk=3)


def test_multibox_transform_loc():
Expand Down Expand Up @@ -278,4 +315,5 @@ def test_threshold():
test_resize()
test_multibox_prior()
test_multibox_transform_loc()
test_get_valid_counts()
test_nms()
52 changes: 7 additions & 45 deletions topi/python/topi/vision/ssd/multibox.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def hybrid_multibox_prior(data, sizes, ratios, steps, offsets):
offset_h = offsets[0]
offset_w = offsets[1]

# Need to define var out of const_range + if
w = 0.0
h = 0.0

for i in parallel(in_height):
center_h = (i + offset_h) * steps_h
for j in range(in_width):
Expand All @@ -57,8 +61,8 @@ def hybrid_multibox_prior(data, sizes, ratios, steps, offsets):
h = sizes[k] / 2.0
else:
w = sizes[0] * in_height / in_width \
* sqrt(ratios[k - num_sizes + 1]) / 2.0
h = sizes[0] * sqrt(ratios[k - num_sizes + 1]) / 2.0
* sqrt(ratios[k - num_sizes + 1] * 1.0) / 2.0
h = sizes[0] / sqrt(ratios[k - num_sizes + 1] * 1.0) / 2.0
count = i * in_width * (num_sizes + num_ratios - 1) \
+ j * (num_sizes + num_ratios - 1) + k
output[0, count, 0] = center_w - w
Expand Down Expand Up @@ -104,6 +108,7 @@ def multibox_prior(data, sizes=(1,), ratios=(1,), steps=(-1, -1), offsets=(0.5,
out = topi.clip(out, 0, 1)
return out


@hybrid.script
def _hybridy_transform_loc(box, pred_loc, variance, clip):
"""Transform prior anchor box to output box through location predictions.
Expand Down Expand Up @@ -166,37 +171,8 @@ def hybrid_multibox_transform_loc(cls_prob, loc_pred, anchor,
Returns
-------
<<<<<<< HEAD
stmt : Stmt
The result IR statement.
"""
def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw, vh):
"""Transform prior anchor box to output box through location predictions.
"""
al = anchor[anchor_base_idx]
at = anchor[anchor_base_idx + 1]
ar = anchor[anchor_base_idx + 2]
ab = anchor[anchor_base_idx + 3]
aw = ar - al
ah = ab - at
ax = (al + ar) / 2.0
ay = (at + ab) / 2.0
px = loc[loc_base_idx]
py = loc[loc_base_idx + 1]
pw = loc[loc_base_idx + 2]
ph = loc[loc_base_idx + 3]
ox = px * vx * aw + ax
oy = py * vy * ah + ay
ow = tvm.exp(pw * vw) * aw / 2.0
oh = tvm.exp(ph * vh) * ah / 2.0
return tvm.if_then_else(clip, tvm.max(0, tvm.min(1, ox - ow)), ox - ow), \
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, oy - oh)), oy - oh), \
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, ox + ow)), ox + ow), \
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, oy + oh)), oy + oh)
=======
out_loc : tvm.Tensor or numpy NDArray
3-D tensor of transformed location.
>>>>>>> Modify SSD tutorial
valid_count : tvm.Tensor or numpy NDArray
1_d tensor of valid counts for boxes.
Expand All @@ -214,19 +190,6 @@ def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw,
valid_count[i] = 0
for j in range(num_anchors):
# Find the predicted class id and probability
<<<<<<< HEAD
score = ib.allocate('float32', (1,), name="score", scope="local")
cls_id = ib.allocate('int32', (1,), name="id", scope="local")
score[0] = -1.0
cls_id[0] = 0
with ib.for_range(0, num_classes, name="j") as j:
with ib.if_scope(j > 0):
temp = p_cls_prob[n * num_anchors * num_classes + j * num_anchors + i]
cls_id[0] = tvm.if_then_else(temp > score[0], j, cls_id[0])
score[0] = tvm.max(temp, score[0])
with ib.if_scope(tvm.all(cls_id[0] > 0, score[0] < threshold)):
cls_id[0] = 0
=======
score = -1.0
cls_id = 0
for k in range(num_classes):
Expand All @@ -236,7 +199,6 @@ def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw,
score = max(temp, score)
if cls_id > 0 and score < threshold:
cls_id = 0
>>>>>>> Modify SSD tutorial
# [id, prob, xmin, ymin, xmax, ymax]
# Remove background, restore original id
if cls_id > 0:
Expand Down

0 comments on commit 81bb789

Please sign in to comment.