Skip to content

Commit 81bb789

Browse files
author
Wang
committed
Add get_valid_counts relay test
1 parent ece6b8d commit 81bb789

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

nnvm/tests/python/compiler/test_top_level4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def test_nms():
629629
[1, 0.5, 100, 60, 70, 110]]]).astype("float32")
630630
np_valid_count = np.array([4]).astype("int32")
631631
np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
632-
[0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79],
632+
[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1],
633633
[-1, -1, -1, -1, -1, -1]]])
634634

635635
target = "llvm"

nnvm/tests/python/frontend/mxnet/test_forward.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,5 @@ def test_forward_l2_normalize():
222222
test_forward_argmax()
223223
test_forward_argmin()
224224
test_forward_box_nms()
225-
#test_forward_slice_axis()
226-
#test_forward_l2_normalize()
225+
test_forward_slice_axis()
226+
test_forward_l2_normalize()

tests/python/relay/test_op_level5.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,47 @@ def verify_multibox_prior(x, dshape, ref_res, sizes=(1.0,),
136136
verify_multibox_prior(x, dshape, ref_res, clip=False, check_type_only=True)
137137

138138

139+
def test_get_valid_counts():
140+
def verify_get_valid_counts(dshape, score_threshold):
141+
dtype = "float32"
142+
batch_size, num_anchor, elem_length = dshape
143+
np_data = np.random.uniform(size=dshape).astype(dtype)
144+
np_out1 = np.zeros(shape=(batch_size,))
145+
np_out2 = np.zeros(shape=dshape).astype(dtype)
146+
for i in range(batch_size):
147+
np_out1[i] = 0
148+
inter_idx = 0
149+
for j in range(num_anchor):
150+
score = np_data[i, j, 1]
151+
if score >= score_threshold:
152+
for k in range(elem_length):
153+
np_out2[i, inter_idx, k] = np_data[i, j, k]
154+
np_out1[i] += 1
155+
inter_idx += 1
156+
if j >= np_out1[i]:
157+
for k in range(elem_length):
158+
np_out2[i, j, k] = -1
159+
160+
x = relay.var("x", relay.ty.TensorType(dshape, dtype))
161+
z = relay.vision.get_valid_counts(x, score_threshold)
162+
assert "score_threshold" in z.astext()
163+
func = relay.Function([x], z.astuple())
164+
func = relay.ir_pass.infer_type(func)
165+
ctx_list = [("llvm", tvm.cpu(0))]
166+
for target, ctx in ctx_list:
167+
intrp = relay.create_executor("debug", ctx=ctx, target=target)
168+
out = intrp.evaluate(func)(np_data)
169+
tvm.testing.assert_allclose(out[0].asnumpy(), np_out1, rtol=1e-3)
170+
tvm.testing.assert_allclose(out[1].asnumpy(), np_out2, rtol=1e-3)
171+
172+
verify_get_valid_counts((1, 2500, 6), 0)
173+
verify_get_valid_counts((1, 2500, 6), -1)
174+
verify_get_valid_counts((3, 1000, 6), 0.55)
175+
verify_get_valid_counts((16, 500, 6), 0.95)
176+
177+
139178
def test_nms():
140-
def verify_nms(x0_data, x1_data, dshape, ref_res, valid_count,
179+
def verify_nms(x0_data, x1_data, dshape, ref_res,
141180
overlap_threshold=0.5, force_suppress=False, topk=-1,
142181
check_type_only=False):
143182
x0 = relay.var("x0", relay.ty.TensorType(dshape, "float32"))
@@ -166,26 +205,24 @@ def verify_nms(x0_data, x1_data, dshape, ref_res, valid_count,
166205
[1, 0.5, 100, 60, 70, 110]]]).astype("float32")
167206
np_valid_count = np.array([4]).astype("int32")
168207
np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
169-
[0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79],
208+
[-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1],
170209
[-1, -1, -1, -1, -1, -1]]])
171210
num_anchors = 5
172211

173212
dshape = (tvm.var("n"), num_anchors, 6)
174-
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
213+
verify_nms(np_data, np_valid_count, dshape, np_result,
175214
force_suppress=True, topk=2, check_type_only=True)
176215
dshape = (1, num_anchors, 6)
177-
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
216+
verify_nms(np_data, np_valid_count, dshape, np_result,
178217
force_suppress=True, topk=2, check_type_only=False)
179218

180219
np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45],
181-
[1, 0.7, 30, 60, 50, 80], [-1, 0.9, 35, 61, 52, 79],
220+
[1, 0.7, 30, 60, 50, 80], [-1, -1, -1, -1, -1, -1],
182221
[-1, -1, -1, -1, -1, -1]]])
183222
dshape = (tvm.var("n"), num_anchors, 6)
184-
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
185-
check_type_only=True)
223+
verify_nms(np_data, np_valid_count, dshape, np_result, check_type_only=True)
186224
dshape = (1, num_anchors, 6)
187-
verify_nms(np_data, np_valid_count, dshape, np_result, dshape[0],
188-
topk=3)
225+
verify_nms(np_data, np_valid_count, dshape, np_result, topk=3)
189226

190227

191228
def test_multibox_transform_loc():
@@ -278,4 +315,5 @@ def test_threshold():
278315
test_resize()
279316
test_multibox_prior()
280317
test_multibox_transform_loc()
318+
test_get_valid_counts()
281319
test_nms()

topi/python/topi/vision/ssd/multibox.py

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def hybrid_multibox_prior(data, sizes, ratios, steps, offsets):
4747
offset_h = offsets[0]
4848
offset_w = offsets[1]
4949

50+
# Need to define var out of const_range + if
51+
w = 0.0
52+
h = 0.0
53+
5054
for i in parallel(in_height):
5155
center_h = (i + offset_h) * steps_h
5256
for j in range(in_width):
@@ -57,8 +61,8 @@ def hybrid_multibox_prior(data, sizes, ratios, steps, offsets):
5761
h = sizes[k] / 2.0
5862
else:
5963
w = sizes[0] * in_height / in_width \
60-
* sqrt(ratios[k - num_sizes + 1]) / 2.0
61-
h = sizes[0] * sqrt(ratios[k - num_sizes + 1]) / 2.0
64+
* sqrt(ratios[k - num_sizes + 1] * 1.0) / 2.0
65+
h = sizes[0] / sqrt(ratios[k - num_sizes + 1] * 1.0) / 2.0
6266
count = i * in_width * (num_sizes + num_ratios - 1) \
6367
+ j * (num_sizes + num_ratios - 1) + k
6468
output[0, count, 0] = center_w - w
@@ -104,6 +108,7 @@ def multibox_prior(data, sizes=(1,), ratios=(1,), steps=(-1, -1), offsets=(0.5,
104108
out = topi.clip(out, 0, 1)
105109
return out
106110

111+
107112
@hybrid.script
108113
def _hybridy_transform_loc(box, pred_loc, variance, clip):
109114
"""Transform prior anchor box to output box through location predictions.
@@ -166,37 +171,8 @@ def hybrid_multibox_transform_loc(cls_prob, loc_pred, anchor,
166171
167172
Returns
168173
-------
169-
<<<<<<< HEAD
170-
stmt : Stmt
171-
The result IR statement.
172-
"""
173-
def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw, vh):
174-
"""Transform prior anchor box to output box through location predictions.
175-
"""
176-
al = anchor[anchor_base_idx]
177-
at = anchor[anchor_base_idx + 1]
178-
ar = anchor[anchor_base_idx + 2]
179-
ab = anchor[anchor_base_idx + 3]
180-
aw = ar - al
181-
ah = ab - at
182-
ax = (al + ar) / 2.0
183-
ay = (at + ab) / 2.0
184-
px = loc[loc_base_idx]
185-
py = loc[loc_base_idx + 1]
186-
pw = loc[loc_base_idx + 2]
187-
ph = loc[loc_base_idx + 3]
188-
ox = px * vx * aw + ax
189-
oy = py * vy * ah + ay
190-
ow = tvm.exp(pw * vw) * aw / 2.0
191-
oh = tvm.exp(ph * vh) * ah / 2.0
192-
return tvm.if_then_else(clip, tvm.max(0, tvm.min(1, ox - ow)), ox - ow), \
193-
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, oy - oh)), oy - oh), \
194-
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, ox + ow)), ox + ow), \
195-
tvm.if_then_else(clip, tvm.max(0, tvm.min(1, oy + oh)), oy + oh)
196-
=======
197174
out_loc : tvm.Tensor or numpy NDArray
198175
3-D tensor of transformed location.
199-
>>>>>>> Modify SSD tutorial
200176
201177
valid_count : tvm.Tensor or numpy NDArray
202178
1_d tensor of valid counts for boxes.
@@ -214,19 +190,6 @@ def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw,
214190
valid_count[i] = 0
215191
for j in range(num_anchors):
216192
# Find the predicted class id and probability
217-
<<<<<<< HEAD
218-
score = ib.allocate('float32', (1,), name="score", scope="local")
219-
cls_id = ib.allocate('int32', (1,), name="id", scope="local")
220-
score[0] = -1.0
221-
cls_id[0] = 0
222-
with ib.for_range(0, num_classes, name="j") as j:
223-
with ib.if_scope(j > 0):
224-
temp = p_cls_prob[n * num_anchors * num_classes + j * num_anchors + i]
225-
cls_id[0] = tvm.if_then_else(temp > score[0], j, cls_id[0])
226-
score[0] = tvm.max(temp, score[0])
227-
with ib.if_scope(tvm.all(cls_id[0] > 0, score[0] < threshold)):
228-
cls_id[0] = 0
229-
=======
230193
score = -1.0
231194
cls_id = 0
232195
for k in range(num_classes):
@@ -236,7 +199,6 @@ def transform_loc(loc, loc_base_idx, anchor, anchor_base_idx, clip, vx, vy, vw,
236199
score = max(temp, score)
237200
if cls_id > 0 and score < threshold:
238201
cls_id = 0
239-
>>>>>>> Modify SSD tutorial
240202
# [id, prob, xmin, ymin, xmax, ymax]
241203
# Remove background, restore original id
242204
if cls_id > 0:

0 commit comments

Comments
 (0)