|
1 | 1 | """ Support level5 operator test cases.
|
2 | 2 | """
|
| 3 | +import numpy as np |
3 | 4 | import tvm
|
4 | 5 | from tvm import relay
|
| 6 | +from tvm.relay.testing import ctx_list |
| 7 | +import topi.testing |
5 | 8 |
|
6 | 9 | def test_resize_infer_type():
|
7 | 10 | n, c, h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w")
|
@@ -70,22 +73,45 @@ def test_nms():
|
70 | 73 | zz = relay.ir_pass.infer_type(z)
|
71 | 74 | assert zz.checked_type == relay.ty.TensorType(
|
72 | 75 | (n, num_anchors, 6), "float32")
|
73 |
| -def test_yolo_reorg(): |
| 76 | + |
| 77 | + |
| 78 | +def test_yolo_reorg_infer_shape(): |
| 79 | + def verify_yolo_reorg(shape, stride, out_shape): |
| 80 | + x = relay.var("x", relay.TensorType(shape, "float32")) |
| 81 | + z = relay.vision.yolo_reorg(x, stride=stride) |
| 82 | + zz = relay.ir_pass.infer_type(z) |
| 83 | + assert "stride=" in z.astext() |
| 84 | + assert zz.checked_type == relay.ty.TensorType(out_shape, "float32") |
| 85 | + |
74 | 86 | n, c, h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w")
|
75 |
| - x = relay.var("x", relay.TensorType((n, c, 20, 20), "float32")) |
76 |
| - z = relay.vision.yolo_reorg(x, stride=10) |
77 |
| - zz = relay.ir_pass.infer_type(z) |
78 |
| - assert "stride=10" in z.astext() |
79 |
| - assert zz.checked_type == relay.ty.TensorType((n, c*10*10, 2, 2), "float32") |
| 87 | + verify_yolo_reorg((n, c, 20, 20), 10, (n, c*10*10, 2, 2)) |
| 88 | + verify_yolo_reorg((n, c, h, w), 2, (n, c*2*2, h/2, w/2)) |
80 | 89 |
|
81 |
| - x = relay.var("x", relay.TensorType((n, c, h, w), "float32")) |
82 |
| - z = relay.vision.yolo_reorg(x, stride=2) |
83 |
| - assert "stride=2" in z.astext() |
84 |
| - zz = relay.ir_pass.infer_type(z) |
85 |
| - assert zz.checked_type == relay.ty.TensorType((n, c*2*2, h/2, w/2), "float32") |
| 90 | +def test_yolo_reorg(): |
| 91 | + def verify_yolo_reorg(shape, stride): |
| 92 | + x_data = np.random.uniform(low=-1, high=1, size=shape).astype("float32") |
| 93 | + ref_res = topi.testing.reorg_python(x_data, stride) |
| 94 | + |
| 95 | + x = relay.var("x", relay.TensorType(shape, "float32")) |
| 96 | + z = relay.vision.yolo_reorg(x, stride=stride) |
| 97 | + zz = relay.ir_pass.infer_type(z) |
| 98 | + assert "stride=" in z.astext() |
| 99 | + assert zz.checked_type == relay.ty.TensorType(ref_res.shape, "float32") |
| 100 | + |
| 101 | + func = relay.Function([x], z) |
| 102 | + |
| 103 | + for target, ctx in ctx_list(): |
| 104 | + for kind in ["graph", "debug"]: |
| 105 | + intrp = relay.create_executor(kind, ctx=ctx, target=target) |
| 106 | + op_res = intrp.evaluate(func)(x_data) |
| 107 | + tvm.testing.assert_allclose(op_res.asnumpy(), ref_res, rtol=1e-5) |
| 108 | + |
| 109 | + verify_yolo_reorg((1, 100, 20, 20), 10) |
| 110 | + verify_yolo_reorg((1, 4, 6, 6), 2) |
86 | 111 |
|
87 | 112 | if __name__ == "__main__":
|
88 | 113 | test_resize_infer_type()
|
89 | 114 | test_multibox_prior()
|
90 | 115 | test_nms()
|
| 116 | + test_yolo_reorg_infer_shape() |
91 | 117 | test_yolo_reorg()
|
0 commit comments