-
Notifications
You must be signed in to change notification settings - Fork 19
/
predict_folds.py
216 lines (167 loc) · 6.76 KB
/
predict_folds.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
import os
from os.path import join
import re
import cv2
import numpy as np
import pandas as pd
from argus import load_model
import torch
from src.transforms import SimpleDepthTransform, SaltTransform, CenterCrop
from src.argus_models import SaltMetaModel
from src.transforms import HorizontalFlip
from src.utils import RLenc, make_dir
from src import config
EXPERIMENT_NAME = 'mos-fpn-lovasz-se-resnext50-001'
FOLDS = list(range(config.N_FOLDS))
ORIG_IMAGE_SIZE = (101, 101)
PRED_IMAGE_SIZE = (128, 128)
TRANSFORM_MODE = 'crop'
TRAIN_FOLDS_PATH = '/workdir/data/train_folds_148_mos_emb_1.csv'
IMAGES_NAME = '148'
FOLDS_DIR = f'/workdir/data/experiments/{EXPERIMENT_NAME}'
PREDICTION_DIR = f'/workdir/data/predictions/{EXPERIMENT_NAME}'
make_dir(PREDICTION_DIR)
SEGM_THRESH = 0.5
PROB_THRESH = 0.5
class Predictor:
def __init__(self, model_path):
self.model = load_model(model_path)
self.model.nn_module.final = torch.nn.Sigmoid() #
self.model.nn_module.eval()
self.depth_trns = SimpleDepthTransform()
self.crop_trns = CenterCrop(ORIG_IMAGE_SIZE)
self.trns = SaltTransform(PRED_IMAGE_SIZE, False, TRANSFORM_MODE)
def __call__(self, image):
tensor = self.depth_trns(image, 0)
tensor = self.trns(tensor)
tensor = tensor.unsqueeze(0).to(self.model.device)
with torch.no_grad():
segm, prob = self.model.nn_module(tensor)
segm = segm.cpu().numpy()[0][0]
segm = self.crop_trns(segm)
segm = (segm * 255).astype(np.uint8)
prob = prob.item()
return segm, prob
class TtaPredictor:
def __init__(self, model_path):
self.model = load_model(model_path)
self.model.nn_module.eval()
self.depth_trns = SimpleDepthTransform()
self.crop_trns = CenterCrop(ORIG_IMAGE_SIZE)
self.trns = SaltTransform(PRED_IMAGE_SIZE, False, TRANSFORM_MODE)
self.flip = HorizontalFlip()
def forward_tta(self, image):
image = self.depth_trns(image)
images = [
image,
self.flip(image)
]
images = [self.trns(img) for img in images]
tensor = torch.stack(images, dim=0)
return tensor
def backward_tta(self, output):
segms, probs = output
segms = segms.cpu().numpy()
probs = probs.cpu().numpy()
mean_prob = float(np.mean(probs))
segm_lst = []
for i in range(segms.shape[0]):
segm = segms[i, 0]
segm = self.crop_trns(segm)
if i == 1:
segm = self.flip(segm)
segm_lst.append(segm)
mean_segm = np.mean(segm_lst, axis=0)
mean_segm = (mean_segm * 255).astype(np.uint8)
return mean_segm, mean_prob
def __call__(self, image):
tensor = self.forward_tta(image)
tensor = tensor.to(self.model.device)
with torch.no_grad():
output = self.model.nn_module(tensor)
segm, prob = self.backward_tta(output)
return segm, prob
def pred_val_fold(model_path, fold):
predictor = Predictor(model_path)
folds_df = pd.read_csv(TRAIN_FOLDS_PATH)
fold_df = folds_df[folds_df.fold == fold]
fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'val')
make_dir(fold_prediction_dir)
prob_dict = {'id': [], 'prob': []}
for i, row in fold_df.iterrows():
image = cv2.imread(row.image_path, cv2.IMREAD_GRAYSCALE)
segm, prob = predictor(image)
segm_save_path = join(fold_prediction_dir, row.id + '.png')
cv2.imwrite(segm_save_path, segm)
prob_dict['id'].append(row.id)
prob_dict['prob'].append(prob)
prob_df = pd.DataFrame(prob_dict)
prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False)
def pred_test_fold(model_path, fold):
predictor = Predictor(model_path)
prob_df = pd.read_csv(config.SAMPLE_SUBM_PATH)
prob_df.rename(columns={'rle_mask': 'prob'}, inplace=True)
fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'test')
make_dir(fold_prediction_dir)
for i, row in prob_df.iterrows():
image_path = join(config.TEST_DIR, 'images'+IMAGES_NAME, row.id + '.png')
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
segm, prob = predictor(image)
row.prob = prob
segm_save_path = join(fold_prediction_dir, row.id + '.png')
cv2.imwrite(segm_save_path, segm)
prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False)
def get_mean_probs_df(pred_dir):
probs_df_lst = []
for i in range(len(FOLDS)):
fold_dir = os.path.join(pred_dir, f'fold_{FOLDS[i]}')
probs_df = pd.read_csv(join(fold_dir, 'test', 'probs.csv'), index_col='id')
probs_df_lst.append(probs_df)
mean_probs_df = probs_df_lst[0].copy()
for probs_df in probs_df_lst[1:]:
mean_probs_df.prob += probs_df.prob
mean_probs_df.prob /= len(probs_df_lst)
return mean_probs_df
def make_mean_submission():
mean_probs_df = get_mean_probs_df(PREDICTION_DIR)
sample_submition = pd.read_csv(config.SAMPLE_SUBM_PATH)
for i, row in sample_submition.iterrows():
pred_name = row.id + '.png'
segm_lst = []
for i in range(len(FOLDS)):
fold_dir = os.path.join(PREDICTION_DIR, f'fold_{FOLDS[i]}')
pred_path = join(fold_dir, 'test', pred_name)
segm = cv2.imread(pred_path, cv2.IMREAD_GRAYSCALE)
segm = segm.astype(np.float32) / 255
segm_lst.append(segm)
mean_segm = np.mean(segm_lst, axis=0)
prob = mean_probs_df.loc[row.id].prob
pred = mean_segm > SEGM_THRESH
prob = int(prob > PROB_THRESH)
pred = (pred * prob).astype(np.uint8)
rle_mask = RLenc(pred)
row.rle_mask = rle_mask
sample_submition.to_csv(join(PREDICTION_DIR, f'{EXPERIMENT_NAME}-mean-subm.csv'), index=False)
def get_best_model_path(dir_path):
model_scores = []
for model_name in os.listdir(dir_path):
score = re.search(r'-(\d+(?:\.\d+)?).pth', model_name)
if score is not None:
score = score.group(0)[1:-4]
model_scores.append((model_name, score))
model_score = sorted(model_scores, key=lambda x: x[1])
best_model_name = model_score[-1][0]
best_model_path = os.path.join(dir_path, best_model_name)
return best_model_path
if __name__ == "__main__":
for i in range(len(FOLDS)):
print("Predict fold", FOLDS[i])
fold_dir = os.path.join(FOLDS_DIR, f'fold_{FOLDS[i]}')
best_model_path = get_best_model_path(fold_dir)
print("Model path", best_model_path)
print("Val predict")
pred_val_fold(best_model_path, FOLDS[i])
print("Test predict")
pred_test_fold(best_model_path, FOLDS[i])
print("Mean submission")
make_mean_submission()