question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Standard deviation for transforms.Normalize

See original GitHub issue

Hey,

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:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kuangliucommented, Jul 19, 2017

See get_mean_and_std(dataset) in utils.py. It’s OK to use a different std for training.

0reactions
renyiryrycommented, Aug 9, 2020

Should be [0.2470, 0.2435, 0.2616]. See here.

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found