-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcompute_mean.py
executable file
·36 lines (30 loc) · 1.04 KB
/
compute_mean.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
#!/usr/bin/env python
import argparse
import os
import sys
import numpy
from PIL import Image
import six.moves.cPickle as pickle
parser = argparse.ArgumentParser(description='Compute images mean array')
parser.add_argument('dataset', help='Path to training image-label list file')
parser.add_argument('--root', '-r', default='.',
help='Root directory path of image files')
parser.add_argument('--output', '-o', default='mean.npy',
help='path to output mean array')
args = parser.parse_args()
sum_image = None
count = 0
for line in open(args.dataset):
filepath = os.path.join(args.root, line.strip().split()[0])
image = numpy.asarray(Image.open(filepath)).transpose(2, 0, 1)
if sum_image is None:
sum_image = numpy.ndarray(image.shape, dtype=numpy.float32)
sum_image[:] = image
else:
sum_image += image
count += 1
sys.stderr.write('\r{}'.format(count))
sys.stderr.flush()
sys.stderr.write('\n')
mean = sum_image / count
pickle.dump(mean, open(args.output, 'wb'), -1)