-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
1084 lines (927 loc) · 47.1 KB
/
utils.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import os
import SimpleITK as sitk
import h5py
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import torch.optim as optim
import torch
import torch.nn.init
import torchvision.transforms as transforms
from torch.autograd import Variable
import cv2
import copy
#Dong add keys here
def Generator_2D_slices(path_patients,batchsize,inputKey='dataMR',outputKey='dataCT'):
#path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients)#every file is a hdf5 patient
while True:
for idx,namepatient in enumerate(patients):
print namepatient
f=h5py.File(os.path.join(path_patients,namepatient))
#dataMRptr=f['dataMR']
dataMRptr=f[inputKey]
dataMR=dataMRptr.value
#dataCTptr=f['dataCT']
dataCTptr=f[outputKey]
dataCT=dataCTptr.value
dataMR=np.squeeze(dataMR)
dataCT=np.squeeze(dataCT)
#print 'mr shape h5 ',dataMR.shape#B,H,W,C
#print 'ct shape h5 ',dataCT.shape#B,H,W
shapedata=dataMR.shape
#Shuffle data
idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
dataMR=dataMR[idx_rnd,...]
dataCT=dataCT[idx_rnd,...]
modulo=np.mod(shapedata[0],batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo!=0:
to_add=batchsize-modulo
inds_toadd=np.random.randint(0,dataMR.shape[0],to_add)
X=np.zeros((dataMR.shape[0]+to_add,dataMR.shape[1],dataMR.shape[2],dataMR.shape[3]))#dataMR
X[:dataMR.shape[0],...]=dataMR
X[dataMR.shape[0]:,...]=dataMR[inds_toadd]
y=np.zeros((dataCT.shape[0]+to_add,dataCT.shape[1],dataCT.shape[2]))#dataCT
y[:dataCT.shape[0],...]=dataCT
y[dataCT.shape[0]:,...]=dataCT[inds_toadd]
else:
X=np.copy(dataMR)
y=np.copy(dataCT)
#X = np.expand_dims(X, axis=3)
X=X.astype(np.float32)
y=np.expand_dims(y, axis=3)#B,H,W,C
y=y.astype(np.float32)
#y[np.where(y==5)]=0
#shuffle the data, by dong
inds = np.arange(X.shape[0])
np.random.shuffle(inds)
X=X[inds,...]
y=y[inds,...]
print 'y shape ', y.shape
for i_batch in xrange(int(X.shape[0]/batchsize)):
yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...], y[i_batch*batchsize:(i_batch+1)*batchsize,...])
#Dong add keys here
def Generator_2D_slicesV1(path_patients,batchsize,inputKey='dataMR',segKey='dataCT', contourKey='dataContour'):
#path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients)#every file is a hdf5 patient
while True:
for idx,namepatient in enumerate(patients):
print namepatient
f=h5py.File(os.path.join(path_patients,namepatient))
#dataMRptr=f['dataMR']
dataMRptr=f[inputKey]
dataMR=dataMRptr.value
#dataCTptr=f['dataCT']
dataCTptr=f[segKey]
dataCT=dataCTptr.value
dataContourptr=f[contourKey]
dataContour=dataContourptr.value
dataMR=np.squeeze(dataMR)
dataCT=np.squeeze(dataCT)
dataContour=np.squeeze(dataContour)
#print 'mr shape h5 ',dataMR.shape#B,H,W,C
#print 'ct shape h5 ',dataCT.shape#B,H,W
shapedata=dataMR.shape
#Shuffle data
idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
dataMR=dataMR[idx_rnd,...]
dataCT=dataCT[idx_rnd,...]
dataContour=dataContour[idx_rnd,...]
modulo=np.mod(shapedata[0],batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo!=0:
to_add = batchsize-modulo
inds_toadd = np.random.randint(0,dataMR.shape[0],to_add)
X = np.zeros((dataMR.shape[0]+to_add, dataMR.shape[1], dataMR.shape[2], dataMR.shape[3]))#dataMR
X[:dataMR.shape[0],...]=dataMR
X[dataMR.shape[0]:,...]=dataMR[inds_toadd]
y=np.zeros((dataCT.shape[0]+to_add,dataCT.shape[1],dataCT.shape[2]))#dataCT
y[:dataCT.shape[0],...]=dataCT
y[dataCT.shape[0]:,...]=dataCT[inds_toadd]
y1=np.zeros((dataContour.shape[0]+to_add,dataContour.shape[1],dataContour.shape[2]))#dataCT
y1[:dataContour.shape[0],...]=dataContour
y1[dataContour.shape[0]:,...]=dataContour[inds_toadd]
else:
X=np.copy(dataMR)
y=np.copy(dataCT)
y1 = np.copy(dataContour)
#X = np.expand_dims(X, axis=3)
X=X.astype(np.float32)
# y=np.expand_dims(y, axis=3)#B,H,W,C
# y=y.astype(np.float32)
#y[np.where(y==5)]=0
#shuffle the data, by dong
inds = np.arange(X.shape[0])
np.random.shuffle(inds)
X=X[inds,...]
y=y[inds,...]
y1=y1[inds,...]
print 'y shape ', y.shape
for i_batch in xrange(int(X.shape[0]/batchsize)):
yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...], y[i_batch*batchsize:(i_batch+1)*batchsize,...],y1[i_batch*batchsize:(i_batch+1)*batchsize,...])
#Dong add a variable of keys here
'''
Input:
path_patients: h5 data path
batchsize: the batchsize we extract patches at a time
keys: a variable number of parameters with a data structure of list
Output:
The list of corresponding values indexed by the keys
'''
def Generator_2D_slices_variousKeys(path_patients,batchsize, keys):
#path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients)#every file is a hdf5 patient
numOfKeys = len(keys)
while True:
for idx,namepatient in enumerate(patients):
print namepatient
f = h5py.File(os.path.join(path_patients,namepatient))
data0 = f[keys[0]].value
shapedata=data0.shape
# idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
assert len(shapedata) == 4, 'data should have shape like: NxCxHxW'
keyvalue = np.zeros((shapedata[0],shapedata[1],shapedata[2],shapedata[3],shapedata[4],numOfKeys))
for keyInd in range(0,numOfKeys):
#dataMRptr=f['dataMR']
key = keys[keyInd]
keyvalue[:,:,:,:,keyInd] = f[key].value
# dataMR=np.squeeze(dataMR)
# dataCT=np.squeeze(dataCT)
#print 'mr shape h5 ',dataMR.shape#B,H,W,C
#print 'ct shape h5 ',dataCT.shape#B,H,W
#Shuffle data
idx_rnd = np.random.choice(shapedata[0], shapedata[0], replace=False)
keyvalue = keyvalue[idx_rnd,...]
modulo = np.mod(shapedata[0],batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo!=0: #we consider the remaining parts (e.g., 10008%8 = 2)
to_add = batchsize-modulo
inds_toadd = np.random.randint(0,keyvalue.shape[0],to_add)
X = np.zeros((keyvalue.shape[0]+to_add,keyvalue.shape[1],keyvalue.shape[2],keyvalue.shape[3], keyvalue.shape[4], numOfKeys))#keyvalue
X[:keyvalue.shape[0],...] = keyvalue
X[keyvalue.shape[0]:,...] = keyvalue[inds_toadd]
# y=np.zeros((dataCT.shape[0]+to_add,dataCT.shape[1],dataCT.shape[2]))#dataCT
# y[:dataCT.shape[0],...]=dataCT
# y[dataCT.shape[0]:,...]=dataCT[inds_toadd]
else:
X = np.copy(keyvalue)
# X=np.copy(dataMR)
# y=np.copy(dataCT)
#X = np.expand_dims(X, axis=3)
X=X.astype(np.float32)
# y=np.expand_dims(y, axis=3)#B,H,W,C
# y=y.astype(np.float32)
#y[np.where(y==5)]=0
#shuffle the data, by dong
# inds = np.arange(X.shape[0])
# np.random.shuffle(inds)
# X=X[inds,...]
# y=y[inds,...]
print 'X shape ', X.shape
for i_batch in xrange(int(X.shape[0]/batchsize)):
# yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...,keyInd], y[i_batch*batchsize:(i_batch+1)*batchsize,...])
yield ([ X[i_batch*batchsize:(i_batch+1)*batchsize,...,keyInd] for keyInd in range(0,numOfKeys)])
'''
only consider the input without labeled data
'''
def Generator_3D_patches_unlabeled(path_patients, batchsize, inputKey='dataMR'):
#path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients)#every file is a hdf5 patient
while True:
for idx,namepatient in enumerate(patients):
print namepatient
f=h5py.File(os.path.join(path_patients,namepatient))
dataMRptr=f[inputKey]
dataMR=dataMRptr.value
#dataMR=np.squeeze(dataMR)
#dataCT=np.squeeze(dataCT)
dataMR=(dataMR)
print 'mr shape h5 ',dataMR.shape
shapedata=dataMR.shape
#Shuffle data
idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
dataMR=dataMR[idx_rnd,...]
modulo=np.mod(shapedata[0],batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo!=0:
to_add=batchsize-modulo
inds_toadd=np.random.randint(0, dataMR.shape[0], to_add)
X=np.zeros((dataMR.shape[0]+to_add, dataMR.shape[1], dataMR.shape[2], dataMR.shape[3], dataMR.shape[4]))#dataMR
X[:dataMR.shape[0],...]=dataMR
X[dataMR.shape[0]:,...]=dataMR[inds_toadd]
else:
X=np.copy(dataMR)
# X = np.expand_dims(X, axis=4)
X=X.astype(np.float32)
print 'X shape ', X.shape
for i_batch in xrange(int(X.shape[0]/batchsize)):
yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...])
def Generator_3D_patches(path_patients, batchsize, inputKey='dataMR', outputKey='dataCT'):
#path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients)#every file is a hdf5 patient
while True:
for idx,namepatient in enumerate(patients):
print namepatient
f=h5py.File(os.path.join(path_patients,namepatient))
dataMRptr=f[inputKey]
dataMR=dataMRptr.value
#dataMR=np.squeeze(dataMR)
dataCTptr=f[outputKey]
dataCT=dataCTptr.value
#dataCT=np.squeeze(dataCT)
dataMR=(dataMR)
dataCT=(dataCT)
print 'mr shape h5 ',dataMR.shape
shapedata=dataMR.shape
#Shuffle data
idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
dataMR=dataMR[idx_rnd,...]
dataCT=dataCT[idx_rnd,...]
modulo=np.mod(shapedata[0],batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo!=0:
to_add=batchsize-modulo
inds_toadd=np.random.randint(0, dataMR.shape[0], to_add)
X=np.zeros((dataMR.shape[0]+to_add, dataMR.shape[1], dataMR.shape[2], dataMR.shape[3], dataMR.shape[4]))#dataMR
X[:dataMR.shape[0],...]=dataMR
X[dataMR.shape[0]:,...]=dataMR[inds_toadd]
y=np.zeros((dataCT.shape[0]+to_add, dataCT.shape[1], dataCT.shape[2], dataCT.shape[3], dataCT.shape[4]))#dataCT
y[:dataCT.shape[0],...]=dataCT
y[dataCT.shape[0]:,...]=dataCT[inds_toadd]
else:
X=np.copy(dataMR)
y=np.copy(dataCT)
# X = np.expand_dims(X, axis=4)
X=X.astype(np.float32)
# y=np.expand_dims(y, axis=4)
y=y.astype(np.float32)
print 'y shape ', y.shape
print 'X shape ', X.shape
for i_batch in xrange(int(X.shape[0]/batchsize)):
yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...], y[i_batch*batchsize:(i_batch+1)*batchsize,...])
# Dong add a variable of keys here
'''
Input:
path_patients: h5 data path
batchsize: the batchsize we extract patches at a time
keys: a variable number of parameters with a data structure of list
Output:
The list of corresponding values indexed by the keys
'''
def Generator_3D_slices_variousKeys(path_patients, batchsize, keys):
# path_patients='/home/dongnie/warehouse/CT_patients/test_set/'
print path_patients
patients = os.listdir(path_patients) # every file is a hdf5 patient
numOfKeys = len(keys)
while True:
for idx, namepatient in enumerate(patients):
print namepatient
f = h5py.File(os.path.join(path_patients, namepatient))
data0 = f[keys[0]].value
shapedata = data0.shape
# idx_rnd=np.random.choice(shapedata[0], shapedata[0], replace=False)
assert len(shapedata) == 5, 'data should have shape like: NxCxHxWxD'
keyvalue = np.zeros((shapedata[0], shapedata[1], shapedata[2], shapedata[3], shapedata[4], numOfKeys))
for keyInd in range(0, numOfKeys):
# dataMRptr=f['dataMR']
key = keys[keyInd]
keyvalue[:, :, :, :, :, keyInd] = f[key].value
# dataMR=np.squeeze(dataMR)
# dataCT=np.squeeze(dataCT)
# print 'mr shape h5 ',dataMR.shape#B,H,W,C
# print 'ct shape h5 ',dataCT.shape#B,H,W
# Shuffle data
idx_rnd = np.random.choice(shapedata[0], shapedata[0], replace=False)
keyvalue = keyvalue[idx_rnd, ...]
modulo = np.mod(shapedata[0], batchsize)
################## always the number of samples will be a multiple of batchsz##########################3
if modulo != 0: # we consider the remaining parts (e.g., 10008%8 = 2)
to_add = batchsize - modulo
inds_toadd = np.random.randint(0, keyvalue.shape[0], to_add)
X = np.zeros((keyvalue.shape[0] + to_add, keyvalue.shape[1], keyvalue.shape[2], keyvalue.shape[3],
keyvalue.shape[4], numOfKeys)) # keyvalue
X[:keyvalue.shape[0], ...] = keyvalue
X[keyvalue.shape[0]:, ...] = keyvalue[inds_toadd]
# y=np.zeros((dataCT.shape[0]+to_add,dataCT.shape[1],dataCT.shape[2]))#dataCT
# y[:dataCT.shape[0],...]=dataCT
# y[dataCT.shape[0]:,...]=dataCT[inds_toadd]
else:
X = np.copy(keyvalue)
# X=np.copy(dataMR)
# y=np.copy(dataCT)
# X = np.expand_dims(X, axis=3)
X = X.astype(np.float32)
# y=np.expand_dims(y, axis=3)#B,H,W,C
# y=y.astype(np.float32)
# y[np.where(y==5)]=0
# shuffle the data, by dong
# inds = np.arange(X.shape[0])
# np.random.shuffle(inds)
# X = X[inds, ...]
# y=y[inds,...]
print 'X shape ', X.shape
for i_batch in xrange(int(X.shape[0] / batchsize)):
# yield (X[i_batch*batchsize:(i_batch+1)*batchsize,...,keyInd], y[i_batch*batchsize:(i_batch+1)*batchsize,...])
yield ([X[i_batch * batchsize:(i_batch + 1) * batchsize, ..., keyInd] for keyInd in range(0, numOfKeys)])
'''
custom weights initialization called on netG and netD
I think this can only goes into the 1st space, instead of recursive initialization
'''
def weights_init(m):
xavier=torch.nn.init.xavier_uniform
classname = m.__class__.__name__
if classname.find('Conv') != -1:
# m.weight.data.normal_(0.0, 0.02)
#print m.weight.data
#print m.bias.data
xavier(m.weight.data)
# print 'come xavier'
#xavier(m.bias.data)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
elif classname.find('Linear')!=-1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
'''
this function is used to compute the dice ratio
input:
im1: gt
im2 pred
tid: the id for consideration
output:
dcs
'''
def dice(im1, im2,tid):
im1=im1==tid #make it boolean
im2=im2==tid #make it boolean
im1=np.asarray(im1).astype(np.bool)
im2=np.asarray(im2).astype(np.bool)
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
dsc=2. * intersection.sum() / (im1.sum() + im2.sum())
return dsc
'''
this function is used to compute the intersection over Union
input:
im1: gt
im2 pred
tid: the id for consideration
output:
iou
'''
def IoU(im1, im2,tid):
im1=im1==tid #make it boolean
im2=im2==tid #make it boolean
im1=np.asarray(im1).astype(np.bool)
im2=np.asarray(im2).astype(np.bool)
eps = 1e-7
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
union = np.logical_or(im1, im2)
iou = 1. * intersection.sum() / (union.sum()+eps)
return iou
'''
for finetune or sth else to transfer the weights from other models
'''
def transfer_weights(model_from, model_to):
wf = copy.deepcopy(model_from.state_dict())
wt = model_to.state_dict()
for k in wt.keys():
if not k in wf:
wf[k] = wt[k]
model_to.load_state_dict(wf)
'''
Evaluate one patch using the latest pytorch model
input:
patch_MR: a np array of shape [H,W,nchans]
output:
patch_CT_pred: segmentation maps for the corresponding input patch
'''
def evaluate_oldversion(patch_MR, netG, modelPath):
patch_MR = torch.from_numpy(patch_MR)
patch_MR = patch_MR.unsqueeze(0)
# patch_MR=np.expand_dims(patch_MR,axis=0)#[1,nchans,H,W]
patch_MR = Variable(patch_MR).float().cuda()
# netG = ResSegNet() #here is your network
# checkpoint = torch.load(modelPath)
# netG.load_state_dict(checkpoint['model'])
# netG.load_state_dict(torch.load(modelPath)) #this is for old version
netG.cuda()
netG.eval()
# print type(patch_MR)
res = netG(patch_MR)
# print res.size(),res.squeeze(0).size()
if isinstance(res, tuple):
res = res[0]
_, tmp = res.squeeze(0).max(0)
patchOut = tmp.data.cpu().numpy().squeeze()
#imsave('mr32.png',np.squeeze(MR16_eval[0,:,:,2]))
#imsave('ctpred.png',np.squeeze(patch_CT_pred[0,:,:,0]))
#print 'mean of layer ',np.mean(MR16_eval)
#print 'min ct estimated ',np.min(patch_CT_pred)
#print 'max ct estimated ',np.max(patch_CT_pred)
#print 'mean of ctpatch estimated ',np.mean(patch_CT_pred)
del tmp
del patch_MR
del _
return patchOut
'''
Evaluate one patch using the latest pytorch model
input:
patch_MR: a np array of shape [H,W,nchans]
output:
patch_CT_pred: probability maps for the corresponding input patch
'''
def evaluate(patch_MR, netG, modelPath):
patch_MR = torch.from_numpy(patch_MR)
patch_MR = patch_MR.unsqueeze(0)
# patch_MR=np.expand_dims(patch_MR,axis=0)#[1,H,W,nchans]
patch_MR = Variable(patch_MR).float().cuda()
# netG = ResSegNet() #here is your network
# netG.load_state_dict(torch.load(modelPath))
netG.cuda()
netG.eval()
# print type(patch_MR)
res = netG(patch_MR)
# print res.size(),res.squeeze(0).size()
if isinstance(res, tuple) or isinstance(res,list):
res = res[0]
# _, tmp = res.squeeze(0).max(0)
# patchOut = tmp.data.cpu().numpy().squeeze()
patchOut = res
#patchOut = res.squeeze(0).data.cpu().numpy() #NxCxWxH
#imsave('mr32.png',np.squeeze(MR16_eval[0,:,:,2]))
#imsave('ctpred.png',np.squeeze(patch_CT_pred[0,:,:,0]))
#print 'mean of layer ',np.mean(MR16_eval)
#print 'min ct estimated ',np.min(patch_CT_pred)
#print 'max ct estimated ',np.max(patch_CT_pred)
#print 'mean of ctpatch estimated ',np.mean(patch_CT_pred)
return patchOut
'''
Receives an MR image and returns an segmentation label maps with the same size
We use averaging at the overlapping regions
input:
MR_image: the raw input data (after preprocessing)
CT_GT: the ground truth data
MR_patch_sz: 3x168x112?
CT_patch_sz: 1x168x112?
step: 1 (along the 1st dimension)
netG: network for the generator
modelPath: the pytorch model path (pth)
output:
matOut: the predicted segmentation map
'''
def testOneSubject(MR_image,CT_GT,MR_patch_sz,CT_patch_sz,step, netG, modelPath):
matFA = MR_image
matSeg = CT_GT
dFA = MR_patch_sz
dSeg = CT_patch_sz
eps = 1e-5
[row,col,leng] = matFA.shape
margin1 = int((dFA[0]-dSeg[0])/2)
margin2 = int((dFA[1]-dSeg[1])/2)
margin3 = int((dFA[2]-dSeg[2])/2)
cubicCnt = 0
marginD = [margin1,margin2,margin3]
print 'matFA shape is ',matFA.shape
matFAOut = np.zeros([row+2*marginD[0],col+2*marginD[1],leng+2*marginD[2]])
print 'matFAOut shape is ',matFAOut.shape
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]] = matFA
matFAOut[0:marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]] = matFA[0:marginD[0],:,:] #we'd better flip it along the first dimension
matFAOut[row+marginD[0]:matFAOut.shape[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]] = matFA[row-marginD[0]:matFA.shape[0],:,:] #we'd better flip it along the 1st dimension
matFAOut[marginD[0]:row+marginD[0],0:marginD[1],marginD[2]:leng+marginD[2]] = matFA[:,0:marginD[1],:] #we'd better flip it along the 2nd dimension
matFAOut[marginD[0]:row+marginD[0],col+marginD[1]:matFAOut.shape[1],marginD[2]:leng+marginD[2]] = matFA[:,col-marginD[1]:matFA.shape[1],:] #we'd better to flip it along the 2nd dimension
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],0:marginD[2]] = matFA[:,:,0:marginD[2]] #we'd better flip it along the 3rd dimension
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]+leng:matFAOut.shape[2]] = matFA[:,:,leng-marginD[2]:matFA.shape[2]]
matOut = np.zeros((matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]))
used = np.zeros((matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]))+eps
#fid=open('trainxxx_list.txt','a');
print 'last i ',row-dSeg[0]
for i in range(0,row-dSeg[0]+1,step[0]):
# print 'i ',i
for j in range(0,col-dSeg[1]+1,step[1]):
for k in range(0,leng-dSeg[2]+1,step[2]):
volSeg = matSeg[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]
#print 'volSeg shape is ',volSeg.shape
volFA = matFAOut[i:i+dSeg[0]+2*marginD[0],j:j+dSeg[1]+2*marginD[1],k:k+dSeg[2]+2*marginD[2]]
#print 'volFA shape is ',volFA.shape
#mynet.blobs['dataMR'].data[0,0,...]=volFA
#mynet.forward()
#temppremat = mynet.blobs['softmax'].data[0].argmax(axis=0) #Note you have add softmax layer in deploy prototxt
temppremat = evaluate(volFA, netG, modelPath)
temppremat = temppremat.squeeze(0).data.cpu().numpy()
temppremat = temppremat.argmax(axis=0).squeeze()
#temppremat = evaluate(volFA, netG, modelPath)
# print 'temppremat shape 1: ',temppremat.shape
if len(temppremat.shape)==2:
temppremat = np.expand_dims(temppremat,axis=0)
#print 'patchout shape ',temppremat.shape
#temppremat=volSeg
# print 'temppremat shape 2: ',temppremat.shape
matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+temppremat;
used[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = used[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+1;
matOut = matOut/used
return matOut
'''
Receives an MR image and returns an segmentation label maps with the same size
We use majority voting at the overlapping regions
input:
MR_image: the raw input data (after preprocessing)
CT_GT: the ground truth data
NumOfClass: number of classes
MR_patch_sz: 3x168x112?
CT_patch_sz: 1x168x112?
step: 1 (along the 1st dimension)
netG: network for the generator
modelPath: the pytorch model path (pth)
output:
matOut: the predicted segmentation map
'''
def testOneSubject(MR_image, CT_GT, NumOfClass, MR_patch_sz, CT_patch_sz, step, netG, modelPath, nd=2):
eps=1e-5
matFA = MR_image
matSeg = CT_GT
dFA = MR_patch_sz
dSeg = CT_patch_sz
[row,col,leng] = matFA.shape
margin1 = (dFA[0]-dSeg[0])/2
margin2 = (dFA[1]-dSeg[1])/2
margin3 = (dFA[2]-dSeg[2])/2
cubicCnt = 0
marginD = [margin1,margin2,margin3]
#print 'matFA shape is ',matFA.shape
matFAOut = np.zeros([row+2*marginD[0],col+2*marginD[1],leng+2*marginD[2]])
#print 'matFAOut shape is ',matFAOut.shape
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]] = matFA
# matFAOut[0:marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[0:marginD[0],:,:] #we'd better flip it along the first dimension
# matFAOut[row+marginD[0]:matFAOut.shape[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[row-marginD[0]:matFA.shape[0],:,:] #we'd better flip it along the 1st dimension
#
# matFAOut[marginD[0]:row+marginD[0],0:marginD[1],marginD[2]:leng+marginD[2]]=matFA[:,0:marginD[1],:] #we'd better flip it along the 2nd dimension
# matFAOut[marginD[0]:row+marginD[0],col+marginD[1]:matFAOut.shape[1],marginD[2]:leng+marginD[2]]=matFA[:,col-marginD[1]:matFA.shape[1],:] #we'd better to flip it along the 2nd dimension
#
# matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],0:marginD[2]]=matFA[:,:,0:marginD[2]] #we'd better flip it along the 3rd dimension
# matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]+leng:matFAOut.shape[2]]=matFA[:,:,leng-marginD[2]:matFA.shape[2]]
if margin1!=0:
matFAOut[0:marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[marginD[0]-1::-1,:,:] #reverse 0:marginD[0]
matFAOut[row+marginD[0]:matFAOut.shape[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[matFA.shape[0]-1:row-marginD[0]-1:-1,:,:] #we'd better flip it along the 1st dimension
if margin2!=0:
matFAOut[marginD[0]:row+marginD[0],0:marginD[1],marginD[2]:leng+marginD[2]]=matFA[:,marginD[1]-1::-1,:] #we'd flip it along the 2nd dimension
matFAOut[marginD[0]:row+marginD[0],col+marginD[1]:matFAOut.shape[1],marginD[2]:leng+marginD[2]]=matFA[:,matFA.shape[1]-1:col-marginD[1]-1:-1,:] #we'd flip it along the 2nd dimension
if margin3!=0:
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],0:marginD[2]]=matFA[:,:,marginD[2]-1::-1] #we'd better flip it along the 3rd dimension
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]+leng:matFAOut.shape[2]]=matFA[:,:,matFA.shape[2]-1:leng-marginD[2]-1:-1]
# dim1=np.arange(80,192)
# dim2=np.arange(35,235)
# x1=80
# x2=192
# y1=35
# y2=235
# # matFAOutScale = matFAOut[:,y1:y2,x1:x2] #note, matFA and matFAOut same size
# # matSegScale = matSeg[:,y1:y2,x1:x2]
matFAOutScale = matFAOut
matSegScale = matSeg
matOut = np.zeros((matSegScale.shape[0],matSegScale.shape[1],matSegScale.shape[2],NumOfClass),dtype=np.int32)
[row,col,leng] = matSegScale.shape
cnt = 0
#fid=open('trainxxx_list.txt','a');
for i in range(0,row-dSeg[0]+1,step[0]):
for j in range(0,col-dSeg[1]+1,step[1]):
for k in range(0,leng-dSeg[2]+1,step[2]):
volSeg = matSeg[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]
#print 'volSeg shape is ',volSeg.shape
volFA = matFAOutScale[i:i+dSeg[0]+2*marginD[0],j:j+dSeg[1]+2*marginD[1],k:k+dSeg[2]+2*marginD[2]]
cnt = cnt + 1
if nd==3:
volFA = np.expand_dims(volFA, axis=0)
temppremat = evaluate(volFA, netG, modelPath)
temppremat = temppremat.squeeze(0).data.cpu().numpy()
temppremat = temppremat.argmax(axis=0).squeeze() #CxWxH->WxH
# volPre = sitk.GetImageFromArray(temppremat)
# sitk.WriteImage(volPre,'volPre_{}'.format(cnt)+'.nii.gz')
if len(temppremat.shape)==2:
temppremat = np.expand_dims(temppremat,axis=0)
for labelInd in range(NumOfClass): #note, start from 0
currLabelMat = np.where(temppremat==labelInd, 1, 0) # true, vote for 1, otherwise 0
#scio.savemat('volOut_%d'%cnt+'_label%d.mat'%labelInd,{'currLabelMat%d'%labelInd:currLabelMat})
matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd] = matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd]+currLabelMat;
#scio.savemat('matOut%s.mat'%fileID,{'matOut':matOut})
matOut = matOut.argmax(axis=3)
matOut = np.rint(matOut) #this is necessary to convert the data type to be accepted by NIFTI, otherwise will appear strange errors
# print 'line 378: matOut shape: ',matOut.shape
# matOut1 = matOut
# matOut1=np.zeros([matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]])
# matOut1[:,y1:y2,x1:x2]=matOut
#matOut1=np.transpose(matOut1,(2,1,0))
#matSeg=np.transpose(matSeg,(2,1,0))
return matOut,matSeg
'''
Receives an MR image and returns an segmentation label maps with the same size
We use majority voting at the overlapping regions
input:
MR_image: the raw input data (after preprocessing)
CT_GT: the ground truth data
NumOfClass: number of classes
MR_patch_sz: 3x168x112?
CT_patch_sz: 1x168x112?
step: 1 (along the 1st dimension)
netG: network for the generator
modelPath: the pytorch model path (pth)
resType: 0: segmentation map (integer); 1: regression map (continuous); 2: segmentation map + probability map
output:
matOut: the predicted segmentation map (or regression map or the probability map)
'''
def testOneSubject(MR_image, CT_GT, NumOfClass, MR_patch_sz, CT_patch_sz, step, netG, modelPath,resType=0,nd=2):
eps=1e-5
matFA = MR_image
matSeg = CT_GT
dFA = MR_patch_sz
dSeg = CT_patch_sz
if matFA.ndim==4:
[ch, row,col,leng] = matFA.shape
else:
[row,col,leng] = matFA.shape
margin1 = (dFA[0]-dSeg[0])/2
margin2 = (dFA[1]-dSeg[1])/2
margin3 = (dFA[2]-dSeg[2])/2
cubicCnt = 0
marginD = [margin1,margin2,margin3]
#print 'matFA shape is ',matFA.shape
if matFA.ndim==3:
matFAOut = np.zeros([row+2*marginD[0],col+2*marginD[1],leng+2*marginD[2]])
#print 'matFAOut shape is ',matFAOut.shape
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]] = matFA
if margin1!=0:
matFAOut[0:marginD[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[marginD[0]-1::-1,:,:] #reverse 0:marginD[0]
matFAOut[row+marginD[0]:matFAOut.shape[0],marginD[1]:col+marginD[1],marginD[2]:leng+marginD[2]]=matFA[matFA.shape[0]-1:row-marginD[0]-1:-1,:,:] #we'd better flip it along the 1st dimension
if margin2!=0:
matFAOut[marginD[0]:row+marginD[0],0:marginD[1],marginD[2]:leng+marginD[2]]=matFA[:,marginD[1]-1::-1,:] #we'd flip it along the 2nd dimension
matFAOut[marginD[0]:row+marginD[0],col+marginD[1]:matFAOut.shape[1],marginD[2]:leng+marginD[2]]=matFA[:,matFA.shape[1]-1:col-marginD[1]-1:-1,:] #we'd flip it along the 2nd dimension
if margin3!=0:
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],0:marginD[2]]=matFA[:,:,marginD[2]-1::-1] #we'd better flip it along the 3rd dimension
matFAOut[marginD[0]:row+marginD[0],marginD[1]:col+marginD[1],marginD[2]+leng:matFAOut.shape[2]]=matFA[:,:,matFA.shape[2]-1:leng-marginD[2]-1:-1]
else:
matFAOut = matFA
matFAOutScale = matFAOut
matSegScale = matSeg
# print 'matFA.shape: ',matFA.shape
matOut = np.zeros((matSegScale.shape[0],matSegScale.shape[1],matSegScale.shape[2],NumOfClass),dtype=np.int32)
matProb = np.zeros((NumOfClass, matSeg.shape[0], matSeg.shape[1], matSeg.shape[2]))
used = np.zeros((NumOfClass, matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]))+eps
# print 'matProb.shape: ',matProb.shape,' matOut.shape:', matOut.shape,' used.shape: ',used.shape
[row,col,leng] = matSegScale.shape
softmax2d = nn.Softmax2d()
cnt = 0
#fid=open('trainxxx_list.txt','a');
for i in range(0,row-dSeg[0]+1,step[0]):
for j in range(0,col-dSeg[1]+1,step[1]):
for k in range(0,leng-dSeg[2]+1,step[2]):
volSeg = matSeg[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]
#print 'volSeg shape is ',volSeg.shape
if matFA.ndim==3:
volFA = matFAOutScale[i:i+dSeg[0]+2*marginD[0],j:j+dSeg[1]+2*marginD[1],k:k+dSeg[2]+2*marginD[2]]
else:
volFA = matFAOutScale[:,i:i+dSeg[0]+2*marginD[0],j:j+dSeg[1]+2*marginD[1],k:k+dSeg[2]+2*marginD[2]]
cnt = cnt + 1
if nd==3 and volFA.ndim==3:
volFA = np.expand_dims(volFA, axis=0)
# print 'volFA.shape: ',volFA.shape
tempprobmat = evaluate(volFA, netG, modelPath)#NxCxWxH
# tempprobmat = tempprobmat.data.cpu().numpy() #NxCxWxH
# volPre = sitk.GetImageFromArray(temppremat)
# sitk.WriteImage(volPre,'volPre_{}'.format(cnt)+'.nii.gz')
if resType==0 or resType==2:
tempprobmat = F.softmax(tempprobmat, dim=1)
tempprobmat = tempprobmat.squeeze(0).data.cpu().numpy()
temppremat = tempprobmat.argmax(axis=0).squeeze() #CxWxH->WxH
if len(temppremat.shape)==2:
temppremat = np.expand_dims(temppremat,axis=0)
for labelInd in range(NumOfClass): #note, start from 0
currLabelMat = np.where(temppremat==labelInd, 1, 0) # true, vote for 1, otherwise 0
#scio.savemat('volOut_%d'%cnt+'_label%d.mat'%labelInd,{'currLabelMat%d'%labelInd:currLabelMat})
matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd] = matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd]+currLabelMat;
if resType==1 or resType==2:
temppremat = tempprobmat.squeeze() #CxWxHx1->CxWxH
#print 'temppremat shape 1: ',temppremat.shape
if len(temppremat.shape)==3:#CxWxH->Cx1xHxW
temppremat = np.expand_dims(temppremat,axis=1)
#print 'patchout shape ',temppremat.shape
#temppremat=volSeg
# print 'temppremat.shape 2: ',temppremat.shape
matProb[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = matProb[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+temppremat
used[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = used[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+1
#scio.savemat('matOut%s.mat'%fileID,{'matOut':matOut})
matOut = matOut.argmax(axis=3)
matOut = np.rint(matOut) #this is necessary to convert the data type to be accepted by NIFTI, otherwise will appear strange errors
# print 'matProb.max: ',np.ndarray.max(matProb),' matProb.min: ',np.ndarray.min(matProb),' used.max: ',np.ndarray.max(used),' used.min: ',np.ndarray.min(used)
matProb = matProb/used
# print 'line 378: matOut shape: ',matOut.shape
# matOut1 = matOut
# matOut1=np.zeros([matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]])
# matOut1[:,y1:y2,x1:x2]=matOut
#matOut1=np.transpose(matOut1,(2,1,0))
#matSeg=np.transpose(matSeg,(2,1,0))
if resType==0:
return matOut,matSeg
elif resType==1:
return matProb,matSeg
else:
return matOut, matProb, matSeg
'''
Receives an MR image and returns an segmentation label maps with the same size
We use majority voting at the overlapping regions
input:
MR_image: the raw input data (after preprocessing)
CT_GT: the ground truth data
NumOfClass: number of classes
MR_patch_sz: 3x168x112?
CT_patch_sz: 1x168x112?
step: 1 (along the 1st dimension)
netG: network for the generator
modelPath: the pytorch model path (pth)
resType: 0: segmentation map (integer); 1: regression map (continuous); 2: segmentation map + probability map
output:
matOut: the predicted segmentation map (or regression map or the probability map)
'''
def testOneSubjectWith4DInput(MR_image, CT_GT, NumOfClass, MR_patch_sz, CT_patch_sz, step, netG, modelPath,resType=0,nd=2):
eps=1e-5
matFA = MR_image
matSeg = CT_GT
dFA = MR_patch_sz
dSeg = CT_patch_sz
[chn, row,col,leng] = matFA.shape
margin1 = (dFA[0]-dSeg[0])/2
margin2 = (dFA[1]-dSeg[1])/2
margin3 = (dFA[2]-dSeg[2])/2
cubicCnt = 0
marginD = [margin1,margin2,margin3]
matFAOutScale = matFA
matSegScale = matSeg
matOut = np.zeros((row,col,leng,NumOfClass),dtype=np.int32)
matProb = np.zeros((NumOfClass, row,col,leng))
used = np.zeros((NumOfClass, row,col,leng))+eps
# print 'matProb.shape: ',matProb.shape,' matOut.shape:', matOut.shape,' used.shape: ',used.shape
[chn, row,col,leng] = matSegScale.shape
softmax2d = nn.Softmax2d()
cnt = 0
#fid=open('trainxxx_list.txt','a');
for i in range(0,row-dSeg[0]+1,step[0]):
for j in range(0,col-dSeg[1]+1,step[1]):
for k in range(0,leng-dSeg[2]+1,step[2]):
volSeg = matSeg[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]
#print 'volSeg shape is ',volSeg.shape
volFA = matFAOutScale[:,i:i+dSeg[0]+2*marginD[0],j:j+dSeg[1]+2*marginD[1],k:k+dSeg[2]+2*marginD[2]]
cnt = cnt + 1
# if nd==3:
# volFA = np.expand_dims(volFA, axis=0)
tempprobmat = evaluate(volFA, netG, modelPath)#NxCxWxH
# tempprobmat = tempprobmat.data.cpu().numpy() #NxCxWxH
# volPre = sitk.GetImageFromArray(temppremat)
# sitk.WriteImage(volPre,'volPre_{}'.format(cnt)+'.nii.gz')
if resType==0 or resType==2:
tempprobmat = F.softmax(tempprobmat, dim=1)
tempprobmat = tempprobmat.squeeze(0).data.cpu().numpy()
temppremat = tempprobmat.argmax(axis=0).squeeze() #CxWxH->WxH
if len(temppremat.shape)==2:
temppremat = np.expand_dims(temppremat,axis=0)
for labelInd in range(NumOfClass): #note, start from 0
currLabelMat = np.where(temppremat==labelInd, 1, 0) # true, vote for 1, otherwise 0
#scio.savemat('volOut_%d'%cnt+'_label%d.mat'%labelInd,{'currLabelMat%d'%labelInd:currLabelMat})
matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd] = matOut[i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2],labelInd]+currLabelMat;
if resType==1 or resType==2:
temppremat = tempprobmat.squeeze() #CxWxHx1->CxWxH
#print 'temppremat shape 1: ',temppremat.shape
if len(temppremat.shape)==3:#CxWxH->Cx1xHxW
temppremat = np.expand_dims(temppremat,axis=1)
#print 'patchout shape ',temppremat.shape
#temppremat=volSeg
# print 'temppremat.shape 2: ',temppremat.shape
matProb[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = matProb[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+temppremat
used[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]] = used[:, i:i+dSeg[0],j:j+dSeg[1],k:k+dSeg[2]]+1
#scio.savemat('matOut%s.mat'%fileID,{'matOut':matOut})
matOut = matOut.argmax(axis=3)
matOut = np.rint(matOut) #this is necessary to convert the data type to be accepted by NIFTI, otherwise will appear strange errors
# print 'matProb.max: ',np.ndarray.max(matProb),' matProb.min: ',np.ndarray.min(matProb),' used.max: ',np.ndarray.max(used),' used.min: ',np.ndarray.min(used)
matProb = matProb/used
# print 'line 378: matOut shape: ',matOut.shape
# matOut1 = matOut
# matOut1=np.zeros([matSeg.shape[0],matSeg.shape[1],matSeg.shape[2]])
# matOut1[:,y1:y2,x1:x2]=matOut
#matOut1=np.transpose(matOut1,(2,1,0))
#matSeg=np.transpose(matSeg,(2,1,0))
if resType==0:
return matOut,matSeg
elif resType==1:
return matProb,matSeg
else:
return matOut, matProb, matSeg
'''
used as list in argparse
'''
def arg_as_list(s):
v = ast.literal_eval(s)
if type(v) is not list:
raise argparse.ArgumentTypeError("Argument \"%s\" is not a list" % (s))
return v
'''
Takes a Tensor of size 1xHxW and create one-hot encoding of size nclassxHxW
OneHotEncoding: [2,1,3,0]->[0 0 1 0; 0 1 0 0; 0 0 0 1; 1 0 0 0]
label: NxWxHxD or NxWxH
'''
class OneHotEncode(object):
def __init__(self, nclass=4, nd=2):
self.nclass = nclass
self.nd = nd
def __call__(self,label):
# label_a = np.array(transforms.ToPILImage()(label.byte().unsqueeze(0)),np.uint8)
label_a = label.numpy().astype(np.uint8)
if self.nd == 2:
ohlabel = np.zeros((label_a.shape[0],self.nclass,label_a.shape[1],label_a.shape[2])).astype(np.uint8)
for c in range(self.nclass):
ohlabel[:,c, :,:] = (label_a == c).astype(np.uint8)