-
Notifications
You must be signed in to change notification settings - Fork 11
/
voco_train.py
279 lines (234 loc) · 11.7 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
# 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 models.voco_head import VoCoHead
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 *
from utils.ops import *
from utils.utils import AverageMeter, distributed_all_gather
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '28890'
import resource
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (8192, rlimit[1]))
print('Setting resource limit:', str(resource.getrlimit(resource.RLIMIT_NOFILE)))
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 = []
run_loss = AverageMeter()
pos_avg, neg_avg, base_avg = AverageMeter(), AverageMeter(), AverageMeter()
for step, batch in enumerate(train_loader):
t1 = time()
img, labels, crops = batch
img, crops = concat_image(img), concat_image(crops)
# print(img.size(), crops.size(), labels.size())
img, crops, labels = img.cuda(), crops.cuda(), labels.cuda()
with autocast(enabled=args.amp):
# loss = model(img, crops, labels)
pos, neg, b_loss = model(img, crops, labels)
loss = pos + neg + b_loss
loss_train.append(loss.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()
run_loss.update(loss.item(), n=args.batch_size)
pos_avg.update(pos.item(), n=args.batch_size)
neg_avg.update(neg.item(), n=args.batch_size)
base_avg.update(b_loss.item(), n=args.batch_size)
lr = optimizer.param_groups[0]["lr"]
if args.distributed:
if dist.get_rank() == 0:
print("Step:{}/{}, Loss:{:.4f}, Time:{:.4f}".format
(global_step, args.num_steps, loss, time() - t1))
else:
print("Step:{}/{}, Loss:{:.4f}, pos:{:.4f}, neg:{:.4f}, base:{:.4f}, "
"lr:{:.8f}, Time:{:.4f}".format(global_step, args.num_steps,
run_loss.avg, pos_avg.avg, neg_avg.avg, base_avg.avg,
lr, 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
freq = 1000
val_freq = global_step % freq == 0
if val_cond:
checkpoint = {
"global_step": global_step,
"state_dict": model.state_dict(),
"optimizer": optimizer,
}
save_ckp(checkpoint, logdir + "/model_current_epoch.pt")
if val_freq:
checkpoint = {
"global_step": global_step,
"state_dict": model.state_dict(),
"optimizer": optimizer,
}
save_ckp(checkpoint, logdir + "/model_step" + str(global_step) + ".pt")
return global_step, loss, val_best
roi = 64
parser = argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument("--logdir", default="logs", type=str, help="directory to save logs")
parser.add_argument("--epochs", default=100, type=int, help="number of training epochs")
parser.add_argument("--num_steps", default=250000, 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=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("--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=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=1e-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", default=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)
# './runs/logs_10k/model_current_epoch.pt'
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", default=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", default=False, help="use monai smartcache Dataset")
parser.add_argument("--cache_dataset", action="store_true", help="use monai cache Dataset")
args = parser.parse_args()
logdir = args.logdir
torch.cuda.set_device(0)
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()
print(
"Training in distributed mode with multiple processes, 1 GPU per process. Process %d, total %d."
% (args.rank, args.world_size)
)
else:
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 = VoCoHead(args)
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, 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:
print('resume from previous checkpoints')
model_pth = args.resume
model_dict = torch.load(model_pth)
model.load_state_dict(model_dict, strict=False)
global_step = model_dict["global_step"]
# optimizer = model_dict["optimizer"]["state_dict"]
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 = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = DistributedDataParallel(model, device_ids=[args.local_rank])
train_loader = get_loader(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, 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(), logdir + "final_model.pth")
dist.destroy_process_group()
else:
torch.save(model.state_dict(), logdir + "final_model.pth")
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
if __name__ == "__main__":
main()