-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
executable file
·66 lines (54 loc) · 1.95 KB
/
predict.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
import numpy as np
from scipy.special import softmax
from pathlib import Path
from sklearn.preprocessing import LabelEncoder
import sys
models_ = sys.argv[1:]
assert len(models_) > 0
for create_for in ['test', 'submission']:
nr_folds = 4
id = 'models_[' + '_and_'.join(models_) +']'
le = LabelEncoder().fit(['airport',
'bus',
'metro',
'metro_station',
'park',
'public_square',
'shopping_mall',
'street_pedestrian',
'street_traffic',
'tram'])
files = []
for m in models_:
p = Path('.').glob('./data/tmp/{}/*{}_best.npy'.format(m, create_for))
f = [x for x in sorted(p) if x.is_file()]
if len(f) != 4:
print(m)
assert False
files.append(f)
print('# nr models: {}'.format(len(files)))
predictions = []
for i, model in enumerate(files):
for file in model:
predictions.append(softmax(np.load(file), axis=1))
prediction = np.mean(predictions, axis=0)
filenames = np.load(Path('./data/tmp/{}_filenames.npy'.format(create_for)))
print('# samples to predict: {}'.format(len(filenames)))
if create_for == 'test':
csv = [('Id', 'Scene_label')]
delimiter = ','
filename_ = '{}'
elif create_for == 'submission':
csv = []
delimiter = '\t'
filename_ = 'audio/{}.wav'
prediction = np.argmax(prediction, axis=1)
for name, label in zip(filenames, le.inverse_transform(prediction)):
csv.append((filename_.format(name.split('/')[-1]), label))
np.savetxt(
Path('./data/tmp/predictions_' + id + '_'+ create_for + '.csv'),
csv,
delimiter=delimiter,
fmt="%s"
)
print('# length of csv: {}'.format(len(csv)))