Skip to content
Merged
Changes from 2 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
36 changes: 19 additions & 17 deletions torchvision/prototype/transforms/_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ def __init__(self, alpha: float, p: float = 0.5) -> None:
self._dist = torch.distributions.Beta(torch.tensor([alpha]), torch.tensor([alpha]))

def forward(self, *inputs: Any) -> Any:
if not (has_any(inputs, features.Image, features.is_simple_tensor) and has_any(inputs, features.OneHotLabel)):
raise TypeError(f"{type(self).__name__}() is only defined for tensor images and one-hot labels.")
if not (has_any(inputs, features.Image, features.Video, features.is_simple_tensor) and has_any(inputs, features.OneHotLabel)):
raise TypeError(f"{type(self).__name__}() is only defined for tensor images/videos and one-hot labels.")
if has_any(inputs, PIL.Image.Image, features.BoundingBox, features.Mask, features.Label):
raise TypeError(
f"{type(self).__name__}() does not support PIL images, bounding boxes, masks and plain labels."
)
return super().forward(*inputs)

def _mixup_onehotlabel(self, inpt: features.OneHotLabel, lam: float) -> features.OneHotLabel:
if inpt.ndim < 2:
if inpt.ndim != 2:
raise ValueError("Need a batch of one hot labels")
output = inpt.clone()
output = output.roll(1, -2).mul_(1 - lam).add_(output.mul_(lam))
output = output.roll(1, 0).mul_(1.0 - lam).add_(output.mul_(lam))
return features.OneHotLabel.wrap_like(inpt, output)


Expand All @@ -129,14 +129,15 @@ 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) or features.is_simple_tensor(inpt):
if inpt.ndim < 4:
raise ValueError("Need a batch of images")
if isinstance(inpt, (features.Image, features.Video)) or features.is_simple_tensor(inpt):
expected_dim = 5 if isinstance(inpt, features.Video) else 4
Comment thread
datumbox marked this conversation as resolved.
Outdated
if inpt.ndim != expected_dim:
Comment thread
datumbox marked this conversation as resolved.
Outdated
raise ValueError("The transform expects a batched input")
output = inpt.clone()
output = output.roll(1, -4).mul_(1 - lam).add_(output.mul_(lam))
output = output.roll(1, 0).mul_(1.0 - lam).add_(output.mul_(lam))
Comment thread
datumbox marked this conversation as resolved.

if isinstance(inpt, features.Image):
output = features.Image.wrap_like(inpt, output)
if isinstance(inpt, (features.Image, features.Video)):
output = type(inpt).wrap_like(inpt, output)

return output
elif isinstance(inpt, features.OneHotLabel):
Expand Down Expand Up @@ -169,17 +170,18 @@ 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) or features.is_simple_tensor(inpt):
if isinstance(inpt, (features.Image, features.Video)) or features.is_simple_tensor(inpt):
box = params["box"]
if inpt.ndim < 4:
raise ValueError("Need a batch of images")
expected_dim = 5 if isinstance(inpt, features.Video) else 4
if inpt.ndim != expected_dim:
raise ValueError("The transform expects a batched input")
Comment thread
pmeier marked this conversation as resolved.
Outdated
x1, y1, x2, y2 = box
image_rolled = inpt.roll(1, -4)
rolled = inpt.roll(1, 0)
output = inpt.clone()
output[..., y1:y2, x1:x2] = image_rolled[..., y1:y2, x1:x2]
output[..., y1:y2, x1:x2] = rolled[..., y1:y2, x1:x2]

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

return output
elif isinstance(inpt, features.OneHotLabel):
Expand Down