Skip to content

Instantly share code, notes, and snippets.

@weiaicunzai
Created June 22, 2018 13:48
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weiaicunzai/e623931921efefd4c331622c344d8151 to your computer and use it in GitHub Desktop.
Save weiaicunzai/e623931921efefd4c331622c344d8151 to your computer and use it in GitHub Desktop.
compute cifar100 mean and std
def compute_mean_std(cifar100_dataset):
"""compute the mean and std of cifar100 dataset
Args:
cifar100_training_dataset or cifar100_test_dataset
witch derived from class torch.utils.data
Returns:
a tuple contains mean, std value of entire dataset
"""
data_r = numpy.dstack([cifar100_dataset[i][1][:, :, 0] for i in range(len(cifar100_dataset))])
data_g = numpy.dstack([cifar100_dataset[i][1][:, :, 1] for i in range(len(cifar100_dataset))])
data_b = numpy.dstack([cifar100_dataset[i][1][:, :, 2] for i in range(len(cifar100_dataset))])
mean = numpy.mean(data_r), numpy.mean(data_g), numpy.mean(data_b)
std = numpy.std(data_r), numpy.std(data_g), numpy.std(data_b)
return mean, std
@weiaicunzai
Copy link
Author

mean = {
'cifar10': (0.4914, 0.4822, 0.4465),
'cifar100': (0.5071, 0.4867, 0.4408),
}

std = {
'cifar10': (0.2023, 0.1994, 0.2010),
'cifar100': (0.2675, 0.2565, 0.2761),
}

@wenwei202
Copy link

std output of cifar10 is wrong. Should be [0.2470, 0.2435, 0.2616]. Check here and code here.
Cifar100 is good.

$ python get_mean_std.py --dataset CIFAR10
==> Preparing data..
Files already downloaded and verified
50000 training samples.
tensor(0.) tensor(1.)
mean: tensor([0.4914, 0.4822, 0.4465])
std: tensor([0.2470, 0.2435, 0.2616])
Done!

$ python get_mean_std.py --dataset CIFAR100
==> Preparing data..
Files already downloaded and verified
50000 training samples.
tensor(0.) tensor(1.)
mean: tensor([0.5071, 0.4865, 0.4409])
std: tensor([0.2673, 0.2564, 0.2762])
Done!

$ python get_mean_std.py --dataset SVHN
==> Preparing data..
Using downloaded and verified file: ./data-SVHN/train_32x32.mat
73257 training samples.
tensor(0.) tensor(1.)
mean: tensor([0.4377, 0.4438, 0.4728])
std: tensor([0.1980, 0.2010, 0.1970])
Done!

$ python get_mean_std.py --dataset FashionMNIST
==> Preparing data..
60000 training samples.
tensor(0.) tensor(1.)
mean: tensor([0.2860])
std: tensor([0.3530])
Done!

$ python get_mean_std.py --dataset MNIST
==> Preparing data..
60000 training samples.
tensor(0.) tensor(1.)
mean: tensor([0.1307])
std: tensor([0.3081])
Done!

@williamFalcon
Copy link

Do you have one for stl-10 and imagenet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment