Denormalize option in torchvision.utils.save_image()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 17.9k
- Forks
- 7.3k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 13
Description
🚀 Feature
A function to denormalize an image based on mean and standard deviation.
Motivation
When working with images on NN's trained on a specific dataset (for example ImageNet), an image is first normalized to the mean and standard deviation of that dataset. When we want to save such an image later in the process we can use the function torchvision.utils.save_image(). However the image is still normalized and will have a different mean and standard deviation compared to the original image. There is no option to denormalize such an image such that the initial normalization is undone and the saved image has the same mean and std.
Pitch
A extra parameter to the torchvision.utils.save_image() function to denormalize an image based on a mean and standardization array.
Alternatives
One way to tackle the problem currently is to use the transforms.Normalize() function. My current implementation is shown below. One flaw of this implementation is that the image has to be clipped to keep the values between 0 and 1. Thus some information is lost. I am not sure how to do this operation lossless.
def img_denorm(img, mean, std):
#for ImageNet the mean and std are:
#mean = np.asarray([ 0.485, 0.456, 0.406 ])
#std = np.asarray([ 0.229, 0.224, 0.225 ])
denormalize = transforms.Normalize((-1 * mean / std), (1.0 / std))
res = img.squeeze(0)
res = denormalize(res)
#Image needs to be clipped since the denormalize function will map some
#values below 0 and above 1
res = torch.clamp(res, 0, 1)
return(res)
cc @vfdev-5
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting torchvision.utils.save_image() and transforms.Normalize(), then trace the existing image-saving tests or entry points. Define the denormalization parameter behavior, including mean and standard deviation handling and values outside the image range; done means the API is documented and covered by tests for normalized inputs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- computer-vision, machine-learning
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100