Standard deviation for transforms.Normalize
See original GitHub issueHey,
How did you calculate the standard deviation values for transforms.Normalize? I am getting the same means, but different standard deviations:
import numpy as np
from torchvision import datasets
from torchvision import transforms
transform_train = transforms.Compose([
# transforms.RandomCrop(32, padding=4),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])
trainset = datasets.CIFAR10(root='data', train=True, download=True, transform=transform_train)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=50_000, shuffle=True)
train = train_loader.__iter__().next()[0]
print('Mean: {}'.format(np.mean(train.numpy(), axis=(0, 2, 3))))
# Mean: [ 0.49139765 0.48215759 0.44653141]
print('STD: {}'.format(np.std(train.numpy(), axis=(0, 2, 3))))
# STD: [ 0.24703199 0.24348481 0.26158789]
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to normalize an image with mean and standard deviation?
The Normalize() transform normalizes an image with mean and standard deviation. The torchvision.transforms module provides many important ...
Read more >How To Calculate the Mean and Standard Deviation
The data can be normalized by subtracting the mean (µ) of each feature and a division by the standard deviation (σ). This way,...
Read more >How to normalize images in PyTorch ? - GeeksforGeeks
Normalization in PyTorch is done using torchvision.transforms.Normalize(). This normalizes the tensor image with mean and standard deviation.
Read more >Normalize — Torchvision main documentation - PyTorch
Normalize. class torchvision.transforms.Normalize(mean, std, inplace=False)[source]. Normalize a tensor image with mean and standard deviation.
Read more >Normalizing Images in PyTorch - Sparrow Computing
For each value in an image, torchvision.transforms.Normalize() subtracts the channel mean and divides by the channel standard deviation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
See
get_mean_and_std(dataset)
inutils.py
. It’s OK to use a different std for training.Not really. The way you computed gives the std of the tensor of size [50000, 32, 32]. However, the desired quantity is the “averaged” std across all the images. Also, when computing the std for a single image, degree of freedom should be 1. (See https://github.com/kuangliu/pytorch-cifar/blob/master/utils.py)