-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
356 lines (309 loc) · 15.4 KB
/
modules.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
import torch
import numpy as np
from PIL import Image
from torch.autograd import Variable
import os
from post_processing import *
from topoLoss import *
from save_history import *
def accuracy_check(mask, prediction):
ims = [mask, prediction]
np_ims = []
for item in ims:
if 'str' in str(type(item)):
item = np.array(Image.open(item))
elif 'PIL' in str(type(item)):
item = np.array(item)
elif 'torch' in str(type(item)):
item = item.numpy()
np_ims.append(item)
compare = np.equal(np_ims[0], np_ims[1])
accuracy = np.sum(compare)
return accuracy / len(np_ims[0].flatten())
def accuracy_check_for_batch(masks, predictions, batch_size):
total_acc = 0
for index in range(batch_size):
total_acc += accuracy_check(masks[index], predictions[index])
return total_acc / batch_size
def train_multi_models(models, data_train, loss_fun, optimizers, device, SLICES_COLLECT):
softmax = nn.Softmax2d()
for model in models:
model.train()
for batch, data in enumerate(data_train):
if len(SLICES_COLLECT) == 3:
images_1, images_2, images_3, masks = data[0][0], data[1][0], data[2][0], data[0][1]
# print(images_1.shape, images_2.shape, images_3.shape, masks.shape)
# ((2, 1, 1250, 1250), (2, 3, 1250, 1250), (2, 5, 1250, 1250), (2, 1250, 1250))
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
outputs_3, likelihoodMap_3 = models[2](images_3.to(device))
predict_map = max_outputs([outputs_1, outputs_2, outputs_3])
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
elif len(SLICES_COLLECT) == 2:
images_1, images_2, masks = data[0][0], data[1][0], data[0][1]
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
predict_map = max_outputs([outputs_1, outputs_2])
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
elif len(SLICES_COLLECT) == 1:
images, masks = data[0], data[1]
# print('model input shape: ', images.shape)
predict_map, likelihoodMap = models[0](images.to(device))
# predict_map = smooth_gaussian(predict_map, device)
loss_ce = loss_fun(predict_map, masks.to(device))
loss = loss_ce
for optimizer in optimizers:
optimizer.zero_grad()
loss.backward()
for optimizer in optimizers:
optimizer.step()
def train_topo_multi_models(models, data_train, loss_fun, optimizers, device, SLICES_COLLECT, epoch, start_topo_epoch, save_dir):
softmax = nn.Softmax2d()
for model in models:
model.train()
for batch, data in enumerate(data_train):
topoLoss = 0
escapes = 0
lamda = 0.01
if len(SLICES_COLLECT) == 3:
images_1, images_2, images_3, masks = data[0][0], data[1][0], data[2][0], data[0][1]
if(epoch >= start_topo_epoch):
img_as_np1 = images_1.cpu().data.numpy()
img_as_np1 = img_as_np1[0] * 255
img_as_np1 = img_as_np1.astype(np.uint8)
img1 = Image.fromarray(img_as_np1.squeeze(0))
img1.save(save_dir + str(batch) +'img.png')
img_as_np1 = masks.cpu().data.numpy()
img_as_np1 = img_as_np1[0] * 255
img_as_np1 = img_as_np1.astype(np.uint8)
img1 = Image.fromarray(img_as_np1)
img1.save(save_dir + str(batch) +'msk.png')
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
outputs_3, likelihoodMap_3 = models[2](images_3.to(device))
predict_map = max_outputs([outputs_1, outputs_2, outputs_3])
if(epoch >= start_topo_epoch):
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
pred_class_1 = torch.argmax(outputs_1, dim=1).float()
pred_class_2 = torch.argmax(outputs_2, dim=1).float()
pred_class_3 = torch.argmax(outputs_3, dim=1).float()
pred_class = torch.argmax(predict_map, dim=1).float()
if epoch >= start_topo_epoch:
img_as_np = pred_class.cpu().data.numpy()
img_as_np = img_as_np[0] * 255
img_as_np = img_as_np.astype(np.uint8)
img = Image.fromarray(img_as_np)
img.save(save_dir + str(batch) +'.png')
topoLoss_1,topoLoss_2,topoLoss_3 = 0, 0, 0
for i in range(len(likelihoodMap_1)):
loss_topo , escape = getTopoLoss([likelihoodMap_1[i], likelihoodMap_2[i], likelihoodMap_3[i]], pred_class[i], masks[i], device, likelihoodMap_final[i])
topoLoss += loss_topo
escapes += escape
elif len(SLICES_COLLECT) == 2:
images_1, images_2, masks = data[0][0], data[1][0], data[0][1]
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
predict_map = max_outputs([outputs_1, outputs_2])
if(epoch >= start_topo_epoch):
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
pred_class_1 = torch.argmax(outputs_1, dim=1).float()
pred_class_2 = torch.argmax(outputs_2, dim=1).float()
topoLoss_1,topoLoss_2 = 0, 0
for i in range(len(likelihoodMap_1)):
topoLoss_1 += getTopoLoss(likelihoodMap_1[i], pred_class_1[i], masks[i], device, likelihoodMap_final[i])
for i in range(len(likelihoodMap_2)):
topoLoss_2 += getTopoLoss(likelihoodMap_2[i], pred_class_2[i], masks[i], device, likelihoodMap_final[i])
topoLoss = (topoLoss_1 + topoLoss_2) / 2
elif len(SLICES_COLLECT) == 1:
images, masks = data[0], data[1]
# print(images.size(),masks.size())
if(epoch >= start_topo_epoch):
img_as_np1 = images.cpu().data.numpy()
img_as_np1 = img_as_np1[0][2] * 255
img_as_np1 = img_as_np1.astype(np.uint8)
img1 = Image.fromarray(img_as_np1)
img1.save(save_dir + str(batch) +'img.png')
img_as_np1 = masks.cpu().data.numpy()
img_as_np1 = img_as_np1[0] * 255
img_as_np1 = img_as_np1.astype(np.uint8)
img1 = Image.fromarray(img_as_np1)
img1.save(save_dir + str(batch) +'msk.png')
# print(images.shape, masks.shape)
predict_map, likelihoodMap = models[0](images.to(device))
if(epoch >= start_topo_epoch):
pred_class = torch.argmax(predict_map, dim=1).float()
for i in range(len(likelihoodMap)):
loss_topo , escape = getTopoLoss([likelihoodMap[i]], pred_class[i], masks[i], device, likelihoodMap[i])
topoLoss += loss_topo
escapes += escape
# topoLoss += getTopoLoss([likelihoodMap[i]], pred_class[i], masks[i], device, likelihoodMap[i])
topoLoss = topoLoss / len(masks)
escapes = escapes / len(masks)
# predict_map = smooth_gaussian(predict_map, device)
loss_ce = loss_fun(predict_map, masks.to(device))
# print(loss_ce)
loss = loss_ce + lamda * torch.log(topoLoss) if (epoch >= start_topo_epoch and topoLoss != 0) else loss_ce
if epoch >= start_topo_epoch:
print('$$$$$$$$$$$$$$$$$$$$$$$$')
print('epoch: ', epoch, 'BATCH: ', batch, 'lamda: ', lamda)
print('loss_total: ', loss, 'loss_ce', loss_ce, 'topoLoss', topoLoss)
print('$$$$$$$$$$$$$$$$$$$$$$$$')
export_topo_train(epoch, batch, lamda, loss, loss_ce, topoLoss, save_dir, escapes)
for optimizer in optimizers:
optimizer.zero_grad()
loss.backward()
for optimizer in optimizers:
optimizer.step()
def get_loss_train(models, data_train, loss_fun, device, SLICES_COLLECT):
for model in models:
model.eval()
total_acc = 0
total_loss = 0
for batch, data in enumerate(data_train):
if len(SLICES_COLLECT) == 3:
images_1, images_2, images_3, masks = data[0][0], data[1][0], data[2][0], data[0][1]
with torch.no_grad():
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
outputs_3, likelihoodMap_3 = models[2](images_3.to(device))
predict_map = max_outputs([outputs_1, outputs_2, outputs_3])
elif len(SLICES_COLLECT) == 2:
images_1, images_2, masks = data[0][0], data[1][0], data[0][1]
with torch.no_grad():
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
predict_map = max_outputs([outputs_1, outputs_2])
elif len(SLICES_COLLECT) == 1:
images, masks = data[0], data[1]
with torch.no_grad():
predict_map, likelihoodMap = models[0](images.to(device))
with torch.no_grad():
# predict_map = smooth_gaussian(predict_map)
loss = loss_fun(predict_map, masks.to(device))
pred_class = torch.argmax(predict_map, dim=1).float()
# pred_class = likelihoodMap > 0.8
acc = accuracy_check_for_batch(masks.cpu(), pred_class.cpu(), masks.size()[0])
total_acc += acc
total_loss += loss.cpu().item()
return total_acc / (batch + 1), total_loss / (batch + 1)
def validate_model(models, data_val, loss_fun, epoch, make_prediction=True, save_folder_name='prediction',
device='cpu', SLICES_COLLECT=[]):
"""
Validation run
"""
# calculating validation loss
total_val_loss = 0
total_val_acc = 0
softmax = nn.Softmax2d()
for batch, data in enumerate(data_val):
if len(SLICES_COLLECT) == 3:
images_1, images_2, images_3, masks = data[0][0], data[1][0], data[2][0], data[0][1]
with torch.no_grad():
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
outputs_3, likelihoodMap_3 = models[2](images_3.to(device))
predict_map = max_outputs([outputs_1, outputs_2, outputs_3])
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
save_prediction_likelihood(likelihoodMap_final, batch, epoch, save_folder_name)
elif len(SLICES_COLLECT) == 2:
images_1, images_2, masks = data[0][0], data[1][0], data[0][1]
with torch.no_grad():
outputs_1, likelihoodMap_1 = models[0](images_1.to(device))
outputs_2, likelihoodMap_2 = models[1](images_2.to(device))
predict_map = max_outputs([outputs_1, outputs_2])
likelihoodMap_final = softmax(predict_map)[:,1,:,:]
save_prediction_likelihood(likelihoodMap_final, batch, epoch, save_folder_name)
elif len(SLICES_COLLECT) == 1:
images, masks = data[0], data[1]
with torch.no_grad():
predict_map, likelihoodMap = models[0](images.to(device))
save_prediction_likelihood(likelihoodMap, batch, epoch, save_folder_name)
save_gt(masks, batch, epoch, save_folder_name)
# save_origin(images, batch, epoch, save_folder_name)
with torch.no_grad():
total_val_loss = total_val_loss + loss_fun(predict_map, masks.to(device)).cpu().item()
# print('out', predict_map.shape) # (1, 2, 1250, 1250)
pred_class = torch.argmax(predict_map, dim=1).float() # (1, 1250, 1250)
# pred_class = likelihoodMap > 0.8
if make_prediction:
im_name = batch
pred_msk = save_prediction_image(pred_class, im_name, epoch, save_folder_name)
acc_val = accuracy_check(masks.cpu(), pred_class.cpu())
total_val_acc += acc_val
return total_val_acc / (batch + 1), total_val_loss / (batch + 1)
def save_prediction_image(pred_class, im_name, epoch, save_folder_name="result_images"):
"""save images to save_path
Args:
pred_class (numpy): pred_class images
save_folder_name (str): saving folder name
"""
img_as_np = pred_class.cpu().data.numpy()
img_as_np = polarize(img_as_np) * 255
img_as_np = img_as_np.astype(np.uint8)
# print(img_as_np, img_as_np.shape)
img = Image.fromarray(img_as_np.squeeze(0))
# organize images in every epoch
desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
# Create the path if it does not exist
if not os.path.exists(desired_path):
os.makedirs(desired_path)
# Save Image!
export_name = str(im_name) + '.png'
img.save(desired_path + export_name)
return img_as_np
def polarize(img):
''' Polarize the value to zero and one
Args:
img (numpy): numpy array of image to be polarized
return:
img (numpy): numpy array only with zero and one
'''
img[img >= 0.5] = 1
img[img < 0.5] = 0
return img
def save_origin(origin, batch, epoch, save_folder_name):
img_as_np = origin.cpu().data.numpy()
img_as_np = img_as_np * 255
img_as_np = img_as_np.astype(np.uint8)
# print(img_as_np, img_as_np.shape)
img = Image.fromarray(img_as_np.squeeze(0).squeeze(0))
# organize images in every epoch
desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
# Create the path if it does not exist
if not os.path.exists(desired_path):
os.makedirs(desired_path)
# Save Image!
export_name = str(batch) + 'org.png'
img.save(desired_path + export_name)
def save_prediction_likelihood(likelihoodMap, batch, epoch, save_folder_name="result_images"):
img_as_np = likelihoodMap.cpu().data.numpy()
img_as_np = img_as_np * 255
img_as_np = img_as_np.astype(np.uint8)
# print(img_as_np, img_as_np.shape)
img = Image.fromarray(img_as_np.squeeze(0))
# organize images in every epoch
desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
# Create the path if it does not exist
if not os.path.exists(desired_path):
os.makedirs(desired_path)
# Save Image!
export_name = str(batch) + 'lh.png'
img.save(desired_path + export_name)
def save_gt(masks, batch, epoch, save_folder_name):
img_as_np = masks.cpu().data.numpy()
img_as_np = img_as_np * 255
img_as_np = img_as_np.astype(np.uint8)
# print(img_as_np, img_as_np.shape)
img = Image.fromarray(img_as_np.squeeze(0))
# organize images in every epoch
desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
# Create the path if it does not exist
if not os.path.exists(desired_path):
os.makedirs(desired_path)
# Save Image!
export_name = str(batch) + 'gt.png'
img.save(desired_path + export_name)
if __name__ == "__main__":
# A full forward pass
im = torch.randn(1, 2, 1250, 1250)
validate_model()