test_transforms_v2_refactored.py 66.8 KB
Newer Older
1
2
import contextlib
import inspect
Philip Meier's avatar
Philip Meier committed
3
import math
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re
from typing import get_type_hints
from unittest import mock

import numpy as np
import PIL.Image
import pytest

import torch
import torchvision.transforms.v2 as transforms
from common_utils import (
    assert_equal,
    assert_no_warnings,
    cache,
    cpu_and_cuda,
    ignore_jit_no_profile_information_warning,
    make_bounding_box,
    make_detection_mask,
    make_image,
    make_segmentation_mask,
    make_video,
Nicolas Hug's avatar
Nicolas Hug committed
25
    set_rng_seed,
26
27
28
)
from torch.testing import assert_close
from torchvision import datapoints
Philip Meier's avatar
Philip Meier committed
29
30

from torchvision.transforms._functional_tensor import _max_value as get_max_value
31
32
33
34
from torchvision.transforms.functional import pil_modes_mapping
from torchvision.transforms.v2 import functional as F


Nicolas Hug's avatar
Nicolas Hug committed
35
36
37
38
39
40
@pytest.fixture(autouse=True)
def fix_rng_seed():
    set_rng_seed(0)
    yield


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def _to_tolerances(maybe_tolerance_dict):
    if not isinstance(maybe_tolerance_dict, dict):
        return dict(rtol=None, atol=None)

    tolerances = dict(rtol=0, atol=0)
    tolerances.update(maybe_tolerance_dict)
    return tolerances


def _check_kernel_cuda_vs_cpu(kernel, input, *args, rtol, atol, **kwargs):
    """Checks if the kernel produces closes results for inputs on GPU and CPU."""
    if input.device.type != "cuda":
        return

    input_cuda = input.as_subclass(torch.Tensor)
    input_cpu = input_cuda.to("cpu")

    actual = kernel(input_cuda, *args, **kwargs)
    expected = kernel(input_cpu, *args, **kwargs)

    assert_close(actual, expected, check_device=False, rtol=rtol, atol=atol)


@cache
def _script(fn):
    try:
        return torch.jit.script(fn)
    except Exception as error:
        raise AssertionError(f"Trying to `torch.jit.script` '{fn.__name__}' raised the error above.") from error


def _check_kernel_scripted_vs_eager(kernel, input, *args, rtol, atol, **kwargs):
    """Checks if the kernel is scriptable and if the scripted output is close to the eager one."""
    if input.device.type != "cpu":
        return

    kernel_scripted = _script(kernel)

    input = input.as_subclass(torch.Tensor)
    with ignore_jit_no_profile_information_warning():
        actual = kernel_scripted(input, *args, **kwargs)
    expected = kernel(input, *args, **kwargs)

    assert_close(actual, expected, rtol=rtol, atol=atol)


def _check_kernel_batched_vs_unbatched(kernel, input, *args, rtol, atol, **kwargs):
    """Checks if the kernel produces close results for batched and unbatched inputs."""
    unbatched_input = input.as_subclass(torch.Tensor)

    for batch_dims in [(2,), (2, 1)]:
        repeats = [*batch_dims, *[1] * input.ndim]

        actual = kernel(unbatched_input.repeat(repeats), *args, **kwargs)

        expected = kernel(unbatched_input, *args, **kwargs)
        # We can't directly call `.repeat()` on the output, since some kernel also return some additional metadata
        if isinstance(expected, torch.Tensor):
            expected = expected.repeat(repeats)
        else:
            tensor, *metadata = expected
            expected = (tensor.repeat(repeats), *metadata)

        assert_close(actual, expected, rtol=rtol, atol=atol)

    for degenerate_batch_dims in [(0,), (5, 0), (0, 5)]:
        degenerate_batched_input = torch.empty(
            degenerate_batch_dims + input.shape, dtype=input.dtype, device=input.device
        )

        output = kernel(degenerate_batched_input, *args, **kwargs)
        # Most kernels just return a tensor, but some also return some additional metadata
        if not isinstance(output, torch.Tensor):
            output, *_ = output

        assert output.shape[: -input.ndim] == degenerate_batch_dims


def check_kernel(
    kernel,
    input,
    *args,
    check_cuda_vs_cpu=True,
    check_scripted_vs_eager=True,
    check_batched_vs_unbatched=True,
    **kwargs,
):
    initial_input_version = input._version

    output = kernel(input.as_subclass(torch.Tensor), *args, **kwargs)
    # Most kernels just return a tensor, but some also return some additional metadata
    if not isinstance(output, torch.Tensor):
        output, *_ = output

    # check that no inplace operation happened
    assert input._version == initial_input_version

    assert output.dtype == input.dtype
    assert output.device == input.device

    if check_cuda_vs_cpu:
        _check_kernel_cuda_vs_cpu(kernel, input, *args, **kwargs, **_to_tolerances(check_cuda_vs_cpu))

    if check_scripted_vs_eager:
        _check_kernel_scripted_vs_eager(kernel, input, *args, **kwargs, **_to_tolerances(check_scripted_vs_eager))

    if check_batched_vs_unbatched:
        _check_kernel_batched_vs_unbatched(kernel, input, *args, **kwargs, **_to_tolerances(check_batched_vs_unbatched))


def _check_dispatcher_scripted_smoke(dispatcher, input, *args, **kwargs):
    """Checks if the dispatcher can be scripted and the scripted version can be called without error."""
    if not isinstance(input, datapoints.Image):
        return

    dispatcher_scripted = _script(dispatcher)
    with ignore_jit_no_profile_information_warning():
        dispatcher_scripted(input.as_subclass(torch.Tensor), *args, **kwargs)


def _check_dispatcher_dispatch(dispatcher, kernel, input, *args, **kwargs):
    """Checks if the dispatcher correctly dispatches the input to the corresponding kernel and that the input type is
    preserved in doing so. For bounding boxes also checks that the format is preserved.
    """
    if isinstance(input, datapoints._datapoint.Datapoint):
        # Due to our complex dispatch architecture for datapoints, we cannot spy on the kernel directly,
        # but rather have to patch the `Datapoint.__F` attribute to contain the spied on kernel.
Philip Meier's avatar
Philip Meier committed
168
        spy = mock.MagicMock(wraps=kernel, name=kernel.__name__)
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
        with mock.patch.object(F, kernel.__name__, spy):
            # Due to Python's name mangling, the `Datapoint.__F` attribute is only accessible from inside the class.
            # Since that is not the case here, we need to prefix f"_{cls.__name__}"
            # See https://docs.python.org/3/tutorial/classes.html#private-variables for details
            with mock.patch.object(datapoints._datapoint.Datapoint, "_Datapoint__F", new=F):
                output = dispatcher(input, *args, **kwargs)

        spy.assert_called_once()
    else:
        with mock.patch(f"{dispatcher.__module__}.{kernel.__name__}", wraps=kernel) as spy:
            output = dispatcher(input, *args, **kwargs)

            spy.assert_called_once()

    assert isinstance(output, type(input))

    if isinstance(input, datapoints.BoundingBox):
        assert output.format == input.format


def check_dispatcher(
    dispatcher,
    kernel,
    input,
    *args,
    check_scripted_smoke=True,
    check_dispatch=True,
    **kwargs,
):
    with mock.patch("torch._C._log_api_usage_once", wraps=torch._C._log_api_usage_once) as spy:
        dispatcher(input, *args, **kwargs)

        spy.assert_any_call(f"{dispatcher.__module__}.{dispatcher.__name__}")

    unknown_input = object()
    with pytest.raises(TypeError, match=re.escape(str(type(unknown_input)))):
        dispatcher(unknown_input, *args, **kwargs)

    if check_scripted_smoke:
        _check_dispatcher_scripted_smoke(dispatcher, input, *args, **kwargs)

    if check_dispatch:
        _check_dispatcher_dispatch(dispatcher, kernel, input, *args, **kwargs)


def _check_dispatcher_kernel_signature_match(dispatcher, *, kernel, input_type):
    """Checks if the signature of the dispatcher matches the kernel signature."""
    dispatcher_signature = inspect.signature(dispatcher)
    dispatcher_params = list(dispatcher_signature.parameters.values())[1:]

    kernel_signature = inspect.signature(kernel)
    kernel_params = list(kernel_signature.parameters.values())[1:]

    if issubclass(input_type, datapoints._datapoint.Datapoint):
        # We filter out metadata that is implicitly passed to the dispatcher through the input datapoint, but has to be
        # explicitly passed to the kernel.
        kernel_params = [param for param in kernel_params if param.name not in input_type.__annotations__.keys()]

    dispatcher_params = iter(dispatcher_params)
    for dispatcher_param, kernel_param in zip(dispatcher_params, kernel_params):
        try:
            # In general, the dispatcher parameters are a superset of the kernel parameters. Thus, we filter out
            # dispatcher parameters that have no kernel equivalent while keeping the order intact.
            while dispatcher_param.name != kernel_param.name:
                dispatcher_param = next(dispatcher_params)
        except StopIteration:
            raise AssertionError(
                f"Parameter `{kernel_param.name}` of kernel `{kernel.__name__}` "
                f"has no corresponding parameter on the dispatcher `{dispatcher.__name__}`."
            ) from None

        if issubclass(input_type, PIL.Image.Image):
            # PIL kernels often have more correct annotations, since they are not limited by JIT. Thus, we don't check
            # them in the first place.
            dispatcher_param._annotation = kernel_param._annotation = inspect.Parameter.empty

        assert dispatcher_param == kernel_param


def _check_dispatcher_datapoint_signature_match(dispatcher):
    """Checks if the signature of the dispatcher matches the corresponding method signature on the Datapoint class."""
    dispatcher_signature = inspect.signature(dispatcher)
    dispatcher_params = list(dispatcher_signature.parameters.values())[1:]

    datapoint_method = getattr(datapoints._datapoint.Datapoint, dispatcher.__name__)
    datapoint_signature = inspect.signature(datapoint_method)
    datapoint_params = list(datapoint_signature.parameters.values())[1:]

    # Some annotations in the `datapoints._datapoint` module
    # are stored as strings. The block below makes them concrete again (non-strings), so they can be compared to the
    # natively concrete dispatcher annotations.
    datapoint_annotations = get_type_hints(datapoint_method)
    for param in datapoint_params:
        param._annotation = datapoint_annotations[param.name]

    assert dispatcher_params == datapoint_params


def check_dispatcher_signatures_match(dispatcher, *, kernel, input_type):
    _check_dispatcher_kernel_signature_match(dispatcher, kernel=kernel, input_type=input_type)
    _check_dispatcher_datapoint_signature_match(dispatcher)


def _check_transform_v1_compatibility(transform, input):
    """If the transform defines the ``_v1_transform_cls`` attribute, checks if the transform has a public, static
    ``get_params`` method, is scriptable, and the scripted version can be called without error."""
    if not hasattr(transform, "_v1_transform_cls"):
        return

    if type(input) is not torch.Tensor:
        return

    if hasattr(transform._v1_transform_cls, "get_params"):
        assert type(transform).get_params is transform._v1_transform_cls.get_params

    scripted_transform = _script(transform)
    with ignore_jit_no_profile_information_warning():
        scripted_transform(input)


def check_transform(transform_cls, input, *args, **kwargs):
    transform = transform_cls(*args, **kwargs)

    output = transform(input)
    assert isinstance(output, type(input))

    if isinstance(input, datapoints.BoundingBox):
        assert output.format == input.format

    _check_transform_v1_compatibility(transform, input)


301
def transform_cls_to_functional(transform_cls, **transform_specific_kwargs):
302
    def wrapper(input, *args, **kwargs):
303
        transform = transform_cls(*args, **transform_specific_kwargs, **kwargs)
304
305
306
307
308
309
310
        return transform(input)

    wrapper.__name__ = transform_cls.__name__

    return wrapper


Philip Meier's avatar
Philip Meier committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
def make_input(input_type, *, dtype=None, device="cpu", spatial_size=(17, 11), mask_type="segmentation", **kwargs):
    if input_type in {torch.Tensor, PIL.Image.Image, datapoints.Image}:
        input = make_image(size=spatial_size, dtype=dtype or torch.uint8, device=device, **kwargs)
        if input_type is torch.Tensor:
            input = input.as_subclass(torch.Tensor)
        elif input_type is PIL.Image.Image:
            input = F.to_image_pil(input)
    elif input_type is datapoints.BoundingBox:
        kwargs.setdefault("format", datapoints.BoundingBoxFormat.XYXY)
        input = make_bounding_box(
            dtype=dtype or torch.float32,
            device=device,
            spatial_size=spatial_size,
            **kwargs,
        )
    elif input_type is datapoints.Mask:
        if mask_type == "segmentation":
            make_mask = make_segmentation_mask
            default_dtype = torch.uint8
        elif mask_type == "detection":
            make_mask = make_detection_mask
            default_dtype = torch.bool
        else:
            raise ValueError(f"`mask_type` can be `'segmentation'` or `'detection'`, but got {mask_type}.")
        input = make_mask(size=spatial_size, dtype=dtype or default_dtype, device=device, **kwargs)
    elif input_type is datapoints.Video:
        input = make_video(size=spatial_size, dtype=dtype or torch.uint8, device=device, **kwargs)
    else:
        raise TypeError(
            f"Input can either be a plain tensor, any TorchVision datapoint, or a PIL image, "
            f"but got {input_type} instead."
        )

    return input


def param_value_parametrization(**kwargs):
    """Helper function to turn

    @pytest.mark.parametrize(
        ("param", "value"),
        ("a", 1),
        ("a", 2),
        ("a", 3),
        ("b", -1.0)
        ("b", 1.0)
    )

    into

    @param_value_parametrization(a=[1, 2, 3], b=[-1.0, 1.0])
    """
    return pytest.mark.parametrize(
        ("param", "value"),
        [(param, value) for param, values in kwargs.items() for value in values],
    )


def adapt_fill(value, *, dtype):
    """Adapt fill values in the range [0.0, 1.0] to the value range of the dtype"""
    if value is None:
        return value

    max_value = get_max_value(dtype)

    if isinstance(value, (int, float)):
        return type(value)(value * max_value)
    elif isinstance(value, (list, tuple)):
        return type(value)(type(v)(v * max_value) for v in value)
    else:
        raise ValueError(f"fill should be an int or float, or a list or tuple of the former, but got '{value}'.")


EXHAUSTIVE_TYPE_FILLS = [
    None,
    1,
    0.5,
    [1],
    [0.2],
    (0,),
    (0.7,),
    [1, 0, 1],
    [0.1, 0.2, 0.3],
    (0, 1, 0),
    (0.9, 0.234, 0.314),
]
CORRECTNESS_FILLS = [
    v for v in EXHAUSTIVE_TYPE_FILLS if v is None or isinstance(v, float) or (isinstance(v, list) and len(v) > 1)
]


402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# We cannot use `list(transforms.InterpolationMode)` here, since it includes some PIL-only ones as well
INTERPOLATION_MODES = [
    transforms.InterpolationMode.NEAREST,
    transforms.InterpolationMode.NEAREST_EXACT,
    transforms.InterpolationMode.BILINEAR,
    transforms.InterpolationMode.BICUBIC,
]


@contextlib.contextmanager
def assert_warns_antialias_default_value():
    with pytest.warns(UserWarning, match="The default value of the antialias parameter of all the resizing transforms"):
        yield


def reference_affine_bounding_box_helper(bounding_box, *, format, spatial_size, affine_matrix):
418
    def transform(bbox):
419
420
421
422
423
424
        # Go to float before converting to prevent precision loss in case of CXCYWH -> XYXY and W or H is 1
        in_dtype = bbox.dtype
        if not torch.is_floating_point(bbox):
            bbox = bbox.float()
        bbox_xyxy = F.convert_format_bounding_box(
            bbox.as_subclass(torch.Tensor),
425
            old_format=format,
426
427
428
429
430
431
432
433
434
435
436
            new_format=datapoints.BoundingBoxFormat.XYXY,
            inplace=True,
        )
        points = np.array(
            [
                [bbox_xyxy[0].item(), bbox_xyxy[1].item(), 1.0],
                [bbox_xyxy[2].item(), bbox_xyxy[1].item(), 1.0],
                [bbox_xyxy[0].item(), bbox_xyxy[3].item(), 1.0],
                [bbox_xyxy[2].item(), bbox_xyxy[3].item(), 1.0],
            ]
        )
437
        transformed_points = np.matmul(points, affine_matrix.T)
438
439
440
441
442
443
444
445
446
447
        out_bbox = torch.tensor(
            [
                np.min(transformed_points[:, 0]).item(),
                np.min(transformed_points[:, 1]).item(),
                np.max(transformed_points[:, 0]).item(),
                np.max(transformed_points[:, 1]).item(),
            ],
            dtype=bbox_xyxy.dtype,
        )
        out_bbox = F.convert_format_bounding_box(
448
            out_bbox, old_format=datapoints.BoundingBoxFormat.XYXY, new_format=format, inplace=True
449
450
        )
        # It is important to clamp before casting, especially for CXCYWH format, dtype=int64
451
        out_bbox = F.clamp_bounding_box(out_bbox, format=format, spatial_size=spatial_size)
452
453
454
        out_bbox = out_bbox.to(dtype=in_dtype)
        return out_bbox

455
    return torch.stack([transform(b) for b in bounding_box.reshape(-1, 4).unbind()]).reshape(bounding_box.shape)
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518


class TestResize:
    INPUT_SIZE = (17, 11)
    OUTPUT_SIZES = [17, [17], (17,), [12, 13], (12, 13)]

    def _make_max_size_kwarg(self, *, use_max_size, size):
        if use_max_size:
            if not (isinstance(size, int) or len(size) == 1):
                # This would result in an `ValueError`
                return None

            max_size = (size if isinstance(size, int) else size[0]) + 1
        else:
            max_size = None

        return dict(max_size=max_size)

    def _compute_output_size(self, *, input_size, size, max_size):
        if not (isinstance(size, int) or len(size) == 1):
            return tuple(size)

        if not isinstance(size, int):
            size = size[0]

        old_height, old_width = input_size
        ratio = old_width / old_height
        if ratio > 1:
            new_height = size
            new_width = int(ratio * new_height)
        else:
            new_width = size
            new_height = int(new_width / ratio)

        if max_size is not None and max(new_height, new_width) > max_size:
            # Need to recompute the aspect ratio, since it might have changed due to rounding
            ratio = new_width / new_height
            if ratio > 1:
                new_width = max_size
                new_height = int(new_width / ratio)
            else:
                new_height = max_size
                new_width = int(new_height * ratio)

        return new_height, new_width

    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize("interpolation", INTERPOLATION_MODES)
    @pytest.mark.parametrize("use_max_size", [True, False])
    @pytest.mark.parametrize("antialias", [True, False])
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_image_tensor(self, size, interpolation, use_max_size, antialias, dtype, device):
        if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)):
            return

        # In contrast to CPU, there is no native `InterpolationMode.BICUBIC` implementation for uint8 images on CUDA.
        # Internally, it uses the float path. Thus, we need to test with an enormous tolerance here to account for that.
        atol = 30 if transforms.InterpolationMode.BICUBIC and dtype is torch.uint8 else 1
        check_cuda_vs_cpu_tolerances = dict(rtol=0, atol=atol / 255 if dtype.is_floating_point else atol)

        check_kernel(
            F.resize_image_tensor,
Philip Meier's avatar
Philip Meier committed
519
            make_input(datapoints.Image, dtype=dtype, device=device, spatial_size=self.INPUT_SIZE),
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
            size=size,
            interpolation=interpolation,
            **max_size_kwarg,
            antialias=antialias,
            check_cuda_vs_cpu=check_cuda_vs_cpu_tolerances,
            check_scripted_vs_eager=not isinstance(size, int),
        )

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize("use_max_size", [True, False])
    @pytest.mark.parametrize("dtype", [torch.float32, torch.int64])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_bounding_box(self, format, size, use_max_size, dtype, device):
        if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)):
            return

Philip Meier's avatar
Philip Meier committed
537
538
539
        bounding_box = make_input(
            datapoints.BoundingBox, dtype=dtype, device=device, format=format, spatial_size=self.INPUT_SIZE
        )
540
541
542
543
544
545
546
547
548
        check_kernel(
            F.resize_bounding_box,
            bounding_box,
            spatial_size=bounding_box.spatial_size,
            size=size,
            **max_size_kwarg,
            check_scripted_vs_eager=not isinstance(size, int),
        )

Philip Meier's avatar
Philip Meier committed
549
550
551
552
553
554
555
    @pytest.mark.parametrize("mask_type", ["segmentation", "detection"])
    def test_kernel_mask(self, mask_type):
        check_kernel(
            F.resize_mask,
            make_input(datapoints.Mask, spatial_size=self.INPUT_SIZE, mask_type=mask_type),
            size=self.OUTPUT_SIZES[-1],
        )
556
557

    def test_kernel_video(self):
Philip Meier's avatar
Philip Meier committed
558
559
560
561
562
563
        check_kernel(
            F.resize_video,
            make_input(datapoints.Video, spatial_size=self.INPUT_SIZE),
            size=self.OUTPUT_SIZES[-1],
            antialias=True,
        )
564
565
566

    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize(
567
        ("input_type", "kernel"),
568
569
570
571
572
573
574
575
576
        [
            (torch.Tensor, F.resize_image_tensor),
            (PIL.Image.Image, F.resize_image_pil),
            (datapoints.Image, F.resize_image_tensor),
            (datapoints.BoundingBox, F.resize_bounding_box),
            (datapoints.Mask, F.resize_mask),
            (datapoints.Video, F.resize_video),
        ],
    )
577
    def test_dispatcher(self, size, input_type, kernel):
578
579
580
        check_dispatcher(
            F.resize,
            kernel,
Philip Meier's avatar
Philip Meier committed
581
            make_input(input_type, spatial_size=self.INPUT_SIZE),
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
            size=size,
            antialias=True,
            check_scripted_smoke=not isinstance(size, int),
        )

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.resize_image_tensor),
            (PIL.Image.Image, F.resize_image_pil),
            (datapoints.Image, F.resize_image_tensor),
            (datapoints.BoundingBox, F.resize_bounding_box),
            (datapoints.Mask, F.resize_mask),
            (datapoints.Video, F.resize_video),
        ],
    )
    def test_dispatcher_signature(self, kernel, input_type):
        check_dispatcher_signatures_match(F.resize, kernel=kernel, input_type=input_type)

    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize("device", cpu_and_cuda())
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    def test_transform(self, size, device, input_type):
Philip Meier's avatar
Philip Meier committed
608
        input = make_input(input_type, device=device, spatial_size=self.INPUT_SIZE)
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

        check_transform(
            transforms.Resize,
            input,
            size=size,
            antialias=True,
        )

    def _check_output_size(self, input, output, *, size, max_size):
        assert tuple(F.get_spatial_size(output)) == self._compute_output_size(
            input_size=F.get_spatial_size(input), size=size, max_size=max_size
        )

    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    # `InterpolationMode.NEAREST` is modeled after the buggy `INTER_NEAREST` interpolation of CV2.
    # The PIL equivalent of `InterpolationMode.NEAREST` is `InterpolationMode.NEAREST_EXACT`
    @pytest.mark.parametrize("interpolation", set(INTERPOLATION_MODES) - {transforms.InterpolationMode.NEAREST})
    @pytest.mark.parametrize("use_max_size", [True, False])
    @pytest.mark.parametrize("fn", [F.resize, transform_cls_to_functional(transforms.Resize)])
    def test_image_correctness(self, size, interpolation, use_max_size, fn):
        if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)):
            return

Philip Meier's avatar
Philip Meier committed
632
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu", spatial_size=self.INPUT_SIZE)
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

        actual = fn(image, size=size, interpolation=interpolation, **max_size_kwarg, antialias=True)
        expected = F.to_image_tensor(
            F.resize(F.to_image_pil(image), size=size, interpolation=interpolation, **max_size_kwarg)
        )

        self._check_output_size(image, actual, size=size, **max_size_kwarg)
        torch.testing.assert_close(actual, expected, atol=1, rtol=0)

    def _reference_resize_bounding_box(self, bounding_box, *, size, max_size=None):
        old_height, old_width = bounding_box.spatial_size
        new_height, new_width = self._compute_output_size(
            input_size=bounding_box.spatial_size, size=size, max_size=max_size
        )

        if (old_height, old_width) == (new_height, new_width):
            return bounding_box

        affine_matrix = np.array(
            [
                [new_width / old_width, 0, 0],
                [0, new_height / old_height, 0],
            ],
            dtype="float64" if bounding_box.dtype == torch.float64 else "float32",
        )

        expected_bboxes = reference_affine_bounding_box_helper(
            bounding_box,
            format=bounding_box.format,
            spatial_size=(new_height, new_width),
            affine_matrix=affine_matrix,
        )
        return datapoints.BoundingBox.wrap_like(bounding_box, expected_bboxes, spatial_size=(new_height, new_width))

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize("use_max_size", [True, False])
    @pytest.mark.parametrize("fn", [F.resize, transform_cls_to_functional(transforms.Resize)])
    def test_bounding_box_correctness(self, format, size, use_max_size, fn):
        if not (max_size_kwarg := self._make_max_size_kwarg(use_max_size=use_max_size, size=size)):
            return

Philip Meier's avatar
Philip Meier committed
675
        bounding_box = make_input(datapoints.BoundingBox, spatial_size=self.INPUT_SIZE)
676
677
678
679
680
681
682
683
684
685
686
687
688

        actual = fn(bounding_box, size=size, **max_size_kwarg)
        expected = self._reference_resize_bounding_box(bounding_box, size=size, **max_size_kwarg)

        self._check_output_size(bounding_box, actual, size=size, **max_size_kwarg)
        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize("interpolation", set(transforms.InterpolationMode) - set(INTERPOLATION_MODES))
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.Video],
    )
    def test_pil_interpolation_compat_smoke(self, interpolation, input_type):
Philip Meier's avatar
Philip Meier committed
689
        input = make_input(input_type, spatial_size=self.INPUT_SIZE)
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

        with (
            contextlib.nullcontext()
            if isinstance(input, PIL.Image.Image)
            # This error is triggered in PyTorch core
            else pytest.raises(NotImplementedError, match=f"got {interpolation.value.lower()}")
        ):
            F.resize(
                input,
                size=self.OUTPUT_SIZES[0],
                interpolation=interpolation,
            )

    def test_dispatcher_pil_antialias_warning(self):
        with pytest.warns(UserWarning, match="Anti-alias option is always applied for PIL Image input"):
Philip Meier's avatar
Philip Meier committed
705
706
707
            F.resize(
                make_input(PIL.Image.Image, spatial_size=self.INPUT_SIZE), size=self.OUTPUT_SIZES[0], antialias=False
            )
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723

    @pytest.mark.parametrize("size", OUTPUT_SIZES)
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    def test_max_size_error(self, size, input_type):
        if isinstance(size, int) or len(size) == 1:
            max_size = (size if isinstance(size, int) else size[0]) - 1
            match = "must be strictly greater than the requested size"
        else:
            # value can be anything other than None
            max_size = -1
            match = "size should be an int or a sequence of length 1"

        with pytest.raises(ValueError, match=match):
Philip Meier's avatar
Philip Meier committed
724
            F.resize(make_input(input_type, spatial_size=self.INPUT_SIZE), size=size, max_size=max_size, antialias=True)
725
726
727
728
729
730
731
732
733
734
735
736

    @pytest.mark.parametrize("interpolation", INTERPOLATION_MODES)
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, datapoints.Image, datapoints.Video],
    )
    def test_antialias_warning(self, interpolation, input_type):
        with (
            assert_warns_antialias_default_value()
            if interpolation in {transforms.InterpolationMode.BILINEAR, transforms.InterpolationMode.BICUBIC}
            else assert_no_warnings()
        ):
Philip Meier's avatar
Philip Meier committed
737
738
739
740
741
            F.resize(
                make_input(input_type, spatial_size=self.INPUT_SIZE),
                size=self.OUTPUT_SIZES[0],
                interpolation=interpolation,
            )
742
743
744
745
746
747
748
749
750
751
752
753
754

    @pytest.mark.parametrize("interpolation", INTERPOLATION_MODES)
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.Video],
    )
    def test_interpolation_int(self, interpolation, input_type):
        # `InterpolationMode.NEAREST_EXACT` has no proper corresponding integer equivalent. Internally, we map it to
        # `0` to be the same as `InterpolationMode.NEAREST` for PIL. However, for the tensor backend there is a
        # difference and thus we don't test it here.
        if issubclass(input_type, torch.Tensor) and interpolation is transforms.InterpolationMode.NEAREST_EXACT:
            return

Philip Meier's avatar
Philip Meier committed
755
        input = make_input(input_type, spatial_size=self.INPUT_SIZE)
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

        expected = F.resize(input, size=self.OUTPUT_SIZES[0], interpolation=interpolation, antialias=True)
        actual = F.resize(
            input, size=self.OUTPUT_SIZES[0], interpolation=pil_modes_mapping[interpolation], antialias=True
        )

        assert_equal(actual, expected)

    def test_transform_unknown_size_error(self):
        with pytest.raises(ValueError, match="size can either be an integer or a list or tuple of one or two integers"):
            transforms.Resize(size=object())

    @pytest.mark.parametrize(
        "size", [min(INPUT_SIZE), [min(INPUT_SIZE)], (min(INPUT_SIZE),), list(INPUT_SIZE), tuple(INPUT_SIZE)]
    )
    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    def test_noop(self, size, input_type):
Philip Meier's avatar
Philip Meier committed
776
        input = make_input(input_type, spatial_size=self.INPUT_SIZE)
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797

        output = F.resize(input, size=size, antialias=True)

        # This identity check is not a requirement. It is here to avoid breaking the behavior by accident. If there
        # is a good reason to break this, feel free to downgrade to an equality check.
        if isinstance(input, datapoints._datapoint.Datapoint):
            # We can't test identity directly, since that checks for the identity of the Python object. Since all
            # datapoints unwrap before a kernel and wrap again afterwards, the Python object changes. Thus, we check
            # that the underlying storage is the same
            assert output.data_ptr() == input.data_ptr()
        else:
            assert output is input

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    def test_no_regression_5405(self, input_type):
        # Checks that `max_size` is not ignored if `size == small_edge_size`
        # See https://github.com/pytorch/vision/issues/5405

Philip Meier's avatar
Philip Meier committed
798
        input = make_input(input_type, spatial_size=self.INPUT_SIZE)
799
800
801
802
803
804

        size = min(F.get_spatial_size(input))
        max_size = size + 1
        output = F.resize(input, size=size, max_size=max_size, antialias=True)

        assert max(F.get_spatial_size(output)) == max_size
805
806
807
808
809
810


class TestHorizontalFlip:
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_image_tensor(self, dtype, device):
Philip Meier's avatar
Philip Meier committed
811
        check_kernel(F.horizontal_flip_image_tensor, make_input(torch.Tensor, dtype=dtype, device=device))
812
813
814
815
816

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("dtype", [torch.float32, torch.int64])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_bounding_box(self, format, dtype, device):
Philip Meier's avatar
Philip Meier committed
817
        bounding_box = make_input(datapoints.BoundingBox, dtype=dtype, device=device, format=format)
818
819
820
821
822
823
824
        check_kernel(
            F.horizontal_flip_bounding_box,
            bounding_box,
            format=format,
            spatial_size=bounding_box.spatial_size,
        )

Philip Meier's avatar
Philip Meier committed
825
826
827
    @pytest.mark.parametrize("mask_type", ["segmentation", "detection"])
    def test_kernel_mask(self, mask_type):
        check_kernel(F.horizontal_flip_mask, make_input(datapoints.Mask, mask_type=mask_type))
828
829

    def test_kernel_video(self):
Philip Meier's avatar
Philip Meier committed
830
        check_kernel(F.horizontal_flip_video, make_input(datapoints.Video))
831
832
833
834
835
836
837
838
839
840
841
842
843

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.horizontal_flip_image_tensor),
            (PIL.Image.Image, F.horizontal_flip_image_pil),
            (datapoints.Image, F.horizontal_flip_image_tensor),
            (datapoints.BoundingBox, F.horizontal_flip_bounding_box),
            (datapoints.Mask, F.horizontal_flip_mask),
            (datapoints.Video, F.horizontal_flip_video),
        ],
    )
    def test_dispatcher(self, kernel, input_type):
Philip Meier's avatar
Philip Meier committed
844
        check_dispatcher(F.horizontal_flip, kernel, make_input(input_type))
845
846
847
848

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
Philip Meier's avatar
Philip Meier committed
849
850
851
852
853
854
            (torch.Tensor, F.horizontal_flip_image_tensor),
            (PIL.Image.Image, F.horizontal_flip_image_pil),
            (datapoints.Image, F.horizontal_flip_image_tensor),
            (datapoints.BoundingBox, F.horizontal_flip_bounding_box),
            (datapoints.Mask, F.horizontal_flip_mask),
            (datapoints.Video, F.horizontal_flip_video),
855
856
857
        ],
    )
    def test_dispatcher_signature(self, kernel, input_type):
Philip Meier's avatar
Philip Meier committed
858
        check_dispatcher_signatures_match(F.horizontal_flip, kernel=kernel, input_type=input_type)
859
860
861
862
863
864
865

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform(self, input_type, device):
Philip Meier's avatar
Philip Meier committed
866
        input = make_input(input_type, device=device)
867
868
869
870
871
872
873

        check_transform(transforms.RandomHorizontalFlip, input, p=1)

    @pytest.mark.parametrize(
        "fn", [F.horizontal_flip, transform_cls_to_functional(transforms.RandomHorizontalFlip, p=1)]
    )
    def test_image_correctness(self, fn):
Philip Meier's avatar
Philip Meier committed
874
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903

        actual = fn(image)
        expected = F.to_image_tensor(F.horizontal_flip(F.to_image_pil(image)))

        torch.testing.assert_close(actual, expected)

    def _reference_horizontal_flip_bounding_box(self, bounding_box):
        affine_matrix = np.array(
            [
                [-1, 0, bounding_box.spatial_size[1]],
                [0, 1, 0],
            ],
            dtype="float64" if bounding_box.dtype == torch.float64 else "float32",
        )

        expected_bboxes = reference_affine_bounding_box_helper(
            bounding_box,
            format=bounding_box.format,
            spatial_size=bounding_box.spatial_size,
            affine_matrix=affine_matrix,
        )

        return datapoints.BoundingBox.wrap_like(bounding_box, expected_bboxes)

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize(
        "fn", [F.horizontal_flip, transform_cls_to_functional(transforms.RandomHorizontalFlip, p=1)]
    )
    def test_bounding_box_correctness(self, format, fn):
Philip Meier's avatar
Philip Meier committed
904
        bounding_box = make_input(datapoints.BoundingBox, format=format)
905
906
907
908
909
910
911
912
913
914
915
916

        actual = fn(bounding_box)
        expected = self._reference_horizontal_flip_bounding_box(bounding_box)

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform_noop(self, input_type, device):
Philip Meier's avatar
Philip Meier committed
917
        input = make_input(input_type, device=device)
918
919
920
921
922
923

        transform = transforms.RandomHorizontalFlip(p=0)

        output = transform(input)

        assert_equal(output, input)
Philip Meier's avatar
Philip Meier committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966


class TestAffine:
    _EXHAUSTIVE_TYPE_AFFINE_KWARGS = dict(
        # float, int
        angle=[-10.9, 18],
        # two-list of float, two-list of int, two-tuple of float, two-tuple of int
        translate=[[6.3, -0.6], [1, -3], (16.6, -6.6), (-2, 4)],
        # float
        scale=[0.5],
        # float, int,
        # one-list of float, one-list of int, one-tuple of float, one-tuple of int
        # two-list of float, two-list of int, two-tuple of float, two-tuple of int
        shear=[35.6, 38, [-37.7], [-23], (5.3,), (-52,), [5.4, 21.8], [-47, 51], (-11.2, 36.7), (8, -53)],
        # None
        # two-list of float, two-list of int, two-tuple of float, two-tuple of int
        center=[None, [1.2, 4.9], [-3, 1], (2.5, -4.7), (3, 2)],
    )
    # The special case for shear makes sure we pick a value that is supported while JIT scripting
    _MINIMAL_AFFINE_KWARGS = {
        k: vs[0] if k != "shear" else next(v for v in vs if isinstance(v, list))
        for k, vs in _EXHAUSTIVE_TYPE_AFFINE_KWARGS.items()
    }
    _CORRECTNESS_AFFINE_KWARGS = {
        k: [v for v in vs if v is None or isinstance(v, float) or (isinstance(v, list) and len(v) > 1)]
        for k, vs in _EXHAUSTIVE_TYPE_AFFINE_KWARGS.items()
    }

    _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES = dict(
        degrees=[30, (-15, 20)],
        translate=[None, (0.5, 0.5)],
        scale=[None, (0.75, 1.25)],
        shear=[None, (12, 30, -17, 5), 10, (-5, 12)],
    )
    _CORRECTNESS_TRANSFORM_AFFINE_RANGES = {
        k: next(v for v in vs if v is not None) for k, vs in _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES.items()
    }

    def _check_kernel(self, kernel, input, *args, **kwargs):
        kwargs_ = self._MINIMAL_AFFINE_KWARGS.copy()
        kwargs_.update(kwargs)
        check_kernel(kernel, input, *args, **kwargs_)

Philip Meier's avatar
Philip Meier committed
967
968
969
970
971
972
973
    @param_value_parametrization(
        angle=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["angle"],
        translate=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["translate"],
        shear=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["shear"],
        center=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["center"],
        interpolation=[transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR],
        fill=EXHAUSTIVE_TYPE_FILLS,
Philip Meier's avatar
Philip Meier committed
974
975
976
977
978
    )
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_image_tensor(self, param, value, dtype, device):
        if param == "fill":
Philip Meier's avatar
Philip Meier committed
979
            value = adapt_fill(value, dtype=dtype)
Philip Meier's avatar
Philip Meier committed
980
981
        self._check_kernel(
            F.affine_image_tensor,
Philip Meier's avatar
Philip Meier committed
982
            make_input(torch.Tensor, dtype=dtype, device=device),
Philip Meier's avatar
Philip Meier committed
983
984
985
986
987
988
989
            **{param: value},
            check_scripted_vs_eager=not (param in {"shear", "fill"} and isinstance(value, (int, float))),
            check_cuda_vs_cpu=dict(atol=1, rtol=0)
            if dtype is torch.uint8 and param == "interpolation" and value is transforms.InterpolationMode.BILINEAR
            else True,
        )

Philip Meier's avatar
Philip Meier committed
990
991
992
993
994
    @param_value_parametrization(
        angle=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["angle"],
        translate=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["translate"],
        shear=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["shear"],
        center=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["center"],
Philip Meier's avatar
Philip Meier committed
995
996
997
998
999
    )
    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("dtype", [torch.float32, torch.int64])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_bounding_box(self, param, value, format, dtype, device):
Philip Meier's avatar
Philip Meier committed
1000
        bounding_box = make_input(datapoints.BoundingBox, format=format, dtype=dtype, device=device)
Philip Meier's avatar
Philip Meier committed
1001
1002
        self._check_kernel(
            F.affine_bounding_box,
Philip Meier's avatar
Philip Meier committed
1003
            make_input(datapoints.BoundingBox, format=format, dtype=dtype, device=device),
Philip Meier's avatar
Philip Meier committed
1004
1005
1006
1007
1008
1009
1010
1011
            format=format,
            spatial_size=bounding_box.spatial_size,
            **{param: value},
            check_scripted_vs_eager=not (param == "shear" and isinstance(value, (int, float))),
        )

    @pytest.mark.parametrize("mask_type", ["segmentation", "detection"])
    def test_kernel_mask(self, mask_type):
Philip Meier's avatar
Philip Meier committed
1012
        self._check_kernel(F.affine_mask, make_input(datapoints.Mask, mask_type=mask_type))
Philip Meier's avatar
Philip Meier committed
1013
1014

    def test_kernel_video(self):
Philip Meier's avatar
Philip Meier committed
1015
        self._check_kernel(F.affine_video, make_input(datapoints.Video))
Philip Meier's avatar
Philip Meier committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.affine_image_tensor),
            (PIL.Image.Image, F.affine_image_pil),
            (datapoints.Image, F.affine_image_tensor),
            (datapoints.BoundingBox, F.affine_bounding_box),
            (datapoints.Mask, F.affine_mask),
            (datapoints.Video, F.affine_video),
        ],
    )
    def test_dispatcher(self, kernel, input_type):
Philip Meier's avatar
Philip Meier committed
1029
        check_dispatcher(F.affine, kernel, make_input(input_type), **self._MINIMAL_AFFINE_KWARGS)
Philip Meier's avatar
Philip Meier committed
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.affine_image_tensor),
            (PIL.Image.Image, F.affine_image_pil),
            (datapoints.Image, F.affine_image_tensor),
            (datapoints.BoundingBox, F.affine_bounding_box),
            (datapoints.Mask, F.affine_mask),
            (datapoints.Video, F.affine_video),
        ],
    )
    def test_dispatcher_signature(self, kernel, input_type):
        check_dispatcher_signatures_match(F.affine, kernel=kernel, input_type=input_type)

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform(self, input_type, device):
Philip Meier's avatar
Philip Meier committed
1051
        input = make_input(input_type, device=device)
Philip Meier's avatar
Philip Meier committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

        check_transform(transforms.RandomAffine, input, **self._CORRECTNESS_TRANSFORM_AFFINE_RANGES)

    @pytest.mark.parametrize("angle", _CORRECTNESS_AFFINE_KWARGS["angle"])
    @pytest.mark.parametrize("translate", _CORRECTNESS_AFFINE_KWARGS["translate"])
    @pytest.mark.parametrize("scale", _CORRECTNESS_AFFINE_KWARGS["scale"])
    @pytest.mark.parametrize("shear", _CORRECTNESS_AFFINE_KWARGS["shear"])
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize(
        "interpolation", [transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR]
    )
Philip Meier's avatar
Philip Meier committed
1063
    @pytest.mark.parametrize("fill", CORRECTNESS_FILLS)
Philip Meier's avatar
Philip Meier committed
1064
    def test_functional_image_correctness(self, angle, translate, scale, shear, center, interpolation, fill):
Philip Meier's avatar
Philip Meier committed
1065
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")
Philip Meier's avatar
Philip Meier committed
1066

Philip Meier's avatar
Philip Meier committed
1067
        fill = adapt_fill(fill, dtype=torch.uint8)
Philip Meier's avatar
Philip Meier committed
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

        actual = F.affine(
            image,
            angle=angle,
            translate=translate,
            scale=scale,
            shear=shear,
            center=center,
            interpolation=interpolation,
            fill=fill,
        )
        expected = F.to_image_tensor(
            F.affine(
                F.to_image_pil(image),
                angle=angle,
                translate=translate,
                scale=scale,
                shear=shear,
                center=center,
                interpolation=interpolation,
                fill=fill,
            )
        )

        mae = (actual.float() - expected.float()).abs().mean()
        assert mae < 2 if interpolation is transforms.InterpolationMode.NEAREST else 8

    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize(
        "interpolation", [transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR]
    )
Philip Meier's avatar
Philip Meier committed
1099
    @pytest.mark.parametrize("fill", CORRECTNESS_FILLS)
Philip Meier's avatar
Philip Meier committed
1100
1101
    @pytest.mark.parametrize("seed", list(range(5)))
    def test_transform_image_correctness(self, center, interpolation, fill, seed):
Philip Meier's avatar
Philip Meier committed
1102
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")
Philip Meier's avatar
Philip Meier committed
1103

Philip Meier's avatar
Philip Meier committed
1104
        fill = adapt_fill(fill, dtype=torch.uint8)
Philip Meier's avatar
Philip Meier committed
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

        transform = transforms.RandomAffine(
            **self._CORRECTNESS_TRANSFORM_AFFINE_RANGES, center=center, interpolation=interpolation, fill=fill
        )

        torch.manual_seed(seed)
        actual = transform(image)

        torch.manual_seed(seed)
        expected = F.to_image_tensor(transform(F.to_image_pil(image)))

        mae = (actual.float() - expected.float()).abs().mean()
        assert mae < 2 if interpolation is transforms.InterpolationMode.NEAREST else 8

    def _compute_affine_matrix(self, *, angle, translate, scale, shear, center):
        rot = math.radians(angle)
        cx, cy = center
        tx, ty = translate
        sx, sy = [math.radians(s) for s in ([shear, 0.0] if isinstance(shear, (int, float)) else shear)]

        c_matrix = np.array([[1, 0, cx], [0, 1, cy], [0, 0, 1]])
        t_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]])
        c_matrix_inv = np.linalg.inv(c_matrix)
        rs_matrix = np.array(
            [
                [scale * math.cos(rot), -scale * math.sin(rot), 0],
                [scale * math.sin(rot), scale * math.cos(rot), 0],
                [0, 0, 1],
            ]
        )
        shear_x_matrix = np.array([[1, -math.tan(sx), 0], [0, 1, 0], [0, 0, 1]])
        shear_y_matrix = np.array([[1, 0, 0], [-math.tan(sy), 1, 0], [0, 0, 1]])
        rss_matrix = np.matmul(rs_matrix, np.matmul(shear_y_matrix, shear_x_matrix))
        true_matrix = np.matmul(t_matrix, np.matmul(c_matrix, np.matmul(rss_matrix, c_matrix_inv)))
        return true_matrix

    def _reference_affine_bounding_box(self, bounding_box, *, angle, translate, scale, shear, center):
        if center is None:
            center = [s * 0.5 for s in bounding_box.spatial_size[::-1]]

        affine_matrix = self._compute_affine_matrix(
            angle=angle, translate=translate, scale=scale, shear=shear, center=center
        )
        affine_matrix = affine_matrix[:2, :]

        expected_bboxes = reference_affine_bounding_box_helper(
            bounding_box,
            format=bounding_box.format,
            spatial_size=bounding_box.spatial_size,
            affine_matrix=affine_matrix,
        )

        return expected_bboxes

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("angle", _CORRECTNESS_AFFINE_KWARGS["angle"])
    @pytest.mark.parametrize("translate", _CORRECTNESS_AFFINE_KWARGS["translate"])
    @pytest.mark.parametrize("scale", _CORRECTNESS_AFFINE_KWARGS["scale"])
    @pytest.mark.parametrize("shear", _CORRECTNESS_AFFINE_KWARGS["shear"])
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    def test_functional_bounding_box_correctness(self, format, angle, translate, scale, shear, center):
Philip Meier's avatar
Philip Meier committed
1166
        bounding_box = make_input(datapoints.BoundingBox, format=format)
Philip Meier's avatar
Philip Meier committed
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190

        actual = F.affine(
            bounding_box,
            angle=angle,
            translate=translate,
            scale=scale,
            shear=shear,
            center=center,
        )
        expected = self._reference_affine_bounding_box(
            bounding_box,
            angle=angle,
            translate=translate,
            scale=scale,
            shear=shear,
            center=center,
        )

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize("seed", list(range(5)))
    def test_transform_bounding_box_correctness(self, format, center, seed):
Philip Meier's avatar
Philip Meier committed
1191
        bounding_box = make_input(datapoints.BoundingBox, format=format)
Philip Meier's avatar
Philip Meier committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210

        transform = transforms.RandomAffine(**self._CORRECTNESS_TRANSFORM_AFFINE_RANGES, center=center)

        torch.manual_seed(seed)
        params = transform._get_params([bounding_box])

        torch.manual_seed(seed)
        actual = transform(bounding_box)

        expected = self._reference_affine_bounding_box(bounding_box, **params, center=center)

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize("degrees", _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES["degrees"])
    @pytest.mark.parametrize("translate", _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES["translate"])
    @pytest.mark.parametrize("scale", _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES["scale"])
    @pytest.mark.parametrize("shear", _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES["shear"])
    @pytest.mark.parametrize("seed", list(range(10)))
    def test_transform_get_params_bounds(self, degrees, translate, scale, shear, seed):
Philip Meier's avatar
Philip Meier committed
1211
        image = make_input(torch.Tensor)
Philip Meier's avatar
Philip Meier committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
        height, width = F.get_spatial_size(image)

        transform = transforms.RandomAffine(degrees=degrees, translate=translate, scale=scale, shear=shear)

        torch.manual_seed(seed)
        params = transform._get_params([image])

        if isinstance(degrees, (int, float)):
            assert -degrees <= params["angle"] <= degrees
        else:
            assert degrees[0] <= params["angle"] <= degrees[1]

        if translate is not None:
            width_max = int(round(translate[0] * width))
            height_max = int(round(translate[1] * height))
            assert -width_max <= params["translate"][0] <= width_max
            assert -height_max <= params["translate"][1] <= height_max
        else:
            assert params["translate"] == (0, 0)

        if scale is not None:
            assert scale[0] <= params["scale"] <= scale[1]
        else:
            assert params["scale"] == 1.0

        if shear is not None:
            if isinstance(shear, (int, float)):
                assert -shear <= params["shear"][0] <= shear
                assert params["shear"][1] == 0.0
            elif len(shear) == 2:
                assert shear[0] <= params["shear"][0] <= shear[1]
                assert params["shear"][1] == 0.0
            elif len(shear) == 4:
                assert shear[0] <= params["shear"][0] <= shear[1]
                assert shear[2] <= params["shear"][1] <= shear[3]
        else:
            assert params["shear"] == (0, 0)

    @pytest.mark.parametrize("param", ["degrees", "translate", "scale", "shear", "center"])
    @pytest.mark.parametrize("value", [0, [0], [0, 0, 0]])
    def test_transform_sequence_len_errors(self, param, value):
        if param in {"degrees", "shear"} and not isinstance(value, list):
            return

        kwargs = {param: value}
        if param != "degrees":
            kwargs["degrees"] = 0

        with pytest.raises(
            ValueError if isinstance(value, list) else TypeError, match=f"{param} should be a sequence of length 2"
        ):
            transforms.RandomAffine(**kwargs)

    def test_transform_negative_degrees_error(self):
        with pytest.raises(ValueError, match="If degrees is a single number, it must be positive"):
            transforms.RandomAffine(degrees=-1)

    @pytest.mark.parametrize("translate", [[-1, 0], [2, 0], [-1, 2]])
    def test_transform_translate_range_error(self, translate):
        with pytest.raises(ValueError, match="translation values should be between 0 and 1"):
            transforms.RandomAffine(degrees=0, translate=translate)

    @pytest.mark.parametrize("scale", [[-1, 0], [0, -1], [-1, -1]])
    def test_transform_scale_range_error(self, scale):
        with pytest.raises(ValueError, match="scale values should be positive"):
            transforms.RandomAffine(degrees=0, scale=scale)

    def test_transform_negative_shear_error(self):
        with pytest.raises(ValueError, match="If shear is a single number, it must be positive"):
            transforms.RandomAffine(degrees=0, shear=-1)

    def test_transform_unknown_fill_error(self):
        with pytest.raises(TypeError, match="Got inappropriate fill arg"):
            transforms.RandomAffine(degrees=0, fill="fill")
Philip Meier's avatar
Philip Meier committed
1286
1287
1288
1289
1290
1291


class TestVerticalFlip:
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_image_tensor(self, dtype, device):
Philip Meier's avatar
Philip Meier committed
1292
        check_kernel(F.vertical_flip_image_tensor, make_input(torch.Tensor, dtype=dtype, device=device))
Philip Meier's avatar
Philip Meier committed
1293
1294
1295
1296
1297

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("dtype", [torch.float32, torch.int64])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_bounding_box(self, format, dtype, device):
Philip Meier's avatar
Philip Meier committed
1298
        bounding_box = make_input(datapoints.BoundingBox, dtype=dtype, device=device, format=format)
Philip Meier's avatar
Philip Meier committed
1299
1300
1301
1302
1303
1304
1305
        check_kernel(
            F.vertical_flip_bounding_box,
            bounding_box,
            format=format,
            spatial_size=bounding_box.spatial_size,
        )

Philip Meier's avatar
Philip Meier committed
1306
1307
1308
    @pytest.mark.parametrize("mask_type", ["segmentation", "detection"])
    def test_kernel_mask(self, mask_type):
        check_kernel(F.vertical_flip_mask, make_input(datapoints.Mask, mask_type=mask_type))
Philip Meier's avatar
Philip Meier committed
1309
1310

    def test_kernel_video(self):
Philip Meier's avatar
Philip Meier committed
1311
        check_kernel(F.vertical_flip_video, make_input(datapoints.Video))
Philip Meier's avatar
Philip Meier committed
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.vertical_flip_image_tensor),
            (PIL.Image.Image, F.vertical_flip_image_pil),
            (datapoints.Image, F.vertical_flip_image_tensor),
            (datapoints.BoundingBox, F.vertical_flip_bounding_box),
            (datapoints.Mask, F.vertical_flip_mask),
            (datapoints.Video, F.vertical_flip_video),
        ],
    )
    def test_dispatcher(self, kernel, input_type):
Philip Meier's avatar
Philip Meier committed
1325
        check_dispatcher(F.vertical_flip, kernel, make_input(input_type))
Philip Meier's avatar
Philip Meier committed
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.vertical_flip_image_tensor),
            (PIL.Image.Image, F.vertical_flip_image_pil),
            (datapoints.Image, F.vertical_flip_image_tensor),
            (datapoints.BoundingBox, F.vertical_flip_bounding_box),
            (datapoints.Mask, F.vertical_flip_mask),
            (datapoints.Video, F.vertical_flip_video),
        ],
    )
    def test_dispatcher_signature(self, kernel, input_type):
        check_dispatcher_signatures_match(F.vertical_flip, kernel=kernel, input_type=input_type)

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform(self, input_type, device):
Philip Meier's avatar
Philip Meier committed
1347
        input = make_input(input_type, device=device)
Philip Meier's avatar
Philip Meier committed
1348
1349
1350
1351
1352

        check_transform(transforms.RandomVerticalFlip, input, p=1)

    @pytest.mark.parametrize("fn", [F.vertical_flip, transform_cls_to_functional(transforms.RandomVerticalFlip, p=1)])
    def test_image_correctness(self, fn):
Philip Meier's avatar
Philip Meier committed
1353
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")
Philip Meier's avatar
Philip Meier committed
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380

        actual = fn(image)
        expected = F.to_image_tensor(F.vertical_flip(F.to_image_pil(image)))

        torch.testing.assert_close(actual, expected)

    def _reference_vertical_flip_bounding_box(self, bounding_box):
        affine_matrix = np.array(
            [
                [1, 0, 0],
                [0, -1, bounding_box.spatial_size[0]],
            ],
            dtype="float64" if bounding_box.dtype == torch.float64 else "float32",
        )

        expected_bboxes = reference_affine_bounding_box_helper(
            bounding_box,
            format=bounding_box.format,
            spatial_size=bounding_box.spatial_size,
            affine_matrix=affine_matrix,
        )

        return datapoints.BoundingBox.wrap_like(bounding_box, expected_bboxes)

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("fn", [F.vertical_flip, transform_cls_to_functional(transforms.RandomVerticalFlip, p=1)])
    def test_bounding_box_correctness(self, format, fn):
Philip Meier's avatar
Philip Meier committed
1381
        bounding_box = make_input(datapoints.BoundingBox, format=format)
Philip Meier's avatar
Philip Meier committed
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393

        actual = fn(bounding_box)
        expected = self._reference_vertical_flip_bounding_box(bounding_box)

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform_noop(self, input_type, device):
Philip Meier's avatar
Philip Meier committed
1394
        input = make_input(input_type, device=device)
Philip Meier's avatar
Philip Meier committed
1395
1396
1397
1398
1399
1400

        transform = transforms.RandomVerticalFlip(p=0)

        output = transform(input)

        assert_equal(output, input)
Philip Meier's avatar
Philip Meier committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657


class TestRotate:
    _EXHAUSTIVE_TYPE_AFFINE_KWARGS = dict(
        # float, int
        angle=[-10.9, 18],
        # None
        # two-list of float, two-list of int, two-tuple of float, two-tuple of int
        center=[None, [1.2, 4.9], [-3, 1], (2.5, -4.7), (3, 2)],
    )
    _MINIMAL_AFFINE_KWARGS = {k: vs[0] for k, vs in _EXHAUSTIVE_TYPE_AFFINE_KWARGS.items()}
    _CORRECTNESS_AFFINE_KWARGS = {
        k: [v for v in vs if v is None or isinstance(v, float) or isinstance(v, list)]
        for k, vs in _EXHAUSTIVE_TYPE_AFFINE_KWARGS.items()
    }

    _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES = dict(
        degrees=[30, (-15, 20)],
    )
    _CORRECTNESS_TRANSFORM_AFFINE_RANGES = {k: vs[0] for k, vs in _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES.items()}

    @param_value_parametrization(
        angle=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["angle"],
        interpolation=[transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR],
        expand=[False, True],
        center=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["center"],
        fill=EXHAUSTIVE_TYPE_FILLS,
    )
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_image_tensor(self, param, value, dtype, device):
        kwargs = {param: value}
        if param != "angle":
            kwargs["angle"] = self._MINIMAL_AFFINE_KWARGS["angle"]
        check_kernel(
            F.rotate_image_tensor,
            make_input(torch.Tensor, dtype=dtype, device=device),
            **kwargs,
            check_scripted_vs_eager=not (param == "fill" and isinstance(value, (int, float))),
        )

    @param_value_parametrization(
        angle=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["angle"],
        expand=[False, True],
        center=_EXHAUSTIVE_TYPE_AFFINE_KWARGS["center"],
    )
    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_kernel_bounding_box(self, param, value, format, dtype, device):
        kwargs = {param: value}
        if param != "angle":
            kwargs["angle"] = self._MINIMAL_AFFINE_KWARGS["angle"]

        bounding_box = make_input(datapoints.BoundingBox, dtype=dtype, device=device, format=format)

        check_kernel(
            F.rotate_bounding_box,
            bounding_box,
            format=format,
            spatial_size=bounding_box.spatial_size,
            **kwargs,
        )

    @pytest.mark.parametrize("mask_type", ["segmentation", "detection"])
    def test_kernel_mask(self, mask_type):
        check_kernel(F.rotate_mask, make_input(datapoints.Mask, mask_type=mask_type), **self._MINIMAL_AFFINE_KWARGS)

    def test_kernel_video(self):
        check_kernel(F.rotate_video, make_input(datapoints.Video), **self._MINIMAL_AFFINE_KWARGS)

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.rotate_image_tensor),
            (PIL.Image.Image, F.rotate_image_pil),
            (datapoints.Image, F.rotate_image_tensor),
            (datapoints.BoundingBox, F.rotate_bounding_box),
            (datapoints.Mask, F.rotate_mask),
            (datapoints.Video, F.rotate_video),
        ],
    )
    def test_dispatcher(self, kernel, input_type):
        check_dispatcher(F.rotate, kernel, make_input(input_type), **self._MINIMAL_AFFINE_KWARGS)

    @pytest.mark.parametrize(
        ("input_type", "kernel"),
        [
            (torch.Tensor, F.rotate_image_tensor),
            (PIL.Image.Image, F.rotate_image_pil),
            (datapoints.Image, F.rotate_image_tensor),
            (datapoints.BoundingBox, F.rotate_bounding_box),
            (datapoints.Mask, F.rotate_mask),
            (datapoints.Video, F.rotate_video),
        ],
    )
    def test_dispatcher_signature(self, kernel, input_type):
        check_dispatcher_signatures_match(F.rotate, kernel=kernel, input_type=input_type)

    @pytest.mark.parametrize(
        "input_type",
        [torch.Tensor, PIL.Image.Image, datapoints.Image, datapoints.BoundingBox, datapoints.Mask, datapoints.Video],
    )
    @pytest.mark.parametrize("device", cpu_and_cuda())
    def test_transform(self, input_type, device):
        input = make_input(input_type, device=device)

        check_transform(transforms.RandomRotation, input, **self._CORRECTNESS_TRANSFORM_AFFINE_RANGES)

    @pytest.mark.parametrize("angle", _CORRECTNESS_AFFINE_KWARGS["angle"])
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize(
        "interpolation", [transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR]
    )
    @pytest.mark.parametrize("expand", [False, True])
    @pytest.mark.parametrize("fill", CORRECTNESS_FILLS)
    def test_functional_image_correctness(self, angle, center, interpolation, expand, fill):
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")

        fill = adapt_fill(fill, dtype=torch.uint8)

        actual = F.rotate(image, angle=angle, center=center, interpolation=interpolation, expand=expand, fill=fill)
        expected = F.to_image_tensor(
            F.rotate(
                F.to_image_pil(image), angle=angle, center=center, interpolation=interpolation, expand=expand, fill=fill
            )
        )

        mae = (actual.float() - expected.float()).abs().mean()
        assert mae < 1 if interpolation is transforms.InterpolationMode.NEAREST else 6

    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize(
        "interpolation", [transforms.InterpolationMode.NEAREST, transforms.InterpolationMode.BILINEAR]
    )
    @pytest.mark.parametrize("expand", [False, True])
    @pytest.mark.parametrize("fill", CORRECTNESS_FILLS)
    @pytest.mark.parametrize("seed", list(range(5)))
    def test_transform_image_correctness(self, center, interpolation, expand, fill, seed):
        image = make_input(torch.Tensor, dtype=torch.uint8, device="cpu")

        fill = adapt_fill(fill, dtype=torch.uint8)

        transform = transforms.RandomRotation(
            **self._CORRECTNESS_TRANSFORM_AFFINE_RANGES,
            center=center,
            interpolation=interpolation,
            expand=expand,
            fill=fill,
        )

        torch.manual_seed(seed)
        actual = transform(image)

        torch.manual_seed(seed)
        expected = F.to_image_tensor(transform(F.to_image_pil(image)))

        mae = (actual.float() - expected.float()).abs().mean()
        assert mae < 1 if interpolation is transforms.InterpolationMode.NEAREST else 6

    def _reference_rotate_bounding_box(self, bounding_box, *, angle, expand, center):
        # FIXME
        if expand:
            raise ValueError("This reference currently does not support expand=True")

        if center is None:
            center = [s * 0.5 for s in bounding_box.spatial_size[::-1]]

        a = np.cos(angle * np.pi / 180.0)
        b = np.sin(angle * np.pi / 180.0)
        cx = center[0]
        cy = center[1]
        affine_matrix = np.array(
            [
                [a, b, cx - cx * a - b * cy],
                [-b, a, cy + cx * b - a * cy],
            ],
            dtype="float64" if bounding_box.dtype == torch.float64 else "float32",
        )

        expected_bboxes = reference_affine_bounding_box_helper(
            bounding_box,
            format=bounding_box.format,
            spatial_size=bounding_box.spatial_size,
            affine_matrix=affine_matrix,
        )

        return expected_bboxes

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    @pytest.mark.parametrize("angle", _CORRECTNESS_AFFINE_KWARGS["angle"])
    # TODO: add support for expand=True in the reference
    @pytest.mark.parametrize("expand", [False])
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    def test_functional_bounding_box_correctness(self, format, angle, expand, center):
        bounding_box = make_input(datapoints.BoundingBox, format=format)

        actual = F.rotate(bounding_box, angle=angle, expand=expand, center=center)
        expected = self._reference_rotate_bounding_box(bounding_box, angle=angle, expand=expand, center=center)

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize("format", list(datapoints.BoundingBoxFormat))
    # TODO: add support for expand=True in the reference
    @pytest.mark.parametrize("expand", [False])
    @pytest.mark.parametrize("center", _CORRECTNESS_AFFINE_KWARGS["center"])
    @pytest.mark.parametrize("seed", list(range(5)))
    def test_transform_bounding_box_correctness(self, format, expand, center, seed):
        bounding_box = make_input(datapoints.BoundingBox, format=format)

        transform = transforms.RandomRotation(**self._CORRECTNESS_TRANSFORM_AFFINE_RANGES, expand=expand, center=center)

        torch.manual_seed(seed)
        params = transform._get_params([bounding_box])

        torch.manual_seed(seed)
        actual = transform(bounding_box)

        expected = self._reference_rotate_bounding_box(bounding_box, **params, expand=expand, center=center)

        torch.testing.assert_close(actual, expected)

    @pytest.mark.parametrize("degrees", _EXHAUSTIVE_TYPE_TRANSFORM_AFFINE_RANGES["degrees"])
    @pytest.mark.parametrize("seed", list(range(10)))
    def test_transform_get_params_bounds(self, degrees, seed):
        transform = transforms.RandomRotation(degrees=degrees)

        torch.manual_seed(seed)
        params = transform._get_params([])

        if isinstance(degrees, (int, float)):
            assert -degrees <= params["angle"] <= degrees
        else:
            assert degrees[0] <= params["angle"] <= degrees[1]

    @pytest.mark.parametrize("param", ["degrees", "center"])
    @pytest.mark.parametrize("value", [0, [0], [0, 0, 0]])
    def test_transform_sequence_len_errors(self, param, value):
        if param == "degrees" and not isinstance(value, list):
            return

        kwargs = {param: value}
        if param != "degrees":
            kwargs["degrees"] = 0

        with pytest.raises(
            ValueError if isinstance(value, list) else TypeError, match=f"{param} should be a sequence of length 2"
        ):
            transforms.RandomRotation(**kwargs)

    def test_transform_negative_degrees_error(self):
        with pytest.raises(ValueError, match="If degrees is a single number, it must be positive"):
            transforms.RandomAffine(degrees=-1)

    def test_transform_unknown_fill_error(self):
        with pytest.raises(TypeError, match="Got inappropriate fill arg"):
            transforms.RandomAffine(degrees=0, fill="fill")