Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6794,6 +6794,16 @@ def test_functional_error(self):
with pytest.raises(TypeError, match="Input can either be a pure Tensor, a numpy array, or a PIL image"):
F.to_image(object())

def test_numpy_channels_last_memory_format(self):
input = np.zeros((224, 224, 3), dtype=np.uint8)

output = F.to_image(input)

assert output.shape == (3, 224, 224)

assert output.unsqueeze(0).is_contiguous(
memory_format=torch.channels_last
)

class TestToPILImage:
@pytest.mark.parametrize("make_input", [make_image_tensor, make_image, make_image_numpy])
Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/v2/functional/_type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tensors.Image:
"""See :class:`~torchvision.transforms.v2.ToImage` for details."""
if isinstance(inpt, np.ndarray):
output = torch.from_numpy(np.atleast_3d(inpt)).permute((2, 0, 1)).contiguous()
output = torch.from_numpy(np.atleast_3d(inpt)).permute((2, 0, 1))
elif isinstance(inpt, PIL.Image.Image):
output = pil_to_tensor(inpt)
elif isinstance(inpt, torch.Tensor):
Expand Down