Skip to content

Commit

Permalink
functional
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHug committed Jul 31, 2023
1 parent 75bf4c5 commit 124db1f
Show file tree
Hide file tree
Showing 23 changed files with 433 additions and 433 deletions.
8 changes: 4 additions & 4 deletions test/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,17 +647,17 @@ def make_bounding_box(
.. code::
image = make_image=(size=size)
bounding_box = make_bounding_box(spatial_size=size)
assert F.get_spatial_size(bounding_box) == F.get_spatial_size(image)
bounding_boxes = make_bounding_box(spatial_size=size)
assert F.get_spatial_size(bounding_boxes) == F.get_spatial_size(image)
For convenience, if both size and spatial_size are omitted, spatial_size defaults to the same value as size for all
other maker functions, e.g.
.. code::
image = make_image=()
bounding_box = make_bounding_box()
assert F.get_spatial_size(bounding_box) == F.get_spatial_size(image)
bounding_boxes = make_bounding_box()
assert F.get_spatial_size(bounding_boxes) == F.get_spatial_size(image)
"""

def sample_position(values, max_value):
Expand Down
12 changes: 6 additions & 6 deletions test/test_prototype_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def test__transform_culling(self, mocker):
assert_equal(output["masks"], masks[is_valid])
assert_equal(output["labels"], labels[is_valid])

def test__transform_bounding_box_clamping(self, mocker):
def test__transform_bounding_boxes_clamping(self, mocker):
batch_size = 3
spatial_size = (10, 10)

Expand All @@ -349,15 +349,15 @@ def test__transform_bounding_box_clamping(self, mocker):
),
)

bounding_box = make_bounding_box(
bounding_boxes = make_bounding_box(
format=BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(batch_size,)
)
mock = mocker.patch("torchvision.prototype.transforms._geometry.F.clamp_bounding_box")
mock = mocker.patch("torchvision.prototype.transforms._geometry.F.clamp_bounding_boxes")

transform = transforms.FixedSizeCrop((-1, -1))
mocker.patch("torchvision.prototype.transforms._geometry.has_any", return_value=True)

transform(bounding_box)
transform(bounding_boxes)

mock.assert_called_once()

Expand Down Expand Up @@ -390,7 +390,7 @@ class TestPermuteDimensions:
def test_call(self, dims, inverse_dims):
sample = dict(
image=make_image(),
bounding_box=make_bounding_box(format=BoundingBoxFormat.XYXY),
bounding_boxes=make_bounding_box(format=BoundingBoxFormat.XYXY),
video=make_video(),
str="str",
int=0,
Expand Down Expand Up @@ -434,7 +434,7 @@ class TestTransposeDimensions:
def test_call(self, dims):
sample = dict(
image=make_image(),
bounding_box=make_bounding_box(format=BoundingBoxFormat.XYXY),
bounding_boxes=make_bounding_box(format=BoundingBoxFormat.XYXY),
video=make_video(),
str="str",
int=0,
Expand Down
16 changes: 8 additions & 8 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def make_pil_images(*args, **kwargs):


def make_vanilla_tensor_bounding_boxes(*args, **kwargs):
for bounding_box in make_bounding_boxes(*args, **kwargs):
yield bounding_box.data
for bounding_boxes in make_bounding_boxes(*args, **kwargs):
yield bounding_boxes.data


def parametrize(transforms_with_inputs):
Expand Down Expand Up @@ -180,16 +180,16 @@ def test_common(self, transform, adapter, container_type, image_or_video, device
image_datapoint=make_image(size=spatial_size),
video_datapoint=make_video(size=spatial_size),
image_pil=next(make_pil_images(sizes=[spatial_size], color_spaces=["RGB"])),
bounding_box_xyxy=make_bounding_box(
bounding_boxes_xyxy=make_bounding_box(
format=datapoints.BoundingBoxFormat.XYXY, spatial_size=spatial_size, batch_dims=(3,)
),
bounding_box_xywh=make_bounding_box(
bounding_boxes_xywh=make_bounding_box(
format=datapoints.BoundingBoxFormat.XYWH, spatial_size=spatial_size, batch_dims=(4,)
),
bounding_box_cxcywh=make_bounding_box(
bounding_boxes_cxcywh=make_bounding_box(
format=datapoints.BoundingBoxFormat.CXCYWH, spatial_size=spatial_size, batch_dims=(5,)
),
bounding_box_degenerate_xyxy=datapoints.BoundingBoxes(
bounding_boxes_degenerate_xyxy=datapoints.BoundingBoxes(
[
[0, 0, 0, 0], # no height or width
[0, 0, 0, 1], # no height
Expand All @@ -201,7 +201,7 @@ def test_common(self, transform, adapter, container_type, image_or_video, device
format=datapoints.BoundingBoxFormat.XYXY,
spatial_size=spatial_size,
),
bounding_box_degenerate_xywh=datapoints.BoundingBoxes(
bounding_boxes_degenerate_xywh=datapoints.BoundingBoxes(
[
[0, 0, 0, 0], # no height or width
[0, 0, 0, 1], # no height
Expand All @@ -213,7 +213,7 @@ def test_common(self, transform, adapter, container_type, image_or_video, device
format=datapoints.BoundingBoxFormat.XYWH,
spatial_size=spatial_size,
),
bounding_box_degenerate_cxcywh=datapoints.BoundingBoxes(
bounding_boxes_degenerate_cxcywh=datapoints.BoundingBoxes(
[
[0, 0, 0, 0], # no height or width
[0, 0, 0, 1], # no height
Expand Down
66 changes: 33 additions & 33 deletions test/test_transforms_v2_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from torchvision.transforms.functional import _get_perspective_coeffs
from torchvision.transforms.v2 import functional as F
from torchvision.transforms.v2.functional._geometry import _center_crop_compute_padding
from torchvision.transforms.v2.functional._meta import clamp_bounding_box, convert_format_bounding_box
from torchvision.transforms.v2.functional._meta import clamp_bounding_boxes, convert_format_bounding_boxes
from torchvision.transforms.v2.utils import is_simple_tensor
from transforms_v2_dispatcher_infos import DISPATCHER_INFOS
from transforms_v2_kernel_infos import KERNEL_INFOS
Expand Down Expand Up @@ -515,15 +515,15 @@ def test_unkown_type(self, info):
[
info
for info in DISPATCHER_INFOS
if datapoints.BoundingBoxes in info.kernels and info.dispatcher is not F.convert_format_bounding_box
if datapoints.BoundingBoxes in info.kernels and info.dispatcher is not F.convert_format_bounding_boxes
],
args_kwargs_fn=lambda info: info.sample_inputs(datapoints.BoundingBoxes),
)
def test_bounding_box_format_consistency(self, info, args_kwargs):
(bounding_box, *other_args), kwargs = args_kwargs.load()
format = bounding_box.format
def test_bounding_boxes_format_consistency(self, info, args_kwargs):
(bounding_boxes, *other_args), kwargs = args_kwargs.load()
format = bounding_boxes.format

output = info.dispatcher(bounding_box, *other_args, **kwargs)
output = info.dispatcher(bounding_boxes, *other_args, **kwargs)

assert output.format == format

Expand Down Expand Up @@ -575,7 +575,7 @@ def test_simple_tensor_insufficient_metadata(self, metadata):
simple_tensor = next(make_bounding_boxes()).as_subclass(torch.Tensor)

with pytest.raises(ValueError, match=re.escape("`format` and `spatial_size` has to be passed")):
F.clamp_bounding_box(simple_tensor, **metadata)
F.clamp_bounding_boxes(simple_tensor, **metadata)

@pytest.mark.parametrize(
"metadata",
Expand All @@ -589,7 +589,7 @@ def test_datapoint_explicit_metadata(self, metadata):
datapoint = next(make_bounding_boxes())

with pytest.raises(ValueError, match=re.escape("`format` and `spatial_size` must not be passed")):
F.clamp_bounding_box(datapoint, **metadata)
F.clamp_bounding_boxes(datapoint, **metadata)


class TestConvertFormatBoundingBoxes:
Expand All @@ -602,19 +602,19 @@ class TestConvertFormatBoundingBoxes:
)
def test_missing_new_format(self, inpt, old_format):
with pytest.raises(TypeError, match=re.escape("missing 1 required argument: 'new_format'")):
F.convert_format_bounding_box(inpt, old_format)
F.convert_format_bounding_boxes(inpt, old_format)

def test_simple_tensor_insufficient_metadata(self):
simple_tensor = next(make_bounding_boxes()).as_subclass(torch.Tensor)

with pytest.raises(ValueError, match=re.escape("`old_format` has to be passed")):
F.convert_format_bounding_box(simple_tensor, new_format=datapoints.BoundingBoxFormat.CXCYWH)
F.convert_format_bounding_boxes(simple_tensor, new_format=datapoints.BoundingBoxFormat.CXCYWH)

def test_datapoint_explicit_metadata(self):
datapoint = next(make_bounding_boxes())

with pytest.raises(ValueError, match=re.escape("`old_format` must not be passed")):
F.convert_format_bounding_box(
F.convert_format_bounding_boxes(
datapoint, old_format=datapoint.format, new_format=datapoints.BoundingBoxFormat.CXCYWH
)

Expand Down Expand Up @@ -658,7 +658,7 @@ def _compute_affine_matrix(angle_, translate_, scale_, shear_, center_):
[-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)]],
],
)
def test_correctness_crop_bounding_box(device, format, top, left, height, width, expected_bboxes):
def test_correctness_crop_bounding_boxes(device, format, top, left, height, width, expected_bboxes):

# Expected bboxes computed using Albumentations:
# import numpy as np
Expand All @@ -681,13 +681,13 @@ def test_correctness_crop_bounding_box(device, format, top, left, height, width,
]
in_boxes = torch.tensor(in_boxes, device=device)
if format != datapoints.BoundingBoxFormat.XYXY:
in_boxes = convert_format_bounding_box(in_boxes, datapoints.BoundingBoxFormat.XYXY, format)
in_boxes = convert_format_bounding_boxes(in_boxes, datapoints.BoundingBoxFormat.XYXY, format)

expected_bboxes = clamp_bounding_box(
expected_bboxes = clamp_bounding_boxes(
datapoints.BoundingBoxes(expected_bboxes, format="XYXY", spatial_size=spatial_size)
).tolist()

output_boxes, output_spatial_size = F.crop_bounding_box(
output_boxes, output_spatial_size = F.crop_bounding_boxes(
in_boxes,
format,
top,
Expand All @@ -697,7 +697,7 @@ def test_correctness_crop_bounding_box(device, format, top, left, height, width,
)

if format != datapoints.BoundingBoxFormat.XYXY:
output_boxes = convert_format_bounding_box(output_boxes, format, datapoints.BoundingBoxFormat.XYXY)
output_boxes = convert_format_bounding_boxes(output_boxes, format, datapoints.BoundingBoxFormat.XYXY)

torch.testing.assert_close(output_boxes.tolist(), expected_bboxes)
torch.testing.assert_close(output_spatial_size, spatial_size)
Expand Down Expand Up @@ -727,7 +727,7 @@ def test_correctness_vertical_flip_segmentation_mask_on_fixed_input(device):
[-5, 5, 35, 45, (32, 34)],
],
)
def test_correctness_resized_crop_bounding_box(device, format, top, left, height, width, size):
def test_correctness_resized_crop_bounding_boxes(device, format, top, left, height, width, size):
def _compute_expected_bbox(bbox, top_, left_, height_, width_, size_):
# bbox should be xyxy
bbox[0] = (bbox[0] - left_) * size_[1] / width_
Expand All @@ -751,12 +751,12 @@ def _compute_expected_bbox(bbox, top_, left_, height_, width_, size_):
in_boxes, format=datapoints.BoundingBoxFormat.XYXY, spatial_size=spatial_size, device=device
)
if format != datapoints.BoundingBoxFormat.XYXY:
in_boxes = convert_format_bounding_box(in_boxes, datapoints.BoundingBoxFormat.XYXY, format)
in_boxes = convert_format_bounding_boxes(in_boxes, datapoints.BoundingBoxFormat.XYXY, format)

output_boxes, output_spatial_size = F.resized_crop_bounding_box(in_boxes, format, top, left, height, width, size)
output_boxes, output_spatial_size = F.resized_crop_bounding_boxes(in_boxes, format, top, left, height, width, size)

if format != datapoints.BoundingBoxFormat.XYXY:
output_boxes = convert_format_bounding_box(output_boxes, format, datapoints.BoundingBoxFormat.XYXY)
output_boxes = convert_format_bounding_boxes(output_boxes, format, datapoints.BoundingBoxFormat.XYXY)

torch.testing.assert_close(output_boxes, expected_bboxes)
torch.testing.assert_close(output_spatial_size, size)
Expand All @@ -776,7 +776,7 @@ def _parse_padding(padding):

@pytest.mark.parametrize("device", cpu_and_cuda())
@pytest.mark.parametrize("padding", [[1], [1, 1], [1, 1, 2, 2]])
def test_correctness_pad_bounding_box(device, padding):
def test_correctness_pad_bounding_boxes(device, padding):
def _compute_expected_bbox(bbox, padding_):
pad_left, pad_up, _, _ = _parse_padding(padding_)

Expand All @@ -785,13 +785,13 @@ def _compute_expected_bbox(bbox, padding_):
bbox = (
bbox.clone()
if format == datapoints.BoundingBoxFormat.XYXY
else convert_format_bounding_box(bbox, new_format=datapoints.BoundingBoxFormat.XYXY)
else convert_format_bounding_boxes(bbox, new_format=datapoints.BoundingBoxFormat.XYXY)
)

bbox[0::2] += pad_left
bbox[1::2] += pad_up

bbox = convert_format_bounding_box(bbox, new_format=format)
bbox = convert_format_bounding_boxes(bbox, new_format=format)
if bbox.dtype != dtype:
# Temporary cast to original dtype
# e.g. float32 -> int
Expand All @@ -808,7 +808,7 @@ def _compute_expected_spatial_size(bbox, padding_):
bboxes_format = bboxes.format
bboxes_spatial_size = bboxes.spatial_size

output_boxes, output_spatial_size = F.pad_bounding_box(
output_boxes, output_spatial_size = F.pad_bounding_boxes(
bboxes, format=bboxes_format, spatial_size=bboxes_spatial_size, padding=padding
)

Expand Down Expand Up @@ -849,7 +849,7 @@ def test_correctness_pad_segmentation_mask_on_fixed_input(device):
[[[3, 2], [32, 3], [30, 24], [2, 25]], [[5, 5], [30, 3], [33, 19], [4, 25]]],
],
)
def test_correctness_perspective_bounding_box(device, startpoints, endpoints):
def test_correctness_perspective_bounding_boxes(device, startpoints, endpoints):
def _compute_expected_bbox(bbox, pcoeffs_):
m1 = np.array(
[
Expand All @@ -864,7 +864,7 @@ def _compute_expected_bbox(bbox, pcoeffs_):
]
)

bbox_xyxy = convert_format_bounding_box(bbox, new_format=datapoints.BoundingBoxFormat.XYXY)
bbox_xyxy = convert_format_bounding_boxes(bbox, new_format=datapoints.BoundingBoxFormat.XYXY)
points = np.array(
[
[bbox_xyxy[0].item(), bbox_xyxy[1].item(), 1.0],
Expand All @@ -891,7 +891,7 @@ def _compute_expected_bbox(bbox, pcoeffs_):
dtype=bbox.dtype,
device=bbox.device,
)
return clamp_bounding_box(convert_format_bounding_box(out_bbox, new_format=bbox.format))
return clamp_bounding_boxes(convert_format_bounding_boxes(out_bbox, new_format=bbox.format))

spatial_size = (32, 38)

Expand All @@ -901,7 +901,7 @@ def _compute_expected_bbox(bbox, pcoeffs_):
for bboxes in make_bounding_boxes(spatial_size=spatial_size, extra_dims=((4,),)):
bboxes = bboxes.to(device)

output_bboxes = F.perspective_bounding_box(
output_bboxes = F.perspective_bounding_boxes(
bboxes.as_subclass(torch.Tensor),
format=bboxes.format,
spatial_size=bboxes.spatial_size,
Expand Down Expand Up @@ -929,12 +929,12 @@ def _compute_expected_bbox(bbox, pcoeffs_):
"output_size",
[(18, 18), [18, 15], (16, 19), [12], [46, 48]],
)
def test_correctness_center_crop_bounding_box(device, output_size):
def test_correctness_center_crop_bounding_boxes(device, output_size):
def _compute_expected_bbox(bbox, output_size_):
format_ = bbox.format
spatial_size_ = bbox.spatial_size
dtype = bbox.dtype
bbox = convert_format_bounding_box(bbox.float(), format_, datapoints.BoundingBoxFormat.XYWH)
bbox = convert_format_bounding_boxes(bbox.float(), format_, datapoints.BoundingBoxFormat.XYWH)

if len(output_size_) == 1:
output_size_.append(output_size_[-1])
Expand All @@ -948,16 +948,16 @@ def _compute_expected_bbox(bbox, output_size_):
bbox[3].item(),
]
out_bbox = torch.tensor(out_bbox)
out_bbox = convert_format_bounding_box(out_bbox, datapoints.BoundingBoxFormat.XYWH, format_)
out_bbox = clamp_bounding_box(out_bbox, format=format_, spatial_size=output_size)
out_bbox = convert_format_bounding_boxes(out_bbox, datapoints.BoundingBoxFormat.XYWH, format_)
out_bbox = clamp_bounding_boxes(out_bbox, format=format_, spatial_size=output_size)
return out_bbox.to(dtype=dtype, device=bbox.device)

for bboxes in make_bounding_boxes(extra_dims=((4,),)):
bboxes = bboxes.to(device)
bboxes_format = bboxes.format
bboxes_spatial_size = bboxes.spatial_size

output_boxes, output_spatial_size = F.center_crop_bounding_box(
output_boxes, output_spatial_size = F.center_crop_bounding_boxes(
bboxes, bboxes_format, bboxes_spatial_size, output_size
)

Expand Down
Loading

0 comments on commit 124db1f

Please sign in to comment.