-
Notifications
You must be signed in to change notification settings - Fork 23
/
ensemble_forsub1.py
83 lines (72 loc) · 2.43 KB
/
ensemble_forsub1.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
import numpy as np
import pandas as pd
from utils.utils import *
# 39000, 39500, 36500, 38000
from tqdm import tqdm
import os
results = [
['seresnext50_semi', 0, 52000],
['seresnext50_semi', 1, 16000],
['seresnext50_semi', 2, 28500],
['seresnext50_semi', 3, 29500],
['seresnext50_semi', 4, 52000],
# ['seresnext50_semi_v2', 1, 34000],
['seresnext101_semi', 0, 28000],
['seresnext101_semi', 1, 21500],
]
ws = [1] * len(results)
print(results)
assert len(results) == len(ws)
def npy2json(model_name, fold_index, cpk, temp=None, w=1):
resultDir = './result/{}_{}'.format(model_name, fold_index)
all_names = np.load(os.path.join(resultDir, '%08d_names.npy' % (cpk)))
all_results = np.load(os.path.join(resultDir, '%08d_results.npy' % (cpk)))
dict_results = {}
if temp is not None:
dict_results = temp
for name, out in tqdm(zip(all_names, all_results)):
# temp = (out > 0.5).astype(np.float)
if out.max() < 0.45:
out = np.zeros_like(out).astype(np.float)
else:
out = (out > 0.2).astype(np.float)
if str(name) in dict_results:
dict_results[str(name)] += out * w
else:
dict_results[str(name)] = out * w
return dict_results
# def load():
# data = pd.read_csv('8822.csv')
# EncodedPixels = data['EncodedPixels'].tolist()
def ensemble(dict_results, best_t=0.6, num_results=1):
count = 0
count2 = 0
all_names = []
all_rles = []
print(len(all_names), best_t)
for name in tqdm(dict_results.keys()):
res = dict_results[name]/float(sum(ws))
# res = dict_results[name]
out = (res > best_t).astype(np.int)
if out.max() == 0:
rle = '-1'
else:
out = (res > 0.2).astype(np.int)
if out.sum() <= 64:
rle = '-1'
count2 += 1
else:
rle = run_length_encode(out)
if rle == '-1':
count += 1
all_names.append(name)
all_rles.append(rle)
pd.DataFrame({"ImageId": all_names, "EncodedPixels": all_rles}).to_csv('ensemble_sub1.csv', index=None)
print(count, count2)
dict_results = None
print(len(results))
for result, w in zip(results, ws):
model_name, fold_index, cpk = result
# dict_results = None
dict_results = npy2json(model_name, fold_index, cpk, dict_results, w)
ensemble(dict_results, best_t=0.45, num_results=len(results))