-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
268 lines (228 loc) · 12.3 KB
/
train.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
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
import argparse
import time
import warnings
warnings.filterwarnings("ignore")
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
from torch.utils.tensorboard import SummaryWriter
from monai.losses import DiceCELoss
from monai.inferers import sliding_window_inference
from monai.data import load_decathlon_datalist, decollate_batch, DistributedSampler
from monai.transforms import AsDiscrete
from monai.metrics import DiceMetric
from model.Universal_model import Universal_model
from dataset.dataloader_2 import get_loader
from utils import loss
from utils.utils import dice_score, check_data, TEMPLATE, get_key, NUM_CLASS
from optimizers.lr_scheduler import LinearWarmupCosineAnnealingLR
torch.multiprocessing.set_sharing_strategy('file_system')
def train(args, train_loader, model, optimizer, loss_seg_DICE, loss_seg_CE):
model.train()
loss_bce_ave = 0
loss_dice_ave = 0
epoch_iterator = tqdm(
train_loader, desc="Training (X / X Steps) (loss=X.X)", dynamic_ncols=True
)
for step, batch in enumerate(epoch_iterator):
x, y, name = batch["image"].to(args.device), batch["post_label"].float().to(args.device), batch['name']
logit_map = model(x)
term_seg_Dice = loss_seg_DICE.forward(logit_map, y, name, TEMPLATE)
term_seg_BCE = loss_seg_CE.forward(logit_map, y, name, TEMPLATE)
loss = term_seg_BCE + term_seg_Dice
loss.backward()
optimizer.step()
optimizer.zero_grad()
epoch_iterator.set_description(
"Epoch=%d: Training (%d / %d Steps) (dice_loss=%2.5f, bce_loss=%2.5f)" % (
args.epoch, step, len(train_loader), term_seg_Dice.item(), term_seg_BCE.item())
)
loss_bce_ave += term_seg_BCE.item()
loss_dice_ave += term_seg_Dice.item()
torch.cuda.empty_cache()
print('Epoch=%d: ave_dice_loss=%2.5f, ave_bce_loss=%2.5f' % (args.epoch, loss_dice_ave/len(epoch_iterator), loss_bce_ave/len(epoch_iterator)))
return loss_dice_ave/len(epoch_iterator), loss_bce_ave/len(epoch_iterator)
def validation(model, ValLoader, args):
model.eval()
dice_list = {}
for key in TEMPLATE.keys():
dice_list[key] = np.zeros((2, NUM_CLASS)) # 1st row for dice, 2nd row for count
for index, batch in enumerate(tqdm(ValLoader)):
# print('%d processd' % (index))
image, label, name = batch["image"].cuda(), batch["post_label"], batch["name"]
print(name, image.shape)
with torch.no_grad():
pred = sliding_window_inference(image, (args.roi_x, args.roi_y, args.roi_z), 1, model)
pred_sigmoid = F.sigmoid(pred)
B = pred_sigmoid.shape[0]
for b in range(B):
template_key = get_key(name[b])
organ_list = TEMPLATE[template_key]
for organ in organ_list:
dice_organ = dice_score(pred_sigmoid[b,organ-1,:,:,:], label[b,organ-1,:,:,:].cuda())
dice_list[template_key][0][organ-1] += dice_organ.item()
dice_list[template_key][1][organ-1] += 1
ave_organ_dice = np.zeros((2, NUM_CLASS))
if args.local_rank == 0:
with open('out/'+args.log_name+f'/val_{args.epoch}.txt', 'w') as f:
for key in TEMPLATE.keys():
organ_list = TEMPLATE[key]
content = 'Task%s| '%(key)
for organ in organ_list:
dice = dice_list[key][0][organ-1] / dice_list[key][1][organ-1]
content += '%s: %.4f, '%(ORGAN_NAME[organ-1], dice)
ave_organ_dice[0][organ-1] += dice_list[key][0][organ-1]
ave_organ_dice[1][organ-1] += dice_list[key][1][organ-1]
print(content)
f.write(content)
f.write('\n')
content = 'Average | '
for i in range(NUM_CLASS):
content += '%s: %.4f, '%(ORGAN_NAME[i], ave_organ_dice[0][organ-1] / ave_organ_dice[1][organ-1])
print(content)
f.write(content)
f.write('\n')
def process(args):
rank = 0
if args.dist:
dist.init_process_group(backend="nccl", init_method="env://")
rank = args.local_rank
args.device = torch.device(f"cuda:{rank}")
torch.cuda.set_device(args.device)
# prepare the 3D model
model = Universal_model(img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=NUM_CLASS,
backbone=args.backbone,
encoding=args.trans_encoding
)
#Load pre-trained weights
if args.pretrain is not None:
model.load_params(torch.load(args.pretrain)["state_dict"])
if args.trans_encoding == 'word_embedding':
word_embedding = torch.load(args.word_embedding)
model.organ_embedding.data = word_embedding.float()
print('load word embedding')
model.to(args.device)
model.train()
if args.dist:
model = DistributedDataParallel(model, device_ids=[args.device])
# criterion and optimizer
# loss_function = DiceCELoss(to_onehot_y=True, softmax=True)
loss_seg_DICE = loss.DiceLoss(num_classes=NUM_CLASS).to(args.device)
loss_seg_CE = loss.Multi_BCELoss(num_classes=NUM_CLASS).to(args.device)
if args.backbone == 'unetpp':
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3, momentum=0.9,
nesterov=False, weight_decay=1e-4)
else:
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
scheduler = LinearWarmupCosineAnnealingLR(optimizer, warmup_epochs=args.warmup_epoch, max_epochs=args.max_epoch)
if args.resume:
checkpoint = torch.load(args.resume)
if args.dist:
model.load_state_dict(checkpoint['net'])
else:
store_dict = model.state_dict()
model_dict = checkpoint['net']
for key in model_dict.keys():
store_dict['.'.join(key.split('.')[1:])] = model_dict[key]
model.load_state_dict(store_dict)
optimizer.load_state_dict(checkpoint['optimizer'])
args.epoch = checkpoint['epoch']
scheduler.load_state_dict(checkpoint['scheduler'])
print('success resume from ', args.resume)
torch.backends.cudnn.benchmark = True
train_loader, train_sampler = get_loader(args)
if rank == 0:
writer = SummaryWriter(log_dir='out/' + args.log_name)
print('Writing Tensorboard logs to ', 'out/' + args.log_name)
while args.epoch < args.max_epoch:
if args.dist:
dist.barrier()
train_sampler.set_epoch(args.epoch)
scheduler.step()
loss_dice, loss_bce = train(args, train_loader, model, optimizer, loss_seg_DICE, loss_seg_CE)
if rank == 0:
writer.add_scalar('train_dice_loss', loss_dice, args.epoch)
writer.add_scalar('train_bce_loss', loss_bce, args.epoch)
writer.add_scalar('lr', np.array(scheduler.get_lr()), args.epoch)
if (args.epoch % args.store_num == 0 and args.epoch != 0) and rank == 0:
checkpoint = {
"net": model.state_dict(),
'optimizer':optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
"epoch": args.epoch
}
if not os.path.isdir('out/' + args.log_name):
os.mkdir('out/' + args.log_name)
torch.save(checkpoint, 'out/' + args.log_name + '/epoch_' + str(args.epoch) + '.pth')
print('save model success')
args.epoch += 1
dist.destroy_process_group()
def main():
parser = argparse.ArgumentParser()
## for distributed training
parser.add_argument('--dist', dest='dist', type=bool, default=False,
help='distributed training or not')
parser.add_argument("--local_rank", type=int, default= int(os.environ['LOCAL_RANK']))
parser.add_argument("--local-rank", type=int)
parser.add_argument("--device")
parser.add_argument("--epoch", default=0)
## logging
parser.add_argument('--log_name', default='swinunetr_org', help='The path resume from checkpoint')
## model load
parser.add_argument('--backbone', default='swinunetr', help='backbone [swinunetr or unet or dints or unetpp]')
parser.add_argument('--resume', default=None, help='The path resume from checkpoint')
parser.add_argument('--pretrain', default=None, #swin_unetr.base_5000ep_f48_lr2e-4_pretrained.pt
help='The path of pretrain model. Eg, ./pretrained_weights/swin_unetr.base_5000ep_f48_lr2e-4_pretrained.pt')
parser.add_argument('--trans_encoding', default='word_embedding',
help='the type of encoding: rand_embedding or word_embedding')
parser.add_argument('--word_embedding', default='./pretrained_weights/txt_encoding.pth',
help='The path of word embedding')
## hyperparameter
parser.add_argument('--max_epoch', default=1000, type=int, help='Number of training epoches')
parser.add_argument('--store_num', default=10, type=int, help='Store model how often')
parser.add_argument('--warmup_epoch', default=50, type=int, help='number of warmup epochs')
parser.add_argument('--lr', default=4e-4, type=float, help='Learning rate')
parser.add_argument('--weight_decay', default=1e-5, help='Weight Decay')
## dataset
parser.add_argument('--dataset_list', nargs='+', default=['PAOT_123457891213', 'PAOT_10_inner']) # 'PAOT', 'felix'
### please check this argment carefully
### PAOT: include PAOT_123457891213 and PAOT_10
### PAOT_123457891213: include 1 2 3 4 5 7 8 9 12 13
### PAOT_10_inner: same with NVIDIA for comparison
### PAOT_10: original division
parser.add_argument('--data_root_path', default='/blue/kgong/s.kapoor/language_guided_segmentation/CLIP-Driven-Universal-Model_org/', help='data root path')
parser.add_argument('--data_txt_path', default='./dataset/dataset_list/', help='data txt path')
parser.add_argument('--batch_size', default=1, help='batch size')
parser.add_argument('--num_workers', default=8, type=int, help='workers numebr for DataLoader')
parser.add_argument('--a_min', default=-175, type=float, help='a_min in ScaleIntensityRanged')
parser.add_argument('--a_max', default=250, type=float, help='a_max in ScaleIntensityRanged')
parser.add_argument('--b_min', default=0.0, type=float, help='b_min in ScaleIntensityRanged')
parser.add_argument('--b_max', default=1.0, type=float, help='b_max in ScaleIntensityRanged')
parser.add_argument('--space_x', default=1.5, type=float, help='spacing in x direction')
parser.add_argument('--space_y', default=1.5, type=float, help='spacing in y direction')
parser.add_argument('--space_z', default=1.5, type=float, help='spacing in z direction')
parser.add_argument('--roi_x', default=96, type=int, help='roi size in x direction')
parser.add_argument('--roi_y', default=96, type=int, help='roi size in y direction')
parser.add_argument('--roi_z', default=96, type=int, help='roi size in z direction')
parser.add_argument('--num_samples', default=2, type=int, help='sample number in each ct')
parser.add_argument('--phase', default='train', help='train or validation or test')
parser.add_argument('--uniform_sample', action="store_true", default=False, help='whether utilize uniform sample strategy')
parser.add_argument('--datasetkey', nargs='+', default=['01', '02', '03', '04', '05',
'07', '08', '09', '12', '13', '10_03',
'10_06', '10_07', '10_08', '10_09', '10_10'],
help='the content for ')
parser.add_argument('--cache_dataset', action="store_true", default=False, help='whether use cache dataset')
parser.add_argument('--cache_rate', default=0.005, type=float, help='The percentage of cached data in total')
args = parser.parse_args()
process(args=args)
if __name__ == "__main__":
main()
# python -m torch.distributed.launch --nproc_per_node=2 --master_port=1234 train.py --dist True --uniform_sample