-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_data.py
152 lines (104 loc) · 4.47 KB
/
prepare_data.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
Module to preprocess the CLaMM data:
- create a train/dev split from the training data and preprocess these
- preprocess the test images
"""
from __future__ import print_function
import os
import shutil
SEED = 1066987
import random
import numpy as np
np.random.seed(SEED)
random.seed(SEED)
from sklearn.cross_validation import train_test_split
def train_dev_split(input_dir = 'data/CLaMM_train',
metafile = 'meta.csv',
target_classes = None,
test_size = 0.1,
random_state = 863863,
output_path = None):
print('Creating train / dev split for data from:', input_dir)
if target_classes:
print('\t-> restricting to classes:', target_classes)
filenames, categories = [], []
for line in open(os.sep.join((input_dir, metafile)), 'r'):
line = line.strip()
if line and not line.startswith('FileName'):
filename, category = line.split(';')
category = category.lower().replace('_', '-')
if target_classes:
if category not in target_classes:
continue
if not os.path.exists(os.sep.join((input_dir, filename))):
raise ValueError('%s not found!' % (filename))
filenames.append(filename)
categories.append(category)
print('categories:', sorted(set(categories)))
assert len(filenames) == len(categories)
print('\t-> splitting', len(filenames), 'items in total')
train_fns, dev_fns, train_categories, dev_categories = \
train_test_split(filenames, categories,
test_size=test_size,
random_state=random_state,
stratify=categories)
print('\n# train images:', len(train_fns))
print('# dev images:', len(dev_fns))
os.mkdir(os.sep.join((output_path, 'train')))
os.mkdir(os.sep.join((output_path, 'dev')))
for fn, category in zip(train_fns, train_categories):
in_ = os.sep.join((input_dir, fn))
out_ = os.sep.join((output_path, 'train', category + '_' + fn))
shutil.copyfile(in_, out_)
for fn, category in zip(dev_fns, dev_categories):
in_ = os.sep.join((input_dir, fn))
out_ = os.sep.join((output_path, 'dev', category + '_' + fn))
shutil.copyfile(in_, out_)
def prepare_test_data(input_dir = 'data/CLaMM_test',
metafile = 'meta.csv',
target_classes = None,
output_path = None):
print('Preprocessing test data available under:', input_dir)
if target_classes:
print('\t-> restricting to classes:', target_classes)
filenames, categories = [], []
for line in open(os.sep.join((input_dir, metafile)), 'r'):
line = line.strip()
if line and not line.startswith('FileName'):
filename, category = line.split(';')
category = category.lower().replace('_', '-')
if target_classes:
if category not in target_classes:
continue
if not os.path.exists(os.sep.join((input_dir, filename))):
raise ValueError('%s not found!' % (filename))
filenames.append(filename)
categories.append(category)
print('categories:', sorted(set(categories)))
assert len(filenames) == len(categories)
print('\n# test images:', len(filenames))
os.mkdir(os.sep.join((output_path, 'test')))
for fn, category in zip(filenames, categories):
in_ = os.sep.join((input_dir, fn))
out_ = os.sep.join((output_path, 'test', category + '_' + fn))
shutil.copyfile(in_, out_)
if __name__ == '__main__':
# which classes?
target_classes = None # e.g. ['caroline', 'textualis']
# create path for the train, dev, test splits
output_path = os.path.dirname(os.path.realpath(__file__))+'/data/splits'
try:
shutil.rmtree(output_path)
except:
pass
os.mkdir(output_path)
# create train-dev split:
train_dev_split(input_dir='data/CLaMM_train',
test_size = .1,
random_state = SEED,
output_path = output_path,
target_classes = target_classes)
# preprocess test data:
prepare_test_data(input_dir='data/CLaMM_test',
output_path = output_path,
target_classes = target_classes)