Skip to content
Merged
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
4 changes: 4 additions & 0 deletions test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def test_decode_jpeg(img_path, pil_mode, mode):
data = read_file(img_path)
img_ljpeg = decode_image(data, mode=mode)

assert img_ljpeg.is_contiguous()

# Permit a small variation on pixel values to account for implementation
# differences between Pillow and LibJPEG.
abs_mean_diff = (img_ljpeg.type(torch.float32) - img_pil).abs().mean().item()
Expand Down Expand Up @@ -173,6 +175,8 @@ def test_decode_png(img_path, pil_mode, mode):
data = read_file(img_path)
img_lpng = decode_image(data, mode=mode)

assert img_lpng.is_contiguous()

tol = 0 if pil_mode is None else 1

if PILLOW_VERSION >= (8, 3) and pil_mode == "LA":
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/io/image/cpu/decode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return tensor.permute({2, 0, 1});
return tensor.permute({2, 0, 1}).contiguous();
}

#endif
Expand Down
2 changes: 1 addition & 1 deletion torchvision/csrc/io/image/cpu/decode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ torch::Tensor decode_png(
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
return tensor.permute({2, 0, 1});
return tensor.permute({2, 0, 1}).contiguous();
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions torchvision/io/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def write_file(filename: str, data: torch.Tensor) -> None:

def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
"""
Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
Decodes a PNG image into a 3 dimensional RGB or grayscale contiguous Tensor.
Optionally converts the image to the desired format.
The values of the output tensor are uint8 in [0, 255].

Expand Down Expand Up @@ -117,7 +117,7 @@ def decode_jpeg(
input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = "cpu"
) -> torch.Tensor:
"""
Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
Decodes a JPEG image into a 3 dimensional RGB or grayscale contiguous Tensor.
Optionally converts the image to the desired format.
The values of the output tensor are uint8 between 0 and 255.

Expand Down Expand Up @@ -185,7 +185,7 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
"""
Detects whether an image is a JPEG or PNG and performs the appropriate
operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
operation to decode the image into a 3 dimensional RGB or grayscale contiguous Tensor.

Optionally converts the image to the desired format.
The values of the output tensor are uint8 in [0, 255].
Expand All @@ -207,7 +207,7 @@ def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN

def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
"""
Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.
Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale contiguous Tensor.
Optionally converts the image to the desired format.
The values of the output tensor are uint8 in [0, 255].

Expand Down