-
Notifications
You must be signed in to change notification settings - Fork 2
/
compute_mean_std.py
51 lines (39 loc) · 1.34 KB
/
compute_mean_std.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""computes mean and std of images in a folder
"""
import os
import torch
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import ImageFolder
from tqdm import tqdm
def compute_mean_std(folder, n_channels):
"""[summary]
Args:
folder ([type]): [description]
n_channels ([type]): [description]
"""
dataset = ImageFolder(folder,
transform=transforms.ToTensor())
full_loader = DataLoader(dataset, shuffle=False,
num_workers=0)
dset_mean = torch.zeros(n_channels)
dset_std = torch.zeros(n_channels)
print('==> Computing mean and std..')
for inputs, _ in tqdm(full_loader):
for i in range(n_channels):
dset_mean[i] += inputs[:, i, :, :].mean()
dset_std[i] += inputs[:, i, :, :].std()
dset_mean.div_(len(dataset))
dset_std.div_(len(dataset))
print(f'\nmean: {dset_mean}')
print(f'std: {dset_std}')
return dset_mean, dset_std
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-fin', '--folder_in')
parser.add_argument('-nchan', '--number_of_channels')
args = parser.parse_args()
compute_mean_std(args.folder_in,
args.number_of_channels,
)