-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_folds.py
51 lines (41 loc) · 1.62 KB
/
make_folds.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
#!/usr/bin/env python
import argparse
import pickle
import random
'''Extracts data from flat files and stores it in more coherent pickles for haptic and audio features.
Does not perform image feature extraction, but instead writes a list of image paths to be processed
on-the-fly during model use.'''
def main():
# Load parameters from command line.
nb_objects = FLAGS_nb_objects
outfile = FLAGS_outfile
props = [float(p) for p in FLAGS_props.split(',')]
assert nb_objects >= len(props)
# Shuffle object IDs and divide.
oidxs = range(nb_objects)
random.shuffle(oidxs)
last = 0
last_idx = 0
fold_obs = []
for idx in range(0, len(props)):
nb_obs_c = props[idx] * nb_objects
next_idx = int(last + nb_obs_c + 0.5)
fold_obs.append(oidxs[last_idx:next_idx])
print "fold " + str(idx) + " got " + str(next_idx-last_idx) + " objects: " + str(fold_obs[-1])
last += nb_obs_c
last_idx = next_idx
# Write fold -> oidxs to file.
with open(outfile, 'wb') as f:
pickle.dump(fold_obs, f)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--nb_objects', type=int, required=True,
help="number of objects to divide")
parser.add_argument('--props', type=str, required=True,
help="comma separated proportions")
parser.add_argument('--outfile', type=str, required=True,
help="place to write list of object ids per fold")
args = parser.parse_args()
for k, v in vars(args).items():
globals()['FLAGS_%s' % k] = v
main()