-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
315 lines (259 loc) · 9.94 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
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
import sys
sys.path.append("ext_deps/Hierarchical-Localization")
sys.path.append("ext_deps/dioad")
import argparse
import datetime
import json
import logging
import os
import pickle
import shutil
import time
from pathlib import Path
import numpy as np
from hloc import extract_features
from imc2023.configs import configs
from imc2023.utils.eval import eval
from imc2023.utils.utils import (
DataPaths,
create_submission,
get_data_from_dict,
get_data_from_dir,
setup_logger,
)
## Kaggle version
# import sys
# sys.path.append("/kaggle/input/imc-23-repo/IMC-2023")
# sys.path.append("/kaggle/input/imc-23-repo/IMC-2023/ext_deps/Hierarchical-Localization")
# sys.path.append("/kaggle/input/imc-23-repo/IMC-2023/ext_deps/dioad")
# import os
# import argparse
# from main import main
# args = {
# "data": "/kaggle/input/image-matching-challenge-2023",
# "configs": ["DISK+LG"],
# "retrieval": "netvlad",
# "n_retrieval": 50,
# "mode": "train",
# "output": "/kaggle/temp",
# "pixsfm": True,
# "pixsfm_max_imgs": 9999,
# "pixsfm_config": "low_memory",
# "pixsfm_script_path": "/kaggle/input/imc-23-repo/IMC-2023/run_pixsfm.py",
# "rotation_matching": True,
# "rotation_wrapper": False,
# "cropping": False,
# "max_rel_crop_size": 0.75,
# "min_rel_crop_size": 0.2,
# "resize": None,
# "shared_camera": True,
# "overwrite": True,
# "kaggle": True,
# "localize_unregistered" = False,
# "skip_scenes": None,
# }
# args = argparse.Namespace(**args)
# os.makedirs(args.output, exist_ok=True)
# main(args)
def get_output_dir(args: argparse.Namespace) -> Path:
"""Get the output directory.
Args:
args (argparse.Namespace): Arguments.
Returns:
Path: Output directory.
"""
name = "+".join(sorted(list(args.configs)))
output_dir = f"{args.output}/{name}"
if args.rotation_matching:
output_dir += "-rot"
if args.rotation_wrapper:
output_dir += "-rotwrap"
if args.pixsfm:
output_dir += "-pixsfm"
if args.pixsfm_force:
output_dir += "-pixsfm-force"
if args.cropping:
output_dir += "-crop"
if args.resize is not None:
output_dir += f"-{args.resize}px"
if args.shared_camera:
output_dir += "-sci"
if args.localize_unregistered:
output_dir += "-loc"
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
return output_dir
def log_args(args: argparse.Namespace) -> None:
"""Log arguments.
Args:
args (argparse.Namespace): Arguments.
"""
logging.info("=" * 80)
logging.info("Arguments:")
logging.info("=" * 80)
for arg in vars(args):
logging.info(f" {arg}: {getattr(args, arg)}")
def main(args):
start = time.time()
setup_logger()
log_args(args)
# PATHS
data_dir = Path(args.data)
output_dir = get_output_dir(args)
metrics_path = output_dir / "metrics.pickle"
results_path = output_dir / "results.pickle"
submission_csv_path = Path(f"{output_dir}/submission.csv")
# CONFIG
assert len(args.configs) > 0, "No configs specified"
confs = [configs[c] for c in args.configs]
config = {
"features": [c["features"] for c in confs],
"matches": [c["matches"] for c in confs],
"retrieval": extract_features.confs[args.retrieval],
"n_retrieval": args.n_retrieval,
}
with open(str(output_dir / "config.json"), "w") as jf:
json.dump(config, jf, indent=4)
# SETUP DATA DICT
data_dict = (
get_data_from_dict(data_dir)
if args.mode == "test"
else get_data_from_dir(data_dir, args.mode)
)
# RUN
metrics = {}
out_results = {}
if config["features"][0] is not None:
from imc2023.pipelines.sparse_pipeline import SparsePipeline as Pipeline
else:
from imc2023.pipelines.dense_pipeline import DensePipeline as Pipeline
for dataset in data_dict:
logging.info("=" * 80)
logging.info(dataset)
logging.info("=" * 80)
if dataset not in metrics:
metrics[dataset] = {}
if dataset not in out_results:
out_results[dataset] = {}
for scene in data_dict[dataset]:
logging.info("=" * 80)
logging.info(f"{dataset} - {scene}")
logging.info("=" * 80)
if args.skip_scenes is not None and scene in args.skip_scenes and args.mode == "train":
logging.info(f"Skipping {dataset} - {scene}")
continue
start_scene = time.time()
# SETUP PATHS
paths = DataPaths(
data_dir=data_dir,
output_dir=output_dir,
dataset=dataset,
scene=scene,
mode=args.mode,
)
img_list = [Path(p).name for p in data_dict[dataset][scene]]
out_results[dataset][scene] = {}
if not paths.image_dir.exists():
logging.info(f"Skipping {dataset} - {scene} (no images)")
continue
# Define and run pipeline
pipeline = Pipeline(config=config, paths=paths, img_list=img_list, args=args)
pipeline.run()
sparse_model = pipeline.sparse_model
if sparse_model is None:
continue
logging.info(f"Extracting results for {dataset} - {scene}")
for _, im in sparse_model.images.items():
img_name = os.path.join(dataset, scene, "images", im.name)
# problem: tvec is a reference! --> force copy
out_results[dataset][scene][img_name] = {"R": im.rotmat(), "t": np.array(im.tvec)}
metrics[dataset][scene] = {
"n_images": len(img_list),
"n_reg_images": sparse_model.num_reg_images(),
}
total_time = time.time() - start_scene
logging.info("Timings:")
for k, v in pipeline.timing.items():
logging.info(f" {k}: {datetime.timedelta(seconds=v)} ({v / total_time:.2%})")
logging.info(f" Total: {datetime.timedelta(seconds=total_time)}")
timings_path = paths.scene_dir / "timings.json"
with open(timings_path, "w") as jf:
json.dump(pipeline.timing, jf, indent=4)
with open(metrics_path, "wb") as handle:
pickle.dump(metrics, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open(results_path, "wb") as handle:
pickle.dump(out_results, handle, protocol=pickle.HIGHEST_PROTOCOL)
# delete scene dir
if args.mode == "test":
shutil.rmtree(paths.scene_dir)
create_submission(out_results, data_dict, submission_csv_path)
create_submission(out_results, data_dict, "submission.csv")
if args.mode == "train":
# # WRITE SUBMISSION
with open(metrics_path, "rb") as handle:
metrics = pickle.load(handle)
with open(results_path, "rb") as handle:
out_results = pickle.load(handle)
for dataset in metrics:
logging.info(dataset)
for scene in metrics[dataset]:
logging.info(
f"\t{scene}: {metrics[dataset][scene]['n_reg_images']} / {metrics[dataset][scene]['n_images']}"
)
# EVALUATE
eval(submission_csv="submission.csv", data_dir=data_dir)
logging.info(f"Done in {datetime.timedelta(seconds=time.time() - start)}")
if __name__ == "__main__":
# PARSE ARGS
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=str, required=True, help="imc dataset")
parser.add_argument(
"--configs", type=str, required=True, nargs="+", choices=configs.keys(), help="configs"
)
parser.add_argument("--retrieval", type=str, default="netvlad", choices=["netvlad", "cosplace"])
parser.add_argument("--n_retrieval", type=int, default=50, help="number of retrieval images")
parser.add_argument(
"--mode", type=str, required=True, choices=["train", "test"], help="train or test"
)
parser.add_argument("--output", type=str, default="outputs", help="output dir")
parser.add_argument("--pixsfm", action="store_true", help="use pixsfm")
parser.add_argument("--pixsfm_force", action="store_true", help="force PixSfM")
parser.add_argument(
"--pixsfm_max_imgs", type=int, default=9999, help="max number of images for PixSfM"
)
parser.add_argument(
"--pixsfm_low_mem_threshold", type=int, default=5, help="low mem threshold for PixSfM"
)
parser.add_argument("--pixsfm_config", type=str, default="low_memory", help="PixSfM config")
parser.add_argument(
"--pixsfm_script_path", type=str, default="run_pixsfm.py", help="PixSfM script path"
)
parser.add_argument("--rotation_matching", action="store_true", help="use rotation matching")
parser.add_argument(
"--rotation_wrapper",
action="store_true",
help="wrapper implementation of rotation matching",
)
parser.add_argument("--cropping", action="store_true", help="use image cropping")
parser.add_argument(
"--max_rel_crop_size",
type=float,
default=0.75,
help="crops must have a smaller relative size",
)
parser.add_argument(
"--min_rel_crop_size",
type=float,
default=0.2,
help="crops must have a larger relative size",
)
parser.add_argument("--resize", type=int, help="resize images")
parser.add_argument("--shared_camera", action="store_true", help="use shared camera intrinsics")
parser.add_argument(
"--localize_unregistered", action="store_true", help="localize unregistered"
)
parser.add_argument("--overwrite", action="store_true", help="overwrite existing results")
parser.add_argument("--kaggle", action="store_true", help="kaggle mode")
parser.add_argument("--skip_scenes", nargs="+", help="scenes to skip")
args = parser.parse_args()
main(args)