Skip to content

Commit a06df0d

Browse files
pmeierNicolasHug
andauthored
add tests for F.crop and transforms.RandomCrop (#7892)
Co-authored-by: Nicolas Hug <[email protected]>
1 parent 58f834a commit a06df0d

File tree

7 files changed

+237
-254
lines changed

7 files changed

+237
-254
lines changed

test/test_transforms_v2.py

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -449,68 +449,6 @@ def test__get_params(self, fill, side_range):
449449
assert 0 <= params["padding"][3] <= (side_range[1] - 1) * h
450450

451451

452-
class TestRandomCrop:
453-
def test_assertions(self):
454-
with pytest.raises(ValueError, match="Please provide only two dimensions"):
455-
transforms.RandomCrop([10, 12, 14])
456-
457-
with pytest.raises(TypeError, match="Got inappropriate padding arg"):
458-
transforms.RandomCrop([10, 12], padding="abc")
459-
460-
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
461-
transforms.RandomCrop([10, 12], padding=[-0.7, 0, 0.7])
462-
463-
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
464-
transforms.RandomCrop([10, 12], padding=1, fill="abc")
465-
466-
with pytest.raises(ValueError, match="Padding mode should be either"):
467-
transforms.RandomCrop([10, 12], padding=1, padding_mode="abc")
468-
469-
@pytest.mark.parametrize("padding", [None, 1, [2, 3], [1, 2, 3, 4]])
470-
@pytest.mark.parametrize("size, pad_if_needed", [((10, 10), False), ((50, 25), True)])
471-
def test__get_params(self, padding, pad_if_needed, size):
472-
h, w = size = (24, 32)
473-
image = make_image(size)
474-
475-
transform = transforms.RandomCrop(size, padding=padding, pad_if_needed=pad_if_needed)
476-
params = transform._get_params([image])
477-
478-
if padding is not None:
479-
if isinstance(padding, int):
480-
pad_top = pad_bottom = pad_left = pad_right = padding
481-
elif isinstance(padding, list) and len(padding) == 2:
482-
pad_left = pad_right = padding[0]
483-
pad_top = pad_bottom = padding[1]
484-
elif isinstance(padding, list) and len(padding) == 4:
485-
pad_left, pad_top, pad_right, pad_bottom = padding
486-
487-
h += pad_top + pad_bottom
488-
w += pad_left + pad_right
489-
else:
490-
pad_left = pad_right = pad_top = pad_bottom = 0
491-
492-
if pad_if_needed:
493-
if w < size[1]:
494-
diff = size[1] - w
495-
pad_left += diff
496-
pad_right += diff
497-
w += 2 * diff
498-
if h < size[0]:
499-
diff = size[0] - h
500-
pad_top += diff
501-
pad_bottom += diff
502-
h += 2 * diff
503-
504-
padding = [pad_left, pad_top, pad_right, pad_bottom]
505-
506-
assert 0 <= params["top"] <= h - size[0] + 1
507-
assert 0 <= params["left"] <= w - size[1] + 1
508-
assert params["height"] == size[0]
509-
assert params["width"] == size[1]
510-
assert params["needs_pad"] is any(padding)
511-
assert params["padding"] == padding
512-
513-
514452
class TestGaussianBlur:
515453
def test_assertions(self):
516454
with pytest.raises(ValueError, match="Kernel size should be a tuple/list of two integers"):

test/test_transforms_v2_consistency.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -318,26 +318,6 @@ def __init__(
318318
],
319319
closeness_kwargs={"rtol": 1e-5, "atol": 1e-5},
320320
),
321-
ConsistencyConfig(
322-
v2_transforms.RandomCrop,
323-
legacy_transforms.RandomCrop,
324-
[
325-
ArgsKwargs(12),
326-
ArgsKwargs((15, 17)),
327-
NotScriptableArgsKwargs(11, padding=1),
328-
ArgsKwargs(11, padding=[1]),
329-
ArgsKwargs((8, 13), padding=(2, 3)),
330-
ArgsKwargs((14, 9), padding=(0, 2, 1, 0)),
331-
ArgsKwargs(36, pad_if_needed=True),
332-
ArgsKwargs((7, 8), fill=1),
333-
NotScriptableArgsKwargs(5, fill=(1, 2, 3)),
334-
ArgsKwargs(12),
335-
NotScriptableArgsKwargs(15, padding=2, padding_mode="edge"),
336-
ArgsKwargs(17, padding=(1, 0), padding_mode="reflect"),
337-
ArgsKwargs(8, padding=(3, 0, 0, 1), padding_mode="symmetric"),
338-
],
339-
make_images_kwargs=dict(DEFAULT_MAKE_IMAGES_KWARGS, sizes=[(26, 26), (18, 33), (29, 22)]),
340-
),
341321
ConsistencyConfig(
342322
v2_transforms.RandomPerspective,
343323
legacy_transforms.RandomPerspective,
@@ -573,7 +553,6 @@ def test_call_consistency(config, args_kwargs):
573553
(v2_transforms.RandomErasing, ArgsKwargs(make_image(), scale=(0.3, 0.7), ratio=(0.5, 1.5))),
574554
(v2_transforms.ColorJitter, ArgsKwargs(brightness=None, contrast=None, saturation=None, hue=None)),
575555
(v2_transforms.GaussianBlur, ArgsKwargs(0.3, 1.4)),
576-
(v2_transforms.RandomCrop, ArgsKwargs(make_image(size=(61, 47)), output_size=(19, 25))),
577556
(v2_transforms.RandomPerspective, ArgsKwargs(23, 17, 0.5)),
578557
(v2_transforms.AutoAugment, ArgsKwargs(5)),
579558
]

test/test_transforms_v2_functional.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -576,63 +576,6 @@ def _compute_affine_matrix(angle_, translate_, scale_, shear_, center_):
576576
return true_matrix
577577

578578

579-
@pytest.mark.parametrize("device", cpu_and_cuda())
580-
@pytest.mark.parametrize(
581-
"format",
582-
[tv_tensors.BoundingBoxFormat.XYXY, tv_tensors.BoundingBoxFormat.XYWH, tv_tensors.BoundingBoxFormat.CXCYWH],
583-
)
584-
@pytest.mark.parametrize(
585-
"top, left, height, width, expected_bboxes",
586-
[
587-
[8, 12, 30, 40, [(-2.0, 7.0, 13.0, 27.0), (38.0, -3.0, 58.0, 14.0), (33.0, 38.0, 44.0, 54.0)]],
588-
[-8, 12, 70, 40, [(-2.0, 23.0, 13.0, 43.0), (38.0, 13.0, 58.0, 30.0), (33.0, 54.0, 44.0, 70.0)]],
589-
],
590-
)
591-
def test_correctness_crop_bounding_boxes(device, format, top, left, height, width, expected_bboxes):
592-
593-
# Expected bboxes computed using Albumentations:
594-
# import numpy as np
595-
# from albumentations.augmentations.crops.functional import crop_bbox_by_coords, normalize_bbox, denormalize_bbox
596-
# expected_bboxes = []
597-
# for in_box in in_boxes:
598-
# n_in_box = normalize_bbox(in_box, *size)
599-
# n_out_box = crop_bbox_by_coords(
600-
# n_in_box, (left, top, left + width, top + height), height, width, *size
601-
# )
602-
# out_box = denormalize_bbox(n_out_box, height, width)
603-
# expected_bboxes.append(out_box)
604-
605-
format = tv_tensors.BoundingBoxFormat.XYXY
606-
canvas_size = (64, 76)
607-
in_boxes = [
608-
[10.0, 15.0, 25.0, 35.0],
609-
[50.0, 5.0, 70.0, 22.0],
610-
[45.0, 46.0, 56.0, 62.0],
611-
]
612-
in_boxes = torch.tensor(in_boxes, device=device)
613-
if format != tv_tensors.BoundingBoxFormat.XYXY:
614-
in_boxes = convert_bounding_box_format(in_boxes, tv_tensors.BoundingBoxFormat.XYXY, format)
615-
616-
expected_bboxes = clamp_bounding_boxes(
617-
tv_tensors.BoundingBoxes(expected_bboxes, format="XYXY", canvas_size=canvas_size)
618-
).tolist()
619-
620-
output_boxes, output_canvas_size = F.crop_bounding_boxes(
621-
in_boxes,
622-
format,
623-
top,
624-
left,
625-
canvas_size[0],
626-
canvas_size[1],
627-
)
628-
629-
if format != tv_tensors.BoundingBoxFormat.XYXY:
630-
output_boxes = convert_bounding_box_format(output_boxes, format, tv_tensors.BoundingBoxFormat.XYXY)
631-
632-
torch.testing.assert_close(output_boxes.tolist(), expected_bboxes)
633-
torch.testing.assert_close(output_canvas_size, canvas_size)
634-
635-
636579
@pytest.mark.parametrize("device", cpu_and_cuda())
637580
def test_correctness_vertical_flip_segmentation_mask_on_fixed_input(device):
638581
mask = torch.zeros((3, 3, 3), dtype=torch.long, device=device)

0 commit comments

Comments
 (0)