-
Notifications
You must be signed in to change notification settings - Fork 558
/
source_only.py
197 lines (170 loc) · 7.34 KB
/
source_only.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
"""
@author: Junguang Jiang
@contact: JiangJunguang1123@outlook.com
"""
import logging
import os
import argparse
import sys
import torch
from torch.nn.parallel import DistributedDataParallel
from detectron2.engine import default_writers, launch
from detectron2.checkpoint import DetectionCheckpointer, PeriodicCheckpointer
import detectron2.utils.comm as comm
from detectron2.solver.build import get_default_optimizer_params, maybe_add_gradient_clipping
from detectron2.solver import build_lr_scheduler
from detectron2.data import (
build_detection_train_loader,
get_detection_dataset_dicts,
)
from detectron2.utils.events import EventStorage
sys.path.append('../../..')
import tllib.vision.models.object_detection.meta_arch as models
import utils
def train(model, logger, cfg, args):
model.train()
distributed = comm.get_world_size() > 1
if distributed:
model_without_parallel = model.module
else:
model_without_parallel = model
# define optimizer and lr scheduler
params = []
for module, lr in model_without_parallel.get_parameters(cfg.SOLVER.BASE_LR):
params.extend(
get_default_optimizer_params(
module,
base_lr=lr,
weight_decay_norm=cfg.SOLVER.WEIGHT_DECAY_NORM,
bias_lr_factor=cfg.SOLVER.BIAS_LR_FACTOR,
weight_decay_bias=cfg.SOLVER.WEIGHT_DECAY_BIAS,
)
)
optimizer = maybe_add_gradient_clipping(cfg, torch.optim.SGD)(
params,
lr=cfg.SOLVER.BASE_LR,
momentum=cfg.SOLVER.MOMENTUM,
nesterov=cfg.SOLVER.NESTEROV,
weight_decay=cfg.SOLVER.WEIGHT_DECAY,
)
scheduler = build_lr_scheduler(cfg, optimizer)
# resume from the last checkpoint
checkpointer = DetectionCheckpointer(
model, cfg.OUTPUT_DIR, optimizer=optimizer, scheduler=scheduler
)
start_iter = (
checkpointer.resume_or_load(cfg.MODEL.WEIGHTS, resume=args.resume).get("iteration", -1) + 1
)
max_iter = cfg.SOLVER.MAX_ITER
periodic_checkpointer = PeriodicCheckpointer(
checkpointer, cfg.SOLVER.CHECKPOINT_PERIOD, max_iter=max_iter
)
writers = default_writers(cfg.OUTPUT_DIR, max_iter) if comm.is_main_process() else []
# Data loading code
train_source_dataset = get_detection_dataset_dicts(args.source)
train_source_loader = build_detection_train_loader(dataset=train_source_dataset, cfg=cfg)
# start training
logger.info("Starting training from iteration {}".format(start_iter))
with EventStorage(start_iter) as storage:
for data_s, iteration in zip(train_source_loader, range(start_iter, max_iter)):
storage.iter = iteration
# compute output
_, loss_dict_s = model(data_s)
losses_s = sum(loss_dict_s.values())
assert torch.isfinite(losses_s).all(), loss_dict_s
loss_dict_reduced_s = {"{}_s".format(k): v.item() for k, v in comm.reduce_dict(loss_dict_s).items()}
losses_reduced_s = sum(loss for loss in loss_dict_reduced_s.values())
if comm.is_main_process():
storage.put_scalars(total_loss_s=losses_reduced_s, **loss_dict_reduced_s)
# compute gradient and do SGD step
optimizer.zero_grad()
losses_s.backward()
optimizer.step()
storage.put_scalar("lr", optimizer.param_groups[0]["lr"], smoothing_hint=False)
scheduler.step()
# evaluate on validation set
if (
cfg.TEST.EVAL_PERIOD > 0
and (iteration + 1) % cfg.TEST.EVAL_PERIOD == 0
and iteration != max_iter - 1
):
utils.validate(model, logger, cfg, args)
comm.synchronize()
if iteration - start_iter > 5 and (
(iteration + 1) % 20 == 0 or iteration == max_iter - 1
):
for writer in writers:
writer.write()
periodic_checkpointer.step(iteration)
def main(args):
logger = logging.getLogger("detectron2")
cfg = utils.setup(args)
# dataset
args.source = utils.build_dataset(args.source[::2], args.source[1::2])
args.target = utils.build_dataset(args.target[::2], args.target[1::2])
args.test = utils.build_dataset(args.test[::2], args.test[1::2])
# create model
model = models.__dict__[cfg.MODEL.META_ARCHITECTURE](cfg, finetune=args.finetune)
model.to(torch.device(cfg.MODEL.DEVICE))
logger.info("Model:\n{}".format(model))
if args.eval_only:
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
return utils.validate(model, logger, cfg, args)
distributed = comm.get_world_size() > 1
if distributed:
model = DistributedDataParallel(
model, device_ids=[comm.get_local_rank()], broadcast_buffers=False
)
train(model, logger, cfg, args)
# evaluate on validation set
return utils.validate(model, logger, cfg, args)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# dataset parameters
parser.add_argument('-s', '--source', nargs='+', help='source domain(s)')
parser.add_argument('-t', '--target', nargs='+', help='target domain(s)')
parser.add_argument('--test', nargs='+', help='test domain(s)')
# model parameters
parser.add_argument('--finetune', action='store_true', help='whether use 10x smaller learning rate for backbone')
parser.add_argument(
"--resume",
action="store_true",
help="Whether to attempt to resume from the checkpoint directory. "
"See documentation of `DefaultTrainer.resume_or_load()` for what it means.",
)
# training parameters
parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file")
parser.add_argument("--eval-only", action="store_true", help="perform evaluation only")
parser.add_argument("--num-gpus", type=int, default=1, help="number of gpus *per machine*")
parser.add_argument("--num-machines", type=int, default=1, help="total number of machines")
parser.add_argument("--machine-rank", type=int, default=0, help="the rank of this machine (unique per machine)")
# PyTorch still may leave orphan processes in multi-gpu training.
# Therefore we use a deterministic way to obtain port,
# so that users are aware of orphan processes by seeing the port occupied.
port = 2 ** 15 + 2 ** 14 + hash(os.getuid() if sys.platform != "win32" else 1) % 2 ** 14
parser.add_argument(
"--dist-url",
default="tcp://127.0.0.1:{}".format(port),
help="initialization URL for pytorch distributed backend. See "
"https://pytorch.org/docs/stable/distributed.html for details.",
)
parser.add_argument(
"opts",
help="Modify config options by adding 'KEY VALUE' pairs at the end of the command. "
"See config references at "
"https://detectron2.readthedocs.io/modules/config.html#config-references",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
print("Command Line Args:", args)
launch(
main,
args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)