-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_submission.py
133 lines (102 loc) · 3.76 KB
/
prepare_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
"""
PREPARES SUBMISSION CSV FILE
REQUIRE ENSEMBLE OF 5 NETWORKS IN tmp FOLDER
"""
import numpy as np
import tensorflow as tf
import keras
import os
import h5py
import cv2 as cv
import csv
from helpers import *
from model import *
from data_augmentation import DataAugmentation
root_folder = os.getcwd()
results_folder = os.path.join(root_folder, 'tmp')
data_folder = os.path.join(root_folder, 'data')
# Parameters
batch_size = 64
partitions = 5
tta = True
slide_augmentation = True
model_prefix = 'trained_model'
########################################################################################################################
print("Loading sample_submission.csv")
submission_path = os.path.join(data_folder, 'sample_submission.csv')
with open(submission_path, 'rt', newline='') as f:
reader = csv.reader(f)
submission_records = list(reader)
########################################################################################################################
print("Loading test images")
img_size = 128
img_m1 = 13
img_m2 = 14
if model_type == 'pspnet':
img_size = 120
img_m1 = 9
img_m2 = 10
path_test_x = os.path.join(root_folder, 'test_x.npy')
test_x = np.load(path_test_x)
if slide_augmentation:
test_x = np.pad(test_x, ((0, 0), (img_m1, img_m2), (img_m1, img_m2), (0, 0)), mode='reflect')
print(test_x.shape)
x = preprocessing(test_x)
if tta:
xf = x.copy()
np.fliplr(xf)
y = None
for partition in range(1, partitions+1):
predition_path = os.path.join(results_folder, '{}_{}_prediction.npy'.format(model_prefix, partition))
if os.path.exists(predition_path):
print("Loading prediction for model {}".format(partition))
yp = np.load(predition_path)
else:
print("Loading pretrained model {}".format(partition))
model_path = os.path.join(results_folder, '{}_{}.h5'.format(model_prefix, partition))
model = keras.models.load_model(model_path, custom_objects={'iou': iou})
print("Predicting outputs {}".format(partition))
yp = model.predict(x, batch_size=batch_size)
if tta:
yp_tta = model.predict(xf, batch_size=batch_size)
np.fliplr(yp_tta)
yp += yp_tta
yp /= 2
del model
if slide_augmentation:
yp = yp[:, img_m1:(img_size-img_m2), img_m1:(img_size-img_m2), :]
print("Saving prediction for model {}".format(partition))
np.save(predition_path, yp)
if yp.shape[1] == img_size:
yp = yp[:, img_m1:(img_size-img_m2), img_m1:(img_size-img_m2), :]
if y is None:
y = yp
else:
y += yp
y /= partitions
########################################################################################################################
print("Fixing outputs for empty inputs")
for i in range(len(x)):
ix = x[i]
if np.min(ix) == np.max(ix):
y[i].fill(0.0)
########################################################################################################################
print("Run-length encoding outputs")
cnt = 0
for i in range(len(submission_records)-1):
mask = y[i] > 0.5
submission_records[i+1][1] = fast_run_length_enc(mask)
cnt += 1
if cnt % 100 == 0:
print(".", end='', flush=True)
print('')
########################################################################################################################
print("Saving submission.csv")
if tta:
submission_file = 'submission_TTA.csv'
else:
submission_file = 'submission.csv'
submission_path = os.path.join(results_folder, submission_file)
with open(submission_path, 'w') as f:
writer = csv.writer(f)
writer.writerows(submission_records)