-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.py
442 lines (364 loc) · 15.6 KB
/
model.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# --------------------------------------------------
# Generator model for brain sythesis
#
# Sergi Valverde 2018
# University of Girona
# --------------------------------------------------
import os
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from ._utils.model_utils import UpdateStatus, EarlyStopping
from ._utils.model_utils import ResCoreElement, Pooling3D
class ResUnet(nn.Module):
"""
Basic U-net model using residual layers. Control the input channels,
output channels, and scaling of output filters
"""
def __init__(self, input_channels,
output_channels,
scale,
classification=False,
use_bn=True,
use_leaky=False,
leaky_p=0.2):
super(ResUnet, self).__init__()
self.use_bn = use_bn
self.use_leaky = use_leaky
self.leaky_p = leaky_p
self.classification = classification
# conv 1 down
self.conv1 = ResCoreElement(input_channels,
int(scale * 32),
use_bn,
use_leaky,
leaky_p)
self.pool1 = Pooling3D(int(scale * 32), use_bn)
# conv 2 down
self.conv2 = ResCoreElement(int(scale * 32), int(scale * 64),
use_bn, use_leaky, leaky_p)
self.pool2 = Pooling3D(int(scale * 64), use_leaky, leaky_p)
# conv 3 down
self.conv3 = ResCoreElement(int(scale * 64), int(scale * 128),
use_bn, use_leaky, leaky_p)
self.pool3 = Pooling3D(int(scale * 128), use_bn, use_leaky, leaky_p)
# conv 4
self.conv4 = ResCoreElement(int(scale*128), int(scale*256),
use_bn, use_leaky, leaky_p)
# up 1
self.up1 = nn.ConvTranspose3d(int(scale*256),
int(scale*128),
kernel_size=2,
stride=2)
# conv 5
self.conv5 = ResCoreElement(int(scale*128), int(scale*128),
use_bn, use_leaky, leaky_p)
# conv 6 up
self.bn_add35 = nn.BatchNorm3d(int(scale*128))
self.conv6 = ResCoreElement(int(scale*128), int(scale*128),
use_bn, use_leaky, leaky_p)
self.up2 = nn.ConvTranspose3d(int(scale*128),
int(scale*64),
kernel_size=2,
stride=2)
# conv 7 up
self.bn_add22 = nn.BatchNorm3d(int(scale*64))
self.conv7 = ResCoreElement(int(scale*64), int(scale*64),
use_bn, use_leaky, leaky_p)
self.up3 = nn.ConvTranspose3d(int(scale*64),
int(scale*32),
kernel_size=2,
stride=2)
# conv 8 up
self.bn_add13 = nn.BatchNorm3d(int(scale*32))
self.conv8 = ResCoreElement(int(scale*32), int(scale*32),
use_bn, use_leaky, leaky_p)
# reconstruction
self.conv9 = nn.Conv3d(int(scale * 32),
output_channels,
kernel_size=1)
model_parameters = filter(lambda p: p.requires_grad, self.parameters())
# nparams = sum([np.prod(p.size()) for p in model_parameters])
# print("ResUnet3D network with {} parameters".format(nparams))
def forward(self, x, encoder=False):
# --------------------
# encoder
# --------------------
x1 = self.conv1(x)
x1d = self.pool1(x1)
x2 = self.conv2(x1d)
x2d = self.pool2(x2)
x3 = self.conv3(x2d)
x3d = self.pool3(x3)
x4 = self.conv4(x3d)
# --------------------
# decoder
# --------------------
up1 = self.up1(x4)
x5 = self.conv5(up1)
add_35 = self.bn_add35(x5 + x3) if self.use_bn is True else x5 + x3
x6 = self.conv6(add_35)
up2 = self.up2(x6)
add_22 = self.bn_add22(up2 + x2) if self.use_bn is True else x2 + up2
x7 = self.conv7(add_22)
up3 = self.up3(x7)
add_13 = self.bn_add13(up3 + x1) if self.use_bn is True else x1 + up3
x8 = self.conv8(add_13)
return F.softmax(self.conv9(x8), dim=1) if self.classification \
else self.conv9(x8)
class Parietal(nn.Module):
"""
Quick and dirty Voxelmorph implementation
"""
def __init__(self,
input_channels=1,
output_channels=1,
patch_shape=(32, 32, 32),
scale=0.5,
training_epochs=200,
shuffle_data=True,
patience=50,
pat_interval=0.00,
batch_size=32,
train_split=0.3,
model_name=None,
gpu_mode=True,
gpu_list=[0],
use_bn=False,
load_weights=False,
loss_weights=None,
model_path=None,
resume_training=False):
super(Parietal, self).__init__()
# network parameters
self.input_channels = input_channels
self.output_channels = output_channels
self.scale = scale
self.patch_size = patch_shape
self.num_epochs = training_epochs
self.shuffle_data = shuffle_data
self.patience = patience
self.pat_interval = pat_interval
self.batch_size = batch_size
self.train_split = train_split
self.model_name = model_name
self.use_bn = use_bn
self.resume_training = resume_training
self.gpu_mode = gpu_mode
# model path
if model_path is None:
self.model_path = os.path.join(os.getcwd(), 'models')
else:
self.model_path = model_path
# lesion classification architecture
self.skull_net = ResUnet(input_channels=self.input_channels,
output_channels=2,
scale=self.scale,
use_bn=True,
classification=True)
self.device_name = 'cuda:' + str(gpu_list[0])
if self.gpu_mode:
self.device = torch.device(self.device_name)
else:
self.device = torch.device('cpu')
self.gpu_list = gpu_list
# load weights if model name if passed as an option
# and load weights flag is activated
if load_weights and model_name:
self.load_weights(model_name)
def train_model(self, t_dataloader, v_dataloader):
"""
train the wnet model
"""
training = True
# send models to device
self.skull_net = self.skull_net.to(self.device)
# optimizers
net_optimizer = optim.Adadelta(self.skull_net.parameters())
epoch = 1
# train
# start early stopping
early_stopper = EarlyStopping(epoch=epoch,
metric='acc',
patience=self.patience)
# color handling for training / testing
update = UpdateStatus(pat_interval=self.pat_interval)
# register measures
update.register_new_element('train_loss', 'decremental')
update.register_new_element('train_accuracy', 'incremental')
update.register_new_element('train_dsc', 'incremental')
update.register_new_element('val_loss', 'decremental')
update.register_new_element('val_accuracy', 'incremental')
update.register_new_element('val_dsc', 'incremental')
try:
while training:
train_loss = 0
train_accuracy = 0
val_loss = 0
val_accuracy = 0
train_dsc = 0
val_dsc = 0
self.skull_net.train()
epoch_time = time.time()
# train on batch
for b, batch in enumerate(t_dataloader):
x = batch[0].to(self.device)
y = batch[1].to(self.device)
net_optimizer.zero_grad()
pred = self.skull_net(x)
loss = F.cross_entropy(
torch.log(torch.clamp(pred, 1E-7, 1.0)),
y.squeeze(dim=1).long(), ignore_index=2)
# loss = dsc_loss(pred, y)
train_loss += loss.item()
loss.backward()
net_optimizer.step()
# relative accuracy
train_dsc += self.DSC_score(pred, y).item()
pred = pred.max(1, keepdim=True)[1]
train_accuracy += pred.eq(
y.view_as(pred).long()).sum().item() / np.prod(y.shape)
# clear cache
# cuda_memory = torch.cuda.memory_allocated(self.device)
# if b % 10 == 0:
# print(b, cuda_memory)
# torch.cuda.empty_cache()
# update losses
train_loss /= (b+1)
train_accuracy /= (b+1)
train_dsc /= (b + 1)
# --------------------------------------------------
# compute validation
# --------------------------------------------------
self.skull_net.eval()
for b, batch in enumerate(v_dataloader):
x = batch[0].to(self.device)
y = batch[1].to(self.device)
with torch.no_grad():
pred = self.skull_net(x)
# loss = dsc_loss(pred, y)
loss = F.cross_entropy(
torch.log(torch.clamp(pred, 1E-7, 1.0)),
y.squeeze(dim=1).long(), ignore_index=2)
# loss = focal_loss(pred, y)
val_loss += loss.item()
# relative accuracy
pred = pred.max(1, keepdim=True)[1]
val_accuracy += pred.eq(
y.view_as(
pred).long()).sum().item() / np.prod(y.shape)
val_dsc += self.DSC_score(pred, y).item()
# update losses
val_loss /= (b+1)
val_accuracy /= (b+1)
val_dsc /= (b+1)
t_time = time.time() - epoch_time
print('Epoch: {} Time: {:.2f}'.format(epoch, t_time),
'Train lesion loss: {}'.format(
update.update_element('train_loss',
train_loss)),
'Train lesion acc: {}'.format(
update.update_element('train_accuracy',
train_accuracy)),
'Train lesion DSC: {}'.format(
update.update_element('train_dsc',
train_dsc)),
'Val lesion loss: {}'.format(
update.update_element('val_loss',
val_loss)),
'Val lesion acc: {}'.format(
update.update_element('val_accuracy',
val_accuracy)),
'Val lesion DSC: {}'.format(
update.update_element('val_dsc',
val_dsc)))
# update epochs
epoch += 1
# check epoch for early stopping:
if early_stopper.save_epoch(val_dsc, epoch):
# save the model
self.save_checkpoint({
'epoch': epoch + 1,
'state_dict_les': self.skull_net.state_dict()})
# check if the model has to be stoped
if early_stopper.stop_model():
training = False
print("--------------------------------------------------")
print("Stopping training at epoch", epoch,
"best val_loss:", early_stopper.get_best_value())
print("--------------------------------------------------")
if epoch > self.num_epochs:
training = False
except KeyboardInterrupt:
pass
def save_checkpoint(self, state):
"""
save the best net state
"""
# if os.path.exists(os.path.join(self.model_path, 'models')) is False:
# os.mkdir(os.path.join(self.model_path, 'models'))
# filename = self.model_path + '/models/' + self.model_name
filename = os.path.join(self.model_path, self.model_name)
torch.save(state, filename)
def load_weights(self, model_name=None):
"""
load network weights
"""
if model_name is not None:
self.model_name = model_name
# send models to device
self.skull_net = self.skull_net.to(self.device)
# load weights
# filename = self.model_path + '/models/' + self.model_name
filename = os.path.join(self.model_path, self.model_name)
# filename = './models/' + self.model_name
# print("--------------------------------------------------")
if os.path.isfile(filename):
checkpoint = torch.load(filename, map_location=self.device)
self.skull_net.load_state_dict(checkpoint['state_dict_les'])
# print("=> loaded weights '{}'".format(self.model_name))
else:
print("=> no checkpoint found at '{}'".format(self.model_name))
# print("--------------------------------------------------")
def test_net(self, test_input):
"""
Testing the network
To doc
"""
# output reconstruction
bs, cs, xs, ys, zs = test_input.shape
lesion_out = np.zeros((bs, 1, xs, ys, zs)).astype('float32')
self.skull_net.eval()
with torch.no_grad():
for b in range(0, len(lesion_out), self.batch_size):
x = torch.tensor(
test_input[b:b+self.batch_size]).to(self.device)
pred = self.skull_net(x)
output = pred[:, 1].unsqueeze(dim=1)
# save the result back
lesion_out[b:b+self.batch_size] = output.cpu().numpy()
return lesion_out
def DSC_score(self, pred, label, smooth=1.):
"""
DSC loss
"""
pred = pred.float() # only lesion probabilities
label = label.float()
label[label == 2] = 0
dice_numerator = 2.0 * torch.sum(pred * label, dim=0)
dice_denominator = torch.sum(pred, dim=0) + torch.sum(label, dim=0)
dice_score = (dice_numerator + smooth) / (dice_denominator + smooth)
return torch.mean(dice_score) * smooth
def accuracy(self, pred, label, smooth=1.):
"""
Accuracy
"""
pred = pred.float() # only lesion probabilities
label = label.float()
pred_max = pred.max(1, keepdim=True)[1].float()
numerator = torch.abs(pred_max - label).sum()
denominator = label.sum()
return numerator / denominator