|
1 | 1 | import functools
|
2 | 2 | import itertools
|
| 3 | +import math |
3 | 4 |
|
| 5 | +import numpy as np |
4 | 6 | import pytest
|
5 | 7 | import torch.testing
|
6 | 8 | import torchvision.prototype.transforms.functional as F
|
7 | 9 | from torch import jit
|
8 | 10 | from torch.nn.functional import one_hot
|
9 | 11 | from torchvision.prototype import features
|
| 12 | +from torchvision.prototype.transforms.functional._meta import convert_bounding_box_format |
10 | 13 | from torchvision.transforms.functional_tensor import _max_value as get_max_value
|
11 | 14 |
|
| 15 | + |
12 | 16 | make_tensor = functools.partial(torch.testing.make_tensor, device="cpu")
|
13 | 17 |
|
14 | 18 |
|
@@ -205,6 +209,45 @@ def resize_bounding_box():
|
205 | 209 | yield SampleInput(bounding_box, size=size, image_size=bounding_box.image_size)
|
206 | 210 |
|
207 | 211 |
|
| 212 | +@register_kernel_info_from_sample_inputs_fn |
| 213 | +def affine_image_tensor(): |
| 214 | + for image, angle, translate, scale, shear in itertools.product( |
| 215 | + make_images(extra_dims=()), |
| 216 | + [-87, 15, 90], # angle |
| 217 | + [5, -5], # translate |
| 218 | + [0.77, 1.27], # scale |
| 219 | + [0, 12], # shear |
| 220 | + ): |
| 221 | + yield SampleInput( |
| 222 | + image, |
| 223 | + angle=angle, |
| 224 | + translate=(translate, translate), |
| 225 | + scale=scale, |
| 226 | + shear=(shear, shear), |
| 227 | + interpolation=F.InterpolationMode.NEAREST, |
| 228 | + ) |
| 229 | + |
| 230 | + |
| 231 | +@register_kernel_info_from_sample_inputs_fn |
| 232 | +def affine_bounding_box(): |
| 233 | + for bounding_box, angle, translate, scale, shear in itertools.product( |
| 234 | + make_bounding_boxes(), |
| 235 | + [-87, 15, 90], # angle |
| 236 | + [5, -5], # translate |
| 237 | + [0.77, 1.27], # scale |
| 238 | + [0, 12], # shear |
| 239 | + ): |
| 240 | + yield SampleInput( |
| 241 | + bounding_box, |
| 242 | + format=bounding_box.format, |
| 243 | + image_size=bounding_box.image_size, |
| 244 | + angle=angle, |
| 245 | + translate=(translate, translate), |
| 246 | + scale=scale, |
| 247 | + shear=(shear, shear), |
| 248 | + ) |
| 249 | + |
| 250 | + |
208 | 251 | @pytest.mark.parametrize(
|
209 | 252 | "kernel",
|
210 | 253 | [
|
@@ -233,3 +276,98 @@ def test_eager_vs_scripted(functional_info, sample_input):
|
233 | 276 | scripted = jit.script(functional_info.functional)(*sample_input.args, **sample_input.kwargs)
|
234 | 277 |
|
235 | 278 | torch.testing.assert_close(eager, scripted)
|
| 279 | + |
| 280 | + |
| 281 | +@pytest.mark.parametrize("angle", range(-90, 90, 36)) |
| 282 | +@pytest.mark.parametrize("translate", range(-10, 10, 5)) |
| 283 | +@pytest.mark.parametrize("scale", [0.77, 1.0, 1.27]) |
| 284 | +@pytest.mark.parametrize("shear", range(-15, 15, 5)) |
| 285 | +@pytest.mark.parametrize("center", [None, (12, 14)]) |
| 286 | +def test_correctness_affine_bounding_box(angle, translate, scale, shear, center): |
| 287 | + def _compute_expected_bbox(bbox, angle_, translate_, scale_, shear_, center_): |
| 288 | + rot = math.radians(angle_) |
| 289 | + cx, cy = center_ |
| 290 | + tx, ty = translate_ |
| 291 | + sx, sy = [math.radians(sh_) for sh_ in shear_] |
| 292 | + |
| 293 | + c_matrix = np.array([[1, 0, cx], [0, 1, cy], [0, 0, 1]]) |
| 294 | + t_matrix = np.array([[1, 0, tx], [0, 1, ty], [0, 0, 1]]) |
| 295 | + c_matrix_inv = np.linalg.inv(c_matrix) |
| 296 | + rs_matrix = np.array( |
| 297 | + [ |
| 298 | + [scale_ * math.cos(rot), -scale_ * math.sin(rot), 0], |
| 299 | + [scale_ * math.sin(rot), scale_ * math.cos(rot), 0], |
| 300 | + [0, 0, 1], |
| 301 | + ] |
| 302 | + ) |
| 303 | + shear_x_matrix = np.array([[1, -math.tan(sx), 0], [0, 1, 0], [0, 0, 1]]) |
| 304 | + shear_y_matrix = np.array([[1, 0, 0], [-math.tan(sy), 1, 0], [0, 0, 1]]) |
| 305 | + rss_matrix = np.matmul(rs_matrix, np.matmul(shear_y_matrix, shear_x_matrix)) |
| 306 | + true_matrix = np.matmul(t_matrix, np.matmul(c_matrix, np.matmul(rss_matrix, c_matrix_inv))) |
| 307 | + true_matrix = true_matrix[:2, :] |
| 308 | + |
| 309 | + bbox_xyxy = convert_bounding_box_format( |
| 310 | + bbox, old_format=bbox.format, new_format=features.BoundingBoxFormat.XYXY |
| 311 | + ) |
| 312 | + points = np.array( |
| 313 | + [ |
| 314 | + [bbox_xyxy[0].item(), bbox_xyxy[1].item(), 1.0], |
| 315 | + [bbox_xyxy[2].item(), bbox_xyxy[1].item(), 1.0], |
| 316 | + [bbox_xyxy[0].item(), bbox_xyxy[3].item(), 1.0], |
| 317 | + [bbox_xyxy[2].item(), bbox_xyxy[3].item(), 1.0], |
| 318 | + ] |
| 319 | + ) |
| 320 | + transformed_points = points @ true_matrix.T |
| 321 | + out_bbox = [ |
| 322 | + np.min(transformed_points[:, 0]), |
| 323 | + np.min(transformed_points[:, 1]), |
| 324 | + np.max(transformed_points[:, 0]), |
| 325 | + np.max(transformed_points[:, 1]), |
| 326 | + ] |
| 327 | + out_bbox = features.BoundingBox( |
| 328 | + out_bbox, format=features.BoundingBoxFormat.XYXY, image_size=(32, 32), dtype=torch.float32 |
| 329 | + ) |
| 330 | + out_bbox = convert_bounding_box_format( |
| 331 | + out_bbox, old_format=features.BoundingBoxFormat.XYXY, new_format=bbox.format |
| 332 | + ) |
| 333 | + return out_bbox |
| 334 | + |
| 335 | + image_size = (32, 32) |
| 336 | + |
| 337 | + for bboxes in make_bounding_boxes( |
| 338 | + image_sizes=[ |
| 339 | + image_size, |
| 340 | + ], |
| 341 | + extra_dims=((4,),), |
| 342 | + ): |
| 343 | + output_bboxes = F.affine_bounding_box( |
| 344 | + bboxes, |
| 345 | + bboxes.format, |
| 346 | + image_size=image_size, |
| 347 | + angle=angle, |
| 348 | + translate=(translate, translate), |
| 349 | + scale=scale, |
| 350 | + shear=(shear, shear), |
| 351 | + center=center, |
| 352 | + ) |
| 353 | + if center is None: |
| 354 | + center = [s // 2 for s in image_size] |
| 355 | + |
| 356 | + bboxes_format = bboxes.format |
| 357 | + bboxes_image_size = bboxes.image_size |
| 358 | + if bboxes.ndim < 2: |
| 359 | + bboxes = [ |
| 360 | + bboxes, |
| 361 | + ] |
| 362 | + |
| 363 | + expected_bboxes = [] |
| 364 | + for bbox in bboxes: |
| 365 | + bbox = features.BoundingBox(bbox, format=bboxes_format, image_size=bboxes_image_size) |
| 366 | + expected_bboxes.append( |
| 367 | + _compute_expected_bbox(bbox, angle, (translate, translate), scale, (shear, shear), center) |
| 368 | + ) |
| 369 | + expected_bboxes = torch.stack(expected_bboxes) |
| 370 | + if expected_bboxes.shape[0] < 2: |
| 371 | + expected_bboxes = expected_bboxes.squeeze(0) |
| 372 | + |
| 373 | + torch.testing.assert_close(output_bboxes, expected_bboxes) |
0 commit comments