From 87c55dca635dc123607549bbde8da02f515b12c3 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 11 May 2021 19:53:21 +0530 Subject: [PATCH 1/2] Fix io imports and expose methods --- docs/source/io.rst | 10 ++++++++++ torchvision/io/__init__.py | 2 ++ torchvision/io/image.py | 32 ++++++++++++++++---------------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/docs/source/io.rst b/docs/source/io.rst index e85951e719a..2e416469d17 100644 --- a/docs/source/io.rst +++ b/docs/source/io.rst @@ -59,14 +59,24 @@ Example of inspecting a video: Image ----- +.. autoclass:: ImageReadMode + .. autofunction:: read_image .. autofunction:: decode_image .. autofunction:: encode_jpeg +.. autofunction:: decode_jpeg + .. autofunction:: write_jpeg .. autofunction:: encode_png +.. autofunction:: decode_png + .. autofunction:: write_png + +.. autofunction:: read_file + +.. autofunction:: write_file diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index fe4cc81ccd7..742344e6b0f 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -17,6 +17,7 @@ write_video, ) from .image import ( + ImageReadMode, decode_image, decode_jpeg, decode_png, @@ -186,6 +187,7 @@ def set_current_stream(self, stream: str): "_read_video_meta_data", "VideoMetaData", "Timebase", + "ImageReadMode", "decode_image", "decode_jpeg", "decode_png", diff --git a/torchvision/io/image.py b/torchvision/io/image.py index 399a6f0d1ac..e789e49820e 100644 --- a/torchvision/io/image.py +++ b/torchvision/io/image.py @@ -52,10 +52,10 @@ class ImageReadMode(Enum): """ 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 + 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. """ UNCHANGED = 0 @@ -102,7 +102,7 @@ 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. Default: `ImageReadMode.UNCHANGED`. + converting the image. Default: ``ImageReadMode.UNCHANGED``. See `ImageReadMode` class for more information on various available modes. @@ -139,7 +139,7 @@ def write_png(input: torch.Tensor, filename: str, compression_level: int = 6): Args: input (Tensor[channels, image_height, image_width]): int8 image tensor of - `c` channels, where `c` must be 1 or 3. + ``c`` channels, where ``c`` must be 1 or 3. filename (str): Path to save the image. compression_level (int): Compression factor for the resulting file, it must be a number between 0 and 9. Default: 6 @@ -160,12 +160,12 @@ def decode_jpeg(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANG the raw bytes of the JPEG image. This tensor must be on CPU, regardless of the ``device`` parameter. mode (ImageReadMode): the read mode used for optionally - converting the image. Default: `ImageReadMode.UNCHANGED`. - See `ImageReadMode` class for more information on various + converting the image. Default: ``ImageReadMode.UNCHANGED``. + See ``ImageReadMode`` class for more information on various available modes. device (str or torch.device): The device on which the decoded image will be stored. If a cuda device is specified, the image will be decoded - with `nvjpeg `_. This is only + with ``nvjpeg `_. This is only supported for CUDA version >= 10.1 Returns: @@ -186,7 +186,7 @@ def encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor: Args: input (Tensor[channels, image_height, image_width])): int8 image tensor of - `c` channels, where `c` must be 1 or 3. + ``c`` channels, where ``c`` must be 1 or 3. quality (int): Quality of the resulting JPEG file, it must be a number between 1 and 100. Default: 75 @@ -207,8 +207,8 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75): Takes an input tensor in CHW layout and saves it in a JPEG file. Args: - input (Tensor[channels, image_height, image_width]): int8 image tensor of `c` - channels, where `c` must be 1 or 3. + input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c`` + channels, where ``c`` must be 1 or 3. filename (str): Path to save the image. quality (int): Quality of the resulting JPEG file, it must be a number between 1 and 100. Default: 75 @@ -229,8 +229,8 @@ def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN input (Tensor): 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. - Default: `ImageReadMode.UNCHANGED`. - See `ImageReadMode` class for more information on various + Default: ``ImageReadMode.UNCHANGED``. + See ``ImageReadMode`` class for more information on various available modes. Returns: @@ -249,8 +249,8 @@ def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torc Args: path (str): path of the JPEG or PNG image. mode (ImageReadMode): the read mode used for optionally converting the image. - Default: `ImageReadMode.UNCHANGED`. - See `ImageReadMode` class for more information on various + Default: ``ImageReadMode.UNCHANGED``. + See ``ImageReadMode`` class for more information on various available modes. Returns: From 612eb78b4b47cc29c8fc5fdc48a42b0ee527afd6 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 11 May 2021 20:05:36 +0530 Subject: [PATCH 2/2] fix fmt --- torchvision/io/image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/io/image.py b/torchvision/io/image.py index e789e49820e..4f824abad60 100644 --- a/torchvision/io/image.py +++ b/torchvision/io/image.py @@ -120,7 +120,7 @@ def encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor: Args: input (Tensor[channels, image_height, image_width]): int8 image tensor of - `c` channels, where `c` must 3 or 1. + ``c`` channels, where ``c`` must 3 or 1. compression_level (int): Compression factor for the resulting file, it must be a number between 0 and 9. Default: 6 @@ -165,7 +165,7 @@ def decode_jpeg(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANG available modes. device (str or torch.device): The device on which the decoded image will be stored. If a cuda device is specified, the image will be decoded - with ``nvjpeg `_. This is only + with `nvjpeg `_. This is only supported for CUDA version >= 10.1 Returns: