-
Notifications
You must be signed in to change notification settings - Fork 5
/
compress_segmenter.py
466 lines (416 loc) · 15.4 KB
/
compress_segmenter.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
'''
* Copyright (c) 2023, Dachuan Shi.
* Copyright (c) 2021, Robin Strudel.
* Copyright (c) INRIA.
* All rights reserved.
* For full license text, see LICENSE.txt file in the repo root
* By Dachuan Shi
'''
import sys, os
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
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, compress, print_compression_statistics
from fvcore.nn import FlopCountAnalysis, flop_count_str, flop_count_table
@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("--pretrained", default="", type=str)
@click.option('--w_sp_attn', default=4.8e-3, type=float, help='weightage to attn sparsity')
@click.option('--w_sp_mlp', default=2e-4, type=float, help='weightage to mlp sparsity')
@click.option('--p', default=0.5, type=float, help='total compression ratio')
@click.option('--interval', default=100, type=int, help='interval of updating compression mask')
@click.option('--lr-search', type=float, default=0.001, help='search learning rate')
@click.option('--epochs-search', default=16, 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,
pretrained,
w_sp_attn,
w_sp_mlp,
p,
interval,
lr_search,
epochs_search,
):
Path(os.path.join('/tmp', os.environ['DATASET'])).mkdir(parents=True, exist_ok=True) #
# start distributed mode
ptu.set_gpu_mode(True)
distributed.init_process()
# upop settings
args = {
'pretrained': pretrained,
'w_sp_attn': w_sp_attn,
'w_sp_mlp': w_sp_mlp,
'p': p,
'interval': interval,
'lr_search': lr_search,
'epochs': epochs,
'epochs_search': epochs_search,
}
# 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"]
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=1e-5,
poly_power=0.9,
poly_step_size=1,
),
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
print(f"Creating model for searching")
net_kwargs["search"] = True
search_model = create_segmenter(net_kwargs)
if args['pretrained']:
checkpoint = torch.load(args['pretrained'], map_location='cpu')
checkpoint_model = checkpoint['model']
msg = search_model.load_state_dict(checkpoint_model, strict=False)
print("missing keys: ", msg.missing_keys)
search_model.to(ptu.device)
### Count parameters and FLOPs ####
search_model.eval()
with torch.no_grad():
input = torch.randn(1, 3, 512, 512).to(ptu.device),
flop = FlopCountAnalysis(search_model, input)
print(flop_count_table(flop, max_depth=7, show_param_shapes=True))
print("Total", flop.total() / 1e9)
search_model.train()
#######################################
# optimizer
optimizer_kwargs = variant["optimizer_kwargs"]
optimizer_kwargs["epochs"] = args['epochs_search']
optimizer_kwargs["iter_max"] = len(train_loader) * optimizer_kwargs["epochs"]
optimizer_kwargs["iter_warmup"] = 0.0
optimizer_kwargs["lr"] = args['lr_search']
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, search_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")
search_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, search_model)
if ptu.distributed:
search_model = DDP(search_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)
# search
start_epoch = 0
num_epochs = args['epochs_search']
eval_freq = variant["algorithm_kwargs"]["eval_freq"]
search_model_without_ddp = search_model
if hasattr(search_model, "module"):
search_model_without_ddp = search_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(search_model_without_ddp.encoder)}")
print(f"Decoder parameters: {num_params(search_model_without_ddp.decoder)}")
print(f"Start searching")
for epoch in range(start_epoch, num_epochs):
# train for one epoch
train_logger = train_one_epoch(
search_model,
train_loader,
optimizer,
None,
epoch,
amp_autocast,
loss_scaler,
args=args,
search=True,
)
print_compression_statistics(search_model)
###############################################################
print(f"Creating model for retrain")
net_kwargs["search"] = False
model = create_segmenter(net_kwargs)
msg = model.load_state_dict(search_model_without_ddp.state_dict(), strict=False)
print("missing keys: ", msg.missing_keys)
compress(model, search_model_without_ddp)
model.to(ptu.device)
### Count parameters and FLOPs ####
model.eval()
with torch.no_grad():
input = torch.randn(1, 3, 512, 512).to(ptu.device),
flop = FlopCountAnalysis(model, input)
print(flop_count_table(flop, max_depth=7, show_param_shapes=True))
print("Total", flop.total() / 1e9)
model.train()
#######################################
# optimizer
optimizer_kwargs = variant["optimizer_kwargs"]
optimizer_kwargs["epochs"] = epochs
optimizer_kwargs["iter_max"] = len(train_loader) * optimizer_kwargs["epochs"]
optimizer_kwargs["iter_warmup"] = 0.0
optimizer_kwargs["lr"] = learning_rate
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)}")
print(f"Start retrain")
best = 0
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,
args=args,
)
# 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,
)
print(f"Stats [{epoch}]:", eval_logger, flush=True)
print("")
# save best checkpoint
if ptu.dist_rank == 0 and eval_logger.meters['mean_iou'].global_avg > best:
torch.save(snapshot, log_dir / "checkpoint_best.pth")
best = eval_logger.meters['mean_iou'].global_avg
# 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()