-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
341 lines (309 loc) · 17.2 KB
/
main.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
# 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 wandb
import numpy as np
import torch
import torch.distributed as dist
import torch.optim as optim
from losses.loss import Loss
from models.ssl_head import SSLHead
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.data_utils import get_loader
from utils.ops import aug_rand, rot_rand
def main():
def save_ckp(state, checkpoint_dir):
torch.save(state, checkpoint_dir)
def train(args, global_step, train_loader, val_best, scaler):
model.train()
loss_train = []
loss_train_recon = []
for step, batch in enumerate(train_loader):
t1 = time()
x = batch["image"].cuda()
x1, rot1 = rot_rand(args, x)
x2, rot2 = rot_rand(args, x)
x1_augment = aug_rand(args, x1)
x2_augment = aug_rand(args, x2)
x1_augment = x1_augment
x2_augment = x2_augment
with autocast(enabled=args.amp):
rot1_p, contrastive1_p, rec_x1 = model(x1_augment)
rot2_p, contrastive2_p, rec_x2 = model(x2_augment)
rot_p = torch.cat([rot1_p, rot2_p], dim=0)
rots = torch.cat([rot1, rot2], dim=0)
imgs_recon = torch.cat([rec_x1, rec_x2], dim=0)
imgs = torch.cat([x1, x2], dim=0)
loss, losses_tasks = loss_function(rot_p, rots, contrastive1_p, contrastive2_p, imgs_recon, imgs)
loss_train.append(loss.item())
loss_train_recon.append(losses_tasks[2].item())
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()
wandb.log({"train_total_loss": loss.item(),
"train_rot_loss": losses_tasks[0].item(),
"train_contrast_loss": losses_tasks[1].item(),
"train_recons_loss": losses_tasks[2].item(),
"train_mci_loss": losses_tasks[3].item(),
"time": time() - t1
})
if args.distributed:
if dist.get_rank() == 0:
logger.info("Step:{}/{}, Loss:{:.4f}, Time:{:.4f}".format(global_step, args.num_steps, loss.item(), time() - t1))
else:
logger.info("Step:{}/{}, Loss:{:.4f}, Time:{:.4f}".format(global_step, args.num_steps, loss.item(), time() - t1))
global_step += 1
if args.distributed:
val_cond = (dist.get_rank() == 0) and (global_step % args.eval_num == 0)
else:
val_cond = global_step % args.eval_num == 0
if val_cond:
val_loss, losses_tasks, img_list = validation(args, test_loader)
val_loss_recon = losses_tasks[2]
wandb.log({
"val_total_loss": val_loss,
"val_rot_loss": losses_tasks[0],
"val_contrast_loss": losses_tasks[1],
"val_recons_loss": losses_tasks[2],
"val_mci_loss": losses_tasks[3],
})
writer.add_scalar("Validation/loss_recon", scalar_value=val_loss_recon, global_step=global_step)
writer.add_scalar("train/loss_total", scalar_value=np.mean(loss_train), global_step=global_step)
writer.add_scalar("train/loss_recon", scalar_value=np.mean(loss_train_recon), global_step=global_step)
writer.add_image("Validation/x1_gt", img_list[0], global_step, dataformats="HW")
writer.add_image("Validation/x1_aug", img_list[1], global_step, dataformats="HW")
writer.add_image("Validation/x1_recon", img_list[2], global_step, dataformats="HW")
if val_loss_recon < val_best:
val_best = val_loss_recon
checkpoint = {
"global_step": global_step,
"state_dict": model.state_dict(),
"optimizer": optimizer.state_dict(),
}
save_ckp(checkpoint, logdir + f"/model_best_{args.exp}.pt")
wandb.save(logdir + f"/model_best_{args.exp}.pt")
logger.info(
f"Model was saved! Best Recon. Val Loss: {val_best:.4f} | Current Recon. Val Loss: {val_loss_recon:.4f} | Current Val Loss: {val_loss:.4f}"
)
else:
logger.info(
f"Model was not saved! Best Recon. Val Loss: {val_best:.4f} | Current Recon. Val Loss: {val_loss_recon:.4f} | Current Val Loss: {val_loss:.4f}"
)
return global_step, loss, val_best
def validation(args, test_loader):
model.eval()
loss_val = []
loss_val_rot = []
loss_val_contrast = []
loss_val_recon = []
loss_val_mci = []
with torch.no_grad():
for step, batch in enumerate(test_loader):
val_inputs = batch["image"].cuda()
x1, rot1 = rot_rand(args, val_inputs)
x2, rot2 = rot_rand(args, val_inputs)
x1_augment = aug_rand(args, x1)
x2_augment = aug_rand(args, x2)
with autocast(enabled=args.amp):
rot1_p, contrastive1_p, rec_x1 = model(x1_augment)
rot2_p, contrastive2_p, rec_x2 = model(x2_augment)
rot_p = torch.cat([rot1_p, rot2_p], dim=0)
rots = torch.cat([rot1, rot2], dim=0)
imgs_recon = torch.cat([rec_x1, rec_x2], dim=0)
imgs = torch.cat([x1, x2], dim=0)
loss, losses_tasks = loss_function(rot_p, rots, contrastive1_p, contrastive2_p, imgs_recon, imgs)
loss_rot = losses_tasks[0]
loss_contrast = losses_tasks[1]
loss_recon = losses_tasks[2]
loss_mci = losses_tasks[3]
loss_val.append(loss.item())
loss_val_rot.append(loss_rot.item())
loss_val_contrast.append(loss_contrast.item())
loss_val_recon.append(loss_recon.item())
loss_val_mci.append(loss_mci.item())
x_gt = x1.detach().cpu().numpy()
x_gt = (x_gt - np.min(x_gt)) / (np.max(x_gt) - np.min(x_gt))
xgt = x_gt[0][0][:, :, 48] * 255.0
xgt = xgt.astype(np.uint8)
x1_augment = x1_augment.detach().cpu().numpy()
x1_augment = (x1_augment - np.min(x1_augment)) / (np.max(x1_augment) - np.min(x1_augment))
x_aug = x1_augment[0][0][:, :, 48] * 255.0
x_aug = x_aug.astype(np.uint8)
rec_x1 = rec_x1.detach().cpu().numpy()
rec_x1 = (rec_x1 - np.min(rec_x1)) / (np.max(rec_x1) - np.min(rec_x1))
recon = rec_x1[0][0][:, :, 48] * 255.0
recon = recon.astype(np.uint8)
img_list = [xgt, x_aug, recon]
logger.info("Validation step:{}, Loss:{:.4f}, Loss Reconstruction:{:.4f}".format(step, loss.item(), loss_recon.item()))
return np.mean(loss_val), (np.mean(loss_val_rot), np.mean(loss_val_contrast), np.mean(loss_val_recon), np.mean(loss_val_mci)), img_list
parser = argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument("--exp", default="test", type=str, help="directory to save the logs")
parser.add_argument("--epochs", default=100, type=int, help="number of training epochs")
parser.add_argument("--num_steps", default=100000, type=int, help="number of training iterations")
parser.add_argument("--eval_num", default=100, type=int, help="evaluation frequency")
parser.add_argument("--warmup_steps", default=500, 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("--dropout_path_rate", default=0.0, type=float, help="drop path rate")
parser.add_argument("--use_checkpoint", action="store_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("--alpha4", default=0.0, type=float, help="mean curvature loss coefficient")
parser.add_argument("--a_min", default=-1000, type=float, help="a_min in ScaleIntensityRanged")
parser.add_argument("--a_max", default=1000, 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=2.0, 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("--batch_size", default=2, type=int, help="number of batch size")
parser.add_argument("--sw_batch_size", default=2, type=int, help="number of sliding window batch size")
parser.add_argument("--lr", default=4e-4, type=float, help="learning rate")
parser.add_argument("--decay", default=0.1, type=float, help="decay rate")
parser.add_argument("--momentum", default=0.9, type=float, help="momentum")
parser.add_argument("--lrdecay", action="store_true", help="enable learning rate decay")
parser.add_argument("--max_grad_norm", default=1.0, type=float, help="maximum gradient norm")
parser.add_argument("--loss_type", default="SSL", type=str)
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=None, type=str, help="resume training")
parser.add_argument("--local_rank", type=int, default=0, help="local rank")
parser.add_argument("--grad_clip", action="store_true", help="gradient clip")
parser.add_argument("--noamp", action="store_true", 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("--smartcache_dataset", action="store_true", help="use monai smartcache Dataset")
parser.add_argument("--cache_dataset", action="store_true", help="use monai cache Dataset")
parser.add_argument("--data_dir", default="./dataset/dataset0/", type=str, help="dataset directory")
parser.add_argument("--json_list", default="./jsons/dataset0.json", type=str, help="dataset json file")
parser.add_argument("--resume_ssl", action="store_true", help="resume training from pretrained self-supervised checkpoint")
parser.add_argument(
"--pretrained_path", default="./pretrained_models/model_swinvit.pt", type=str, help="path to pretrained checkpoint")
args = parser.parse_args()
logdir = "./runs/" + args.exp
os.makedirs(logdir, exist_ok=True)
wandb.init(project="SwinUNETR-Pretrain", name=args.exp, config=args)
logger = logging.getLogger('logger')
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler(os.path.join(logdir, 'logFile.log'))
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
args.amp = not args.noamp
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
if args.distributed:
args.local_rank = int(os.environ['LOCAL_RANK'])
args.device = "cuda:0"
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()
logger.info(
"Training in distributed mode with multiple processes, 1 GPU per process. Process %d, total %d."
% (args.rank, args.world_size)
)
else:
logger.info("Training with a single process on 1 GPUs.")
assert args.rank >= 0
if args.rank == 0:
writer = SummaryWriter(logdir)
else:
writer = None
model = SSLHead(args)
wandb.watch(model, log='all', log_freq=100)
if args.resume_ssl:
try:
model_dict = torch.load(args.pretrained_path)
state_dict = model_dict["state_dict"]
# fix potential differences in state dict keys from pre-training to
# fine-tuning
if "module." in list(state_dict.keys())[0]:
logger.info("Tag 'module.' found in state dict - fixing!")
for key in list(state_dict.keys()):
state_dict[key.replace("module.", "")] = state_dict.pop(key)
if "swin_vit" in list(state_dict.keys())[0]:
logger.info("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)
# We now load model weights, setting param `strict` to False, i.e.:
# this load the encoder weights (Swin-ViT, SSL pre-trained), but leaves
# the decoder weights untouched (CNN UNet decoder).
model.load_state_dict(state_dict, strict=False)
logger.info("Using pretrained self-supervised Swin UNETR backbone weights !")
except ValueError:
raise ValueError("Self-supervised pre-trained weights not available for" + str(args.model_name))
model.cuda()
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, weight_decay=args.decay)
elif args.opt == "sgd":
optimizer = optim.SGD(params=model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.decay)
if args.resume:
model_pth = args.resume
model_dict = torch.load(model_pth)
model.load_state_dict(model_dict["state_dict"])
model.epoch = model_dict["epoch"]
model.optimizer = model_dict["optimizer"]
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)
loss_function = Loss(args.batch_size * args.sw_batch_size, args)
if args.distributed:
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = DistributedDataParallel(model, device_ids=[args.local_rank])
train_loader, test_loader = get_loader(args)
global_step = 0
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, train_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.state_dict(), os.path.join(logdir, f"{args.exp}_final_model.pth"))
dist.destroy_process_group()
else:
torch.save(model.state_dict(), os.path.join(logdir, f"{args.exp}_final_model.pth"))
save_ckp(checkpoint, os.path.join(logdir, f"{args.exp}_final_epoch.pth"))
if __name__ == "__main__":
main()