-
Notifications
You must be signed in to change notification settings - Fork 6
/
voco_train.py
350 lines (287 loc) · 14.5 KB
/
voco_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
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
# Copyright 2020 - 2022 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
from time import time
import logging
import numpy as np
import torch
import torch.distributed as dist
import torch.optim as optim
from optimizers.lr_scheduler import WarmupCosineSchedule
from torch.cuda.amp import GradScaler, autocast
from torch.nn.parallel import DistributedDataParallel
from torch.utils.tensorboard import SummaryWriter
from utils import *
from monai.networks.nets import SwinUNETR
from utils.ops import *
from utils.utils import AverageMeter, distributed_all_gather
import torch.multiprocessing
from monai.losses import DiceCELoss
from utils.data_utils import get_loader, get_loader_unlabeled
torch.multiprocessing.set_sharing_strategy('file_system')
import resource
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (8192, rlimit[1]))
def main():
def save_ckp(state, checkpoint_dir):
torch.save(state, checkpoint_dir)
def train(args, global_step, labeled_loader, unlabeled_loader, val_best, scaler):
model.train()
run_loss = AverageMeter()
labeled_iter = iter(labeled_loader)
unlabeled_iter = iter(unlabeled_loader)
for step in range(args.num_steps):
t1 = time()
labeled = next(labeled_iter)
labeled_img, labeled_label = labeled['image'], labeled['label']
labeled_img, labeled_label = labeled_img.cuda(), labeled_label.cuda()
with autocast(enabled=args.amp):
# seg_loss
labeled_outputs = model(labeled_img)
dice_loss = DiceCELoss(include_background=False, to_onehot_y=True, softmax=True)
seg_loss = dice_loss(labeled_outputs, labeled_label)
loss = seg_loss
if step == args.num_steps // 2:
### start semi on unlabeled data !!!
teacher_model = SwinUNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=args.out_channels,
feature_size=args.feature_size,
use_v2=True
)
model_dict = model.state_dict()
teacher_model = load(teacher_model, model_dict)
teacher_model.cuda()
teacher_model.eval()
if step > args.num_steps // 2:
unlabeled = next(unlabeled_iter)
unlabeled_img = unlabeled['image']
unlabeled_img = unlabeled_img.cuda()
with autocast(enabled=args.amp):
with torch.no_grad():
## pseudo labels for unlabeled data
new_labels = teacher_model(unlabeled_img)
new_labels = new_labels.argmax(1).unsqueeze(1)
unlabeled_outputs = model(unlabeled_img)
dice_loss = DiceCELoss(include_background=False, to_onehot_y=True, softmax=True)
semi_loss = dice_loss(unlabeled_outputs, new_labels)
loss = seg_loss + semi_loss
if args.amp:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
if args.grad_clip:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
if args.lrdecay:
scheduler.step()
optimizer.zero_grad()
run_loss.update(loss.item(), n=args.batch_size)
lr = optimizer.param_groups[0]["lr"]
print_num = 1
if args.distributed:
print_cond = (args.rank == 0) and (global_step % print_num == 0)
else:
print_cond = global_step % print_num == 0
if print_cond:
print("Step:{}/{}, Loss:{:.4f} "
"lr:{:.8f}, Time:{:.4f}".format(global_step, args.num_steps,
run_loss.avg,
lr, time() - t1))
global_step += 1
if args.distributed:
val_cond = (args.rank == 0) and (global_step % args.eval_num == 0)
else:
val_cond = global_step % args.eval_num == 0
if val_cond:
checkpoint = {
"global_step": global_step,
"state_dict": model.state_dict(),
}
save_ckp(checkpoint, logdir + "/model_current_epoch.pt")
save_ckp(checkpoint, logdir + "/model_step" + str(global_step) + ".pt")
return global_step, loss, val_best
roi = 96
parser = argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument("--logdir", default="logs", type=str, help="directory to save logs")
parser.add_argument("--num_steps", default=1000000, type=int, help="number of training iterations")
parser.add_argument("--eval_num", default=1000, type=int, help="evaluation frequency")
parser.add_argument("--warmup_steps", default=5000, type=int, help="warmup steps")
parser.add_argument("--in_channels", default=1, type=int, help="number of input channels")
parser.add_argument("--feature_size", default=48, type=int, help="embedding size")
parser.add_argument("--out_channels", default=21, type=int, help="number of output channels")
parser.add_argument("--dropout_path_rate", default=0.0, type=float, help="drop path rate")
parser.add_argument("--use_checkpoint", default=True, help="use gradient checkpointing to save memory")
parser.add_argument("--spatial_dims", default=3, type=int, help="spatial dimension of input data")
parser.add_argument("--a_min", default=-175.0, type=float, help="a_min in ScaleIntensityRanged")
parser.add_argument("--a_max", default=250.0, 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=roi, type=int, help="roi size in x direction")
parser.add_argument("--roi_y", default=roi, type=int, help="roi size in y direction")
parser.add_argument("--roi_z", default=roi, type=int, help="roi size in z direction")
parser.add_argument("--batch_size", default=4, type=int, help="number of batch size")
parser.add_argument("--sw_batch_size", default=4, type=int, help="number of sliding window batch size")
parser.add_argument("--RandFlipd_prob", default=0.2, type=float, help="RandFlipd aug probability")
parser.add_argument("--RandRotate90d_prob", default=0.2, type=float, help="RandRotate90d aug probability")
parser.add_argument("--RandScaleIntensityd_prob", default=0.1, type=float,
help="RandScaleIntensityd aug probability")
parser.add_argument("--RandShiftIntensityd_prob", default=0.1, type=float,
help="RandShiftIntensityd aug probability")
parser.add_argument("--lr", default=1e-4, type=float, help="learning rate")
parser.add_argument("--decay", default=1e-3, type=float, help="decay rate")
parser.add_argument("--momentum", default=0.9, type=float, help="momentum")
parser.add_argument("--lrdecay", default=True, help="enable learning rate decay")
parser.add_argument("--workers", default=16, type=int, help="number of batch size")
parser.add_argument("--max_grad_norm", default=1.0, type=float, help="maximum gradient norm")
parser.add_argument("--opt", default="adamw", type=str, help="optimization algorithm")
parser.add_argument("--lr_schedule", default="warmup_cosine", type=str)
parser.add_argument("--resume", default=False, type=str, help="resume training")
parser.add_argument("--local-rank", type=int, default=0, help="local rank")
parser.add_argument("--grad_clip", default=False, help="gradient clip")
parser.add_argument("--noamp", default=False, help="do NOT use amp for training")
parser.add_argument("--dist-url", default="env://", help="url used to set up distributed training")
parser.add_argument("--cache", default=False, help="use monai cache Dataset")
args = parser.parse_args()
logdir = args.logdir
args.amp = True
torch.backends.cudnn.benchmark = True
# torch.autograd.set_detect_anomaly(True)
args.distributed = False
if "WORLD_SIZE" in os.environ:
args.distributed = int(os.environ["WORLD_SIZE"]) > 1
args.world_size = 1
args.rank = 0
if args.distributed:
args.device = "cuda:%d" % args.local_rank
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend="nccl", init_method=args.dist_url)
args.world_size = torch.distributed.get_world_size()
args.rank = torch.distributed.get_rank()
if args.rank == 0:
print(
"Training in distributed mode with multiple processes, 1 GPU per process. Process %d, total %d."
% (args.rank, args.world_size)
)
else:
torch.cuda.set_device(0)
print("Training with a single process on 1 GPUs.")
assert args.rank >= 0
if args.rank == 0:
os.makedirs(logdir, exist_ok=True)
logger = init_log('global', logging.INFO)
logger.propagate = 0
model = SwinUNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=args.out_channels,
feature_size=args.feature_size,
use_v2=True
)
model.cuda()
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
if args.rank == 0 or args.distributed is False:
print("Total parameters count", pytorch_total_params)
if args.opt == "adam":
optimizer = optim.Adam(params=model.parameters(), lr=args.lr, weight_decay=args.decay)
elif args.opt == "adamw":
optimizer = optim.AdamW(params=model.parameters(), lr=args.lr, amsgrad=True)
elif args.opt == "sgd":
optimizer = optim.SGD(params=model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.decay)
global_step = 0
if args.resume:
if args.rank == 0 or args.distributed is False:
print('resume from previous checkpoints')
model_pt = os.path.join(args.logdir, 'model_current_epoch.pt')
model_dict = torch.load(model_pt)
model = load(model, model_dict)
global_step = model_dict["global_step"]
if args.lrdecay:
if args.lr_schedule == "warmup_cosine":
scheduler = WarmupCosineSchedule(optimizer, warmup_steps=args.warmup_steps, t_total=args.num_steps)
elif args.lr_schedule == "poly":
def lambdas(epoch):
return (1 - float(epoch) / float(args.epochs)) ** 0.9
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambdas)
if args.distributed:
model = DistributedDataParallel(model, device_ids=[args.local_rank], find_unused_parameters=True)
labeled_loader = get_loader(args)
unlabeled_loader = get_loader_unlabeled(args)
best_val = 1e8
if args.amp:
scaler = GradScaler()
else:
scaler = None
while global_step < args.num_steps:
global_step, loss, best_val = train(args, global_step, labeled_loader, unlabeled_loader, best_val, scaler)
checkpoint = {"epoch": args.epochs, "state_dict": model.state_dict(), "optimizer": optimizer.state_dict()}
if args.distributed:
if dist.get_rank() == 0:
torch.save(model.module.state_dict(), logdir + "final_model.pt")
dist.destroy_process_group()
else:
torch.save(model.state_dict(), logdir + "final_model.pt")
save_ckp(checkpoint, logdir + "/model_final_epoch.pt")
logs = set()
def init_log(name, level=logging.INFO):
if (name, level) in logs:
return
logs.add((name, level))
logger = logging.getLogger(name)
logger.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(level)
if "SLURM_PROCID" in os.environ:
rank = int(os.environ["SLURM_PROCID"])
logger.addFilter(lambda record: rank == 0)
else:
rank = 0
format_str = "[%(asctime)s][%(levelname)8s] %(message)s"
formatter = logging.Formatter(format_str)
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
def load(model, model_dict):
if "state_dict" in model_dict.keys():
state_dict = model_dict["state_dict"]
elif "network_weights" in model_dict.keys():
state_dict = model_dict["network_weights"]
elif "net" in model_dict.keys():
state_dict = model_dict["net"]
else:
state_dict = model_dict
if "module." in list(state_dict.keys())[0]:
# print("Tag 'module.' found in state dict - fixing!")
for key in list(state_dict.keys()):
state_dict[key.replace("module.", "")] = state_dict.pop(key)
if "backbone." in list(state_dict.keys())[0]:
# print("Tag 'backbone.' found in state dict - fixing!")
for key in list(state_dict.keys()):
state_dict[key.replace("backbone.", "")] = state_dict.pop(key)
if "swin_vit" in list(state_dict.keys())[0]:
# print("Tag 'swin_vit' found in state dict - fixing!")
for key in list(state_dict.keys()):
state_dict[key.replace("swin_vit", "swinViT")] = state_dict.pop(key)
current_model_dict = model.state_dict()
new_state_dict = {
k: state_dict[k] if (k in state_dict.keys()) and (state_dict[k].size() == current_model_dict[k].size()) else current_model_dict[k]
for k in current_model_dict.keys()}
model.load_state_dict(new_state_dict, strict=True)
return model
if __name__ == "__main__":
main()