-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
180 lines (160 loc) · 7.08 KB
/
main.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
# Copyright 2023 Huawei Technologies Co., Ltd
#
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import argparse
import json
import os
import random
import sys
import time
from pathlib import Path
import mindspore
import numpy as np
from mindspore import context
import util.misc as utils
from models.focus_detr.coco_eval import CocoEvaluator, post_process
from models.focus_detr.dataset import build_dataset
from models.focus_detr.focus_detr import build_focus_detr
from util.logger import setup_logger
from util.slconfig import DictAction, SLConfig
def get_args_parser():
parser = argparse.ArgumentParser("Set transformer detector", add_help=False)
parser.add_argument("--config_file", "-c", type=str, required=True)
parser.add_argument(
"--options",
nargs="+",
action=DictAction,
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file.",
)
#
# dataset parameters
parser.add_argument("--dataset_file", default="coco")
parser.add_argument("--coco_path", type=str, default="/comp_robot/cv_public_dataset/COCO2017/")
parser.add_argument("--coco_panoptic_path", type=str)
parser.add_argument("--remove_difficult", action="store_true")
parser.add_argument("--fix_size", action="store_true")
# training parameters
parser.add_argument("--output_dir", default="", help="path where to save, empty for no saving")
parser.add_argument("--note", default="", help="add some notes to the experiment")
parser.add_argument("--device", default="cuda", help="device to use for training / testing")
parser.add_argument("--seed", default=42, type=int)
parser.add_argument("--resume", default="", help="resume from checkpoint")
parser.add_argument("--pretrain_model_path", help="load from other checkpoint")
parser.add_argument("--finetune_ignore", type=str, nargs="+")
parser.add_argument("--start_epoch", default=0, type=int, metavar="N", help="start epoch")
parser.add_argument("--eval", action="store_true")
parser.add_argument("--num_workers", default=2, type=int)
parser.add_argument("--test", action="store_true")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--find_unused_params", action="store_true")
parser.add_argument("--save_results", action="store_true")
parser.add_argument("--save_log", action="store_true")
# distributed training parameters
parser.add_argument("--world_size", default=1, type=int, help="number of distributed processes")
parser.add_argument("--dist_url", default="env://", help="url used to set up distributed training")
parser.add_argument("--rank", default=0, type=int, help="number of distributed processes")
parser.add_argument("--local_rank", type=int, help="local rank for DistributedDataParallel")
parser.add_argument("--amp", action="store_true", help="Train with mixed precision")
return parser
class dataset_param(object):
def __init__(self):
# self.image_set="val"
self.eval = True
self.max_img_size = 1333
self.num_queries = 900
self.img_scales = [480, 512, 640, 800]
self.device_num = 1
self.num_classes = 91
self.rank = 0
self.num_workers = 1
self.batch_size = 1
self.coco_path = "coco2017/"
def main(args):
utils.init_distributed_mode(args)
print("Loading config file from {}".format(args.config_file))
time.sleep(args.rank * 0.02)
cfg = SLConfig.fromfile(args.config_file)
if args.options is not None:
cfg.merge_from_dict(args.options)
if args.rank == 0:
save_cfg_path = os.path.join(args.output_dir, "config_cfg.py")
cfg.dump(save_cfg_path)
save_json_path = os.path.join(args.output_dir, "config_args_raw.json")
with open(save_json_path, "w") as f:
json.dump(vars(args), f, indent=2)
cfg_dict = cfg._cfg_dict.to_dict()
args_vars = vars(args)
for k, v in cfg_dict.items():
if k not in args_vars:
setattr(args, k, v)
else:
raise ValueError("Key {} can used by args only".format(k))
# update some new args temporally
if not getattr(args, "use_ema", None):
args.use_ema = False
if not getattr(args, "debug", None):
args.debug = False
# setup logger
os.makedirs(args.output_dir, exist_ok=True)
logger = setup_logger(
output=os.path.join(args.output_dir, "info.txt"), distributed_rank=args.rank, color=False, name="detr"
)
logger.info("git:\n {}\n".format(utils.get_sha()))
logger.info("Command: " + " ".join(sys.argv))
if args.rank == 0:
save_json_path = os.path.join(args.output_dir, "config_args_all.json")
with open(save_json_path, "w") as f:
json.dump(vars(args), f, indent=2)
logger.info("Full config saved to {}".format(save_json_path))
logger.info("world size: {}".format(args.world_size))
logger.info("rank: {}".format(args.rank))
logger.info("local_rank: {}".format(args.local_rank))
logger.info("args: " + str(args) + "\n")
if args.frozen_weights is not None:
assert args.masks, "Frozen training is meant for segmentation only"
print(args)
seed = args.seed + utils.get_rank()
# torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
device_id = int(os.getenv("DEVICE_ID", "0"))
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU", device_id=device_id)
focus_detr = build_focus_detr(args)
param_dict = mindspore.load_checkpoint(args.resume)
param_not_load = mindspore.load_param_into_net(focus_detr, param_dict)
print(f"-----param_not_load:{param_not_load}")
focus_detr.set_train(False)
ds_param = dataset_param()
dataset, base_ds, length_dataset = build_dataset(ds_param)
log_freq = length_dataset // 5
data_loader = dataset.create_dict_iterator()
coco_evaluator = CocoEvaluator(base_ds, ["bbox"])
cnt = 0
test_cnt = 100
for i, sample in enumerate(data_loader):
images = sample["image"]
mask_ms = sample["mask"]
input_data = {"data": images, "mask": mask_ms}
outputs = focus_detr(input_data)
outputs = {"pred_logits": outputs["pred_logits"], "pred_boxes": outputs["pred_boxes"]}
##
orig_target_sizes = sample["orig_sizes"].asnumpy()
results = post_process(outputs, orig_target_sizes)
res = {img_id: output for img_id, output in zip(sample["img_id"].asnumpy(), results)}
coco_evaluator.update(res)
cnt += 1
############################################
if cnt % 10 == 0:
print(f"---process--img--nums:{cnt}")
if cnt > test_cnt:
break
coco_evaluator.synchronize_between_processes()
coco_evaluator.accumulate()
coco_evaluator.summarize()
if __name__ == "__main__":
parser = argparse.ArgumentParser("DETR training and evaluation script", parents=[get_args_parser()])
args = parser.parse_args()
if args.output_dir:
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)