Skip to content

Commit

Permalink
Fixed acceptable and non-acceptable types for Cutmix/Mixup
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed Jul 8, 2022
1 parent 42b4bf3 commit 3ce23ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 21 additions & 1 deletion test/test_prototype_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import pytest
import torch
from common_utils import assert_equal
from test_prototype_transforms_functional import make_images, make_bounding_boxes, make_one_hot_labels
from test_prototype_transforms_functional import (
make_images,
make_bounding_boxes,
make_one_hot_labels,
make_segmentation_masks,
make_label,
)
from torchvision.prototype import transforms, features
from torchvision.transforms.functional import to_pil_image, pil_to_tensor

Expand Down Expand Up @@ -102,6 +108,20 @@ def test_common(self, transform, input):
def test_mixup_cutmix(self, transform, input):
transform(input)

@pytest.mark.parametrize("transform", [transforms.RandomMixup(alpha=1.0), transforms.RandomCutmix(alpha=1.0)])
def test_mixup_cutmix_assertions(self, transform):
for bbox in make_bounding_boxes():
with pytest.raises(TypeError, match="does not support"):
transform(bbox)
break
for mask in make_segmentation_masks():
with pytest.raises(TypeError, match="does not support"):
transform(mask)
break
label = make_label()
with pytest.raises(TypeError, match="does not support"):
transform(label)

@parametrize(
[
(
Expand Down
8 changes: 5 additions & 3 deletions torchvision/prototype/transforms/_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from torchvision.prototype.transforms import Transform, functional as F

from ._transform import _RandomApplyTransform
from ._utils import query_image, get_image_dimensions, has_all
from ._utils import query_image, get_image_dimensions, has_any


class RandomErasing(_RandomApplyTransform):
Expand Down Expand Up @@ -106,8 +106,10 @@ def __init__(self, *, alpha: float) -> None:

def forward(self, *inpts: Any) -> Any:
sample = inpts if len(inpts) > 1 else inpts[0]
if not has_all(sample, features.Image, features.OneHotLabel):
raise TypeError(f"{type(self).__name__}() is only defined for Image's *and* OneHotLabel's.")
if has_any(sample, features.BoundingBox, features.SegmentationMask, features.Label):
raise TypeError(
f"{type(self).__name__}() does not support bounding boxes, segmentation masks and plain labels."
)
return super().forward(sample)

def _mixup_onehotlabel(self, inpt: features.OneHotLabel, lam: float) -> features.OneHotLabel:
Expand Down

0 comments on commit 3ce23ef

Please sign in to comment.