-
Notifications
You must be signed in to change notification settings - Fork 15
/
train_unsup.py
executable file
·234 lines (199 loc) · 9.46 KB
/
train_unsup.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
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import argparse
import os
import numpy as np
from morphologicalpool.morphpoollayer import MorphPool3D
from data.datasets import Directory_Image_Train, Single_Image_Eval
from networks.segmentation import SegmentNet3D_Resnet
from networks.utils import GradXYZ, norm_range
from utils import Saver, TensorboardSummary
from networks.loss_functions import euler_lagrange, level_set
# args
parser = argparse.ArgumentParser(description='PyTorch Unsupervised Vessels Segmentation')
parser.add_argument('--epochs', type=int, default=100, metavar='N',
help='Number of epochs')
parser.add_argument('--batch-size', type=int, default=6, metavar='N',
help='#CUDA * batch_size')
parser.add_argument('--lr', type=float, default=1e-4, metavar='N',
help='Learning rate')
parser.add_argument('--lmd1', type=int, default=1, metavar='N',
help='lambda 1')
parser.add_argument('--lmd2', type=int, default=2, metavar='N',
help='lambda 2')
parser.add_argument('--range-norm', action='store_true',
help='range-norm')
parser.add_argument('--loss', type=str, default='LS', metavar='N',
help='Loss function')
parser.add_argument('--train-dataset', type=str, default='VesselNN', metavar='N',
help='Training dataset name')
parser.add_argument('--train-images-path', type=str, default='/path/to/VesselNN/train/images', metavar='N',
help='Training dataset images path')
parser.add_argument('--train-labels-path', type=str, default='/path/to/VesselNN/train/labels', metavar='N',
help='Training dataset labels path')
parser.add_argument('--val-image-path', type=str, default='/path/to/VesselNN/train/image', metavar='N',
help='Validation image path')
parser.add_argument('--val-label-path', type=str, default='/path/to/VesselNN/train/label', metavar='N',
help='Validation label path')
parser.add_argument('--validate', action='store_true',
help='validate')
# checking point
parser.add_argument('--resume', type=str, default=None,
help='put the path to resuming file if needed')
parser.add_argument('--checkname', type=str, default='VesselNN_Unsupervised',
help='set the checkpoint name')
args = parser.parse_args()
# Define Saver
saver = Saver(args)
saver.save_experiment_config()
# Define Tensorboard Summary
summary = TensorboardSummary(saver.experiment_dir)
writer = summary.create_summary()
# Data
dataset = Directory_Image_Train(images_path=args.train_images_path,
labels_path=args.train_labels_path,
data_shape=(32, 128, 128),
lables_shape=(32, 128, 128),
range_norm=args.range_norm)
dataloader = DataLoader(dataset, batch_size=torch.cuda.device_count() * args.batch_size, shuffle=True, num_workers=2)
# Data - validation
dataset_val = Single_Image_Eval(image_path=args.val_image_path,
label_path=args.val_label_path,
data_shape=(32, 128, 128),
lables_shape=(32, 128, 128),
stride=(8, 16, 16),
range_norm=args.range_norm)
dataloader_val = DataLoader(dataset_val, batch_size=1, shuffle=False, num_workers=2)
# Train
model = SegmentNet3D_Resnet().cuda()
grad_fn = GradXYZ().cuda()
mp3d = MorphPool3D().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
# DataParallel
model = torch.nn.DataParallel(model)
grad_fn = torch.nn.DataParallel(grad_fn)
mp3d = torch.nn.DataParallel(mp3d)
if args.resume:
if not os.path.isfile(args.resume):
raise RuntimeError("=> no checkpoint found at '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
model.module.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
best_pred = checkpoint['best_pred']
is_best = False
best_pred = 0
for epoch in range(args.epochs):
model.train()
iterator = tqdm(dataloader,
leave=True,
dynamic_ncols=True)
for i, (data, _) in enumerate(iterator):
# To CUDA
data = data.cuda()
# Misc
dimsum = list(range(1, len(data.shape)))
# Network
seg, rec = model(data)
grad_seg = grad_fn(seg)
grad_rec = grad_fn(rec)
# References
area = seg.sum(dim=dimsum, keepdim=True)
area_m = (1 - seg).sum(dim=dimsum, keepdim=True)
c0 = (data * seg).sum(dim=dimsum, keepdim=True) / (area + 1e-8)
c1 = (data * (1 - seg)).sum(dim=dimsum, keepdim=True) / (area_m + 1e-8)
# Smooth
seg = mp3d(seg)
seg = mp3d(seg, True)
seg = mp3d(seg)
seg = mp3d(seg, True)
seg = mp3d(seg)
seg = mp3d(seg, True)
# loss function
if args.loss == 'EL':
loss = euler_lagrange(data, seg, area, c0, c1, rec, grad_seg, grad_rec, args)
elif args.loss == 'LS':
loss = level_set(data, seg, area, c0, c1)
else:
raise Exception('Unsupported loss function')
# Optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print loss
iterator.set_description(
'Epoch [{epoch}/{epochs}] :: Train Loss {loss:.4f}'.format(epoch=epoch, epochs=args.epochs,
loss=loss.item()))
writer.add_scalar('train/{loss_type}/total_loss_iter', loss.item(), epoch * len(dataloader) + i)
if i % (len(dataloader) // 10):
summary.visualize_image(writer, data, seg, epoch * len(dataloader) + i)
if not epoch % 1:
saver.save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.module.state_dict(),
'optimizer': optimizer.state_dict(),
'best_pred': best_pred,
}, is_best)
if args.validate:
# Validate
with torch.no_grad():
model.eval()
iterator = tqdm(dataloader_val,
leave=True,
dynamic_ncols=True,
desc='Validation ::')
input = dataset_val.img[
dataset_val.effective_lable_idx[0][0]:dataset_val.effective_lable_idx[0][1],
dataset_val.effective_lable_idx[1][0]:dataset_val.effective_lable_idx[1][1],
dataset_val.effective_lable_idx[2][0]:dataset_val.effective_lable_idx[2][1]
]
input_gt = dataset_val.lbl[
dataset_val.effective_lable_idx[0][0]:dataset_val.effective_lable_idx[0][1],
dataset_val.effective_lable_idx[1][0]:dataset_val.effective_lable_idx[1][1],
dataset_val.effective_lable_idx[2][0]:dataset_val.effective_lable_idx[2][1]
]
input_gt = input_gt // input_gt.max()
output = np.zeros((1,
dataset_val.effective_lable_shape[0],
dataset_val.effective_lable_shape[1],
dataset_val.effective_lable_shape[2]))
idx_sum = np.zeros((1,
dataset_val.effective_lable_shape[0],
dataset_val.effective_lable_shape[1],
dataset_val.effective_lable_shape[2]))
for index, (data, lables) in enumerate(iterator):
# To CUDA
data = data.cuda()
lables = lables.cuda()
# Network
seg, _ = model(data)
# Smooth
seg = mp3d(seg)
seg = mp3d(seg, True)
seg = mp3d(seg)
seg = mp3d(seg, True)
seg = mp3d(seg)
seg = mp3d(seg, True)
for batch_idx, val in enumerate(seg[:, 0]):
out_i = index * dataloader_val.batch_size + batch_idx
z, y, x = np.unravel_index(out_i, (dataset_val.dz, dataset_val.dy, dataset_val.dx))
z = z * dataset_val.stride[0]
y = y * dataset_val.stride[1]
x = x * dataset_val.stride[2]
idx_sum[0,
z: z + dataset_val.lables_shape[0],
y: y + dataset_val.lables_shape[1],
x: x + dataset_val.lables_shape[2]] += 1
output[0,
z: z + dataset_val.lables_shape[0],
y: y + dataset_val.lables_shape[1],
x: x + dataset_val.lables_shape[2]] += val.cpu().data.numpy()
output = output / idx_sum
output = torch.Tensor(output).unsqueeze(0).cuda()
input_gt = torch.Tensor(input_gt).unsqueeze(0)
# Normalize
output = norm_range(output)
# Plot
input = torch.Tensor(input).unsqueeze(0).unsqueeze(0)
summary.visualize_image_val(writer, input, output, epoch)