Skip to content

cleanup prototype transforms functional test #5668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions test/test_prototype_transforms_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,15 @@ def _compute_expected_bbox(bbox, angle_, translate_, scale_, shear_, center_):
np.max(transformed_points[:, 1]),
]
out_bbox = features.BoundingBox(
out_bbox, format=features.BoundingBoxFormat.XYXY, image_size=bbox.image_size, dtype=torch.float32
out_bbox,
format=features.BoundingBoxFormat.XYXY,
image_size=bbox.image_size,
dtype=torch.float32,
device=bbox.device,
)
out_bbox = convert_bounding_box_format(
return convert_bounding_box_format(
out_bbox, old_format=features.BoundingBoxFormat.XYXY, new_format=bbox.format, copy=False
)
return out_bbox.to(bbox.device)

image_size = (32, 38)

Expand Down Expand Up @@ -439,8 +442,8 @@ def test_correctness_affine_bounding_box_on_fixed_input(device):
[1, 1, 5, 5],
]
in_boxes = features.BoundingBox(
in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=image_size, dtype=torch.float64
).to(device)
in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=image_size, dtype=torch.float64, device=device
)
# Tested parameters
angle = 63
scale = 0.89
Expand Down Expand Up @@ -473,9 +476,7 @@ def test_correctness_affine_bounding_box_on_fixed_input(device):
shear=(0, 0),
)

assert len(output_boxes) == len(expected_bboxes)
for a_out_box, out_box in zip(expected_bboxes, output_boxes.cpu()):
np.testing.assert_allclose(out_box.cpu().numpy(), a_out_box)
torch.testing.assert_close(output_boxes.tolist(), expected_bboxes)


@pytest.mark.parametrize("angle", [-54, 56])
Expand Down Expand Up @@ -589,12 +590,15 @@ def _compute_expected_bbox(bbox, angle_, expand_, center_):
out_bbox[3] -= tr_y

out_bbox = features.BoundingBox(
out_bbox, format=features.BoundingBoxFormat.XYXY, image_size=image_size, dtype=torch.float32
out_bbox,
format=features.BoundingBoxFormat.XYXY,
image_size=image_size,
dtype=torch.float32,
device=bbox.device,
)
out_bbox = convert_bounding_box_format(
return convert_bounding_box_format(
out_bbox, old_format=features.BoundingBoxFormat.XYXY, new_format=bbox.format, copy=False
)
return out_bbox.to(bbox.device)

image_size = (32, 38)

Expand Down Expand Up @@ -630,9 +634,6 @@ def _compute_expected_bbox(bbox, angle_, expand_, center_):
expected_bboxes = torch.stack(expected_bboxes)
else:
expected_bboxes = expected_bboxes[0]
print("input:", bboxes)
print("output_bboxes:", output_bboxes)
print("expected_bboxes:", expected_bboxes)
torch.testing.assert_close(output_bboxes, expected_bboxes)


Expand All @@ -649,8 +650,8 @@ def test_correctness_rotate_bounding_box_on_fixed_input(device, expand):
[image_size[1] // 2 - 10, image_size[0] // 2 - 10, image_size[1] // 2 + 10, image_size[0] // 2 + 10],
]
in_boxes = features.BoundingBox(
in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=image_size, dtype=torch.float64
).to(device)
in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=image_size, dtype=torch.float64, device=device
)
# Tested parameters
angle = 45
center = None if expand else [12, 23]
Expand Down Expand Up @@ -687,6 +688,4 @@ def test_correctness_rotate_bounding_box_on_fixed_input(device, expand):
center=center,
)

assert len(output_boxes) == len(expected_bboxes)
for a_out_box, out_box in zip(expected_bboxes, output_boxes.cpu()):
np.testing.assert_allclose(out_box.cpu().numpy(), a_out_box)
torch.testing.assert_close(output_boxes.tolist(), expected_bboxes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_close refuses to compare a list to a tensor?
IIRC numpy testing utils allow to compare array-likes with array-likes right?

Copy link
Collaborator Author

@pmeier pmeier Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and yes. numpy testing functions coerce everything into an np.ndarray first and than compare. We consciously opted against that for mainly two reasons:

  1. If you have tests parametrized over operators like PyTorch core does, it is impossible to see if the returned type is correct. For example

    >>> expected = 1.0
    >>> np.testing.assert_allclose(np.array(expected), expected)
    >>> torch.testing.assert_close(torch.tensor(expected), expected)
    TypeError: No comparison pair was able to handle inputs of type <class 'torch.Tensor'> and <class 'float'>.
  2. It allows us to compare sequences of tensors with different shapes elementwise

    >>> shapes = [(2, 3), (2, 4)]
    >>> np.testing.assert_allclose(*([np.ones(shape) for shape in shapes],) * 2)
    ValueError: could not broadcast input array from shape (2,3) into shape (2,)
    >>> torch.testing.assert_close(*([torch.ones(shape) for shape in shapes],) * 2)

    This aligns nicely with the ability of torch.testing.assert_close to compare mappings elementwise, which is not possible with the numpy functions.