forked from gbrlfaria/rune-breaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_dataset.py
121 lines (81 loc) · 3.5 KB
/
make_dataset.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import argparse
import os
import random
import re
import sys
sys.path.insert(0, os.path.abspath('.'))
import colorful as cf
import numpy as np
import pandas as pd
import common
TRAINING_SET_RATIO = 0.9
VALIDATION_SET_RATIO = 0.5
def main(training_set_ratio):
common.create_directories()
arrows = pd.DataFrame(
np.zeros((3, 4), dtype=np.int32),
index=('round', 'wide', 'narrow'),
columns=('down', 'left', 'right', 'up')
)
images = [(p, f) for p, f in common.get_files(common.SAMPLES_DIR) if f[-5] != 'F']
if images:
for _, filename in images:
arrow_direction, arrow_type = common.arrow_labels(filename)
arrows[arrow_direction][arrow_type] += 1
num_samples = int(arrows.min().min() * training_set_ratio)
print("Samples per type: {}".format(num_samples * 4))
for t, _ in arrows.iterrows():
print("\nProcessing {} arrows...".format(t))
for direction in arrows:
candidates = [(p, f) for p, f in images if common.arrow_labels(f) == (direction, t)]
print("{}: {}".format(direction, len(candidates)))
training = random.sample(candidates, num_samples)
for path, filename in training:
dst_dir = common.TRAINING_DIR + direction + '/'
os.rename(path, dst_dir + filename)
os.rename(flipped(path), dst_dir + flipped(filename))
candidates = [c for c in candidates if c not in training]
validation = random.sample(
candidates, int(len(candidates) * VALIDATION_SET_RATIO)
)
for path, filename in validation:
dst_dir = common.VALIDATION_DIR + direction + '/'
os.rename(path, dst_dir + filename)
os.rename(flipped(path), dst_dir + flipped(filename))
testing = [c for c in candidates if c not in validation]
for path, filename in testing:
dst_dir = common.TESTING_DIR + direction + '/'
os.rename(path, dst_dir + filename)
os.rename(flipped(path), dst_dir + flipped(filename))
show_summary()
print("\nFinished!")
def flipped(s):
return s[:-4] + 'F' + s[-4:]
def show_summary():
print("\n" + cf.skyBlue("Training set"))
print(get_summary_matrix(common.TRAINING_DIR))
print("\n" + cf.salmon("Validation set"))
print(get_summary_matrix(common.VALIDATION_DIR))
print("\n" + cf.lightGreen("Testing set"))
print(get_summary_matrix(common.TESTING_DIR))
def get_summary_matrix(directory):
matrix = pd.DataFrame(
np.zeros((4, 5), dtype=np.int32),
index=('round', 'wide', 'narrow', 'total'),
columns=('down', 'left', 'right', 'up', 'total')
)
images = common.get_files(directory)
for _, filename in images:
arrow_direction, arrow_type = common.arrow_labels(filename)
matrix[arrow_direction][arrow_type] += 1
matrix['total'][arrow_type] += 1
matrix[arrow_direction]['total'] += 1
matrix['total']['total'] += 1
return matrix
if __name__ == "__main__":
os.system('color')
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--ratio', type=float, default=TRAINING_SET_RATIO,
help="Specifies the training/validation set proportion")
args = parser.parse_args()
main(args.ratio)