-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_best_params.py
227 lines (168 loc) · 7.7 KB
/
find_best_params.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import torch
from utils.dataset import read_dataset, BinaryClassSteelDataset, TrainSteelDataset, MultiClassSteelDataset
import albumentations as A
from torch.utils.data import DataLoader
from mlcomp.contrib.transform.albumentations import ChannelTranspose
from mlcomp.contrib.transform.rle import rle2mask, mask2rle
from mlcomp.contrib.transform.tta import TtaWrap
import numpy as np
from tqdm import tqdm
from joblib import Parallel, delayed
def get_tta_loaders(df, tta, cls, batch=16):
datasets = [TtaWrap(cls(df, transforms=t), tfms=t) for t
in tta]
loaders = [DataLoader(d, num_workers=6, batch_size=batch, shuffle=False) for d
in datasets]
return loaders
def predict_tta_class(models: dict, loaders_batch):
results = {i: [] for i in models}
data = None
for i, data in enumerate(loaders_batch):
features = data.pop('features').cuda()
for key, model in models.items():
results[key].append(torch.sigmoid(model(features)))
data = data
# TTA mean
for key in list(results.keys()):
preds = torch.stack(results.pop(key))
preds = torch.mean(preds, dim=0).squeeze()
results[key] = preds
return results, data
def find_best_multiclass(df, tta, model, thresholds):
loaders = get_tta_loaders(df, tta, MultiClassSteelDataset)
results = {i: [] for i in thresholds}
for batch in tqdm(zip(*loaders), total=len(loaders[0])):
preds, data = predict_tta_class({'model': model}, batch)
preds = preds['model']
true = data['targets'].cuda().long()
for t in thresholds:
p = (preds > t).long()
results[t].append((p == true).detach().cpu().numpy().astype(int))
shape = results[list(results.keys())[0]][0].shape
accuracy = {cls: {} for cls in range(shape[1])}
for t, values in results.items():
values = np.concatenate(values, axis=0)
for cls in list(accuracy.keys()):
accuracy[cls][t] = np.sum(values[:, cls]) / len(values[:, cls])
bests = {}
for cls, (key, data) in enumerate(accuracy.items()):
d = [(i, val) for i, val in data.items()]
d = sorted(d, key=lambda x: x[1])
print(f'Class: {cls}; Best: {d[-1]} Worse: {d[0]} Mean: {np.mean([i for _, i in d])}')
bests[cls] = d[-1]
return bests
def find_best_binary(df, tta, model, thresholds):
loaders = get_tta_loaders(df, tta, BinaryClassSteelDataset)
results = {i: [] for i in thresholds}
for batch in tqdm(zip(*loaders), total=len(loaders[0])):
preds, data = predict_tta_class({'model': model}, batch)
preds = preds['model']
true = data['targets'].cuda().long()
for t in thresholds:
p = (preds > t).long()
results[t].append((p == true).detach().cpu().numpy().astype(int).flatten())
accuracy = []
for t, values in results.items():
values = np.concatenate(values, axis=0)
accuracy.append([t, np.sum(values) / len(values)])
accuracy = sorted(accuracy, key=lambda x: x[1])
print(f'Best: {accuracy[-1]} Worse: {accuracy[0]} Mean: {np.mean([i for _, i in accuracy])}')
return accuracy[-1]
def dice(preds, true, eps=1e-7):
batch_size = len(true)
outputs = preds.view(batch_size, 4, -1)
targets = true.view(batch_size, 4, -1)
intersection = torch.sum(targets * outputs, dim=-1)
union = torch.sum(targets, dim=-1) + torch.sum(outputs, dim=-1)
dice = (2 * intersection / (union + eps))
return dice
def find_best_mask(df, tta, model, conf_thresholds, min_size_thresholds, batch=16):
loaders = get_tta_loaders(df, tta, TrainSteelDataset, batch=batch)
results = {}
for batch in tqdm(zip(*loaders), total=len(loaders[0])):
preds, data = predict_tta_class({'model': model}, batch)
preds = preds['model']
true = data['targets'].detach().cuda()
true_not_exists = true.sum(dim=(2, 3)) == 0
for conf_t in conf_thresholds:
_preds = (preds > conf_t).float()
_preds_sum = _preds.sum(axis=(2, 3))
for size_t in min_size_thresholds:
key = (conf_t, size_t)
if key not in results:
results[key] = []
cond = _preds_sum < size_t
_p = _preds.clone()
_p[cond] = torch.zeros_like(_p[cond])
data = dice(_p, true)
data = torch.where((data < 1e-7) & true_not_exists, torch.ones_like(data), data)
results[key].append(data)
for key in list(results.keys()):
results[key] = torch.cat(results[key], dim=0).mean(dim=0).cpu().numpy()
_results = {i: [] for i in range(4)}
for key, item in results.items():
for i in range(4):
_results[i].append([key, item[i]])
bests = {}
for cls, item in _results.items():
item = sorted(item, key=lambda x: x[1])
best = item[-1]
params, d = best
print(f'Class: {cls}; Best: conf_thres={params[0]:.2f} size_thres={params[1]:.0f} dice={d:.5f}')
bests[cls] = best
return bests
def create_transforms(additional):
res = list(additional)
# add necessary transformations
res.extend([
A.Normalize(
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
),
ChannelTranspose()
])
res = A.Compose(res)
return res
if __name__ == '__main__':
df = read_dataset(
'/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/dataset/train.csv',
'/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/dataset/train_images')
# Different transforms for TTA wrapper
transforms = [
[],
[A.HorizontalFlip(p=1)],
[A.VerticalFlip(p=1)],
[A.HorizontalFlip(p=1), A.VerticalFlip(p=1)]
]
transforms = [create_transforms(t) for t in transforms]
device = 'cuda'
print('resnet34-class01')
model = torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/resnet34-class01/torchscript.pth', map_location=device)
find_best_multiclass(df, transforms, model, np.arange(0.05, 1.0, 0.05))
print()
print('12mobilenet-severstal')
model = torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/12mobilenet-severstal/torchscript.pth', map_location=device)
find_best_binary(df, transforms, model, np.arange(0.05, 1.0, 0.05))
class Model(torch.nn.Module):
def __init__(self, models, weights):
self.models = models
self.weights = weights
def __call__(self, x):
res = []
x = x.cuda()
s = np.sum(self.weights)
with torch.no_grad():
for m, w in zip(self.models, self.weights):
res.append(m(x) * w / s)
res = torch.stack(res)
return torch.sum(res, dim=0)
models = [
torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/11resnet50-hard-severstal/torchscript.pth', map_location=device),
torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/11se-resnext101-severstal/torchscript.pth', map_location=device),
torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/severstalmodels/unet_mobilenet2.pth', map_location=device),
torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/severstalmodels/unet_resnet34.pth', map_location=device),
torch.jit.load('/mnt/HDD/home/druzhinin/kaggle/kaggle_severstal/download/severstalmodels/unet_se_resnext50_32x4d.pth', map_location=device)
]
model = Model(models, [2, 3, 1, 1, 1])
print()
print('Mask models')
find_best_mask(df, transforms, model, np.arange(0.05, 1.0, 0.05), [0, 500, 1000, 1500, 2000, 2500, 3000])