forked from NVlabs/dlinputs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dli-testsplit2
executable file
·64 lines (53 loc) · 1.73 KB
/
dli-testsplit2
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
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
import argparse
import hashlib
import imp
import pickle
import dlinputs as dli
from dlinputs import filters, gopen, tarrecords, utils
parser = argparse.ArgumentParser("Split a dataset into training and test sets.")
parser.add_argument("-n", "--nohash", action="store_true")
parser.add_argument("-s", "--split", type=int, default=10)
parser.add_argument("-m", "--mod", type=int, default=0)
parser.add_argument("-M", "--maxcount", type=int, default=1e6)
parser.add_argument("-S", "--maxsize", type=int, default=1e9)
parser.add_argument("-k", "--key", default="__key__")
parser.add_argument("input")
parser.add_argument("training")
parser.add_argument("testing")
args = parser.parse_args()
source = gopen.sharditerator_once(args.input, decode=False)
def hash(x):
assert x is not None
if isinstance(x, buffer):
x = str(buffer)
if not isinstance(x, str):
x = pickle.dumps(x, -1)
h = hashlib.md5()
h.update(x)
return int(h.hexdigest()[-7:], 16)
def openout(fname):
if "%" in fname:
return tarrecords.ShardWriter(fname, maxcount=args.maxcount, maxsize=args.maxsize, encode=False)
else:
return tarrecords.TarWriter(fname, encode=False)
training = openout(args.training)
testing = openout(args.testing)
num_training = 0
num_testing = 0
for i, sample in enumerate(source):
if i%1000==0:
print i
key = sample.get(args.key)
assert key is not None
h = i if args.nohash else hash(key)
match = (h % args.split == args.mod)
if match:
testing.write(sample)
num_testing += 1
else:
training.write(sample)
num_training += 1
testing.close()
training.close()
print "#testing", num_testing, "#training", num_training, "total", i