-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfsod_train_net.py
executable file
·212 lines (181 loc) · 7.25 KB
/
fsod_train_net.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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Created on Thursday, April 14, 2022
This script is a simplified version of the training script in detectron2/tools.
@author: Guangxing Han
"""
import os
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2.engine import DefaultTrainer, default_argument_parser, default_setup, launch
from detectron2.data import build_batch_data_loader
from detectron2.evaluation import (
DatasetEvaluator,
inference_on_dataset,
print_csv_format,
verify_results,
)
from QA_FewDet.config import get_cfg
from QA_FewDet.data import DatasetMapperWithSupportCOCO, DatasetMapperWithSupportVOC
from QA_FewDet.data.build import build_detection_train_loader, build_detection_test_loader
from QA_FewDet.solver import build_optimizer
from QA_FewDet.evaluation import COCOEvaluator, PascalVOCDetectionEvaluator
import bisect
import copy
import itertools
import logging
import numpy as np
import operator
import pickle
import torch.utils.data
from collections import OrderedDict
import detectron2.utils.comm as comm
from detectron2.utils.logger import setup_logger
class Trainer(DefaultTrainer):
@classmethod
def build_train_loader(cls, cfg):
"""
Returns:
iterable
It calls :func:`detectron2.data.build_detection_train_loader` with a customized
DatasetMapper, which adds categorical labels as a semantic mask.
"""
if 'coco' in cfg.DATASETS.TRAIN[0]:
mapper = DatasetMapperWithSupportCOCO(cfg)
else:
mapper = DatasetMapperWithSupportVOC(cfg)
return build_detection_train_loader(cfg, mapper)
@classmethod
def build_test_loader(cls, cfg, dataset_name):
"""
Returns:
iterable
It now calls :func:`detectron2.data.build_detection_test_loader`.
Overwrite it if you'd like a different data loader.
"""
return build_detection_test_loader(cfg, dataset_name)
@classmethod
def build_optimizer(cls, cfg, model):
"""
Returns:
torch.optim.Optimizer:
It now calls :func:`detectron2.solver.build_optimizer`.
Overwrite it if you'd like a different optimizer.
"""
return build_optimizer(cfg, model)
@classmethod
def build_evaluator(cls, cfg, dataset_name, output_folder=None):
if output_folder is None:
output_folder = os.path.join(cfg.OUTPUT_DIR, "inference")
if 'coco' in dataset_name:
return COCOEvaluator(dataset_name, cfg, True, output_folder)
else:
return PascalVOCDetectionEvaluator(dataset_name)
@classmethod
def test(cls, cfg, model, evaluators=None):
"""
Args:
cfg (CfgNode):
model (nn.Module):
evaluators (list[DatasetEvaluator] or None): if None, will call
:meth:`build_evaluator`. Otherwise, must have the same length as
`cfg.DATASETS.TEST`.
Returns:
dict: a dict of result metrics
"""
logger = logging.getLogger(__name__)
if isinstance(evaluators, DatasetEvaluator):
evaluators = [evaluators]
if evaluators is not None:
assert len(cfg.DATASETS.TEST) == len(evaluators), "{} != {}".format(
len(cfg.DATASETS.TEST), len(evaluators)
)
results = OrderedDict()
for idx, dataset_name in enumerate(cfg.DATASETS.TEST):
data_loader = cls.build_test_loader(cfg, dataset_name)
# When evaluators are passed in as arguments,
# implicitly assume that evaluators can be created before data_loader.
if evaluators is not None:
evaluator = evaluators[idx]
else:
try:
evaluator = cls.build_evaluator(cfg, dataset_name)
except NotImplementedError:
logger.warn(
"No evaluator found. Use `DefaultTrainer.test(evaluators=)`, "
"or implement its `build_evaluator` method."
)
results[dataset_name] = {}
continue
test_seeds = cfg.DATASETS.SEEDS
test_shots = cfg.DATASETS.TEST_SHOTS
cur_test_shots_set = set(test_shots)
if 'coco' in cfg.DATASETS.TRAIN[0]:
evaluation_dataset = 'coco'
coco_test_shots_set = set([1,2,3,5,10,30])
test_shots_join = cur_test_shots_set.intersection(coco_test_shots_set)
test_keepclasses = cfg.DATASETS.TEST_KEEPCLASSES
else:
evaluation_dataset = 'voc'
voc_test_shots_set = set([1,2,3,5,10])
test_shots_join = cur_test_shots_set.intersection(voc_test_shots_set)
test_keepclasses = cfg.DATASETS.TEST_KEEPCLASSES
if cfg.INPUT.FS.FEW_SHOT:
test_shots = [cfg.INPUT.FS.SUPPORT_SHOT]
test_shots_join = set(test_shots)
print("================== test_shots_join=", test_shots_join)
for shot in test_shots_join:
print("evaluating {}.{} for {} shot".format(evaluation_dataset, test_keepclasses, shot))
if isinstance(model, torch.nn.parallel.DistributedDataParallel):
model.module.init_support_features(evaluation_dataset, shot, test_keepclasses, test_seeds)
else:
model.init_support_features(evaluation_dataset, shot, test_keepclasses, test_seeds)
results_i = inference_on_dataset(model, data_loader, evaluator)
results[dataset_name] = results_i
if comm.is_main_process():
assert isinstance(
results_i, dict
), "Evaluator must return a dict on the main process. Got {} instead.".format(
results_i
)
logger.info("Evaluation results for {} in csv format:".format(dataset_name))
print_csv_format(results_i)
if len(results) == 1:
results = list(results.values())[0]
return results
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
default_setup(cfg, args)
rank = comm.get_rank()
setup_logger(cfg.OUTPUT_DIR, distributed_rank=rank, name="meta_faster_rcnn")
return cfg
def main(args):
cfg = setup(args)
if args.eval_only:
model = Trainer.build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
res = Trainer.test(cfg, model)
return res
trainer = Trainer(cfg)
trainer.resume_or_load(resume=args.resume)
return trainer.train()
if __name__ == "__main__":
args = default_argument_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,),
)