forked from alexander-rakhlin/ICIAR2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossvalidate_blending.py
212 lines (191 loc) · 9.03 KB
/
crossvalidate_blending.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""Do cross-validation, output and dump statistics in data/roc_scores.pkl,
data/conf_mx.pkl, submission/crossvalidation.csv"""
import pickle
from os.path import join
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from collections import defaultdict
from utils import load_data
import pandas as pd
import argparse
PREDS_DIR = "predictions/"
N_FOLDS = 10
N_SEEDS = 5
DEFAULT_N_CLASSES = 4
USE_PREDICTIONS = True
LGBM_MODELS_ROOT = "models/LGBMs"
DEFAULT_PREPROCESSED_ROOT = "data/preprocessed/train/"
with open("data/folds-10.pkl", "rb") as f:
folds = pickle.load(f)
AUGMENTATIONS_PER_IMAGE = 50
models = [
"ResNet-0.5-400",
"ResNet-0.5-650",
"VGG-0.5-400",
"VGG-0.5-650",
"Inception-0.5-400",
"Inception-0.5-650",
]
def combine_model_scores(scores, y=None, cross_val=True):
"""
Combine predictions across multiple models and augmentations and return labels.
By default we use simple average without optimization on predicted labels (y=None)
Arguments
scores: Numpy array of probabilities, (n_models x n_samples x augmentations x n_classes)
y: true labels, (n_samples,). Simple average if None. Logistic regression if given.
cross_val: when y is given, blend via one-out cross-validation if True. Blend in-sample if False.
Returns
y_pred: labels, (n_samples,)
"""
if y is None:
return np.argmax(scores.mean(axis=(0, 2)), axis=1), scores.mean(axis=(0, 2))
else:
n_models, n_samples, n_aug, n_classes = scores.shape
y = np.repeat(y[:, None], n_aug, axis=1)
lr = LogisticRegression()
if cross_val:
pred = np.zeros((len(y), n_classes))
for i in range(n_samples):
idx = [k for k in range(len(y)) if k != i]
x_train = scores[:, idx, ...].transpose(1, 2, 3, 0).reshape((n_samples - 1) * n_aug,
n_models * n_classes)
y_train = y[idx].flatten()
x_test = scores[:, i, ...].transpose(1, 2, 0).reshape(n_aug, n_models * n_classes)
lr.fit(x_train, y_train)
pred[i] = lr.predict_proba(x_test).mean(axis=0)
else:
x = scores.transpose(1, 2, 3, 0).reshape(n_samples * n_aug, n_models * n_classes)
lr.fit(x, y.flatten())
pred = lr.predict_proba(x)
pred = pred.reshape(n_samples, n_aug, n_classes).mean(axis=1)
return np.argmax(pred, axis=1), pred
if __name__ == "__main__":
parser = argparse.ArgumentParser()
arg = parser.add_argument
arg("--features",
required=False,
default=DEFAULT_PREPROCESSED_ROOT,
metavar="",
help="Feature root dir. Default: data/preprocessed/train")
arg("--n_classes",
required=False,
default=DEFAULT_N_CLASSES,
type=int, choices=[2, 4],
metavar="",
help="Number of classes. Can be 2 or 4. Default: 4")
arg("-predict",
action="store_true",
default=False,
help="Predict folds during blending, do not use pre-saved CV predictions")
args = parser.parse_args()
PREPROCESSED_ROOT = args.features
N_CLASSES = args.n_classes
USE_PREDICTIONS = not args.predict
blended_scores = []
y_true = []
y_pred = []
scores = []
files = []
verification = defaultdict(dict)
for fold in range(N_FOLDS):
y_true_f = []
scores_f = []
files_f = []
for model_name in models:
name, scale, crop = model_name.split("-")
scores_seeded = []
for seed in range(N_SEEDS):
if USE_PREDICTIONS:
# use pre-saved predictions
preds_file = "lgbm_preds-{}-{}-{}-f{}-s{}.pkl".format(name, scale, crop, fold, seed)
with open(join(PREDS_DIR, name, preds_file), "rb") as f:
preds = pickle.load(f)
else:
# predict during CV
model_file = "lgbm-{}-{}-{}-f{}-s{}.pkl".format(name, scale, crop, fold, seed)
with open(join(LGBM_MODELS_ROOT, name, model_file), "rb") as f:
model = pickle.load(f)
_, _, x_test, y_test = load_data(join(PREPROCESSED_ROOT, "{}-{}-{}".format(name, scale, crop)),
folds, fold)
sc = model.predict(x_test)
sc = sc.reshape(-1, AUGMENTATIONS_PER_IMAGE, DEFAULT_N_CLASSES)
preds = {
"files": folds[fold]["test"]["x"],
"y_true": y_test,
"scores": sc,
}
n_samples, apm, _ = preds["scores"].shape # apm ~ AUGMENTATIONS_PER_IMAGE
scores_seeded.append(preds["scores"]) # N_SEEDS * (N x AUGMENTATIONS_PER_IMAGE x NUM_CLASSES))
y_true_f.append(preds["y_true"][::apm])
files_f.append(preds["files"])
scores_seeded = np.stack(scores_seeded) # (N_SEEDS x N x AUGMENTATIONS_PER_IMAGE x NUM_CLASSES)
# used to compare with seeds x folds stats for a model obtained during training
verification[model_name][fold] = scores_seeded # (N_SEEDS x N x AUGMENTATIONS_PER_IMAGE x NUM_CLASSES)
# average across seeds, we do not need them anymore
scores_f.append(scores_seeded.mean(axis=0)) # (N x AUGMENTATIONS_PER_IMAGE x NUM_CLASSES)
y_true_f = np.stack(y_true_f)
assert np.all(y_true_f == y_true_f[0])
files_f = np.stack(files_f)
assert np.all(files_f == files_f[0])
y_true.append(y_true_f[0])
files.append(files_f[0])
scores_f = np.stack(scores_f)
y_pred_f, scores_f = combine_model_scores(scores_f, y=None, cross_val=True)
y_pred.append(y_pred_f)
scores.append(scores_f)
if N_CLASSES == 2:
for fold in range(N_FOLDS):
# scores[i] = scores[i].reshape(-1, 2, 2).sum(axis=-1)
scores[fold] = scores[fold].reshape(-1, 2, 2).max(axis=-1)
scores[fold] = scores[fold] / scores[fold].sum(axis=-1, keepdims=True)
y_pred[fold][y_pred[fold] == 1] = 0
y_pred[fold][y_pred[fold] > 1] = 1
y_true[fold][y_true[fold] == 1] = 0
y_true[fold][y_true[fold] > 1] = 1
for model_name in models:
v = verification[model_name][fold]
N = v.shape[1]
v = v.reshape(N_SEEDS, N, AUGMENTATIONS_PER_IMAGE, 2, 2).max(axis=-1)
verification[model_name][fold] = v / v.sum(axis=-1, keepdims=True)
# verify predictions
all_models_folds = np.zeros((len(models), N_FOLDS))
for m_i, model_name in enumerate(models):
model_preds = []
for fold in range(N_FOLDS):
# (N_SEEDS x N x AUGMENTATIONS_PER_IMAGE x NUM_CLASSES)
fold_preds = verification[model_name][fold].mean((0, 2)).argmax(-1)
model_preds.append(fold_preds)
all_models_folds[m_i, fold] = accuracy_score(y_true[fold], fold_preds)
model_acc = accuracy_score(np.concatenate(y_true), np.concatenate(model_preds))
print("{}: average across seeds: [{}]. All folds {}({:0.2})".
format(model_name, ", ".join(map(lambda s: "{:5.3}".format(s), all_models_folds[m_i])),
model_acc, all_models_folds[m_i].std()))
print()
print("Std across models: [{}]. All folds {:5.3}".
format(", ".join(map(lambda s: "{:5.3}".format(s), all_models_folds.std(0))), all_models_folds.std(0).mean()))
acc_folded = np.array([accuracy_score(y_t, y_p) for y_t, y_p in zip(y_true, y_pred)]) # y_true, y_pred contain folds
acc_blend = accuracy_score(np.concatenate(y_true), np.concatenate(y_pred))
print("Blended model: [{}], mean {:5.3}, std {:5.3}".format(", ".join(map(lambda s: "{:5.3}".format(s), acc_folded)),
acc_blend, acc_folded.std()))
# Dump crossvalidation stats
y_pred = np.concatenate(y_pred)
y_true = np.concatenate(y_true)
scores = np.concatenate(scores)
files = np.concatenate(files)
if N_CLASSES == 2:
with open("data/roc_scores.pkl", "wb") as f:
pickle.dump((scores, y_true), f)
elif N_CLASSES == 4:
with open("data/conf_mx.pkl", "wb") as f:
pickle.dump((y_true, y_pred), f)
CLASSES = ["Normal", "Benign", "InSitu", "Invasive"]
labels = [CLASSES[i] for i in y_pred]
df = pd.DataFrame(list(zip(map(lambda s: s.replace(".npy", ".tif"), files), labels)), columns=["image", "label"])
df = df.sort_values("image")
df.to_csv("submission/crossvalidation.csv", header=False, index=False)
"""
2-class Blended model: [ 0.95, 0.925, 0.9, 0.975, 0.925, 0.975, 0.925, 0.925, 0.95, 0.925], mean 0.938, std 0.023
4-class Blended model: [0.925, 0.825, 0.875, 0.875, 0.875, 0.9, 0.85, 0.875, 0.875, 0.85], mean 0.873, std 0.0261
"""