-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expand has_any and has_all to also accept check callables (#6447)
* expand has_any and has_all to also accept check callables * add test and fix has_all * add support for simple tensor images to CutMix, MixUp and RandomIoUCrop * remove TODO * remove pythonic syntax sugar * simplify * use concreate examples in test rather than abstract ones * simplify further
- Loading branch information
Showing
4 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import PIL.Image | ||
import pytest | ||
|
||
import torch | ||
|
||
from test_prototype_transforms_functional import make_bounding_box, make_image, make_segmentation_mask | ||
|
||
from torchvision.prototype import features | ||
from torchvision.prototype.transforms._utils import has_all, has_any, is_simple_tensor | ||
from torchvision.prototype.transforms.functional import to_image_pil | ||
|
||
|
||
IMAGE = make_image(color_space=features.ColorSpace.RGB) | ||
BOUNDING_BOX = make_bounding_box(format=features.BoundingBoxFormat.XYXY, image_size=IMAGE.image_size) | ||
SEGMENTATION_MASK = make_segmentation_mask(size=IMAGE.image_size) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("sample", "types", "expected"), | ||
[ | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.BoundingBox,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.SegmentationMask,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.BoundingBox), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.SegmentationMask), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.BoundingBox, features.SegmentationMask), True), | ||
((SEGMENTATION_MASK,), (features.Image, features.BoundingBox), False), | ||
((BOUNDING_BOX,), (features.Image, features.SegmentationMask), False), | ||
((IMAGE,), (features.BoundingBox, features.SegmentationMask), False), | ||
( | ||
(IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), | ||
(features.Image, features.BoundingBox, features.SegmentationMask), | ||
True, | ||
), | ||
((), (features.Image, features.BoundingBox, features.SegmentationMask), False), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (lambda obj: isinstance(obj, features.Image),), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (lambda _: False,), False), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (lambda _: True,), True), | ||
((IMAGE,), (features.Image, PIL.Image.Image, is_simple_tensor), True), | ||
((torch.Tensor(IMAGE),), (features.Image, PIL.Image.Image, is_simple_tensor), True), | ||
((to_image_pil(IMAGE),), (features.Image, PIL.Image.Image, is_simple_tensor), True), | ||
], | ||
) | ||
def test_has_any(sample, types, expected): | ||
assert has_any(sample, *types) is expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("sample", "types", "expected"), | ||
[ | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.BoundingBox,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.SegmentationMask,), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.BoundingBox), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.SegmentationMask), True), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (features.BoundingBox, features.SegmentationMask), True), | ||
( | ||
(IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), | ||
(features.Image, features.BoundingBox, features.SegmentationMask), | ||
True, | ||
), | ||
((BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.BoundingBox), False), | ||
((BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.SegmentationMask), False), | ||
((IMAGE, SEGMENTATION_MASK), (features.BoundingBox, features.SegmentationMask), False), | ||
( | ||
(IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), | ||
(features.Image, features.BoundingBox, features.SegmentationMask), | ||
True, | ||
), | ||
((BOUNDING_BOX, SEGMENTATION_MASK), (features.Image, features.BoundingBox, features.SegmentationMask), False), | ||
((IMAGE, SEGMENTATION_MASK), (features.Image, features.BoundingBox, features.SegmentationMask), False), | ||
((IMAGE, BOUNDING_BOX), (features.Image, features.BoundingBox, features.SegmentationMask), False), | ||
( | ||
(IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), | ||
(lambda obj: isinstance(obj, (features.Image, features.BoundingBox, features.SegmentationMask)),), | ||
True, | ||
), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (lambda _: False,), False), | ||
((IMAGE, BOUNDING_BOX, SEGMENTATION_MASK), (lambda _: True,), True), | ||
], | ||
) | ||
def test_has_all(sample, types, expected): | ||
assert has_all(sample, *types) is expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters