-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpred_crop.py
155 lines (120 loc) · 4.6 KB
/
pred_crop.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
import os
import sys
import glob
import scipy.io as sio
import numpy as np
import torch
import torch.nn.functional as F
from framework_pytorch.core import Trainer, BasicDataset
from framework_pytorch.modelnet.model_vnet_nointer import VNet
from framework_pytorch.modelnet.model import SegModel
from framework_pytorch.utils import loss_fuctions as LF, process_methods as P, utils as U, eval_function as EF
from framework_pytorch.utils.data_augmentation import RandomAugmentation
from PIL import Image
from skimage.measure import label
def getLargestCC(segmentation):
labels = label(segmentation)
assert( labels.max() != 0 ) # assume at least 1 CC
largestCC = labels == np.argmax(np.bincount(labels.flat)[1:])+1
return largestCC
def getBoundingBox(lab, padding=2, minsize=[96,96]):
xs = 0
xe = lab.shape[0]
ys = 0
ye = lab.shape[1]
for x in range(lab.shape[0]):
if lab[x,:,:].max() > 0.5:
xs = max(x-padding, 0)
break
for x in range(lab.shape[0]-1, -1, -1):
if lab[x,:,:].max() > 0.5:
xe = min(x+1+padding, lab.shape[0])
break
for y in range(lab.shape[1]):
if lab[:,y,:].max() > 0.5:
ys = max(y-padding, 0)
break
for y in range(lab.shape[1]-1, -1, -1):
if lab[:,y,:].max() > 0.5:
ye = min(y+1+padding, lab.shape[1])
break
while xe-xs < minsize[0]:
xe = min(xe+1, lab.shape[0])
xs = max(xs-1, 0)
while ye-ys < minsize[1]:
ye = min(ye+1, lab.shape[1])
ys = max(ys-1, 0)
return xs, xe, ys, ye
# training parameters
epochs = 500
learning_rate = 1e-4
train_batch_size = 1
eval_batch_size = 1
loss_function = {LF.CrossEntropy(): 1.0, LF.SoftDice(): 1.0} # loss functions {method: weight}
aug_rate = 0.8
segmodel_path = './results/cmr_unet'
data_path = './CMRxMotion/training/'
data_list = np.load(data_path + '/train_list.npy', allow_pickle=True).item()
# set suffix for image and label (the difference between image path and label path) for data loading
img_suffix = '.nii.gz'
lab_suffix = '-label.nii.gz'
pre = {img_suffix: [np.squeeze,
P.CenterCrop([256,256,20]),
P.min_max,
P.ExpandDim(0),
]
}
pre_nc = {img_suffix: [np.squeeze, P.min_max]}
# set device to gpu if gpu is available, otherwise use cpu
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = VNet(input_channel=1, class_number=2)
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
model = SegModel(net, optimizer, device, img_suffix, lab_suffix, dropout_rate=0, loss_functions=loss_function)
# init train and start train
trainer = Trainer(model)
trainer.restore(segmodel_path+'/ckpt/model_final.pt')
data_list = glob.glob(data_path + '/data/**/*.nii.gz')
data_list = [x for x in data_list if '-label.nii.gz' not in x]
data_set = BasicDataset(data_list, [img_suffix], pre)
data_set_nc = BasicDataset(data_list, [img_suffix], pre_nc)
l1 = np.loadtxt(data_path + '/lab1.txt', dtype='str')
l2 = np.loadtxt(data_path + '/lab2.txt', dtype='str')
l3 = np.loadtxt(data_path + '/lab3.txt', dtype='str')
xls = []
yls = []
model.net.eval()
for i in range(len(data_set)):
fpath = data_set._file_list[i]
fname = os.path.basename(fpath).split('.nii.gz')[0]
clab = None
if fname in l1:
clab = 1
elif fname in l2:
clab = 2
elif fname in l3:
clab = 3
else:
raise 'error!'
data_dict = data_set.__getitem__(i)
img = np.expand_dims(data_dict[img_suffix], 0)
img = torch.from_numpy(img)
img_g = img.to(device=device)
with torch.no_grad():
logits = model.net(img_g)
pred = F.softmax(logits, dim=1)
pred = np.argmax(pred.cpu().numpy(), 1)[0]
pred_l = getLargestCC(pred).astype(np.uint8)
print(f'{i}/{len(data_set)}: {fname} - {clab}')
cls_file_path = './data_cls'
img = img.numpy().squeeze()
nc_img = data_set_nc.__getitem__(i)[img_suffix].squeeze()
assert nc_img.shape[-1] == pred_l.shape[-1]
nc_pred_l = np.uint8(P.CenterPadding(nc_img.shape)(pred_l))
tpath = f'{cls_file_path}/lab_{clab}/{fname}'
if not os.path.exists(tpath):
os.makedirs(tpath)
for j in range(img.shape[-1]):
if np.sum(nc_pred_l[...,j]) == 0:
continue
Image.fromarray(nc_pred_l[...,j]*255).save(f'{tpath}/{j}_pdorg_lab.png')
Image.fromarray(nc_img[...,j]).save(f'{tpath}/{j}_pdorg.tif')