Skip to content

Commit 71584c0

Browse files
authored
Merge branch 'main' into kpt_gall
2 parents 47c3d9f + 85b7858 commit 71584c0

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

test/test_image.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def test_decode_jpeg(img_path, pil_mode, mode):
9494
data = read_file(img_path)
9595
img_ljpeg = decode_image(data, mode=mode)
9696

97+
assert img_ljpeg.is_contiguous()
98+
9799
# Permit a small variation on pixel values to account for implementation
98100
# differences between Pillow and LibJPEG.
99101
abs_mean_diff = (img_ljpeg.type(torch.float32) - img_pil).abs().mean().item()
@@ -173,6 +175,8 @@ def test_decode_png(img_path, pil_mode, mode):
173175
data = read_file(img_path)
174176
img_lpng = decode_image(data, mode=mode)
175177

178+
assert img_lpng.is_contiguous()
179+
176180
tol = 0 if pil_mode is None else 1
177181

178182
if PILLOW_VERSION >= (8, 3) and pil_mode == "LA":

torchvision/csrc/io/image/cpu/decode_jpeg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
148148

149149
jpeg_finish_decompress(&cinfo);
150150
jpeg_destroy_decompress(&cinfo);
151-
return tensor.permute({2, 0, 1});
151+
return tensor.permute({2, 0, 1}).contiguous();
152152
}
153153

154154
#endif

torchvision/csrc/io/image/cpu/decode_png.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ torch::Tensor decode_png(
224224
}
225225
}
226226
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
227-
return tensor.permute({2, 0, 1});
227+
return tensor.permute({2, 0, 1}).contiguous();
228228
}
229229
#endif
230230

torchvision/io/image.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def write_file(filename: str, data: torch.Tensor) -> None:
5959

6060
def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
6161
"""
62-
Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
62+
Decodes a PNG image into a 3 dimensional RGB or grayscale contiguous Tensor.
6363
Optionally converts the image to the desired format.
6464
The values of the output tensor are uint8 in [0, 255].
6565
@@ -117,7 +117,7 @@ def decode_jpeg(
117117
input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = "cpu"
118118
) -> torch.Tensor:
119119
"""
120-
Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
120+
Decodes a JPEG image into a 3 dimensional RGB or grayscale contiguous Tensor.
121121
Optionally converts the image to the desired format.
122122
The values of the output tensor are uint8 between 0 and 255.
123123
@@ -185,7 +185,7 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
185185
def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
186186
"""
187187
Detects whether an image is a JPEG or PNG and performs the appropriate
188-
operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
188+
operation to decode the image into a 3 dimensional RGB or grayscale contiguous Tensor.
189189
190190
Optionally converts the image to the desired format.
191191
The values of the output tensor are uint8 in [0, 255].
@@ -207,7 +207,7 @@ def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN
207207

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

0 commit comments

Comments
 (0)