-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble.py
More file actions
174 lines (153 loc) · 7.02 KB
/
ensemble.py
File metadata and controls
174 lines (153 loc) · 7.02 KB
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
from trainer.predict import Predict
import pandas as pd
import os
import argparse
from scipy.stats import rankdata
from sklearn.metrics import roc_auc_score
class Ensemble(object):
def __init__(self, params):
# get learners with their trained weights
self.params = params
def predict(self, df, image_dir, validate=False, is_gpu_available=True):
# initialization
params = self.params
archs = params['archs']
paths = params['paths']
sizes = params['sizes']
names = params['names']
# loop over predictors
predictions = {}
for arch, path, size, name in zip(archs, paths, sizes, names):
# get predictor
print('________Prediction for_____', arch, 'under name', name)
predict = Predict(arch=arch,
classifier_path=path,
features_extractor_path=None,
is_gpu_available=is_gpu_available)
# predict without data augmentation
all_probs = predict(df, image_dir, validate=validate, size=size)
predictions[name] = all_probs
del predict
predictions = pd.DataFrame(predictions)
# add image names and targets
predictions['image_name'] = df['image_name']
try:
predictions['target'] = df['target']
except:
predictions['target'] = -1
columns = ['image_name'] + names + ['target']
predictions = predictions[columns]
return predictions
@staticmethod
def assemble(predictions, names, method='voting', weights=None):
print('------Method------', method)
df = predictions.copy()
if method == 'voting':
if weights is None:
df['prediction'] = df[names].mean(axis=1)
else:
s = 0. * df[names[0]]
for name, weight in zip(names, weights):
print(name, weight)
s = s + weight * df[name]
df['prediction'] = s / s.max()
if method == 'rankdata':
for name in names:
df[name] = rankdata(df[name], method='average')
if weights is None:
df['prediction'] = df[names].sum(axis=1)
df['prediction'] = df['prediction'] / df['prediction'].max()
else:
s = 0. * df[names[0]]
for name, weight in zip(names, weights):
print(name, weight)
s = s + weight * df[name]
df[name] = df[name] / df[name].max()
df['prediction'] = s / s.max()
return df
def main(parser):
# hard coded params
#params = {}
#params['archs'] = [
# 'efficientnet-b7', 'efficientnet-b7', 'efficientnet-b7',
# 'iternet_extractor_classifier_data', 'iternet_extractor_classifier_data', 'iternet_extractor_classifier_data',
# 'residual_attention', 'residual_attention', 'residual_attention'
#]
#params['paths'] = [
# 'exp/best-models/efficientnet-b7-K1-pretrained-ISIC2019.pth',
# 'exp/best-models/efficientnet-b7-K2-pretrained-ISIC2019.pth',
# 'exp/best-models/efficientnet-b7-K3-pretrained-ISIC2019.pth',
# 'exp/best-models/iternet-K-1.pth',
# 'exp/best-models/iternet-K-2.pth',
# 'exp/best-models/iternet-K-3.pth',
# 'exp/best-models/residual_attention-K-1.pth',
# 'exp/best-models/residual_attention-K-2.pth',
# 'exp/best-models/residual_attention-K-3.pth',
#]
#params['sizes'] = [224, 224, 224,
# 256, 256,
# 448, 448, 448
# ]
#params['names'] = ['efficientnet-b7-K-1', 'efficientnet-b7-K-2', 'efficientnet-b7-K-3',
# 'iternet-K-1', 'iternet-K-2',
# 'residual_attention-K-1', 'residual_attention-K-2', 'residual_attention-K-3'
# ]
# only efficient-nets
params = {}
params['archs'] = ['efficientnet-b7', 'efficientnet-b7', 'efficientnet-b7',
'iternet_extractor_classifier_data', 'iternet_extractor_classifier_data',
'iternet_extractor_classifier_data',
'residual_attention', 'residual_attention', 'residual_attention'
]
params['paths'] = [
'exp/best-models/efficientnet-b7-K1-pretrained-ISIC2019.pth',
'exp/best-models/efficientnet-b7-K2-pretrained-ISIC2019.pth',
'exp/best-models/efficientnet-b7-K3-pretrained-ISIC2019.pth',
'exp/best-models/iternet-K-1.pth',
'exp/best-models/iternet-K-2.pth',
'exp/best-models/iternet-K-3.pth',
'exp/best-models/residual_attention-K-1.pth',
'exp/best-models/residual_attention-K-2.pth',
'exp/best-models/residual_attention-K-3.pth'
]
params['sizes'] = [224, 224, 224,
256, 256, 256,
448, 448, 448
]
params['names'] = ['efficientnet-b7-K-1', 'efficientnet-b7-K-2', 'efficientnet-b7-K-3',
'iternet-K-1', 'iternet-K-2', 'iternet-K-3',
'residual_attention-K-1', 'residual_attention-K-2', 'residual_attention-K-3']
# read dataframe
df = pd.read_csv(args.csv_path)
if not args.validate:
df['target'] = 0
else:
target = df['target'].tolist()
# predict using the ensemble method
ensemble = Ensemble(params)
predictions = ensemble.predict(df, args.image_dir, validate=args.validate, is_gpu_available=args.use_gpu)
df = Ensemble.assemble(predictions, params['names'], method=args.ensemble_method, weights=None)
# save predictions
df.to_csv(os.path.join(args.results_path, 'results.csv'), index=None)
df['target'] = df['prediction']
df = df[['image_name', 'target']]
df.to_csv(os.path.join(args.results_path, 'sample_submission.csv'), index=None)
if args.validate:
roc = roc_auc_score(target, df['target'].tolist())
print('Final ROC', roc)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Model Training')
parser.add_argument('--csv_path', default="data/val_split.csv",
type=str, help='path of csv file')
parser.add_argument('--results_path', default="mohamed/submission",
type=str, help='loss type')
parser.add_argument('--image_dir', default='data/resized-jpeg/train/',
type=str, help='Images folder path')
parser.add_argument('--ensemble_method', default='voting',
type=str, help='ensemble method')
parser.add_argument('--validate', action='store_true',
help='if to validate target')
parser.add_argument('--use_gpu', action='store_true',
help='use gpu')
args = parser.parse_args()
main(args)