Skip to content

[proto] Fixes Transform._transformed_types and torch.Tensor #6487

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

Merged
merged 6 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion test/test_prototype_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,18 @@ def test_random_resized_crop(self, transform, input):
)
]
)
def test_convertolor_space(self, transform, input):
def test_convert_color_space(self, transform, input):
transform(input)

def test_convert_color_space_unsupported_types(self):
transform = transforms.ConvertColorSpace(
color_space=features.ColorSpace.RGB, old_color_space=features.ColorSpace.GRAY
)

for inpt in [make_bounding_box(format="XYXY"), make_segmentation_mask()]:
output = transform(inpt)
assert output is inpt


@pytest.mark.parametrize("p", [0.0, 1.0])
class TestRandomHorizontalFlip:
Expand Down
12 changes: 11 additions & 1 deletion torchvision/prototype/transforms/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
from torch import nn
from torch.utils._pytree import tree_flatten, tree_unflatten
from torchvision.prototype.features import _Feature
from torchvision.prototype.transforms._utils import is_simple_tensor
from torchvision.utils import _log_api_usage_once


def _isinstance(obj: Any, types: Tuple[Type, ...]) -> bool:
has_tensor = torch.Tensor in types
if not has_tensor:
return isinstance(obj, types)
types_ = tuple(t for t in types if t != torch.Tensor)
return isinstance(obj, types_) or is_simple_tensor(obj)


class Transform(nn.Module):

# Class attribute defining transformed types. Other types are passed-through without any transformation
Expand All @@ -31,7 +40,8 @@ def forward(self, *inputs: Any) -> Any:

flat_inputs, spec = tree_flatten(sample)
flat_outputs = [
self._transform(inpt, params) if isinstance(inpt, self._transformed_types) else inpt for inpt in flat_inputs
self._transform(inpt, params) if _isinstance(inpt, self._transformed_types) else inpt
for inpt in flat_inputs
]
return tree_unflatten(flat_outputs, spec)

Expand Down