Skip to content

Commit 330301c

Browse files
authored
Merge pull request #1 from bhack/patch-2
Add dummy test
2 parents 9d41c0a + e277308 commit 330301c

13 files changed

+784
-768
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
d367a01a18a3ae6bee13d8be3b63fd6a581ea46f
88
# Upgrade usort to 1.0.2 and black to 22.3.0 (#5106)
99
6ca9c76adb6daf2695d603ad623a9cf1c4f4806f
10+
# Fix unnecessary exploded black formatting (#7709)
11+
a335d916db0694770e8152f41e19195de3134523

test/test_functional_tensor.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,8 @@ def test_translations(self, device, height, width, dt, t, fn):
293293
(33, (5, -4), 1.0, [0.0, 0.0], [0, 0, 0]),
294294
(45, [-5, 4], 1.2, [0.0, 0.0], (1, 2, 3)),
295295
(33, (-4, -8), 2.0, [0.0, 0.0], [255, 255, 255]),
296-
(
297-
85,
298-
(10, -10),
299-
0.7,
300-
[0.0, 0.0],
301-
[
302-
1,
303-
],
304-
),
305-
(
306-
0,
307-
[0, 0],
308-
1.0,
309-
[
310-
35.0,
311-
],
312-
(2.0,),
313-
),
296+
(85, (10, -10), 0.7, [0.0, 0.0], [1]),
297+
(0, [0, 0], 1.0, [35.0], (2.0,)),
314298
(-25, [0, 0], 1.2, [0.0, 15.0], None),
315299
(-45, [-10, 0], 0.7, [2.0, 5.0], None),
316300
(-45, [-10, -10], 1.2, [4.0, 5.0], None),
@@ -392,19 +376,7 @@ def _get_data_dims_and_points_for_perspective():
392376
@pytest.mark.parametrize("device", cpu_and_cuda())
393377
@pytest.mark.parametrize("dims_and_points", _get_data_dims_and_points_for_perspective())
394378
@pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
395-
@pytest.mark.parametrize(
396-
"fill",
397-
(
398-
None,
399-
[0, 0, 0],
400-
[1, 2, 3],
401-
[255, 255, 255],
402-
[
403-
1,
404-
],
405-
(2.0,),
406-
),
407-
)
379+
@pytest.mark.parametrize("fill", (None, [0, 0, 0], [1, 2, 3], [255, 255, 255], [1], (2.0,)))
408380
@pytest.mark.parametrize("fn", [F.perspective, torch.jit.script(F.perspective)])
409381
def test_perspective_pil_vs_tensor(device, dims_and_points, dt, fill, fn):
410382

@@ -475,19 +447,7 @@ def test_perspective_interpolation_type():
475447

476448
@pytest.mark.parametrize("device", cpu_and_cuda())
477449
@pytest.mark.parametrize("dt", [None, torch.float32, torch.float64, torch.float16])
478-
@pytest.mark.parametrize(
479-
"size",
480-
[
481-
32,
482-
26,
483-
[
484-
32,
485-
],
486-
[32, 32],
487-
(32, 32),
488-
[26, 35],
489-
],
490-
)
450+
@pytest.mark.parametrize("size", [32, 26, [32], [32, 32], (32, 32), [26, 35]])
491451
@pytest.mark.parametrize("max_size", [None, 34, 40, 1000])
492452
@pytest.mark.parametrize("interpolation", [BILINEAR, BICUBIC, NEAREST, NEAREST_EXACT])
493453
def test_resize(device, dt, size, max_size, interpolation):

test/test_ops.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import torch.fx
1212
import torch.nn.functional as F
1313
from common_utils import assert_equal, cpu_and_cuda, needs_cuda
14-
from PIL import Image
14+
from PIL import Image, ImageDraw
1515
from torch import nn, Tensor
1616
from torch.autograd import gradcheck
1717
from torch.nn.modules.utils import _pair
@@ -621,6 +621,32 @@ def test_is_leaf_node(self, device):
621621
assert len(graph_node_names[0]) == len(graph_node_names[1])
622622
assert len(graph_node_names[0]) == 1 + op_obj.n_inputs
623623

624+
class TestMasksToBoundaries(ABC):
625+
626+
@pytest.mark.parametrize("device", ['cpu', 'cuda'])
627+
def test_masks_to_boundaries(self, device):
628+
# Create masks
629+
mask = torch.zeros(4, 32, 32, dtype=torch.bool)
630+
mask[0, 1:10, 1:10] = True
631+
mask[0, 12:20, 12:20] = True
632+
mask[0, 15:18, 20:32] = True
633+
mask[1, 15:23, 15:23] = True
634+
mask[1, 22:33, 22:33] = True
635+
mask[2, 1:5, 22:30] = True
636+
mask[2, 5:14, 25:27] = True
637+
pil_img = Image.new("L", (32, 32))
638+
draw = ImageDraw.Draw(pil_img)
639+
draw.ellipse([2, 7, 26, 26], fill=1, outline=1, width=1)
640+
mask[3, ...] = torch.from_numpy(np.asarray(pil_img))
641+
mask = mask.to(device)
642+
dilation_ratio = 0.02
643+
boundaries = ops.masks_to_boundaries(mask, dilation_ratio)
644+
# Generate expected output
645+
# TODO: How we generate handle the expected output?
646+
# replace with actual code to generate expected output
647+
expected_boundaries = torch.zeros_like(mask)
648+
torch.testing.assert_close(expected_boundaries, boundaries)
649+
624650

625651
class TestNMS:
626652
def _reference_nms(self, boxes, scores, iou_threshold):

test/test_transforms_v2.py

Lines changed: 1 addition & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from torch.utils._pytree import tree_flatten, tree_unflatten
3030
from torchvision import datapoints
3131
from torchvision.ops.boxes import box_iou
32-
from torchvision.transforms.functional import InterpolationMode, pil_to_tensor, to_pil_image
32+
from torchvision.transforms.functional import InterpolationMode, to_pil_image
3333
from torchvision.transforms.v2 import functional as F
3434
from torchvision.transforms.v2.utils import check_type, is_simple_tensor, query_chw
3535

@@ -406,112 +406,6 @@ def was_applied(output, inpt):
406406
assert transform.was_applied(output, input)
407407

408408

409-
@pytest.mark.parametrize("p", [0.0, 1.0])
410-
class TestRandomHorizontalFlip:
411-
def input_expected_image_tensor(self, p, dtype=torch.float32):
412-
input = torch.tensor([[[0, 1], [0, 1]], [[1, 0], [1, 0]]], dtype=dtype)
413-
expected = torch.tensor([[[1, 0], [1, 0]], [[0, 1], [0, 1]]], dtype=dtype)
414-
415-
return input, expected if p == 1 else input
416-
417-
def test_simple_tensor(self, p):
418-
input, expected = self.input_expected_image_tensor(p)
419-
transform = transforms.RandomHorizontalFlip(p=p)
420-
421-
actual = transform(input)
422-
423-
assert_equal(expected, actual)
424-
425-
def test_pil_image(self, p):
426-
input, expected = self.input_expected_image_tensor(p, dtype=torch.uint8)
427-
transform = transforms.RandomHorizontalFlip(p=p)
428-
429-
actual = transform(to_pil_image(input))
430-
431-
assert_equal(expected, pil_to_tensor(actual))
432-
433-
def test_datapoints_image(self, p):
434-
input, expected = self.input_expected_image_tensor(p)
435-
transform = transforms.RandomHorizontalFlip(p=p)
436-
437-
actual = transform(datapoints.Image(input))
438-
439-
assert_equal(datapoints.Image(expected), actual)
440-
441-
def test_datapoints_mask(self, p):
442-
input, expected = self.input_expected_image_tensor(p)
443-
transform = transforms.RandomHorizontalFlip(p=p)
444-
445-
actual = transform(datapoints.Mask(input))
446-
447-
assert_equal(datapoints.Mask(expected), actual)
448-
449-
def test_datapoints_bounding_box(self, p):
450-
input = datapoints.BoundingBox([0, 0, 5, 5], format=datapoints.BoundingBoxFormat.XYXY, spatial_size=(10, 10))
451-
transform = transforms.RandomHorizontalFlip(p=p)
452-
453-
actual = transform(input)
454-
455-
expected_image_tensor = torch.tensor([5, 0, 10, 5]) if p == 1.0 else input
456-
expected = datapoints.BoundingBox.wrap_like(input, expected_image_tensor)
457-
assert_equal(expected, actual)
458-
assert actual.format == expected.format
459-
assert actual.spatial_size == expected.spatial_size
460-
461-
462-
@pytest.mark.parametrize("p", [0.0, 1.0])
463-
class TestRandomVerticalFlip:
464-
def input_expected_image_tensor(self, p, dtype=torch.float32):
465-
input = torch.tensor([[[1, 1], [0, 0]], [[1, 1], [0, 0]]], dtype=dtype)
466-
expected = torch.tensor([[[0, 0], [1, 1]], [[0, 0], [1, 1]]], dtype=dtype)
467-
468-
return input, expected if p == 1 else input
469-
470-
def test_simple_tensor(self, p):
471-
input, expected = self.input_expected_image_tensor(p)
472-
transform = transforms.RandomVerticalFlip(p=p)
473-
474-
actual = transform(input)
475-
476-
assert_equal(expected, actual)
477-
478-
def test_pil_image(self, p):
479-
input, expected = self.input_expected_image_tensor(p, dtype=torch.uint8)
480-
transform = transforms.RandomVerticalFlip(p=p)
481-
482-
actual = transform(to_pil_image(input))
483-
484-
assert_equal(expected, pil_to_tensor(actual))
485-
486-
def test_datapoints_image(self, p):
487-
input, expected = self.input_expected_image_tensor(p)
488-
transform = transforms.RandomVerticalFlip(p=p)
489-
490-
actual = transform(datapoints.Image(input))
491-
492-
assert_equal(datapoints.Image(expected), actual)
493-
494-
def test_datapoints_mask(self, p):
495-
input, expected = self.input_expected_image_tensor(p)
496-
transform = transforms.RandomVerticalFlip(p=p)
497-
498-
actual = transform(datapoints.Mask(input))
499-
500-
assert_equal(datapoints.Mask(expected), actual)
501-
502-
def test_datapoints_bounding_box(self, p):
503-
input = datapoints.BoundingBox([0, 0, 5, 5], format=datapoints.BoundingBoxFormat.XYXY, spatial_size=(10, 10))
504-
transform = transforms.RandomVerticalFlip(p=p)
505-
506-
actual = transform(input)
507-
508-
expected_image_tensor = torch.tensor([0, 5, 5, 10]) if p == 1.0 else input
509-
expected = datapoints.BoundingBox.wrap_like(input, expected_image_tensor)
510-
assert_equal(expected, actual)
511-
assert actual.format == expected.format
512-
assert actual.spatial_size == expected.spatial_size
513-
514-
515409
class TestPad:
516410
def test_assertions(self):
517411
with pytest.raises(TypeError, match="Got inappropriate padding arg"):
@@ -721,130 +615,6 @@ def test_boundingbox_spatial_size(self, angle, expand):
721615
assert out_img.spatial_size == out_bbox.spatial_size
722616

723617

724-
class TestRandomAffine:
725-
def test_assertions(self):
726-
with pytest.raises(ValueError, match="is a single number, it must be positive"):
727-
transforms.RandomAffine(-0.7)
728-
729-
for d in [[-0.7], [-0.7, 0, 0.7]]:
730-
with pytest.raises(ValueError, match="degrees should be a sequence of length 2"):
731-
transforms.RandomAffine(d)
732-
733-
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
734-
transforms.RandomAffine(12, fill="abc")
735-
736-
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
737-
transforms.RandomAffine(12, fill="abc")
738-
739-
for kwargs in [
740-
{"center": 12},
741-
{"translate": 12},
742-
{"scale": 12},
743-
]:
744-
with pytest.raises(TypeError, match="should be a sequence of length"):
745-
transforms.RandomAffine(12, **kwargs)
746-
747-
for kwargs in [{"center": [1, 2, 3]}, {"translate": [1, 2, 3]}, {"scale": [1, 2, 3]}]:
748-
with pytest.raises(ValueError, match="should be a sequence of length"):
749-
transforms.RandomAffine(12, **kwargs)
750-
751-
with pytest.raises(ValueError, match="translation values should be between 0 and 1"):
752-
transforms.RandomAffine(12, translate=[-1.0, 2.0])
753-
754-
with pytest.raises(ValueError, match="scale values should be positive"):
755-
transforms.RandomAffine(12, scale=[-1.0, 2.0])
756-
757-
with pytest.raises(ValueError, match="is a single number, it must be positive"):
758-
transforms.RandomAffine(12, shear=-10)
759-
760-
for s in [[-0.7], [-0.7, 0, 0.7]]:
761-
with pytest.raises(ValueError, match="shear should be a sequence of length 2"):
762-
transforms.RandomAffine(12, shear=s)
763-
764-
@pytest.mark.parametrize("degrees", [23, [0, 45], (0, 45)])
765-
@pytest.mark.parametrize("translate", [None, [0.1, 0.2]])
766-
@pytest.mark.parametrize("scale", [None, [0.7, 1.2]])
767-
@pytest.mark.parametrize("shear", [None, 2.0, [5.0, 15.0], [1.0, 2.0, 3.0, 4.0]])
768-
def test__get_params(self, degrees, translate, scale, shear, mocker):
769-
image = mocker.MagicMock(spec=datapoints.Image)
770-
image.num_channels = 3
771-
image.spatial_size = (24, 32)
772-
h, w = image.spatial_size
773-
774-
transform = transforms.RandomAffine(degrees, translate=translate, scale=scale, shear=shear)
775-
params = transform._get_params([image])
776-
777-
if not isinstance(degrees, (list, tuple)):
778-
assert -degrees <= params["angle"] <= degrees
779-
else:
780-
assert degrees[0] <= params["angle"] <= degrees[1]
781-
782-
if translate is not None:
783-
w_max = int(round(translate[0] * w))
784-
h_max = int(round(translate[1] * h))
785-
assert -w_max <= params["translate"][0] <= w_max
786-
assert -h_max <= params["translate"][1] <= h_max
787-
else:
788-
assert params["translate"] == (0, 0)
789-
790-
if scale is not None:
791-
assert scale[0] <= params["scale"] <= scale[1]
792-
else:
793-
assert params["scale"] == 1.0
794-
795-
if shear is not None:
796-
if isinstance(shear, float):
797-
assert -shear <= params["shear"][0] <= shear
798-
assert params["shear"][1] == 0.0
799-
elif len(shear) == 2:
800-
assert shear[0] <= params["shear"][0] <= shear[1]
801-
assert params["shear"][1] == 0.0
802-
else:
803-
assert shear[0] <= params["shear"][0] <= shear[1]
804-
assert shear[2] <= params["shear"][1] <= shear[3]
805-
else:
806-
assert params["shear"] == (0, 0)
807-
808-
@pytest.mark.parametrize("degrees", [23, [0, 45], (0, 45)])
809-
@pytest.mark.parametrize("translate", [None, [0.1, 0.2]])
810-
@pytest.mark.parametrize("scale", [None, [0.7, 1.2]])
811-
@pytest.mark.parametrize("shear", [None, 2.0, [5.0, 15.0], [1.0, 2.0, 3.0, 4.0]])
812-
@pytest.mark.parametrize("fill", [0, [1, 2, 3], (2, 3, 4)])
813-
@pytest.mark.parametrize("center", [None, [2.0, 3.0]])
814-
def test__transform(self, degrees, translate, scale, shear, fill, center, mocker):
815-
interpolation = InterpolationMode.BILINEAR
816-
transform = transforms.RandomAffine(
817-
degrees,
818-
translate=translate,
819-
scale=scale,
820-
shear=shear,
821-
interpolation=interpolation,
822-
fill=fill,
823-
center=center,
824-
)
825-
826-
if isinstance(degrees, (tuple, list)):
827-
assert transform.degrees == [float(degrees[0]), float(degrees[1])]
828-
else:
829-
assert transform.degrees == [float(-degrees), float(degrees)]
830-
831-
fn = mocker.patch("torchvision.transforms.v2.functional.affine")
832-
inpt = mocker.MagicMock(spec=datapoints.Image)
833-
inpt.num_channels = 3
834-
inpt.spatial_size = (24, 32)
835-
836-
# vfdev-5, Feature Request: let's store params as Transform attribute
837-
# This could be also helpful for users
838-
# Otherwise, we can mock transform._get_params
839-
torch.manual_seed(12)
840-
_ = transform(inpt)
841-
torch.manual_seed(12)
842-
params = transform._get_params([inpt])
843-
844-
fill = transforms._utils._convert_fill_arg(fill)
845-
fn.assert_called_once_with(inpt, **params, interpolation=interpolation, fill=fill, center=center)
846-
847-
848618
class TestRandomCrop:
849619
def test_assertions(self):
850620
with pytest.raises(ValueError, match="Please provide only two dimensions"):

0 commit comments

Comments
 (0)