Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions torchvision/prototype/transforms/_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ def __init__(self, *, alpha: float, p: float = 0.5) -> None:

def forward(self, *inpts: Any) -> Any:
sample = inpts if len(inpts) > 1 else inpts[0]
if not (
has_any(sample, features.Image, PIL.Image.Image, is_simple_tensor) and has_any(sample, features.OneHotLabel)
):
raise TypeError(f"{type(self).__name__}() is only defined for Image's *and* OneHotLabel's.")
if not (has_any(sample, features.Image, is_simple_tensor) and has_any(sample, features.OneHotLabel)):
raise TypeError(f"{type(self).__name__}() is only defined for tensor images and one-hot labels.")
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."
Expand All @@ -123,12 +121,16 @@ def _get_params(self, sample: Any) -> Dict[str, Any]:

def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
lam = params["lam"]
if isinstance(inpt, features.Image):
if isinstance(inpt, features.Image) or is_simple_tensor(inpt):
if inpt.ndim < 4:
raise ValueError("Need a batch of images")
output = inpt.clone()
output = output.roll(1, -4).mul_(1 - lam).add_(output.mul_(lam))
return features.Image.new_like(inpt, output)

if isinstance(inpt, features.Image):
output = features.Image.new_like(inpt, output)

return output
elif isinstance(inpt, features.OneHotLabel):
return self._mixup_onehotlabel(inpt, lam)
else:
Expand Down Expand Up @@ -159,15 +161,19 @@ def _get_params(self, sample: Any) -> Dict[str, Any]:
return dict(box=box, lam_adjusted=lam_adjusted)

def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:
if isinstance(inpt, features.Image):
if isinstance(inpt, features.Image) or is_simple_tensor(inpt):
box = params["box"]
if inpt.ndim < 4:
raise ValueError("Need a batch of images")
x1, y1, x2, y2 = box
image_rolled = inpt.roll(1, -4)
output = inpt.clone()
output[..., y1:y2, x1:x2] = image_rolled[..., y1:y2, x1:x2]
return features.Image.new_like(inpt, output)

if isinstance(inpt, features.Image):
output = features.Image.new_like(inpt, output)

return output
elif isinstance(inpt, features.OneHotLabel):
lam_adjusted = params["lam_adjusted"]
return self._mixup_onehotlabel(inpt, lam_adjusted)
Expand Down