-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateSplitData.py
75 lines (65 loc) · 2.04 KB
/
GenerateSplitData.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import os, shutil
from sklearn.model_selection import train_test_split
"""
Format of
Data
RawData
ClassifiedData
hit
miss
SplitData
train
hit
miss
test
hit
miss
validation
hit
miss
Generate SplitData
GenerateSplitData.py:
- creates the folder hierarchy found under SplitData above if the folders do not already exist
- copies over files from ClassifiedData to populate SplitData
- adds the ratio of files to train, test, and validation folders when it copies over the files
"""
train_size = .6
test_size = .2
validation_size = .2
assert train_size + test_size + validation_size == 1
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
split_dir = os.path.join(base_dir, 'SplitData')
class_dir = os.path.join(base_dir, 'ClassifiedData')
folders = ('train', 'test', 'val')
classes = ('hit', 'miss')
def safe_mkdir(path):
"""More safely runs mkdir only if path doesn't already exist."""
if os.path.exists(path):
#print('Cannot make path' + path + '. Path already exists.')
pass
else:
os.mkdir(path)
for folder in folders:
print(" OMFGGGG " + os.path.join(split_dir, folder))
safe_mkdir(os.path.join(split_dir, folder))
for c in classes:
safe_mkdir(os.path.join(split_dir, folder, c))
print("Done making subdirectories.")
split = {}
for c in classes:
lst_hit = os.listdir(os.path.join(class_dir, c))
train, test = train_test_split(lst_hit, shuffle = True, train_size = train_size)
test, val = train_test_split(test, train_size = test_size / (1 - train_size))
split[c] = {'train': train, 'test': test, 'val':val}
print("Done generating split.")
for folder in folders:
for c in classes:
for fname in split[c][folder]:
source = os.path.join(class_dir, c, fname)
destination = os.path.join(split_dir, folder, c, fname)
shutil.copyfile(source, destination)
print("Done populating subdirectories.")