forked from lRomul/argus-freesound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacking_predict.py
124 lines (96 loc) · 3.43 KB
/
stacking_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
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
import numpy as np
import pandas as pd
from scipy.stats.mstats import gmean
from src.predictor import Predictor
from src.transforms import get_transforms
from src.utils import get_best_model_path
from src.datasets import get_test_data
from src import config
from src.stacking.predictor import StackPredictor
NAME = "stacking_008"
EXPERIMENTS = [
'auxiliary_016',
'auxiliary_019',
'corr_noisy_003',
'corr_noisy_004',
'corr_noisy_007',
'corrections_002',
'corrections_003'
]
STACKING_EXPERIMENTS = [
'stacking_008_fcnet_43040',
'stacking_008_fcnet_45041',
'stacking_008_fcnet_50013'
]
DEVICE = 'cuda'
CROP_SIZE = 256
BATCH_SIZE = 16
STACK_BATCH_SIZE = 256
TILE_STEP = 2
def pred_test(predictor, images_lst):
pred_lst = []
for image in images_lst:
pred = predictor.predict(image)
pred = pred.mean(axis=0)
pred_lst.append(pred)
preds = np.stack(pred_lst, axis=0)
return preds
def experiment_pred(experiment_dir, images_lst):
print(f"Start predict: {experiment_dir}")
transforms = get_transforms(False, CROP_SIZE)
pred_lst = []
for fold in config.folds:
print("Predict fold", fold)
fold_dir = experiment_dir / f'fold_{fold}'
model_path = get_best_model_path(fold_dir)
print("Model path", model_path)
predictor = Predictor(model_path, transforms,
BATCH_SIZE,
(config.audio.n_mels, CROP_SIZE),
(config.audio.n_mels, CROP_SIZE//TILE_STEP),
device=DEVICE)
pred = pred_test(predictor, images_lst)
pred_lst.append(pred)
preds = gmean(pred_lst, axis=0)
return preds
def stacking_pred(experiment_dir, stack_probs):
print(f"Start predict: {experiment_dir}")
pred_lst = []
for fold in config.folds:
print("Predict fold", fold)
fold_dir = experiment_dir / f'fold_{fold}'
model_path = get_best_model_path(fold_dir)
print("Model path", model_path)
predictor = StackPredictor(model_path, STACK_BATCH_SIZE,
device=DEVICE)
pred = predictor.predict(stack_probs)
pred_lst.append(pred)
preds = gmean(pred_lst, axis=0)
return preds
if __name__ == "__main__":
print("Name", NAME)
print("Experiments", EXPERIMENTS)
print("Stacking experiments", STACKING_EXPERIMENTS)
print("Device", DEVICE)
print("Crop size", CROP_SIZE)
print("Batch size", BATCH_SIZE)
print("Stacking batch size", STACK_BATCH_SIZE)
print("Tile step", TILE_STEP)
fname_lst, images_lst = get_test_data()
exp_pred_lst = []
for experiment in EXPERIMENTS:
experiment_dir = config.experiments_dir / experiment
exp_pred = experiment_pred(experiment_dir, images_lst)
exp_pred_lst.append(exp_pred)
stack_probs = np.concatenate(exp_pred_lst, axis=1)
stack_pred_lst = []
for experiment in STACKING_EXPERIMENTS:
experiment_dir = config.experiments_dir / experiment
stack_pred = stacking_pred(experiment_dir, stack_probs)
stack_pred_lst.append(stack_pred)
stack_pred = gmean(exp_pred_lst + stack_pred_lst, axis=0)
stack_pred_df = pd.DataFrame(data=stack_pred,
index=fname_lst,
columns=config.classes)
stack_pred_df.index.name = 'fname'
stack_pred_df.to_csv('submission.csv')