-
Notifications
You must be signed in to change notification settings - Fork 12
/
main-entropy-minimization.py
698 lines (483 loc) · 25.6 KB
/
main-entropy-minimization.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
devicess = [0]
import time
import argparse
import numpy as np
from PIL import Image
import torch
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torch import nn
from torchvision import transforms
import torch.distributed as dist
import math
import torchio
from torchio.transforms import (
RandomFlip,
RandomAffine,
RandomElasticDeformation,
RandomNoise,
RandomMotion,
RandomBiasField,
RescaleIntensity,
Resample,
ToCanonical,
ZNormalization,
CropOrPad,
HistogramStandardization,
OneOf,
Compose,
)
from medpy.io import load,save
from tqdm import tqdm
from torchvision import utils
from hparam import hparams as hp
from utils.metric import metric
from torch.optim.lr_scheduler import ReduceLROnPlateau,StepLR,CosineAnnealingLR
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
from torch.nn.modules.loss import CrossEntropyLoss
source_train_dir = hp.source_train_dir
label_train_dir = hp.label_train_dir
unlabel_dir_train = hp.unlabel_dir
source_test_dir = hp.source_test_dir
label_test_dir = hp.label_test_dir
output_int_dir = hp.output_int_dir
output_float_dir = hp.output_float_dir
def entropy_loss(p, C=2):
# p N*C*W*H*D
y1 = -1*torch.sum(p*torch.log(p+1e-6), dim=1) / \
torch.tensor(np.log(C)).cuda()
ent = torch.mean(y1)
return ent
class DiceLoss(nn.Module):
def __init__(self, n_classes):
super(DiceLoss, self).__init__()
self.n_classes = n_classes
def _one_hot_encoder(self, input_tensor):
tensor_list = []
for i in range(self.n_classes):
temp_prob = input_tensor == i * torch.ones_like(input_tensor)
tensor_list.append(temp_prob)
output_tensor = torch.cat(tensor_list, dim=1)
return output_tensor.float()
def _dice_loss(self, score, target):
target = target.float()
smooth = 1e-5
intersect = torch.sum(score * target)
y_sum = torch.sum(target * target)
z_sum = torch.sum(score * score)
loss = (2 * intersect + smooth) / (z_sum + y_sum + smooth)
loss = 1 - loss
return loss
def forward(self, inputs, target, weight=None, softmax=False):
if softmax:
inputs = torch.softmax(inputs, dim=1)
target = self._one_hot_encoder(target)
if weight is None:
weight = [1] * self.n_classes
assert inputs.size() == target.size(), 'predict & target shape do not match'
class_wise_dice = []
loss = 0.0
for i in range(0, self.n_classes):
dice = self._dice_loss(inputs[:, i], target[:, i])
class_wise_dice.append(1.0 - dice.item())
loss += dice * weight[i]
return loss / self.n_classes
def update_ema_variables(model, ema_model, alpha, global_step):
# Use the true average until the exponential average is more correct
alpha = min(1 - 1 / (global_step + 1), alpha)
for ema_param, param in zip(ema_model.parameters(), model.parameters()):
ema_param.data.mul_(alpha).add_(1 - alpha, param.data)
def parse_training_args(parser):
"""
"""
parser.add_argument('-o', '--output_dir', type=str, default='logs-0506-1', required=False, help='Directory to save checkpoints')
parser.add_argument('--latest-checkpoint-file', type=str, default='checkpoint_latest.pt', help='Store the latest checkpoint in each epoch')
# training
training = parser.add_argument_group('training setup')
training.add_argument('--epochs', type=int, default=20, help='Number of total epochs to run')
training.add_argument('--epochs-per-checkpoint', type=int, default=1, help='Number of epochs per checkpoint')
training.add_argument('--batch', type=int, default=1, help='batch-size') #12
training.add_argument('--sample', type=int, default=12, help='number of samples during training') #12
parser.add_argument(
'-k',
"--ckpt",
type=str,
default=None,
help="path to the checkpoints to resume training",
)
parser.add_argument("--init-lr", type=float, default=0.005, help="learning rate") #0.001
parser.add_argument(
"--wandb", action="store_true", help="use weights and biases logging"
)
parser.add_argument(
"--local_rank", type=int, default=0, help="local rank for distributed training"
)
training.add_argument('--amp-run', action='store_true', help='Enable AMPa')
training.add_argument('--cudnn-enabled', default=True, help='En able cudnn')
training.add_argument('--cudnn-benchmark', default=True, help='Run cudnn benchmark')
training.add_argument('--disable-uniform-initialize-bn-weight', action='store_true', help='disable uniform initialization of batchnorm layer weight')
return parser
def train():
parser = argparse.ArgumentParser(description='PyTorch Medical Segmentation Training')
parser = parse_training_args(parser)
args, _ = parser.parse_known_args()
args = parser.parse_args()
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = args.cudnn_enabled
torch.backends.cudnn.benchmark = args.cudnn_benchmark
from data_function import MedData_train,Med_unlabel_train,My_unlabel
os.makedirs(args.output_dir, exist_ok=True)
if hp.mode == '2d':
#from models.two_d.unet import Unet
#model = Unet(in_channels=hp.in_class, classes=hp.out_class)
from models.two_d.miniseg import MiniSeg
model = MiniSeg(in_input=hp.in_class, classes=hp.out_class)
# from models.two_d.fcn import FCN32s as fcn
# model = fcn(in_class =hp.in_class,n_class=hp.out_class)
# from models.two_d.segnet import SegNet
# model = SegNet(input_nbr=hp.in_class,label_nbr=hp.out_class)
#from models.two_d.deeplab import DeepLabV3
#model = DeepLabV3(in_class=hp.in_class,class_num=hp.out_class)
# from models.two_d.unetpp import ResNet34UnetPlus
# model = ResNet34UnetPlus(num_channels=hp.in_class,num_class=hp.out_class)
# from models.two_d.pspnet import PSPNet
# model = PSPNet(in_class=hp.in_class,n_classes=hp.out_class)
elif hp.mode == '3d':
# from models.three_d.unet3d import UNet3D
# model = UNet3D(in_channels=hp.in_class, out_channels=hp.out_class, init_features=8)#, base_n_filter=2) #2
# from models.three_d.Residual_unet3d import UNet
# model = UNet(in_channels=hp.in_class, n_classes=hp.out_class, base_n_filter=4) #1
# from models.three_d.gaborunet import UNet3D #8->11 4->
# model = UNet3D(in_channels=hp.in_class, out_channels=hp.out_class, init_features=16)#, base_n_filter=2)
#from models.three_d.fcn3d import FCN_Net
#model = FCN_Net(in_channels =hp.in_class,n_class =hp.out_class)
# from models.three_d.highresnet import HighRes3DNet
# model = HighRes3DNet(in_channels=hp.in_class,out_channels=hp.out_class)
# from models.three_d.densenet3d import SkipDenseNet3D
# model = SkipDenseNet3D(in_channels=hp.in_class, classes=hp.out_class)
# from models.three_d.densevoxelnet3d import DenseVoxelNet #1
# model = DenseVoxelNet(in_channels=hp.in_class, classes=hp.out_class)
# from models.three_d.vnet3d import VNet #2
# model = VNet(in_channels=hp.in_class, classes=hp.out_class)
# from models.three_d.unetr import UNETR
# model = UNETR(img_shape=(hp.crop_or_pad_size,hp.crop_or_pad_size,hp.crop_or_pad_size), input_dim=hp.in_class, output_dim=hp.out_class)
from models.three_d.chen_trans import UNETR
model = UNETR(img_shape=(hp.crop_or_pad_size,hp.crop_or_pad_size,hp.crop_or_pad_size), input_dim=hp.in_class, output_dim=hp.out_class)
model_ema = UNETR(img_shape=(hp.crop_or_pad_size,hp.crop_or_pad_size,hp.crop_or_pad_size), input_dim=hp.in_class, output_dim=hp.out_class)
for param in model_ema.parameters():
param.detach_()
model = torch.nn.DataParallel(model)
model_ema = torch.nn.DataParallel(model_ema)
optimizer = torch.optim.Adam(model.parameters(), lr=args.init_lr)
# scheduler = ReduceLROnPlateau(optimizer, 'min',factor=0.5, patience=20, verbose=True)
scheduler = StepLR(optimizer, step_size=30, gamma=0.8)
# scheduler = CosineAnnealingLR(optimizer, T_max=50, eta_min=5e-6)
if args.ckpt is not None:
print("load model:", args.ckpt)
print(os.path.join(args.output_dir, args.latest_checkpoint_file))
ckpt = torch.load(os.path.join(args.output_dir, args.latest_checkpoint_file), map_location=lambda storage, loc: storage)
model.load_state_dict(ckpt["model"])
optimizer.load_state_dict(ckpt["optim"])
for state in optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
# scheduler.load_state_dict(ckpt["scheduler"])
elapsed_epochs = ckpt["epoch"]
else:
elapsed_epochs = 0
model.cuda()
from loss_function import Binary_Loss,MSE_Loss,weightbceloss1,BinaryDiceLoss
criterion = Binary_Loss().cuda()
criterion_1 = weightbceloss1().cuda()
# ce_loss = CrossEntropyLoss()
ce_loss = criterion
# dice_loss = DiceLoss(1)
dice_loss = BinaryDiceLoss()
writer = SummaryWriter(args.output_dir)
train_unlabel_dataset = Med_unlabel_train(unlabel_dir_train)
# train_unlabel_dataset = Med_unlabel_train(source_train_dir)
my_unlabel = My_unlabel(train_unlabel_dataset.queue_dataset,train_unlabel_dataset.queue_dataset_2)
# train_unlabel_loader = DataLoader(train_unlabel_dataset.queue_dataset,
# batch_size=args.batch,
# shuffle=False,
# pin_memory=True,
# drop_last=True)
# train_unlabel_loader_unaug = DataLoader(train_unlabel_dataset.queue_dataset_2,
# batch_size=args.batch,
# shuffle=False,
# pin_memory=True,
# drop_last=True)
train_unlabel_loader = DataLoader(my_unlabel,
batch_size=args.batch,
shuffle=True,
pin_memory=True,
drop_last=True)
train_dataset = MedData_train(source_train_dir,label_train_dir)
train_loader = DataLoader(train_dataset.queue_dataset,
batch_size=args.batch,
shuffle=True,
pin_memory=True,
drop_last=True)
model.train()
epochs = args.epochs - elapsed_epochs
iteration = elapsed_epochs * len(train_loader)
for epoch in range(1, epochs + 1):
print("epoch:"+str(epoch))
epoch += elapsed_epochs
train_epoch_avg_loss = 0.0
num_iters = 0
for i, batch in enumerate(train_loader):
# random_value = np.random.randint(0,len(train_unlabel_loader))
# for jj,(aug,un_aug) in enumerate(zip(train_unlabel_loader,train_unlabel_loader_unaug)):
# unlabel_img = aug['source']['data']
# unlabel_img_unaug = un_aug['source']['data']
# if jj == random_value:
# break
for data_unlabel,data_unlabel_2 in train_unlabel_loader:
unlabel_img = data_unlabel['source']['data']
unlabel_img_unaug = data_unlabel_2['source']['data']
break #
if hp.debug:
if i >=1:
break
print(f'Batch: {i}/{len(train_loader)} epoch {epoch}')
optimizer.zero_grad()
if (hp.in_class == 1) and (hp.out_class == 1) :
x = batch['source']['data']
y = batch['label']['data']
x = x.type(torch.FloatTensor).cuda()
y = y.type(torch.FloatTensor).cuda()
else:
x = batch['source']['data']
y_atery = batch['atery']['data']
y_lung = batch['lung']['data']
y_trachea = batch['trachea']['data']
y_vein = batch['atery']['data']
x = x.type(torch.FloatTensor).cuda()
y = torch.cat((y_atery,y_lung,y_trachea,y_vein),1)
y = y.type(torch.FloatTensor).cuda()
if hp.mode == '2d':
x = x.squeeze(4)
y = y.squeeze(4)
y = y/255.
# print(y.max())
#################
outputs = model(x)
outputs_soft = torch.sigmoid(outputs)
loss_ce = ce_loss(outputs,
y)
loss_dice = dice_loss(
outputs_soft, y)
# supervised_loss = 0.5 * (loss_dice + loss_ce)
supervised_loss = 0.5 * ( loss_ce)
consistency_weight = math.exp(-0.1*(20-epoch)**1.5)
consistency_loss = entropy_loss(outputs_soft, C=2)
loss = supervised_loss + consistency_weight * consistency_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
update_ema_variables(model, model_ema, 0.99, epoch)
#################
# for metrics
logits = torch.sigmoid(outputs)
labels = logits.clone()
labels[labels>0.5] = 1
labels[labels<=0.5] = 0
num_iters += 1
iteration += 1
false_positive_rate,false_negtive_rate,dice = metric(y.cpu(),labels.cpu())
## log
writer.add_scalar('Training/Loss', loss.item(),iteration)
writer.add_scalar('Training/false_positive_rate', false_positive_rate,iteration)
writer.add_scalar('Training/false_negtive_rate', false_negtive_rate,iteration)
writer.add_scalar('Training/dice', dice,iteration)
print("loss:"+str(loss.item()))
print('lr:'+str(scheduler._last_lr[0]))
scheduler.step()
# Store latest checkpoint in each epoch
torch.save(
{
"model": model.state_dict(),
"optim": optimizer.state_dict(),
"scheduler":scheduler.state_dict(),
"epoch": epoch,
},
os.path.join(args.output_dir, args.latest_checkpoint_file),
)
# Save checkpoint
if epoch % args.epochs_per_checkpoint == 0:
torch.save(
{
"model": model.state_dict(),
"optim": optimizer.state_dict(),
"epoch": epoch,
},
os.path.join(args.output_dir, f"checkpoint_{epoch:04d}.pt"),
)
with torch.no_grad():
if hp.mode == '2d':
x = x.unsqueeze(4)
y = y.unsqueeze(4)
outputs = outputs.unsqueeze(4)
x = x[0].cpu().detach().numpy()
y = y[0].cpu().detach().numpy()
outputs = outputs[0].cpu().detach().numpy()
affine = batch['source']['affine'][0].numpy()
if (hp.in_class == 1) and (hp.out_class == 1) :
source_image = torchio.ScalarImage(tensor=x, affine=affine)
source_image.save(os.path.join(args.output_dir,("step-{}-source.mhd").format(epoch)))
label_image = torchio.ScalarImage(tensor=y, affine=affine)
label_image.save(os.path.join(args.output_dir,("step-{}-gt.mhd").format(epoch)))
output_image = torchio.ScalarImage(tensor=outputs, affine=affine)
output_image.save(os.path.join(args.output_dir,("step-{}-predict.mhd").format(epoch)))
else:
y = np.expand_dims(y, axis=1)
outputs = np.expand_dims(outputs, axis=1)
source_image = torchio.ScalarImage(tensor=x, affine=affine)
source_image.save(os.path.join(args.output_dir,("step-{}-source.mhd").format(epoch)))
label_image_artery = torchio.ScalarImage(tensor=y[0], affine=affine)
label_image_artery.save(os.path.join(args.output_dir,("step-{}-gt_artery.mhd").format(epoch)))
output_image_artery = torchio.ScalarImage(tensor=outputs[0], affine=affine)
output_image_artery.save(os.path.join(args.output_dir,("step-{}-predict_artery.mhd").format(epoch)))
label_image_lung = torchio.ScalarImage(tensor=y[1], affine=affine)
label_image_lung.save(os.path.join(args.output_dir,("step-{}-gt_lung.mhd").format(epoch)))
output_image_lung = torchio.ScalarImage(tensor=outputs[1], affine=affine)
output_image_lung.save(os.path.join(args.output_dir,("step-{}-predict_lung.mhd").format(epoch)))
label_image_trachea = torchio.ScalarImage(tensor=y[2], affine=affine)
label_image_trachea.save(os.path.join(args.output_dir,("step-{}-gt_trachea.mhd").format(epoch)))
output_image_trachea = torchio.ScalarImage(tensor=outputs[2], affine=affine)
output_image_trachea.save(os.path.join(args.output_dir,("step-{}-predict_trachea.mhd").format(epoch)))
label_image_vein = torchio.ScalarImage(tensor=y[3], affine=affine)
label_image_vein.save(os.path.join(args.output_dir,("step-{}-gt_vein.mhd").format(epoch)))
output_image_vein = torchio.ScalarImage(tensor=outputs[3], affine=affine)
output_image_vein.save(os.path.join(args.output_dir,("step-{}-predict_vein.mhd").format(epoch)))
writer.close()
def test():
parser = argparse.ArgumentParser(description='PyTorch Medical Segmentation Testing')
parser = parse_training_args(parser)
args, _ = parser.parse_known_args()
args = parser.parse_args()
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = args.cudnn_enabled
torch.backends.cudnn.benchmark = args.cudnn_benchmark
from data_function import MedData_test
os.makedirs(output_float_dir, exist_ok=True)
os.makedirs(output_int_dir, exist_ok=True)
if hp.mode == '2d':
#from models.two_d.unet import Unet
#model = Unet(in_channels=hp.in_class, classes=hp.out_class)
from models.two_d.miniseg import MiniSeg
model = MiniSeg(in_input=hp.in_class, classes=hp.out_class)
# from models.two_d.fcn import FCN32s as fcn
# model = fcn(in_class =hp.in_class,n_class=hp.out_class)
# from models.two_d.segnet import SegNet
# model = SegNet(input_nbr=hp.in_class,label_nbr=hp.out_class)
#from models.two_d.deeplab import DeepLabV3
#model = DeepLabV3(in_class=hp.in_class,class_num=hp.out_class)
# from models.two_d.unetpp import ResNet34UnetPlus
# model = ResNet34UnetPlus(num_channels=hp.in_class,num_class=hp.out_class)
# from models.two_d.pspnet import PSPNet
# model = PSPNet(in_class=hp.in_class,n_classes=hp.out_class)
elif hp.mode == '3d':
# from models.three_d.unet3d import UNet3D
# model = UNet3D(in_channels=hp.in_class, out_channels=hp.out_class, init_features=8)#, base_n_filter=2)
# from models.three_d.Residual_unet3d import UNet
# model = UNet(in_channels=hp.in_class, n_classes=hp.out_class, base_n_filter=4)
# from models.three_d.gaborunet import UNet3D
# model = UNet3D(in_channels=hp.in_class, out_channels=hp.out_class, init_features=16)#, base_n_filter=2)
# from models.three_d.fcn3d import FCN_Net
# model = FCN_Net(in_channels =hp.in_class,n_class =hp.out_class)
# from models.three_d.highresnet import HighRes3DNet
# model = HighRes3DNet(in_channels=hp.in_class,out_channels=hp.out_class)
# from models.three_d.densenet3d import SkipDenseNet3D
# model = SkipDenseNet3D(in_channels=hp.in_class, classes=hp.out_class)
# from models.three_d.densevoxelnet3d import DenseVoxelNet
# model = DenseVoxelNet(in_channels=hp.in_class, classes=hp.out_clas
# from models.three_d.vnet3d import VNet
# model = VNet(in_channels=hp.in_class, classes=hp.out_class)
# from models.three_d.unetr import UNETR
# model = UNETR(img_shape=(hp.crop_or_pad_size,hp.crop_or_pad_size,hp.crop_or_pad_size), input_dim=hp.in_class, output_dim=hp.out_class)
from models.three_d.chen_trans import UNETR
model = UNETR(img_shape=(hp.crop_or_pad_size,hp.crop_or_pad_size,hp.crop_or_pad_size), input_dim=hp.in_class, output_dim=hp.out_class)
model = torch.nn.DataParallel(model)
print("load model:", args.ckpt)
print(os.path.join(args.output_dir, args.latest_checkpoint_file))
ckpt = torch.load(os.path.join(args.output_dir, args.latest_checkpoint_file), map_location=lambda storage, loc: storage)
model.load_state_dict(ckpt["model"])
model.cuda()
model.eval()
test_dataset = MedData_test(source_test_dir,label_test_dir)
znorm = ZNormalization()
if hp.mode == '3d':
patch_overlap = 4,4,4
patch_size = hp.patch_size,hp.patch_size,hp.patch_size
# patch_size = 128, 128, 64
elif hp.mode == '2d':
patch_overlap = 4,4,0
patch_size = hp.patch_size,hp.patch_size,1
for i,subj in enumerate(test_dataset.subjects):
subj = znorm(subj)
grid_sampler = torchio.inference.GridSampler(
subj,
patch_size,
patch_overlap,
)
patch_loader = torch.utils.data.DataLoader(grid_sampler, batch_size=1)
aggregator = torchio.inference.GridAggregator(grid_sampler)
aggregator_1 = torchio.inference.GridAggregator(grid_sampler)
with torch.no_grad():
for patches_batch in tqdm(patch_loader):
input_tensor = patches_batch['source'][torchio.DATA].to(device)
locations = patches_batch[torchio.LOCATION]
if hp.mode == '2d':
input_tensor = input_tensor.squeeze(4)
outputs = model(input_tensor)
if hp.mode == '2d':
outputs = outputs.unsqueeze(4)
logits = torch.sigmoid(outputs)
labels = logits.clone()
labels[labels>0.5] = 1
labels[labels<=0.5] = 0
aggregator.add_batch(logits, locations)
aggregator_1.add_batch(labels, locations)
output_tensor = aggregator.get_output_tensor()
output_tensor_1 = aggregator_1.get_output_tensor()
affine = subj['source']['affine']
if (hp.in_class == 1) and (hp.out_class == 1) :
if i<9:
label_image = torchio.ScalarImage(tensor=output_tensor.numpy(), affine=affine)
label_image.save(os.path.join(output_float_dir,"0"+str(i+1)+".mhd"))
output_image = torchio.ScalarImage(tensor=output_tensor_1.numpy(), affine=affine)
output_image.save(os.path.join(output_int_dir,"0"+str(i+1)+".mhd"))
else:
label_image = torchio.ScalarImage(tensor=output_tensor.numpy(), affine=affine)
label_image.save(os.path.join(output_float_dir,str(i+1)+".mhd"))
output_image = torchio.ScalarImage(tensor=output_tensor_1.numpy(), affine=affine)
output_image.save(os.path.join(output_int_dir,str(i+1)+".mhd"))
else:
output_tensor = output_tensor.unsqueeze(1)
output_tensor_1= output_tensor_1.unsqueeze(1)
output_image_artery_float = torchio.ScalarImage(tensor=output_tensor[0].numpy(), affine=affine)
output_image_artery_float.save(os.path.join(output_int_dir,str(i)+"_result_float_artery.mhd"))
output_image_artery_int = torchio.ScalarImage(tensor=output_tensor_1[0].numpy(), affine=affine)
output_image_artery_int.save(os.path.join(output_int_dir,str(i)+"_result_int_artery.mhd"))
output_image_lung_float = torchio.ScalarImage(tensor=output_tensor[1].numpy(), affine=affine)
output_image_lung_float.save(os.path.join(output_int_dir,str(i)+"_result_float_lung.mhd"))
output_image_lung_int = torchio.ScalarImage(tensor=output_tensor_1[1].numpy(), affine=affine)
output_image_lung_int.save(os.path.join(output_int_dir,str(i)+"_result_int_lung.mhd"))
output_image_trachea_float = torchio.ScalarImage(tensor=output_tensor[2].numpy(), affine=affine)
output_image_trachea_float.save(os.path.join(output_int_dir,str(i)+"_result_float_trachea.mhd"))
output_image_trachea_int = torchio.ScalarImage(tensor=output_tensor_1[2].numpy(), affine=affine)
output_image_trachea_int.save(os.path.join(output_int_dir,str(i)+"_result_int_trachea.mhd"))
output_image_vein_float = torchio.ScalarImage(tensor=output_tensor[3].numpy(), affine=affine)
output_image_vein_float.save(os.path.join(output_int_dir,str(i)+"_result_float_veiny.mhd"))
output_image_vein_int = torchio.ScalarImage(tensor=output_tensor_1[3].numpy(), affine=affine)
output_image_vein_int.save(os.path.join(output_int_dir,str(i)+"_result_int_vein.mhd"))
if __name__ == '__main__':
if hp.train_or_test == 'train':
train()
elif hp.train_or_test == 'test':
test()