forked from ildoonet/data-science-bowl-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission.py
359 lines (300 loc) · 12 KB
/
submission.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
from collections import OrderedDict
import cv2
import logging
import json
import numpy as np
import pandas as pd
import time
import pickle
import sys
from data_augmentation import get_rect_of_mask
from data_feeder import CellImageDataManagerTest, CellImageDataManagerValid
from hyperparams import HyperParams
try:
from kaggle.api.kaggle_api_extended import KaggleApi
except:
logging.warning('~/.kaggle/kaggle.json not set. Can not submit to kaggle automatically.')
logger = logging.getLogger('submission')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.handlers = []
logger.addHandler(ch)
thr_list = np.arange(0.5, 1.0, 0.05)
def rle_encoding(x):
"""
reference : https://www.kaggle.com/keegil/keras-u-net-starter-lb-0-277
:param x: (h, w, 1) numpy
:return: run length encoded list
"""
dots = np.where(x.T.flatten() == 1)[0]
rle = []
prev = -2
cnt = 0
for b in dots:
if b > prev + 1:
rle.extend((b + 1, 0))
rle[-1] += 1
cnt += 1
prev = b
return rle, cnt
def get_iou1(a, b):
if len(a.shape) == 2:
a = a[..., np.newaxis]
if len(b.shape) == 2:
b = b[..., np.newaxis]
a = a.astype(np.bool_)
b = b.astype(np.bool_)
intersection = np.sum(np.logical_and(a, b), dtype=np.float32)
if intersection == 0:
return 0.0
union = np.sum(np.logical_or(a, b), dtype=np.float32)
if union == 0:
return 0.0
iou = intersection / union
return iou
def get_iou2(a, b):
try:
rmin1, rmax1, cmin1, cmax1 = get_rect_of_mask(a)
rmin2, rmax2, cmin2, cmax2 = get_rect_of_mask(b)
overlap_r = (rmin1 <= rmin2 <= rmax1 or rmin1 <= rmax2 <= rmax1) or (rmin2 <= rmin1 <= rmax2 or rmin2 <= rmax1 <= rmax2)
overlap_c = (cmin1 <= cmin2 <= cmax1 or cmin1 <= cmax2 <= cmax1) or (cmin2 <= cmin1 <= cmax2 or cmin2 <= cmax1 <= cmax2)
if not (overlap_r and overlap_c):
return 0.0
except:
pass
if len(a.shape) == 2:
a = a[..., np.newaxis]
if len(b.shape) == 2:
b = b[..., np.newaxis]
intersection = a & b
intersection = np.sum(intersection)
if intersection == 0:
return 0.0
union = a | b
union = np.sum(union)
if union == 0:
return 0.0
return intersection / union
# get_iou2 is faster version of get_iou1
get_iou = get_iou2
def get_metric(instances, label_trues, thr_list):
"""
:param instances: list of (h, w) numpy array
:param label_trues: list of (h, w) numpy array
:param thr_list:
:return:
"""
if len(label_trues) == 0:
return 0.0
cnt_tps = np.zeros((len(thr_list)), dtype=np.int32)
cnt_fps = np.zeros((len(thr_list)), dtype=np.int32)
cnt_ass = np.zeros((len(thr_list), len(label_trues)), dtype=np.int32)
max_found = set()
for label_pred in instances:
max_label_idx = -1
max_label_iou = 0.0
max_label = None
for idx_label, label_true in enumerate(label_trues):
if idx_label in max_found:
continue
# measure ious between label_preds & label_true
iou = get_iou(label_true, label_pred)
if iou > max_label_iou:
max_label_idx = idx_label
max_label_iou = iou
max_label = label_true
if max_label is None:
# false positive
cnt_fps = cnt_fps + 1
else:
max_found.add(idx_label) # TODO
for th_idx, thr in enumerate(thr_list):
if max_label_iou > thr:
cnt_tps[th_idx] += 1
cnt_ass[th_idx][max_label_idx] = 1
else:
cnt_fps[th_idx] += 1
cnt_fns = len(label_trues) - np.sum(cnt_ass, axis=1)
# return the metric
return cnt_tps, cnt_fps, cnt_fns
def get_multiple_metric(thr_list, instances, label_trues):
"""
:param thr_list:
:param instances: list of (h, w) numpy array
:param label_trues: list of (h, w) numpy array
:return:
"""
t = time.time()
cnt_tp, cnt_fp, cnt_fn = get_metric(instances, label_trues, thr_list)
# print('thr_miou', time.time() - t)
return cnt_tp, cnt_fp, cnt_fn
class KaggleSubmission:
BASEPATH = os.path.dirname(os.path.realpath(__file__)) + ("/submissions" if HyperParams.get().dataset_stage == 1 else "/submissions_stage2")
CNAME = 'data-science-bowl-2018'
def __init__(self, name):
self.name = name
self.test_ids = []
self.rles = []
self.train_scores = OrderedDict()
self.valid_scores = OrderedDict()
self.test_scores = OrderedDict()
self.valid_instances = {} # key : id -> (instances, scores)
self.test_instances = {}
logger.info('creating: %s' % os.path.join(KaggleSubmission.BASEPATH, self.name))
os.makedirs(os.path.join(KaggleSubmission.BASEPATH, self.name), exist_ok=True)
logger.info('creating: %s' % os.path.join(KaggleSubmission.BASEPATH, self.name, 'valid'))
os.makedirs(os.path.join(KaggleSubmission.BASEPATH, self.name, 'valid'), exist_ok=True)
logger.info('creating: %s' % os.path.join(KaggleSubmission.BASEPATH, self.name, 'train'))
os.makedirs(os.path.join(KaggleSubmission.BASEPATH, self.name, 'train'), exist_ok=True)
def save_train_image(self, idx, image, loss=0.0, score=0.0, score_desc=[]):
cv2.imwrite(os.path.join(KaggleSubmission.BASEPATH, self.name, 'train', idx + '.jpg'), image)
if isinstance(idx, bytes):
idx = idx.decode("utf-8")
self.train_scores[idx] = (loss, score, score_desc)
def save_valid_image(self, idx, image, loss=0.0, score=0.0, score_desc=[]):
cv2.imwrite(os.path.join(KaggleSubmission.BASEPATH, self.name, 'valid', idx + '.jpg'), image)
if isinstance(idx, bytes):
idx = idx.decode("utf-8")
self.valid_scores[idx] = (loss, score, score_desc)
def save_image(self, idx, image, loss=0.0):
cv2.imwrite(os.path.join(KaggleSubmission.BASEPATH, self.name, idx + '.jpg'), image)
self.test_scores[idx] = (loss, 0.0)
def add_result(self, idx, instances):
"""
:param idx: test sample id
:param instances: list of (h, w, 1) numpy containing
"""
if len(instances) == 0:
self.test_ids.append(idx)
self.rles.append([])
return
for instance in instances:
rles, cnt = rle_encoding(instance)
if cnt < 3:
continue
assert len(rles) % 2 == 0
self.test_ids.append(idx)
self.rles.append(rles)
def get_filepath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'submission_%s.csv' % self.name)
return filepath
def get_confpath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'config.json')
return filepath
def get_train_htmlpath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'train', 'train.html')
return filepath
def get_valid_htmlpath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'valid', 'valid.html')
return filepath
def get_test_htmlpath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'test.html')
return filepath
def get_pklpath(self):
filepath = os.path.join(KaggleSubmission.BASEPATH, self.name, 'submission.pkl')
return filepath
def save(self):
sub = pd.DataFrame()
sub['ImageId'] = self.test_ids
sub['EncodedPixels'] = pd.Series(self.rles).apply(lambda x: ' '.join(str(y) for y in x))
# save a submission file
filepath = self.get_filepath()
f = open(filepath, 'w')
f.close()
sub.to_csv(filepath, index=False)
logger.info('%s saved at %s.' % (self.name, filepath))
# save hyperparameters
filepath = self.get_confpath()
f = open(filepath, 'w')
a = json.dumps(HyperParams.get().__dict__, indent=4)
f.write(a)
f.close()
total_html = "<html><body>Average Score=$avg_score$<br/><br/><table>" \
" <tr>" \
" <th>ID</th><th>Image</th>" \
" </tr>" \
" $rows$" \
"</table></body></html>"
row_html = "<tr>" \
" <td><b>{idx}</b><br/>{iou}<br/>{iou2}</td><td><img src=\"./{idx}.jpg\"</td>" \
"</tr>"
# save training results
rows = []
metrics = []
for idx, (loss, metric, metric_desc) in self.train_scores.items():
row = row_html.format(idx=idx, iou=format(metric, '.3f'), iou2='<br/>'.join(metric_desc))
rows.append(row)
metrics.append(metric)
html = total_html.replace('$rows$', ''.join(rows)).replace('$avg_score$', str(np.mean(metrics)))
filepath = self.get_train_htmlpath()
f = open(filepath, 'w')
f.write(html)
f.close()
# save validation results
rows = []
metrics = []
for idx, (loss, metric, metric_desc) in self.valid_scores.items():
row = row_html.format(idx=idx, iou=format(metric, '.3f'), iou2='<br/>'.join(metric_desc))
rows.append(row)
metrics.append(metric)
html = total_html.replace('$rows$', ''.join(rows)).replace('$avg_score$', str(np.mean(metrics)))
filepath = self.get_valid_htmlpath()
f = open(filepath, 'w')
f.write(html)
f.close()
# save test results
total_html = "<html><body><table>" \
" <tr>" \
" <th>IDX</th><th>ID</th><th>Image</th>" \
" </tr>" \
" $rows$" \
"</table></body></html>"
row_html = "<tr>" \
" <td>{idx}</td><td><img src=\"./{idx}.jpg\"</td>" \
"</tr>"
rows = []
for idx, (loss, metric) in self.test_scores.items():
row = row_html.format(idx=idx)
rows.append(row)
html = total_html.replace('$rows$', ''.join(rows))
filepath = self.get_test_htmlpath()
f = open(filepath, 'w')
f.write(html)
f.close()
# save pkl
f = open(self.get_pklpath(), 'wb')
pickle.dump({
'valid_instances': self.valid_instances,
'test_instances': self.test_instances
}, f, pickle.HIGHEST_PROTOCOL)
f.close()
def submit_result(self, submit_msg='KakaoAutoML'):
"""
Submit result to kaggle and wait for getting the result.
"""
logger.info('kaggle.submit_result: initialization')
api_client = KaggleApi()
api_client.authenticate()
submissions = api_client.competitionSubmissions(KaggleSubmission.CNAME)
last_idx = submissions[0].ref if len(submissions) > 0 else -1
# submit
logger.info('kaggle.submit_result: trying to submit @ %s' % self.get_filepath())
submit_result = api_client.competitionSubmit(self.get_filepath(), submit_msg, KaggleSubmission.CNAME)
logger.info('kaggle.submit_result: submitted!')
# wait for the updated LB
wait_interval = 10 # in seconds
for _ in range(60 // wait_interval * 5):
submissions = api_client.competitionSubmissions(KaggleSubmission.CNAME)
if len(submissions) == 0:
continue
if submissions[0].status == 'complete' and submissions[0].ref != last_idx:
# updated
logger.info('kaggle.submit_result: LB Score Updated!')
return submit_result, submissions[0]
time.sleep(wait_interval)
logger.info('kaggle.submit_result: LB Score NOT Updated!')
return submit_result, None