pytorch / pytorch/vision

Issue in save_image utility in torch vision.utils

Open
#6,255 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🐛 Describe the bug

The make_grid utility here - https://github.com/pytorch/vision/blob/main/torchvision/utils.py#L24 -

  1. The normalize option here - https://github.com/pytorch/vision/blob/main/torchvision/utils.py#L43 - shifts the image to 0 and 1 only if normalize = True, which by default is False.
  2. Then in the save_image function here - https://github.com/pytorch/vision/blob/main/torchvision/utils.py#L138 - the images are being multiplied by 255 irrespective of weather the normalize argument is passed or not.

The issue -

  1. Only when normalize = True should we multiply by 255 since if the values aren't between 0 and 1, then the multiplication by 255 will not make sense since the values can overflow above 255 which can cause problems depending on which file format is being used to save the PIL.Image. Also, 255 is very specific to the PNG format. If one wants to save the image as a TIFF file say, then the TIFF format takes care of floating point values for pixels as well (this could be made a feature).

The solution according to me -

  1. if condition in save_image to multiply by 255 only when normalize = True.
  2. 255 should only be multiplied if the Image is being saved as a .png image
Versions

Collecting environment information...
PyTorch version: 1.11.0
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: CentOS Linux release 7.9.2009 (Core) (x86_64)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Clang version: Could not collect
CMake version: version 2.8.12.2
Libc version: glibc-2.17

Python version: 3.8.13 (default, Mar 28 2022, 11:38:47) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-3.10.0-514.26.2.el7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 2080 Ti
GPU 1: NVIDIA GeForce RTX 2080 Ti

Nvidia driver version: 470.74
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.5
[pip3] numpydoc==1.2
[pip3] torch==1.11.0
[pip3] torchaudio==0.11.0
[pip3] torchvision==0.12.0
[conda] blas 1.0 mkl
[conda] cudatoolkit 11.3.1 h2bc3f7f_2
[conda] ffmpeg 4.3 hf484d3e_0 pytorch
[conda] mkl 2021.4.0 h06a4308_640
[conda] mkl-service 2.4.0 py38h7f8727e_0
[conda] mkl_fft 1.3.1 py38hd3c417c_0
[conda] mkl_random 1.2.2 py38h51133e4_0
[conda] numpy 1.21.5 py38he7a7128_1
[conda] numpy-base 1.21.5 py38hf524024_1
[conda] numpydoc 1.2 pyhd3eb1b0_0
[conda] pytorch 1.11.0 py3.8_cuda11.3_cudnn8.2.0_0 pytorch
[conda] pytorch-mutex 1.0 cuda pytorch
[conda] torchaudio 0.11.0 py38_cu113 pytorch
[conda] torchvision 0.12.0 py38_cu113 pytorch

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in torchvision/utils.py at make_grid and save_image, then reproduce the reported behavior with normalized and unnormalized image values. Clarify the intended scaling and file-format behavior, and consider the existing callers before changing it. Done means save_image handles both normalization states without unintended overflow and the expected PNG/TIFF behavior is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.