-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ensamble.py
51 lines (34 loc) · 1.58 KB
/
Ensamble.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
import torch
import os
import numpy as np
from ConvModel import CNNModel
class Ensamble:
def __init__(self, models_path, no_model):
self.models_path = models_path
self.no_model = no_model
self.best_models = []
self.find_best_models()
def find_best_models(self):
models_filenames = [x for x in (os.listdir(self.models_path)) if not x.startswith('.')]
scores = []
for filename in models_filenames:
m_path_full = os.path.join(self.models_path, filename)
args = torch.load(m_path_full, map_location=torch.device("cpu"))
scores.append([m_path_full, args['points']])
scores = sorted(scores, reverse=True, key = lambda model : model[1])
self.best_models = [mods[0] for mods in scores[:self.no_model]]
print(self.best_models)
def make_predictions(self, test_dl):
predictions = []
for model_path in self.best_models:
args = torch.load(model_path, map_location=torch.device('cpu'))
model = CNNModel(*args["args"])
model.load_state_dict(args['state_dict'])
model.eval()
with torch.no_grad():
ps = []
for X in test_dl:
out = model(X[0])
ps.append(torch.softmax(out, dim = 1)[:, 1].detach().numpy())
predictions.append(np.concatenate(ps, axis = 0))
return np.stack(predictions).mean(axis=0)