-
Notifications
You must be signed in to change notification settings - Fork 9
/
train_semi_CCT_3d.py
340 lines (274 loc) · 16.5 KB
/
train_semi_CCT_3d.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
from torchvision import transforms, datasets
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import DataLoader
import argparse
import time
import os
import numpy as np
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
from torch.backends import cudnn
import random
import torchio as tio
from config.dataset_config.dataset_cfg import dataset_cfg
from config.train_test_config.train_test_config import print_train_loss_EM, print_val_loss_sup, print_train_eval_sup, print_val_eval_sup, save_val_best_sup_3d, print_best_sup
from config.visdom_config.visual_visdom import visdom_initialization_EM, visualization_EM
from config.warmup_config.warmup import GradualWarmupScheduler
from config.augmentation.online_aug import data_transform_3d
from loss.loss_function import segmentation_loss
from models.getnetwork import get_network
from dataload.dataset_3d import dataset_it
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
def init_seeds(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(0)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--path_trained_models', default='/mnt/data1/XNet/checkpoints/semi')
parser.add_argument('--path_seg_results', default='/mnt/data1/XNet/seg_pred/semi')
parser.add_argument('--path_dataset', default='/mnt/data1/XNet/dataset/LiTS')
parser.add_argument('--dataset_name', default='LiTS', help='LiTS, Atrial')
parser.add_argument('--input1', default='image')
parser.add_argument('--sup_mark', default='20')
parser.add_argument('--unsup_mark', default='80')
parser.add_argument('-b', '--batch_size', default=1, type=int)
parser.add_argument('-e', '--num_epochs', default=200, type=int)
parser.add_argument('-s', '--step_size', default=50, type=int)
parser.add_argument('-l', '--lr', default=0.1, type=float)
parser.add_argument('-g', '--gamma', default=0.5, type=float)
parser.add_argument('-c', '--unsup_weight', default=1, type=float)
parser.add_argument('--loss', default='dice', type=str)
parser.add_argument('--patch_size', default=(112, 112, 32))
parser.add_argument('-w', '--warm_up_duration', default=20)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--wd', default=-5, type=float, help='weight decay pow')
parser.add_argument('--queue_length', default=48, type=int)
parser.add_argument('--samples_per_volume_train', default=8, type=int)
parser.add_argument('--samples_per_volume_val', default=12, type=int)
parser.add_argument('-i', '--display_iter', default=5, type=int)
parser.add_argument('-n', '--network', default='unet3d_cct', type=str)
parser.add_argument('--local_rank', default=-1, type=int)
parser.add_argument('--rank_index', default=0, help='0, 1, 2, 3')
parser.add_argument('-v', '--vis', default=True, help='need visualization or not')
parser.add_argument('--visdom_port', default=16672, help='16672')
args = parser.parse_args()
torch.cuda.set_device(args.local_rank)
dist.init_process_group(backend='nccl', init_method='env://')
rank = torch.distributed.get_rank()
ngpus_per_node = torch.cuda.device_count()
init_seeds(rank + 1)
dataset_name = args.dataset_name
cfg = dataset_cfg(dataset_name)
print_num = 77 + (cfg['NUM_CLASSES'] - 3) * 14
print_num_minus = print_num - 2
print_num_half = int(print_num / 2 - 1)
path_trained_models = args.path_trained_models + '/' + str(os.path.split(args.path_dataset)[1])
if not os.path.exists(path_trained_models) and rank == args.rank_index:
os.mkdir(path_trained_models)
path_trained_models = path_trained_models + '/' + 'CCT' + '-l=' + str(args.lr) + '-e=' + str(args.num_epochs) + '-s=' + str(args.step_size) + '-g=' + str(args.gamma) + '-b=' + str(args.batch_size) + '-cw=' + str(args.unsup_weight) + '-w=' + str(args.warm_up_duration) + '-' + str(args.sup_mark) + '-' + str(args.unsup_mark) + '-' + str(args.input1)
if not os.path.exists(path_trained_models) and rank == args.rank_index:
os.mkdir(path_trained_models)
path_seg_results = args.path_seg_results + '/' + str(os.path.split(args.path_dataset)[1])
if not os.path.exists(path_seg_results) and rank == args.rank_index:
os.mkdir(path_seg_results)
path_seg_results = path_seg_results + '/' + 'CCT' + '-l=' + str(args.lr) + '-e=' + str(args.num_epochs) + '-s=' + str(args.step_size) + '-g=' + str(args.gamma) + '-b=' + str(args.batch_size) + '-cw=' + str(args.unsup_weight) + '-w=' + str(args.warm_up_duration) + '-' + str(args.sup_mark) + '-' + str(args.unsup_mark) + '-' + str(args.input1)
if not os.path.exists(path_seg_results) and rank == args.rank_index:
os.mkdir(path_seg_results)
path_mask_results = path_seg_results + '/mask'
if not os.path.exists(path_mask_results) and rank == args.rank_index:
os.mkdir(path_mask_results)
path_seg_results = path_seg_results + '/pred'
if not os.path.exists(path_seg_results) and rank == args.rank_index:
os.mkdir(path_seg_results)
if args.vis and rank == args.rank_index:
visdom_env = str('Semi-CCT-' + str(os.path.split(args.path_dataset)[1]) + '-' + args.network + '-l=' + str(args.lr) + '-e=' + str(args.num_epochs) + '-s=' + str(args.step_size) + '-g=' + str(args.gamma) + '-b=' + str(args.batch_size) + '-cw=' + str(args.unsup_weight)+ '-w=' + str(args.warm_up_duration) + '-' + str(args.sup_mark) + '-' + str(args.unsup_mark) + '-' + str(args.input1))
visdom = visdom_initialization_EM(env=visdom_env, port=args.visdom_port)
# Dataset
data_transform = data_transform_3d(cfg['NORMALIZE'])
dataset_train_unsup = dataset_it(
data_dir=args.path_dataset + '/train_unsup_' + args.unsup_mark,
input1=args.input1,
transform_1=data_transform['train'],
queue_length=args.queue_length,
samples_per_volume=args.samples_per_volume_train,
patch_size=args.patch_size,
num_workers=8,
shuffle_subjects=True,
shuffle_patches=True,
sup=False,
num_images=None
)
num_images_unsup = len(dataset_train_unsup.dataset_1)
dataset_train_sup = dataset_it(
data_dir=args.path_dataset + '/train_sup_' + args.sup_mark,
input1=args.input1,
transform_1=data_transform['train'],
queue_length=args.queue_length,
samples_per_volume=args.samples_per_volume_train,
patch_size=args.patch_size,
num_workers=8,
shuffle_subjects=True,
shuffle_patches=True,
sup=True,
num_images=num_images_unsup
)
dataset_val = dataset_it(
data_dir=args.path_dataset + '/val',
input1=args.input1,
transform_1=data_transform['val'],
queue_length=args.queue_length,
samples_per_volume=args.samples_per_volume_val,
patch_size=args.patch_size,
num_workers=8,
shuffle_subjects=False,
shuffle_patches=False,
sup=True,
num_images=None
)
train_sampler_unsup = torch.utils.data.distributed.DistributedSampler(dataset_train_unsup.queue_train_set_1, shuffle=True)
train_sampler_sup = torch.utils.data.distributed.DistributedSampler(dataset_train_sup.queue_train_set_1, shuffle=True)
val_sampler = torch.utils.data.distributed.DistributedSampler(dataset_val.queue_train_set_1, shuffle=False)
dataloaders = dict()
dataloaders['train_sup'] = DataLoader(dataset_train_sup.queue_train_set_1, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=0, sampler=train_sampler_sup)
dataloaders['train_unsup'] = DataLoader(dataset_train_unsup.queue_train_set_1, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=0, sampler=train_sampler_unsup)
dataloaders['val'] = DataLoader(dataset_val.queue_train_set_1, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=0, sampler=val_sampler)
num_batches = {'train_sup': len(dataloaders['train_sup']), 'train_unsup': len(dataloaders['train_unsup']), 'val': len(dataloaders['val'])}
# Model
model1 = get_network(args.network, cfg['IN_CHANNELS'], cfg['NUM_CLASSES'])
model1 = model1.cuda()
model1 = DistributedDataParallel(model1, device_ids=[args.local_rank], find_unused_parameters=True)
dist.barrier()
# Training Strategy
criterion = segmentation_loss(args.loss, False).cuda()
kl_distance = nn.KLDivLoss(reduction='none')
optimizer1 = optim.SGD(model1.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=5 * 10 ** args.wd)
exp_lr_scheduler1 = lr_scheduler.StepLR(optimizer1, step_size=args.step_size, gamma=args.gamma)
scheduler_warmup1 = GradualWarmupScheduler(optimizer1, multiplier=1.0, total_epoch=args.warm_up_duration, after_scheduler=exp_lr_scheduler1)
# Train & Val
since = time.time()
count_iter = 0
best_val_eval_list = [0 for i in range(4)]
for epoch in range(args.num_epochs):
count_iter += 1
if (count_iter - 1) % args.display_iter == 0:
begin_time = time.time()
dataloaders['train_sup'].sampler.set_epoch(epoch)
dataloaders['train_unsup'].sampler.set_epoch(epoch)
model1.train()
train_loss_sup_1 = 0.0
train_loss_unsup = 0.0
train_loss = 0.0
val_loss_sup_1 = 0.0
unsup_weight = args.unsup_weight * (epoch + 1) / args.num_epochs
dist.barrier()
dataset_train_sup = iter(dataloaders['train_sup'])
dataset_train_unsup = iter(dataloaders['train_unsup'])
for i in range(num_batches['train_sup']):
unsup_index = next(dataset_train_unsup)
img_train_unsup_1 = Variable(unsup_index['image'][tio.DATA].cuda())
optimizer1.zero_grad()
pred_train_unsup1, pred_train_unsup2, pred_train_unsup3, pred_train_unsup4 = model1(img_train_unsup_1)
pred_train_unsup1 = torch.softmax(pred_train_unsup1, 1)
pred_train_unsup2 = torch.softmax(pred_train_unsup2, 1)
pred_train_unsup3 = torch.softmax(pred_train_unsup3, 1)
pred_train_unsup4 = torch.softmax(pred_train_unsup4, 1)
consistency_loss_aux1 = torch.mean((pred_train_unsup1 - pred_train_unsup2) ** 2)
consistency_loss_aux2 = torch.mean((pred_train_unsup1 - pred_train_unsup3) ** 2)
consistency_loss_aux3 = torch.mean((pred_train_unsup1 - pred_train_unsup4) ** 2)
loss_train_unsup = (consistency_loss_aux1 + consistency_loss_aux2 + consistency_loss_aux3) / 3
loss_train_unsup = loss_train_unsup * unsup_weight
loss_train_unsup.backward(retain_graph=True)
torch.cuda.empty_cache()
sup_index = next(dataset_train_sup)
img_train_sup_1 = Variable(sup_index['image'][tio.DATA].cuda())
mask_train_sup = Variable(sup_index['mask'][tio.DATA].squeeze(1).long().cuda())
pred_train_sup1, pred_train_sup2, pred_train_sup3, pred_train_sup4 = model1(img_train_sup_1)
if count_iter % args.display_iter == 0:
if i == 0:
score_list_train1 = pred_train_sup1
mask_list_train = mask_train_sup
# else:
elif 0 < i <= num_batches['train_sup'] / 32:
score_list_train1 = torch.cat((score_list_train1, pred_train_sup1), dim=0)
mask_list_train = torch.cat((mask_list_train, mask_train_sup), dim=0)
loss_train_sup1 = (criterion(pred_train_sup1, mask_train_sup)+criterion(pred_train_sup2, mask_train_sup)+criterion(pred_train_sup3, mask_train_sup)+criterion(pred_train_sup4, mask_train_sup)) / 4
loss_train_sup = loss_train_sup1
loss_train_sup.backward()
optimizer1.step()
torch.cuda.empty_cache()
loss_train = loss_train_unsup + loss_train_sup
train_loss_unsup += loss_train_unsup.item()
train_loss_sup_1 += loss_train_sup1.item()
train_loss += loss_train.item()
scheduler_warmup1.step()
torch.cuda.empty_cache()
if count_iter % args.display_iter == 0:
score_gather_list_train1 = [torch.zeros_like(score_list_train1) for _ in range(ngpus_per_node)]
torch.distributed.all_gather(score_gather_list_train1, score_list_train1)
score_list_train1 = torch.cat(score_gather_list_train1, dim=0)
mask_gather_list_train = [torch.zeros_like(mask_list_train) for _ in range(ngpus_per_node)]
torch.distributed.all_gather(mask_gather_list_train, mask_list_train)
mask_list_train = torch.cat(mask_gather_list_train, dim=0)
if rank == args.rank_index:
torch.cuda.empty_cache()
print('=' * print_num)
print('| Epoch {}/{}'.format(epoch + 1, args.num_epochs).ljust(print_num_minus, ' '), '|')
train_epoch_loss_sup_1, train_epoch_loss_cps, train_epoch_loss = print_train_loss_EM(train_loss_sup_1, train_loss_unsup, train_loss, num_batches, print_num, print_num_minus)
train_eval_list_1, train_m_jc_1 = print_train_eval_sup(cfg['NUM_CLASSES'], score_list_train1, mask_list_train, print_num_minus)
torch.cuda.empty_cache()
with torch.no_grad():
model1.eval()
for i, data in enumerate(dataloaders['val']):
# if 0 <= i <= num_batches['val']:
inputs_val_1 = Variable(data['image'][tio.DATA].cuda())
mask_val = Variable(data['mask'][tio.DATA].squeeze(1).long().cuda())
optimizer1.zero_grad()
outputs_val_1, outputs_val_2, outputs_val_3, outputs_val_4 = model1(inputs_val_1)
torch.cuda.empty_cache()
if i == 0:
score_list_val_1 = outputs_val_1
mask_list_val = mask_val
else:
score_list_val_1 = torch.cat((score_list_val_1, outputs_val_1), dim=0)
mask_list_val = torch.cat((mask_list_val, mask_val), dim=0)
loss_val_sup_1 = criterion(outputs_val_1, mask_val)
val_loss_sup_1 += loss_val_sup_1.item()
torch.cuda.empty_cache()
score_gather_list_val_1 = [torch.zeros_like(score_list_val_1) for _ in range(ngpus_per_node)]
torch.distributed.all_gather(score_gather_list_val_1, score_list_val_1)
score_list_val_1 = torch.cat(score_gather_list_val_1, dim=0)
mask_gather_list_val = [torch.zeros_like(mask_list_val) for _ in range(ngpus_per_node)]
torch.distributed.all_gather(mask_gather_list_val, mask_list_val)
mask_list_val = torch.cat(mask_gather_list_val, dim=0)
torch.cuda.empty_cache()
if rank == args.rank_index:
val_epoch_loss_sup_1 = print_val_loss_sup(val_loss_sup_1, num_batches, print_num, print_num_minus)
val_eval_list_1, val_m_jc_1 = print_val_eval_sup(cfg['NUM_CLASSES'], score_list_val_1, mask_list_val, print_num_minus)
best_val_eval_list = save_val_best_sup_3d(cfg['NUM_CLASSES'], best_val_eval_list, model1, score_list_val_1, mask_list_val, val_eval_list_1, path_trained_models, path_seg_results, path_mask_results, 'CCT', cfg['FORMAT'])
torch.cuda.empty_cache()
if args.vis:
visualization_EM(visdom, epoch + 1, train_epoch_loss, train_epoch_loss_sup_1, train_epoch_loss_cps, train_m_jc_1, val_epoch_loss_sup_1, val_m_jc_1)
print('-' * print_num)
print('| Epoch Time: {:.4f}s'.format((time.time() - begin_time) / args.display_iter).ljust(print_num_minus, ' '), '|')
torch.cuda.empty_cache()
torch.cuda.empty_cache()
if rank == args.rank_index:
time_elapsed = time.time() - since
m, s = divmod(time_elapsed, 60)
h, m = divmod(m, 60)
print('=' * print_num)
print('| Training Completed In {:.0f}h {:.0f}mins {:.0f}s'.format(h, m, s).ljust(print_num_minus, ' '), '|')
print('-' * print_num)
print_best_sup(cfg['NUM_CLASSES'], best_val_eval_list, print_num_minus)
print('=' * print_num)