Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 19 additions & 0 deletions test/test_prototype_datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
from torchvision.prototype import datapoints


@pytest.mark.parametrize(
("data", "input_requires_grad", "expected_requires_grad"),
[
([0.0], None, False),
([0.0], False, False),
([0.0], True, True),
(torch.tensor([0.0], requires_grad=False), None, False),
(torch.tensor([0.0], requires_grad=False), False, False),
(torch.tensor([0.0], requires_grad=False), True, True),
(torch.tensor([0.0], requires_grad=True), None, True),
(torch.tensor([0.0], requires_grad=True), False, False),
(torch.tensor([0.0], requires_grad=True), True, True),
],
)
def test_new_requires_grad(data, input_requires_grad, expected_requires_grad):
datapoint = datapoints.Label(data, requires_grad=input_requires_grad)
assert datapoint.requires_grad is expected_requires_grad


def test_isinstance():
assert isinstance(
datapoints.Label([0, 1, 0], categories=["foo", "bar"]),
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datapoints/_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __new__(
spatial_size: Tuple[int, int],
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> BoundingBox:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)

Expand Down
6 changes: 4 additions & 2 deletions torchvision/prototype/datapoints/_datapoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def _to_tensor(
data: Any,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> torch.Tensor:
if requires_grad is None:
requires_grad = data.requires_grad if isinstance(data, torch.Tensor) else False
return torch.as_tensor(data, dtype=dtype, device=device).requires_grad_(requires_grad)

# FIXME: this is just here for BC with the prototype datasets. Some datasets use the Datapoint directly to have a
Expand All @@ -36,7 +38,7 @@ def __new__(
data: Any,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> Datapoint:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
return tensor.as_subclass(Datapoint)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datapoints/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __new__(
*,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> Image:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
if tensor.ndim < 2:
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datapoints/_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __new__(
categories: Optional[Sequence[str]] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> L:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
return cls._wrap(tensor, categories=categories)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datapoints/_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __new__(
*,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> Mask:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
return cls._wrap(tensor)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datapoints/_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __new__(
*,
dtype: Optional[torch.dtype] = None,
device: Optional[Union[torch.device, str, int]] = None,
requires_grad: bool = False,
requires_grad: Optional[bool] = None,
) -> Video:
tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
if data.ndim < 4:
Expand Down