From aad6f0e43e2d1b1b9bf28c3688a3976870aff2ff Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Fri, 31 Mar 2023 09:59:13 -0500 Subject: [PATCH] Use randomly-generated boxes for unit tests This mimics the example usage of `tf.image.crop_and_resize`, whose API this operator is intended to follow. Using any boxes with edges representable as integer fractions has the potential for the in-bounds check to be impacted by rounding error (e.g. `0.2*x + 0.8*x < x`). Unfortunately, there's no way to remove this possibility altogether without changing the API, such as accepting the box location as an integer fraction, rather than a `float32`, but that would break compatibility. To avoid the risk of a flaky unit test based on the specific random boxes used, the PRNG is seeded prior to generation. --- tests/python/relay/test_op_level5.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/python/relay/test_op_level5.py b/tests/python/relay/test_op_level5.py index 47f90647500b..db910661ca2c 100644 --- a/tests/python/relay/test_op_level5.py +++ b/tests/python/relay/test_op_level5.py @@ -235,29 +235,24 @@ def test_crop_and_resize(self, target, dev, executor_kind, layout, interpolate_m pytest.xfail("Known failing case for these parameters") extrapolation_value = 0.0 + + np.random.seed(0) + eps = 1e-4 if layout == "NHWC": img_shape = (10, 224, 224, 3) - boxes = np.array( - [ - [0.125 + eps, 0.25 + eps, 0.8125 - eps, 0.71875 - eps], - [0.25 + eps, 0 + eps, 1 - eps, 0.625 - eps], - ] - ).astype("float32") + boxes = np.random.uniform(size=(2, 4)).astype("float32") box_indices = np.array([1, 0]).astype("int32") crop_size = np.array([20, 30]).astype("int32") elif layout == "NCHW": img_shape = (5, 3, 255, 255) - boxes = np.array( - [[0, 0, 1, 1], [0.25 + eps, 0.125 + eps, 1 - eps, 0.9375 - eps]] - ).astype("float32") + boxes = np.random.uniform(size=(2, 4)).astype("float32") box_indices = np.array([0, 1]).astype("int32") crop_size = np.array([30, 30]).astype("int32") else: raise ValueError(f"Unknown layout: {layout}") - np.random.seed(0) image_data = np.random.uniform(size=img_shape).astype("float32") ref_res = tvm.topi.testing.crop_and_resize_python(