Skip to content

[PoC] reinstate get_params #7095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 17 additions & 2 deletions torchvision/prototype/transforms/_augment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
import numbers
import warnings
from types import SimpleNamespace
from typing import Any, cast, Dict, List, Optional, Tuple, Union

import PIL.Image
Expand Down Expand Up @@ -83,14 +84,28 @@ def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
else:
v = torch.tensor(self.value)[:, None, None]

i = torch.randint(0, img_h - h + 1, size=(1,)).item()
j = torch.randint(0, img_w - w + 1, size=(1,)).item()
i = int(torch.randint(0, img_h - h + 1, size=(1,)))
j = int(torch.randint(0, img_w - w + 1, size=(1,)))
break
else:
i, j, h, w, v = 0, 0, img_h, img_w, None

return dict(i=i, j=j, h=h, w=w, v=v)

@staticmethod
def get_params(
image: torch.Tensor,
scale: Tuple[float, float],
ratio: Tuple[float, float],
value: Optional[List[float]] = None,
) -> Tuple[int, int, int, int, torch.Tensor]:
self = SimpleNamespace(scale=scale, _log_ratio=torch.log(torch.tensor(ratio)), value=value)
params = RandomErasing._get_params(self, [image]) # type: ignore[arg-type]
v = params["v"]
if v is None:
v = image
Comment on lines +105 to +106
Copy link
Collaborator Author

@pmeier pmeier Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v2, this whole transformation is a no-op in case the value is None for performance. In v1 we returned the input image instead and performed an "erasing", by replacing the image with itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #7092 (comment) for a detailed explanation.

return params["i"], params["j"], params["h"], params["w"], v

def _transform(
self, inpt: Union[datapoints.ImageType, datapoints.VideoType], params: Dict[str, Any]
) -> Union[datapoints.ImageType, datapoints.VideoType]:
Expand Down