-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[proto] Added RandomCrop transform and tests #6271
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
Resize, | ||
CenterCrop, | ||
RandomResizedCrop, | ||
RandomCrop, | ||
FiveCrop, | ||
TenCrop, | ||
BatchMultiCrop, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,28 @@ | |
|
||
import PIL.Image | ||
import torch | ||
from torch.utils._pytree import tree_flatten | ||
from torchvision.prototype import features | ||
from torchvision.prototype.utils._internal import query_recursively | ||
|
||
from .functional._meta import get_dimensions_image_tensor, get_dimensions_image_pil | ||
|
||
|
||
def query_image(sample: Any) -> Union[PIL.Image.Image, torch.Tensor, features.Image]: | ||
flat_sample, _ = tree_flatten(sample) | ||
for i in flat_sample: | ||
if type(i) == torch.Tensor or isinstance(i, (PIL.Image.Image, features.Image)): | ||
return i | ||
|
||
raise TypeError("No image was found in the sample") | ||
|
||
|
||
# vfdev-5: let's use tree_flatten instead of query_recursively and internal fn to make the code simplier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's simplify the code using |
||
def query_image_(sample: Any) -> Union[PIL.Image.Image, torch.Tensor, features.Image]: | ||
def fn( | ||
id: Tuple[Any, ...], input: Any | ||
) -> Optional[Tuple[Tuple[Any, ...], Union[PIL.Image.Image, torch.Tensor, features.Image]]]: | ||
if type(input) in {torch.Tensor, features.Image} or isinstance(input, PIL.Image.Image): | ||
if type(input) == torch.Tensor or isinstance(input, (PIL.Image.Image, features.Image)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pmeier I suggest to check for image-like types in the following way. This helps to use mocker with spec type. |
||
return id, input | ||
|
||
return None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we can't just override
_transform
method as we need to access image data and generate params on transformed sample.Proposed solution is to 1) flatten sample structure into a list, 2) apply crop+pad logic, 3) unflatten output into input sample structure