Skip to content

Commit 32d7591

Browse files
committed
Preserve channels-last memory format in to_image
1 parent 541c083 commit 32d7591

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

test/test_transforms_v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6794,6 +6794,16 @@ def test_functional_error(self):
67946794
with pytest.raises(TypeError, match="Input can either be a pure Tensor, a numpy array, or a PIL image"):
67956795
F.to_image(object())
67966796

6797+
def test_numpy_channels_last_memory_format(self):
6798+
input = np.zeros((224, 224, 3), dtype=np.uint8)
6799+
6800+
output = F.to_image(input)
6801+
6802+
assert output.shape == (3, 224, 224)
6803+
6804+
assert output.unsqueeze(0).is_contiguous(
6805+
memory_format=torch.channels_last
6806+
)
67976807

67986808
class TestToPILImage:
67996809
@pytest.mark.parametrize("make_input", [make_image_tensor, make_image, make_image_numpy])

torchvision/transforms/v2/functional/_type_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tensors.Image:
1212
"""See :class:`~torchvision.transforms.v2.ToImage` for details."""
1313
if isinstance(inpt, np.ndarray):
14-
output = torch.from_numpy(np.atleast_3d(inpt)).permute((2, 0, 1)).contiguous()
14+
output = torch.from_numpy(np.atleast_3d(inpt)).permute((2, 0, 1))
1515
elif isinstance(inpt, PIL.Image.Image):
1616
output = pil_to_tensor(inpt)
1717
elif isinstance(inpt, torch.Tensor):

0 commit comments

Comments
 (0)