forked from CherBass/CapsPix2Pix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_u_net.py
538 lines (423 loc) · 21.9 KB
/
train_u_net.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from __future__ import print_function
import matplotlib
matplotlib.use('agg')
import torch
import cv2
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torchvision.utils as vutils
from torch.autograd import Variable
from Capsule_Networks import capspix2pixG as NetG
from AxonDataset import AxonDataset
import os
import json
import numpy as np
import matplotlib.pyplot as plt
from helper_functions import weights_init
import argparse
from custom_losses import dice_loss as dice_loss
from custom_losses import dice_coeff as dice_coeff
from custom_losses import dice_coeff as dice_test
import time
from torch.utils.data.sampler import SubsetRandomSampler
print(['Using device: ', torch.cuda.get_device_name(0)])
from u_net import UNet
plt.switch_backend('agg')
def achieve_args():
parse = argparse.ArgumentParser()
# experiment params
parse.add_argument('--experiment', type=str, default='unet_capspix2pix_SSM_',
help='Experiment name.')
parse.add_argument('--group_exp', type=str, default='unet_capspix2pix_SSM',
help='Group experiment name (for repeats).')
parse.add_argument('--data_load_name', type=str,
default='capspix2pix_SSM',
help='data to load, as described in the paper:'
'capspix2pix_SSM'
'capspix2pix_AR'
'pix2pix_SSM'
'pix2pix_AR'
'PBAM_SSM'
'real_data')
parse.add_argument('--pretrained', action='store_true',
help='whether to load pretrained model')
# loading model params if generating online/ loading pretrained model
parse.add_argument('--generate_online', action='store_true'
, help='whether GAN generates new data each epoch')
parse.add_argument('--experiment_load', type=str,
default='', help='experiment name for pretrained models or online gen')
parse.add_argument('--model_load', type=str, default='Model_test_best.pt',
help='model load name for pretrained models or online generation')
parse.add_argument('--dilation', type=int, default=0,
help='whether to dilate the labels')
# u-net params
parse.add_argument('--image_size', type=int, default=64,
help='image size (default=64)')
parse.add_argument('--val_image_size', type=int, default=64,
help='validation image size (default=64)')
parse.add_argument('--test_image_size', type=int, default=64,
help='test image size (default=64)')
parse.add_argument('--val_split', type=float, default=0.2,
help='val split ratio (default=0.2)')
parse.add_argument('--n_class', type=int, default=1,
help='num classes (default=1)')
parse.add_argument('--batch_size', type=int, default=32,
help='Batch size (default=32)')
parse.add_argument('--iter_size', type=int, default=1,
help='How many batches before update (default=1)')
parse.add_argument('--val_batch_size', type=int, default=32,
help='Val Batch size (default=32)')
parse.add_argument('--normalise_data', type=bool, default=False,
help='normalise data between [-1,1] (default=False)')
parse.add_argument('--drop_out', type=float, default=0.5,
help='dropout (default=0.5)')
parse.add_argument('--dropout_train', type=bool, default=True,
help='whether to apply dropout during training (default=True)')
parse.add_argument('--batch_norm', type=bool, default=False,
help='whether to apply batch norm during training (default=False)')
parse.add_argument('--lr', type=float, default=1e-05,
help='Learning rate (default=1e-05)')
parse.add_argument('--alpha', type=float, default=0.9,
help='momentum (default=0.9)')
parse.add_argument('--betas', type=float, default=(0.5, 0.999),
help='betas (default=0.5, 0.999)')
parse.add_argument('--optim', type=str, default='Adam', # options: Adam, RSMprop, SGD
help='optimiser (default=Adam)')
parse.add_argument('--epochs', type=int, default=100,
help='Number of training epochs.')
parse.add_argument('--save_every', type=int, default=200, help='save error every')
parse.add_argument('--image_checkpoint', action='store_true'
, help='whether to save images during training')
parse.add_argument('--save_dir', type=str, default='results/',
help='Path to save the trained models.')
parse.add_argument('--lambdaIMG', type=float, default=1, help='lambdaIMG')
parse.add_argument('--lambda', type=float, default=0.5, help='lambda')
args = parse.parse_args()
return args
if __name__ == '__main__':
args = vars(achieve_args())
# Setting parameters
timestr = time.strftime("%d%m%Y-%H%M")
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
args['time_date'] = timestr
experiment = args['experiment']
directory = args['save_dir'] + '/' + args['group_exp'] + '/' + experiment + timestr
path = os.path.join(__location__,directory)
if not os.path.exists(path):
os.makedirs(path)
# save parameters
with open(path + '/parameters.json', 'w') as file:
json.dump(args, file, indent=4, sort_keys=True)
args['cuda'] = torch.cuda.is_available()
all_error = np.zeros(0)
all_error_L1 = np.zeros(0)
all_error_dice = np.zeros(0)
all_dice = np.zeros(0)
all_val_dice = np.zeros(1)
all_val_error = np.zeros(0)
all_test_dice = np.zeros(1)
all_test_error = np.zeros(0)
axon_dataset = AxonDataset(data_name=args['data_load_name'], folder='aug_images_64/', type='train', normalise=args['normalise_data'])
axon_dataset_test = AxonDataset(data_name='org64', folder='org64/', type='test')
## We need to further split our training dataset into training and validation sets.
# Define the indices
indices = list(range(len(axon_dataset))) # start with all the indices in training set
split = int(len(indices)*args['val_split']) # define the split size
# Define your batch_size
batch_size = args['batch_size']
# Random, non-contiguous split
validation_idx = np.random.choice(indices, size=split, replace=False)
train_idx = list(set(indices) - set(validation_idx))
train_sampler = SubsetRandomSampler(train_idx)
validation_sampler = SubsetRandomSampler(validation_idx)
train_loader = torch.utils.data.DataLoader(axon_dataset, batch_size = args['batch_size'],
sampler=train_sampler) # We use dataLoader to get the images of the training set batch by batch.
val_loader = torch.utils.data.DataLoader(axon_dataset, batch_size = args['val_batch_size'],
sampler=validation_sampler) # We use dataLoader to get the images of the training set batch by batch.
test_loader = torch.utils.data.DataLoader(axon_dataset_test, batch_size=32, shuffle=False) # We use dataLoader to get the images of the training set batch by batch.
# initialise networks
net = UNet(args)
# if generate online
if args['generate_online']:
experiment = args['experiment_load']
directory = 'results/' + experiment
path_2 = os.path.join(__location__, directory)
with open(path_2 + '/parameters.json') as file:
args_gan = json.load(file)
args_gan['batch_size'] = 32
args_gan['state'] = 'val'
args_gan['noise_source'] = 'input'
args_gan['train_fc'] = True
args_gan['drop_out_train'] = False
netG = NetG(args_gan)
if args['cuda']:
netG = netG.cuda()
netG.load_state_dict(torch.load(path_2 + '/' + args['model_load']))
netG.train()
if args['cuda']:
net = net.cuda()
if args['pretrained']:
experiment_load = args['experiment_load']
load_directory = args['save_dir'] + experiment_load
load_path = os.path.join(__location__, load_directory)
net.load_state_dict(torch.load(load_path+'/'+args['model_load']))
if args['optim'] == 'RSMprop':
optimizer = optim.RMSprop(filter(lambda p: p.requires_grad, net.parameters()), lr=args['lr'], alpha=args['alpha'], weight_decay=0)
elif args['optim'] == 'Adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=args['lr'], betas=args['betas'])
elif args['optim'] == 'SGD':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args['lr'])
optimizer.zero_grad()
for epoch in range(args['epochs']):
##########
# Train
##########
t0 = time.time()
for i, (data, label) in enumerate(train_loader):
if args['dilation'] > 0:
for d in range(label.size(0)):
kernel = np.ones((3, 3), np.uint8)
label_dilation = cv2.dilate(label.cpu().numpy()[d, 0, :, :], kernel=kernel, iterations=args['dilation'])
label_dilation = torch.Tensor(label_dilation)
label[d] = label_dilation
args['state'] = 'train'
net.train()
# first train discriminator on real data- target = 1
net.zero_grad()
target_real = torch.ones(data.size()[0])
batch_size = data.size()[0]
if args['cuda']:
data, target_real, label = data.cuda(), target_real.cuda(), label.cuda()
data, target_real, label = Variable(data), Variable(target_real), Variable(label)
if args['generate_online']:
if args_gan['noise_source'] == 'input':
noise = torch.randn(data.size()[0], args_gan['noise_size'])
elif (args_gan['noise_source'] == 'broadcast'):
noise = torch.randn(data.size()[0], args_gan['noise_size'], 1, 1)
num_copies = args_gan['image_size'] // args_gan['noise_size']
if args_gan['image_size'] % args_gan['noise_size'] == 0:
noise = noise.repeat(1, num_copies, args_gan['image_size']) # specifies number of copies
noise = noise.unsqueeze(1)
else:
print('noise size is indivisible by image size')
elif (args_gan['noise_source'] == 'broadcast_conv'):
noise = torch.randn(data.size()[0], args_gan['noise_size'], 1, 1)
noise = noise.repeat(1, 1, args_gan['image_size'], args_gan['image_size']) # specifies number of copies
else:
noise = torch.zeros(0)
if args['cuda']:
noise = noise.cuda()
noise = Variable(noise)
data, gan_reconstruction = netG(label, noise, args_gan)
pred = net(data, args)
err = dice_loss(pred, label)
# compare generated image to data- metric
dice_value = dice_coeff(pred, label).item()
err.backward()
optimizer.step()
optimizer.zero_grad()
time_elapsed = time.time() - t0
print('[{:d}/{:d}][{:d}/{:d}] Elapsed_time: {:.0f}m{:.0f}s Loss: {:.4f} Dice: {:.4f}'
.format(epoch, args['epochs'], i, len(train_loader), time_elapsed // 60, time_elapsed % 60,
err.item(), dice_value))
if i % args['save_every'] == 0:
# eval mode to remove dropout and batchnorm
net.eval()
args['state'] = 'val'
if args['image_checkpoint']:
vutils.save_image(data.data, '%s/epoch_%03d_i_%03d_train_data.png' % (path, epoch, i),
normalize=True)
vutils.save_image(label.data, '%s/epoch_%03d_i_%03d_train_label.png' % (path, epoch, i),
normalize=True)
vutils.save_image(pred.data, '%s/epoch_%03d_i_%03d_train_pred.png' % (path, epoch, i),
normalize=True)
error = err.item()
all_error = np.append(all_error, error)
all_dice = np.append(all_dice, dice_value)
np.save(path + '/train_error.npy', all_error)
np.save(path + '/train_dice.npy', all_dice)
if all_dice[-1] >= np.max(all_dice):
torch.save(net.state_dict(), '%s/Model_train_best.pt' % (path))
args['best_train_dice_model_saved'] = 'epoch_' + str(epoch) + '_itr_' + str(i)
with open(path + '/parameters.json', 'w') as file:
json.dump(args, file, indent=4, sort_keys=True)
# #############
# # Validation
# #############
mean_error = np.zeros(0)
mean_dice = np.zeros(0)
t0 = time.time()
for i, (data, label) in enumerate(val_loader):
if args['dilation'] > 0:
for d in range(label.size(0)):
kernel = np.ones((3, 3), np.uint8)
label_dilation = cv2.dilate(label.cpu().numpy()[d, 0, :, :], kernel=kernel, iterations=args['dilation'])
label_dilation = torch.Tensor(label_dilation)
label[d] = label_dilation
net.eval()
batch_size = data.size()[0]
if args['cuda']:
data, label = data.cuda(), label.cuda()
data, label = Variable(data), Variable(label)
if args['generate_online']:
if args_gan['noise_source'] == 'input':
noise = torch.randn(data.size()[0], args_gan['noise_size'])
elif (args_gan['noise_source'] == 'broadcast'):
noise = torch.randn(data.size()[0], args_gan['noise_size'], 1, 1)
num_copies = args_gan['image_size'] // args_gan['noise_size']
if args_gan['image_size'] % args_gan['noise_size'] == 0:
noise = noise.repeat(1, num_copies, args_gan['image_size']) # specifies number of copies
noise = noise.unsqueeze(1)
else:
print('noise size is indivisible by image size')
elif (args_gan['noise_source'] == 'broadcast_conv'):
noise = torch.randn(data.size()[0], args_gan['noise_size'], 1, 1)
noise = noise.repeat(1, 1, args_gan['image_size'], args_gan['image_size']) # specifies number of copies
else:
noise = torch.zeros(0)
if args['cuda']:
noise = noise.cuda()
noise = Variable(noise)
data, gan_reconstruction = netG(label, noise, args_gan)
pred = net(data, args)
err = dice_loss(pred, label)
# compare generated image to data- metric
dice_value = dice_coeff(pred, label).item()
if i == 0:
vutils.save_image(data.data, '%s/epoch_%03d_i_%03d_val_data.png' % (path, epoch, i),
normalize=True)
vutils.save_image(label.data, '%s/epoch_%03d_i_%03d_val_label.png' % (path, epoch, i),
normalize=True)
vutils.save_image(pred.data, '%s/epoch_%03d_i_%03d_val_pred.png' % (path, epoch, i),
normalize=True)
error = err.item()
mean_error = np.append(mean_error, error)
mean_dice = np.append(mean_dice, dice_value)
all_val_error = np.append(all_val_error, np.mean(mean_error))
all_val_dice = np.append(all_val_dice, np.mean(mean_dice))
if all_val_dice[-1] >= np.max(all_val_dice):
torch.save(net.state_dict(), '%s/Model_val_best.pt' % (path))
args['best_val_dice_model_saved'] = 'epoch_' + str(epoch) + '_itr_' + str(i)
with open(path + '/parameters.json', 'w') as file:
json.dump(args, file, indent=4, sort_keys=True)
np.save(path + '/val_error.npy', all_val_error)
np.save(path + '/val_dice.npy', all_val_dice)
time_elapsed = time.time() - t0
print('Elapsed_time: {:.0f}m{:.0f}s Val Dice: {:.4f}'
.format(time_elapsed // 60, time_elapsed % 60, mean_dice.mean()))
#############
# Test
#############
test_scores = np.zeros(0)
thresh = np.linspace(0, 1, num=50)
temp_dice = np.zeros(len(axon_dataset_test))
thresh = torch.Tensor(thresh)
test_pred = torch.zeros(len(axon_dataset_test), 1,
args['test_image_size'], args['test_image_size'])
test_pred_binary = torch.zeros(len(axon_dataset_test), 1,
args['test_image_size'], args['test_image_size'])
test_data = torch.zeros(len(axon_dataset_test), 1,
args['test_image_size'], args['test_image_size'])
test_label = torch.zeros(len(axon_dataset_test), 1,
args['test_image_size'], args['test_image_size'])
thresh_dice = np.zeros(len(thresh))
t0 = time.time()
a = 0
for i, (data, label) in enumerate(test_loader):
net.eval()
batch_size = data.size()[0]
num_test = (data.size(0))
if args['cuda']:
data, label = data.cuda(), label.cuda()
data, label = Variable(data), Variable(label)
pred = net(data, args)
for n in np.arange(0, num_test):
test_pred[a] = pred.data[n]
test_data[a] = data.data[n]
test_label[a] = label.data[n]
a = a + 1
ta = 0
for t in thresh:
a = 0
for n in np.arange(0, len(axon_dataset_test)):
temp_pred = (test_pred[n] > t).type(torch.FloatTensor)
if args['cuda']:
temp_pred = temp_pred.cuda()
temp_label = test_label[n].cuda()
temp_pred = Variable(temp_pred)
temp_label = Variable(temp_label)
temp_dice[a] = dice_coeff(temp_pred, temp_label)
a = a+1
thresh_dice[ta] = temp_dice.mean()
ta=ta+1
best_thresh_ind = np.argmax(thresh_dice)
best_thresh = thresh[best_thresh_ind]
a = 0
for n in np.arange(len(axon_dataset_test)):
temp_pred = (test_pred[n] > best_thresh).type(torch.FloatTensor)
if args['cuda']:
temp_pred = temp_pred.cuda()
temp_label = test_label[n].cuda()
temp_pred = Variable(temp_pred)
temp_label = Variable(temp_label)
temp_dice[a] = dice_coeff(temp_pred, temp_label)
test_pred_binary[a] = temp_pred.data
a = a + 1
test_scores = temp_dice
vutils.save_image(test_data, '%s/epoch_%03d_i_%03d_test_data.png' % (path, epoch, i),
normalize=True, nrow=20)
vutils.save_image(test_label, '%s/epoch_%03d_i_%03d_test_label.png' % (path, epoch, i),
normalize=True, nrow=20)
vutils.save_image(test_pred_binary,
'%s/epoch_%03d_i_%03d_test_pred.png' % (path, epoch, i),
normalize=True, nrow=20)
mean_test_scores = np.mean(test_scores)
all_test_dice = np.append(all_test_dice, mean_test_scores)
np.save(path + '/test_dice.npy', all_test_dice)
if mean_test_scores >= np.max(all_test_dice):
torch.save(net.state_dict(), '%s/Model_test_best.pt' % (path))
args['best_test_dice_model_saved'] = 'epoch_' + str(epoch) + '_itr_' + str(i)
args['best_test_results'] = test_scores.tolist()
args['best_mean_test_results'] = mean_test_scores
with open(path + '/parameters.json', 'w') as file:
json.dump(args, file, indent=4, sort_keys=True)
time_elapsed = time.time() - t0
print('Elapsed_time: {:.0f}m{:.0f}s Test Dice: {:.4f} Best Test Dice: {:.4f}'
.format(time_elapsed // 60, time_elapsed % 60, mean_test_scores, np.max(all_test_dice)))
########
# Save
########
num_it_per_epoch_train = ((train_loader.dataset.x_data.shape[0] * (1 - args['val_split'])) // (
args['save_every'] * args['batch_size'])) + 1
epochs_train = np.arange(1,all_error.size+1) / num_it_per_epoch_train
epochs_val = np.arange(0,all_val_dice.size)
epochs_val_error = np.arange(1,all_val_error.size+1)
epochs_test = np.arange(0,all_test_dice.size)
plt.figure()
plt.plot(epochs_train, all_error, label='error_train')
plt.plot(epochs_val_error, all_val_error, label='error_val')
plt.xlabel('epochs')
plt.legend()
plt.title('Loss')
plt.savefig(path + '/loss_train.png')
plt.close()
plt.figure()
plt.plot(epochs_train, all_dice, label='dice_train')
plt.plot(epochs_val, all_val_dice, label='dice_val')
plt.xlabel('epochs')
plt.legend()
plt.title('Dice score')
plt.savefig(path + '/dice_train.png')
plt.close()
plt.figure()
plt.plot(epochs_test, all_test_dice)
plt.xlabel('epochs')
plt.legend()
plt.title('Dice score')
plt.savefig(path + '/dice_test.png')
plt.close()
torch.save(net.state_dict(), '%s/Model_epoch_%03d.pt' % (path, args['epochs']))
print('finished')