From 618620c0ed2cb28c121bd62b35793149937b0971 Mon Sep 17 00:00:00 2001 From: siddhantb Date: Sat, 19 Dec 2020 11:48:25 +0530 Subject: [PATCH 1/3] Update ImageReadMode error messages, add newline at the end of image_read_mode.h, replace define with const in image_read_mode.h, add documentation to ImageReadMode enum --- torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp | 2 +- torchvision/csrc/io/image/cpu/readpng_cpu.cpp | 2 +- torchvision/csrc/io/image/image_read_mode.h | 10 +++++----- torchvision/io/image.py | 7 +++++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp b/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp index 7f1a411bc3d..41c334f7747 100644 --- a/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp +++ b/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp @@ -117,7 +117,7 @@ torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) { */ default: jpeg_destroy_decompress(&cinfo); - TORCH_CHECK(false, "Provided mode not supported"); + TORCH_CHECK(false, "Provided mode not supported as the input is JPEG"); } jpeg_calc_output_dimensions(&cinfo); diff --git a/torchvision/csrc/io/image/cpu/readpng_cpu.cpp b/torchvision/csrc/io/image/cpu/readpng_cpu.cpp index adbbe52e097..1b517d801f0 100644 --- a/torchvision/csrc/io/image/cpu/readpng_cpu.cpp +++ b/torchvision/csrc/io/image/cpu/readpng_cpu.cpp @@ -143,7 +143,7 @@ torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) { break; default: png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); - TORCH_CHECK(false, "Provided mode not supported"); + TORCH_CHECK(false, "Provided mode not supported as the input is PNG"); } png_read_update_info(png_ptr, info_ptr); diff --git a/torchvision/csrc/io/image/image_read_mode.h b/torchvision/csrc/io/image/image_read_mode.h index 00ff4f6b581..5df82d373d9 100644 --- a/torchvision/csrc/io/image/image_read_mode.h +++ b/torchvision/csrc/io/image/image_read_mode.h @@ -2,8 +2,8 @@ /* Should be kept in-sync with Python ImageReadMode enum */ using ImageReadMode = int64_t; -#define IMAGE_READ_MODE_UNCHANGED 0 -#define IMAGE_READ_MODE_GRAY 1 -#define IMAGE_READ_MODE_GRAY_ALPHA 2 -#define IMAGE_READ_MODE_RGB 3 -#define IMAGE_READ_MODE_RGB_ALPHA 4 \ No newline at end of file +const ImageReadMode IMAGE_READ_MODE_UNCHANGED = 0; +const ImageReadMode IMAGE_READ_MODE_GRAY = 1; +const ImageReadMode IMAGE_READ_MODE_GRAY_ALPHA = 2; +const ImageReadMode IMAGE_READ_MODE_RGB = 3; +const ImageReadMode IMAGE_READ_MODE_RGB_ALPHA = 4; diff --git a/torchvision/io/image.py b/torchvision/io/image.py index f11a7ccb634..92c27cb09d3 100644 --- a/torchvision/io/image.py +++ b/torchvision/io/image.py @@ -50,6 +50,13 @@ class ImageReadMode(Enum): + """ + Use `ImageReadMode.UNCHANGED` for loading + the image as-is, `ImageReadMode.GRAY` for converting to grayscale, + `ImageReadMode.GRAY_ALPHA` for grayscale with transparency, + `ImageReadMode.RGB` for RGB and `ImageReadMode.RGB_ALPHA` for + RGB with transparency. Default: `ImageReadMode.UNCHANGED` + """ UNCHANGED = 0 GRAY = 1 GRAY_ALPHA = 2 From 044dc181b590df587d8c8742d0a7edfe11d2506d Mon Sep 17 00:00:00 2001 From: siddhantb Date: Sun, 20 Dec 2020 08:39:12 +0530 Subject: [PATCH 2/3] Update readpng_cpu and readjpeg_cpu error messages --- torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp | 2 +- torchvision/csrc/io/image/cpu/readpng_cpu.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp b/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp index 41c334f7747..94121b4e78f 100644 --- a/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp +++ b/torchvision/csrc/io/image/cpu/readjpeg_cpu.cpp @@ -117,7 +117,7 @@ torch::Tensor decodeJPEG(const torch::Tensor& data, ImageReadMode mode) { */ default: jpeg_destroy_decompress(&cinfo); - TORCH_CHECK(false, "Provided mode not supported as the input is JPEG"); + TORCH_CHECK(false, "The provided mode is not supported for JPEG files"); } jpeg_calc_output_dimensions(&cinfo); diff --git a/torchvision/csrc/io/image/cpu/readpng_cpu.cpp b/torchvision/csrc/io/image/cpu/readpng_cpu.cpp index 1b517d801f0..bb2d70b47cd 100644 --- a/torchvision/csrc/io/image/cpu/readpng_cpu.cpp +++ b/torchvision/csrc/io/image/cpu/readpng_cpu.cpp @@ -143,7 +143,7 @@ torch::Tensor decodePNG(const torch::Tensor& data, ImageReadMode mode) { break; default: png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); - TORCH_CHECK(false, "Provided mode not supported as the input is PNG"); + TORCH_CHECK(false, "The provided mode is not supported for PNG files"); } png_read_update_info(png_ptr, info_ptr); From 35a8c5b9695cf63f933e559acfd54525e3ef30e9 Mon Sep 17 00:00:00 2001 From: siddhantb Date: Sun, 20 Dec 2020 17:52:05 +0530 Subject: [PATCH 3/3] Update image.py documentation --- torchvision/io/image.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/torchvision/io/image.py b/torchvision/io/image.py index 92c27cb09d3..6ea4f0c3157 100644 --- a/torchvision/io/image.py +++ b/torchvision/io/image.py @@ -51,11 +51,13 @@ class ImageReadMode(Enum): """ - Use `ImageReadMode.UNCHANGED` for loading - the image as-is, `ImageReadMode.GRAY` for converting to grayscale, + Support for various modes while reading images. + + Use `ImageReadMode.UNCHANGED` for loading the image as-is, + `ImageReadMode.GRAY` for converting to grayscale, `ImageReadMode.GRAY_ALPHA` for grayscale with transparency, `ImageReadMode.RGB` for RGB and `ImageReadMode.RGB_ALPHA` for - RGB with transparency. Default: `ImageReadMode.UNCHANGED` + RGB with transparency. """ UNCHANGED = 0 GRAY = 1 @@ -101,11 +103,9 @@ def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGE input (Tensor[1]): a one dimensional uint8 tensor containing the raw bytes of the PNG image. mode (ImageReadMode): the read mode used for optionally - converting the image. Use `ImageReadMode.UNCHANGED` for loading - the image as-is, `ImageReadMode.GRAY` for converting to grayscale, - `ImageReadMode.GRAY_ALPHA` for grayscale with transparency, - `ImageReadMode.RGB` for RGB and `ImageReadMode.RGB_ALPHA` for - RGB with transparency. Default: `ImageReadMode.UNCHANGED` + converting the image. Default: `ImageReadMode.UNCHANGED`. + See `ImageReadMode` class for more information on various + available modes. Returns: output (Tensor[image_channels, image_height, image_width]) @@ -166,9 +166,9 @@ def decode_jpeg(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANG input (Tensor[1]): a one dimensional uint8 tensor containing the raw bytes of the JPEG image. mode (ImageReadMode): the read mode used for optionally - converting the image. Use `ImageReadMode.UNCHANGED` for loading - the image as-is, `ImageReadMode.GRAY` for converting to grayscale - and `ImageReadMode.RGB` for RGB. Default: `ImageReadMode.UNCHANGED` + converting the image. Default: `ImageReadMode.UNCHANGED`. + See `ImageReadMode` class for more information on various + available modes. Returns: output (Tensor[image_channels, image_height, image_width]) @@ -236,11 +236,10 @@ def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN a one dimensional uint8 tensor containing the raw bytes of the PNG or JPEG image. mode: ImageReadMode - the read mode used for optionally converting the image. JPEG - and PNG images have different permitted values. The default - value is `ImageReadMode.UNCHANGED` and it keeps the image as-is. - See `decode_jpeg()` and `decode_png()` for more information. - Default: `ImageReadMode.UNCHANGED` + the read mode used for optionally converting the image. + Default: `ImageReadMode.UNCHANGED`. + See `ImageReadMode` class for more information on various + available modes. Returns ------- @@ -261,11 +260,10 @@ def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torc path: str path of the JPEG or PNG image. mode: ImageReadMode - the read mode used for optionally converting the image. JPEG - and PNG images have different permitted values. The default - value is `ImageReadMode.UNCHANGED` and it keeps the image as-is. - See `decode_jpeg()` and `decode_png()` for more information. - Default: `ImageReadMode.UNCHANGED` + the read mode used for optionally converting the image. + Default: `ImageReadMode.UNCHANGED`. + See `ImageReadMode` class for more information on various + available modes. Returns -------