Skip to content

Commit

Permalink
Use randomly-generated boxes for unit tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lunderberg committed Mar 31, 2023
1 parent 4f920e0 commit aad6f0e
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions tests/python/relay/test_op_level5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit aad6f0e

Please sign in to comment.