forked from bfortuner/pytorch-kaggle-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathens_utils.py
67 lines (54 loc) · 1.82 KB
/
ens_utils.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
import os
import time
import random
import numpy as np
import pandas as pd
import shutil
import config as cfg
import constants as c
import utils
import predictions
def get_ensemble_fpath(basename, dset):
fname = '{:s}_{:s}_{:s}'.format(basename, 'ens', dset + c.PRED_FILE_EXT)
return os.path.join(cfg.PATHS['predictions'], fname)
def get_ensemble_meta(name, fpaths):
preds = [predictions.load_pred(f) for f in fpaths]
meta = preds[0].attrs['meta'].copy()
meta['name'] = name
meta['members'] = {p.attrs['meta']['name']:p.attrs['meta'] for p in preds}
print("members", list(meta['members'].keys()))
return meta
def ens_prediction_files(ens_fpath, pred_fpaths, block_size=1,
method=c.MEAN, meta=None):
preds = [predictions.load_pred(f) for f in pred_fpaths]
n_inputs = preds[0].shape[0]
if os.path.exists(ens_fpath):
print('Ens file exists. Overwriting')
time.sleep(2)
shutil.rmtree(ens_fpath)
i = 0
start = time.time()
while i < n_inputs:
pred_block = np.array([p[i:i+block_size] for p in preds])
ens_block = predictions.ensemble_with_method(pred_block, method)
if i == 0:
ens_pred = predictions.save_pred(ens_fpath, ens_block, meta)
else:
ens_pred = predictions.append_to_pred(ens_pred, ens_block)
i += block_size
print(utils.logger.get_time_msg(start))
return ens_fpath
def build_scores(loss, score):
return {
c.LOSS: loss,
c.SCORE: score
}
def build_metadata(labels, scores, thresholds, pred_type, dset):
return {
'label_names': labels,
'scores': scores,
'thresholds': thresholds,
'pred_type': pred_type,
'dset': dset,
'created': time.strftime("%m/%d/%Y %H:%M:%S", time.localtime())
}