-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
327 lines (294 loc) · 10.4 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
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
import sys
from pathlib import Path
import yaml
import json
import numpy as np
import torch
import click
import argparse
from torch.nn.parallel import DistributedDataParallel as DDP
from segm.utils import distributed
import segm.utils.torch as ptu
from segm import config
from segm.model.factory import create_segmenter
from segm.optim.factory import create_optimizer, create_scheduler
from segm.data.factory import create_dataset
from segm.model.utils import num_params
from timm.utils import NativeScaler
from contextlib import suppress
from segm.utils.distributed import sync_model
from segm.engine import train_one_epoch, evaluate
@click.command(help="")
@click.option("--log-dir", type=str, help="logging directory")
@click.option("--dataset", type=str)
@click.option("--im-size", default=None, type=int, help="dataset resize size")
@click.option("--crop-size", default=None, type=int)
@click.option("--window-size", default=None, type=int)
@click.option("--window-stride", default=None, type=int)
@click.option("--backbone", default="", type=str)
@click.option("--decoder", default="", type=str)
@click.option("--optimizer", default="sgd", type=str)
@click.option("--scheduler", default="polynomial", type=str)
@click.option("--weight-decay", default=0.0, type=float)
@click.option("--dropout", default=0.0, type=float)
@click.option("--drop-path", default=0.1, type=float)
@click.option("--batch-size", default=None, type=int)
@click.option("--epochs", default=None, type=int)
@click.option("-lr", "--learning-rate", default=None, type=float)
@click.option("--normalization", default=None, type=str)
@click.option("--eval-freq", default=None, type=int)
@click.option("--amp/--no-amp", default=False, is_flag=True)
@click.option("--resume/--no-resume", default=True, is_flag=True)
@click.option("--policy-method", default='policy_net', type=str)
@click.option("--num-tokens-notshared", default=None, type=int)
@click.option("--num-tokens-shared", default=None, type=int)
@click.option("--policynet-ckpt", default=None, type=str)
@click.option("--warmup-iter", default=0, type=int)
@click.option("--min-lr", default=1e-5, type=float)
@click.option("--decoder-layers", default=2, type=int)
def main(
log_dir,
dataset,
im_size,
crop_size,
window_size,
window_stride,
backbone,
decoder,
optimizer,
scheduler,
weight_decay,
dropout,
drop_path,
batch_size,
epochs,
learning_rate,
normalization,
eval_freq,
amp,
resume,
policy_method,
num_tokens_notshared,
num_tokens_shared,
policynet_ckpt,
warmup_iter,
min_lr,
decoder_layers,
):
# start distributed mode
ptu.set_gpu_mode(True)
distributed.init_process()
# set up configuration
cfg = config.load_config()
model_cfg = cfg["model"][backbone]
dataset_cfg = cfg["dataset"][dataset]
if "mask_transformer" in decoder:
decoder_cfg = cfg["decoder"]["mask_transformer"]
decoder_cfg["n_layers"] = decoder_layers
else:
decoder_cfg = cfg["decoder"][decoder]
# model config
if not im_size:
im_size = dataset_cfg["im_size"]
if not crop_size:
crop_size = dataset_cfg.get("crop_size", im_size)
if not window_size:
window_size = dataset_cfg.get("window_size", im_size)
if not window_stride:
window_stride = dataset_cfg.get("window_stride", im_size)
model_cfg["image_size"] = (crop_size, crop_size)
model_cfg["backbone"] = backbone
model_cfg["dropout"] = dropout
model_cfg["drop_path_rate"] = drop_path
decoder_cfg["name"] = decoder
model_cfg["decoder"] = decoder_cfg
# dataset config
world_batch_size = dataset_cfg["batch_size"]
num_epochs = dataset_cfg["epochs"]
lr = dataset_cfg["learning_rate"]
if batch_size:
world_batch_size = batch_size
if epochs:
num_epochs = epochs
if learning_rate:
lr = learning_rate
if eval_freq is None:
eval_freq = dataset_cfg.get("eval_freq", 1)
if normalization:
model_cfg["normalization"] = normalization
# experiment config
batch_size = world_batch_size // ptu.world_size
variant = dict(
world_batch_size=world_batch_size,
version="normal",
resume=resume,
dataset_kwargs=dict(
dataset=dataset,
image_size=im_size,
crop_size=crop_size,
batch_size=batch_size,
normalization=model_cfg["normalization"],
split="train",
num_workers=10,
),
algorithm_kwargs=dict(
batch_size=batch_size,
start_epoch=0,
num_epochs=num_epochs,
eval_freq=eval_freq,
),
optimizer_kwargs=dict(
opt=optimizer,
lr=lr,
weight_decay=weight_decay,
momentum=0.9,
clip_grad=None,
sched=scheduler,
epochs=num_epochs,
min_lr=min_lr,
poly_power=0.9,
poly_step_size=1,
iter_warmup=warmup_iter,
),
net_kwargs=model_cfg,
amp=amp,
log_dir=log_dir,
inference_kwargs=dict(
im_size=im_size,
window_size=window_size,
window_stride=window_stride,
),
)
log_dir = Path(log_dir)
log_dir.mkdir(parents=True, exist_ok=True)
checkpoint_path = log_dir / "checkpoint.pth"
# dataset
dataset_kwargs = variant["dataset_kwargs"]
train_loader = create_dataset(dataset_kwargs)
val_kwargs = dataset_kwargs.copy()
val_kwargs["split"] = "val"
val_kwargs["batch_size"] = 1
val_kwargs["crop"] = False
val_loader = create_dataset(val_kwargs)
n_cls = train_loader.unwrapped.n_cls
# model
net_kwargs = variant["net_kwargs"]
net_kwargs["n_cls"] = n_cls
net_kwargs["policy_method"] = policy_method
net_kwargs["policy_schedule"] = (num_tokens_notshared, num_tokens_shared)
net_kwargs["policynet_ckpt"] = policynet_ckpt
model = create_segmenter(net_kwargs)
model.to(ptu.device)
# optimizer
optimizer_kwargs = variant["optimizer_kwargs"]
optimizer_kwargs["iter_max"] = len(train_loader) * optimizer_kwargs["epochs"]
optimizer_kwargs["iter_warmup"] = warmup_iter
optimizer_kwargs["min_lr"] = min_lr
optimizer_kwargs["weight_decay"] = weight_decay
optimizer_kwargs["opt"] = optimizer
opt_args = argparse.Namespace()
opt_vars = vars(opt_args)
for k, v in optimizer_kwargs.items():
opt_vars[k] = v
optimizer = create_optimizer(opt_args, model)
lr_scheduler = create_scheduler(opt_args, optimizer)
num_iterations = 0
amp_autocast = suppress
loss_scaler = None
if amp:
amp_autocast = torch.cuda.amp.autocast
loss_scaler = NativeScaler()
# resume
if resume and checkpoint_path.exists():
print(f"Resuming training from checkpoint: {checkpoint_path}")
checkpoint = torch.load(checkpoint_path, map_location="cpu")
model.load_state_dict(checkpoint["model"])
optimizer.load_state_dict(checkpoint["optimizer"])
if loss_scaler and "loss_scaler" in checkpoint:
loss_scaler.load_state_dict(checkpoint["loss_scaler"])
lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])
variant["algorithm_kwargs"]["start_epoch"] = checkpoint["epoch"] + 1
else:
sync_model(log_dir, model)
if ptu.distributed:
model = DDP(model, device_ids=[ptu.device], find_unused_parameters=True)
# save config
variant_str = yaml.dump(variant)
print(f"Configuration:\n{variant_str}")
variant["net_kwargs"] = net_kwargs
variant["dataset_kwargs"] = dataset_kwargs
log_dir.mkdir(parents=True, exist_ok=True)
with open(log_dir / "variant.yml", "w") as f:
f.write(variant_str)
# train
start_epoch = variant["algorithm_kwargs"]["start_epoch"]
num_epochs = variant["algorithm_kwargs"]["num_epochs"]
eval_freq = variant["algorithm_kwargs"]["eval_freq"]
model_without_ddp = model
if hasattr(model, "module"):
model_without_ddp = model.module
val_seg_gt = val_loader.dataset.get_gt_seg_maps()
print(f"Train dataset length: {len(train_loader.dataset)}")
print(f"Val dataset length: {len(val_loader.dataset)}")
print(f"Encoder parameters: {num_params(model_without_ddp.encoder)}")
print(f"Decoder parameters: {num_params(model_without_ddp.decoder)}")
for epoch in range(start_epoch, num_epochs):
# train for one epoch
train_logger = train_one_epoch(
model,
train_loader,
optimizer,
lr_scheduler,
epoch,
amp_autocast,
loss_scaler,
)
# save checkpoint
if ptu.dist_rank == 0:
snapshot = dict(
model=model_without_ddp.state_dict(),
optimizer=optimizer.state_dict(),
n_cls=model_without_ddp.n_cls,
lr_scheduler=lr_scheduler.state_dict(),
)
if loss_scaler is not None:
snapshot["loss_scaler"] = loss_scaler.state_dict()
snapshot["epoch"] = epoch
torch.save(snapshot, checkpoint_path)
# evaluate
eval_epoch = epoch % eval_freq == 0 or epoch == num_epochs - 1
if eval_epoch:
eval_logger = evaluate(
model,
val_loader,
val_seg_gt,
window_size,
window_stride,
amp_autocast,
model_cfg
)
print(f"Stats [{epoch}]:", eval_logger, flush=True)
print("")
# log stats
if ptu.dist_rank == 0:
train_stats = {
k: meter.global_avg for k, meter in train_logger.meters.items()
}
val_stats = {}
if eval_epoch:
val_stats = {
k: meter.global_avg for k, meter in eval_logger.meters.items()
}
log_stats = {
**{f"train_{k}": v for k, v in train_stats.items()},
**{f"val_{k}": v for k, v in val_stats.items()},
"epoch": epoch,
"num_updates": (epoch + 1) * len(train_loader),
}
with open(log_dir / "log.txt", "a") as f:
f.write(json.dumps(log_stats) + "\n")
distributed.barrier()
distributed.destroy_process()
sys.exit(1)
if __name__ == "__main__":
main()