|
| 1 | +import functools |
| 2 | +import itertools |
| 3 | + |
| 4 | +import pytest |
| 5 | +import torch.testing |
| 6 | +import torchvision.prototype.transforms.functional as F |
| 7 | +from torch import jit |
| 8 | +from torchvision.prototype import features |
| 9 | + |
| 10 | +make_tensor = functools.partial(torch.testing.make_tensor, device="cpu") |
| 11 | + |
| 12 | + |
| 13 | +def make_image(size=None, *, color_space, extra_dims=(), dtype=torch.float32): |
| 14 | + size = size or torch.randint(16, 33, (2,)).tolist() |
| 15 | + |
| 16 | + if isinstance(color_space, str): |
| 17 | + color_space = features.ColorSpace[color_space] |
| 18 | + num_channels = { |
| 19 | + features.ColorSpace.GRAYSCALE: 1, |
| 20 | + features.ColorSpace.RGB: 3, |
| 21 | + }[color_space] |
| 22 | + |
| 23 | + shape = (*extra_dims, num_channels, *size) |
| 24 | + if dtype.is_floating_point: |
| 25 | + data = torch.rand(shape, dtype=dtype) |
| 26 | + else: |
| 27 | + data = torch.randint(0, torch.iinfo(dtype).max, shape, dtype=dtype) |
| 28 | + return features.Image(data, color_space=color_space) |
| 29 | + |
| 30 | + |
| 31 | +make_grayscale_image = functools.partial(make_image, color_space=features.ColorSpace.GRAYSCALE) |
| 32 | +make_rgb_image = functools.partial(make_image, color_space=features.ColorSpace.RGB) |
| 33 | + |
| 34 | + |
| 35 | +def make_images( |
| 36 | + sizes=((16, 16), (7, 33), (31, 9)), |
| 37 | + color_spaces=(features.ColorSpace.GRAYSCALE, features.ColorSpace.RGB), |
| 38 | + dtypes=(torch.float32, torch.uint8), |
| 39 | + extra_dims=((4,), (2, 3)), |
| 40 | +): |
| 41 | + for size, color_space, dtype in itertools.product(sizes, color_spaces, dtypes): |
| 42 | + yield make_image(size, color_space=color_space) |
| 43 | + |
| 44 | + for color_space, extra_dims_ in itertools.product(color_spaces, extra_dims): |
| 45 | + yield make_image(color_space=color_space, extra_dims=extra_dims_) |
| 46 | + |
| 47 | + |
| 48 | +def randint_with_tensor_bounds(arg1, arg2=None, **kwargs): |
| 49 | + low, high = torch.broadcast_tensors( |
| 50 | + *[torch.as_tensor(arg) for arg in ((0, arg1) if arg2 is None else (arg1, arg2))] |
| 51 | + ) |
| 52 | + try: |
| 53 | + return torch.stack( |
| 54 | + [ |
| 55 | + torch.randint(low_scalar, high_scalar, (), **kwargs) |
| 56 | + for low_scalar, high_scalar in zip(low.flatten().tolist(), high.flatten().tolist()) |
| 57 | + ] |
| 58 | + ).reshape(low.shape) |
| 59 | + except RuntimeError as error: |
| 60 | + raise error |
| 61 | + |
| 62 | + |
| 63 | +def make_bounding_box(*, format, image_size=(32, 32), extra_dims=(), dtype=torch.int64): |
| 64 | + if isinstance(format, str): |
| 65 | + format = features.BoundingBoxFormat[format] |
| 66 | + |
| 67 | + height, width = image_size |
| 68 | + |
| 69 | + if format == features.BoundingBoxFormat.XYXY: |
| 70 | + x1 = torch.randint(0, width // 2, extra_dims) |
| 71 | + y1 = torch.randint(0, height // 2, extra_dims) |
| 72 | + x2 = randint_with_tensor_bounds(x1 + 1, width - x1) + x1 |
| 73 | + y2 = randint_with_tensor_bounds(y1 + 1, height - y1) + y1 |
| 74 | + parts = (x1, y1, x2, y2) |
| 75 | + elif format == features.BoundingBoxFormat.XYWH: |
| 76 | + x = torch.randint(0, width // 2, extra_dims) |
| 77 | + y = torch.randint(0, height // 2, extra_dims) |
| 78 | + w = randint_with_tensor_bounds(1, width - x) |
| 79 | + h = randint_with_tensor_bounds(1, height - y) |
| 80 | + parts = (x, y, w, h) |
| 81 | + elif format == features.BoundingBoxFormat.CXCYWH: |
| 82 | + cx = torch.randint(1, width - 1, ()) |
| 83 | + cy = torch.randint(1, height - 1, ()) |
| 84 | + w = randint_with_tensor_bounds(1, torch.minimum(cx, width - cx) + 1) |
| 85 | + h = randint_with_tensor_bounds(1, torch.minimum(cy, width - cy) + 1) |
| 86 | + parts = (cx, cy, w, h) |
| 87 | + else: # format == features.BoundingBoxFormat._SENTINEL: |
| 88 | + raise ValueError() |
| 89 | + |
| 90 | + return features.BoundingBox(torch.stack(parts, dim=-1).to(dtype), format=format, image_size=image_size) |
| 91 | + |
| 92 | + |
| 93 | +make_xyxy_bounding_box = functools.partial(make_bounding_box, format=features.BoundingBoxFormat.XYXY) |
| 94 | + |
| 95 | + |
| 96 | +def make_bounding_boxes( |
| 97 | + formats=(features.BoundingBoxFormat.XYXY, features.BoundingBoxFormat.XYWH, features.BoundingBoxFormat.CXCYWH), |
| 98 | + image_sizes=((32, 32),), |
| 99 | + dtypes=(torch.int64, torch.float32), |
| 100 | + extra_dims=((4,), (2, 3)), |
| 101 | +): |
| 102 | + for format, image_size, dtype in itertools.product(formats, image_sizes, dtypes): |
| 103 | + yield make_bounding_box(format=format, image_size=image_size, dtype=dtype) |
| 104 | + |
| 105 | + for format, extra_dims_ in itertools.product(formats, extra_dims): |
| 106 | + yield make_bounding_box(format=format, extra_dims=extra_dims_) |
| 107 | + |
| 108 | + |
| 109 | +class SampleInput: |
| 110 | + def __init__(self, *args, **kwargs): |
| 111 | + self.args = args |
| 112 | + self.kwargs = kwargs |
| 113 | + |
| 114 | + |
| 115 | +class KernelInfo: |
| 116 | + def __init__(self, name, *, sample_inputs_fn): |
| 117 | + self.name = name |
| 118 | + self.kernel = getattr(F, name) |
| 119 | + self._sample_inputs_fn = sample_inputs_fn |
| 120 | + |
| 121 | + def sample_inputs(self): |
| 122 | + yield from self._sample_inputs_fn() |
| 123 | + |
| 124 | + def __call__(self, *args, **kwargs): |
| 125 | + if len(args) == 1 and not kwargs and isinstance(args[0], SampleInput): |
| 126 | + sample_input = args[0] |
| 127 | + return self.kernel(*sample_input.args, **sample_input.kwargs) |
| 128 | + |
| 129 | + return self.kernel(*args, **kwargs) |
| 130 | + |
| 131 | + |
| 132 | +KERNEL_INFOS = [] |
| 133 | + |
| 134 | + |
| 135 | +def register_kernel_info_from_sample_inputs_fn(sample_inputs_fn): |
| 136 | + KERNEL_INFOS.append(KernelInfo(sample_inputs_fn.__name__, sample_inputs_fn=sample_inputs_fn)) |
| 137 | + return sample_inputs_fn |
| 138 | + |
| 139 | + |
| 140 | +@register_kernel_info_from_sample_inputs_fn |
| 141 | +def horizontal_flip_image(): |
| 142 | + for image in make_images(): |
| 143 | + yield SampleInput(image) |
| 144 | + |
| 145 | + |
| 146 | +@register_kernel_info_from_sample_inputs_fn |
| 147 | +def horizontal_flip_bounding_box(): |
| 148 | + for bounding_box in make_bounding_boxes(formats=[features.BoundingBoxFormat.XYXY]): |
| 149 | + yield SampleInput(bounding_box, image_size=bounding_box.image_size) |
| 150 | + |
| 151 | + |
| 152 | +@register_kernel_info_from_sample_inputs_fn |
| 153 | +def resize_image(): |
| 154 | + for image, interpolation in itertools.product( |
| 155 | + make_images(), |
| 156 | + [ |
| 157 | + F.InterpolationMode.BILINEAR, |
| 158 | + F.InterpolationMode.NEAREST, |
| 159 | + ], |
| 160 | + ): |
| 161 | + height, width = image.shape[-2:] |
| 162 | + for size in [ |
| 163 | + (height, width), |
| 164 | + (int(height * 0.75), int(width * 1.25)), |
| 165 | + ]: |
| 166 | + yield SampleInput(image, size=size, interpolation=interpolation) |
| 167 | + |
| 168 | + |
| 169 | +@register_kernel_info_from_sample_inputs_fn |
| 170 | +def resize_bounding_box(): |
| 171 | + for bounding_box in make_bounding_boxes(): |
| 172 | + height, width = bounding_box.image_size |
| 173 | + for new_image_size in [ |
| 174 | + (height, width), |
| 175 | + (int(height * 0.75), int(width * 1.25)), |
| 176 | + ]: |
| 177 | + yield SampleInput(bounding_box, old_image_size=bounding_box.image_size, new_image_size=new_image_size) |
| 178 | + |
| 179 | + |
| 180 | +class TestKernelsCommon: |
| 181 | + @pytest.mark.parametrize("kernel_info", KERNEL_INFOS, ids=lambda kernel_info: kernel_info.name) |
| 182 | + def test_scriptable(self, kernel_info): |
| 183 | + jit.script(kernel_info.kernel) |
| 184 | + |
| 185 | + @pytest.mark.parametrize( |
| 186 | + ("kernel_info", "sample_input"), |
| 187 | + [ |
| 188 | + pytest.param(kernel_info, sample_input, id=f"{kernel_info.name}-{idx}") |
| 189 | + for kernel_info in KERNEL_INFOS |
| 190 | + for idx, sample_input in enumerate(kernel_info.sample_inputs()) |
| 191 | + ], |
| 192 | + ) |
| 193 | + def test_eager_vs_scripted(self, kernel_info, sample_input): |
| 194 | + eager = kernel_info(sample_input) |
| 195 | + scripted = jit.script(kernel_info.kernel)(*sample_input.args, **sample_input.kwargs) |
| 196 | + |
| 197 | + torch.testing.assert_close(eager, scripted) |
0 commit comments