Debian Codegraph @

benchmark/reconstruction/compare.py

raw
1 # Copyright (c), ETH Zurich and UNC Chapel Hill.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 #
14 # * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 # its contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 """Compare the reports of two evaluate.py runs (A vs B).
31
32 With one report per variant, this prints the result tables for A, B and A - B.
33
34 With several reports per variant -- one per random seed, produced by an
35 evaluate.py --seeds/--num_seeds run -- it instead prints mean +/- std over the
36 seeds. The std is the run-to-run spread; use enough seeds that it is small
37 relative to the A-B effect you care about. Seeds are shared between the
38 variants, so A - B is computed per seed before averaging (a paired difference).
39 Reports should be generated with --threads_per_scene 1 so that a fixed seed is
40 deterministic.
41
42 To produce such reports for two colmap binaries, run evaluate.py once per
43 binary with the same seeds and --run_name -- so that they share the scene
44 workspaces -- and a different --report_name, which becomes the report path
45 prefix passed here. Pass --overwrite_two_view_geometries to both, otherwise
46 they reuse the two-view geometries cached in the shared database and a change
47 to geometric verification has no effect on A - B.
48
49 Each variant is given either as a single report, or as a report path prefix,
50 in which case its <prefix>_s<seed>.pkl reports are discovered and matched by
51 seed against the other variant's. A prefix resolves to <prefix>.pkl when the
52 runs were made without seeds. Prefixed variants may live in different run
53 directories.
54
55 Examples:
56 # Two individual reports:
57 python compare.py --report_a_path runs/X/base.pkl \\
58 --report_b_path runs/X/msac.pkl --labels base msac
59
60 # Every seed shared by two multi-seed runs:
61 python compare.py --report_a_path_prefix runs/X/base \\
62 --report_b_path_prefix runs/Y/msac
63
64 # ... restricted to a subset of the seeds:
65 python compare.py --report_a_path_prefix runs/X/base \\
66 --report_b_path_prefix runs/Y/msac --seeds 0 1 2 7 9
67
68 The same prefix invocation also covers unseeded runs, where it resolves to
69 runs/X/base.pkl and runs/Y/msac.pkl.
70 """
71
72 import argparse
73 from pathlib import Path
74
75 from evaluation.utils import collect_reports, compare_reports, pair_reports
76
77
78 def parse_args() -> argparse.Namespace:
79 parser = argparse.ArgumentParser(
80 description=__doc__,
81 formatter_class=argparse.RawDescriptionHelpFormatter,
82 )
83 parser.add_argument(
84 "--report_a_path",
85 type=Path,
86 default=None,
87 metavar="PKL",
88 help="Variant A report.",
89 )
90 parser.add_argument(
91 "--report_b_path",
92 type=Path,
93 default=None,
94 metavar="PKL",
95 help="Variant B report, compared against --report_a_path.",
96 )
97 parser.add_argument(
98 "--report_a_path_prefix",
99 type=Path,
100 default=None,
101 metavar="PREFIX",
102 help="Variant A given as a report path without the _s<seed>.pkl "
103 "suffix, e.g. runs/X/base. Its seeds are discovered from the "
104 "filenames and matched against variant B's, which may live in a "
105 "different run directory.",
106 )
107 parser.add_argument(
108 "--report_b_path_prefix",
109 type=Path,
110 default=None,
111 metavar="PREFIX",
112 help="Variant B report path prefix, paired by seed with "
113 "--report_a_path_prefix.",
114 )
115 parser.add_argument(
116 "--labels",
117 nargs=2,
118 default=None,
119 metavar=("A", "B"),
120 help="Variant labels used in the table titles. Defaults to the "
121 "prefix names, or to A and B for explicit report paths.",
122 )
123 seed_group = parser.add_mutually_exclusive_group()
124 seed_group.add_argument(
125 "--seeds",
126 type=int,
127 nargs="+",
128 default=None,
129 help="Compare only these seeds of the prefixed reports, e.g. --seeds "
130 "0 1 2 7 9. Defaults to every seed the two variants share.",
131 )
132 seed_group.add_argument(
133 "--num_seeds",
134 type=int,
135 default=None,
136 help="Shorthand for --seeds 0 1 ... N-1.",
137 )
138 args = parser.parse_args()
139
140 # Each variant is given as a report or as a prefix, independently: a
141 # single report may be compared against every seed of a prefixed one.
142 for side in ("a", "b"):
143 path = getattr(args, f"report_{side}_path")
144 prefix = getattr(args, f"report_{side}_path_prefix")
145 if (path is None) == (prefix is None):
146 parser.error(
147 f"provide exactly one of --report_{side}_path and "
148 f"--report_{side}_path_prefix"
149 )
150 args.use_prefixes = (
151 args.report_a_path_prefix is not None
152 or args.report_b_path_prefix is not None
153 )
154 if args.num_seeds is not None:
155 if args.num_seeds <= 0:
156 parser.error("--num_seeds must be > 0")
157 args.seeds = list(range(args.num_seeds))
158 if args.seeds is not None and not args.use_prefixes:
159 parser.error(
160 "--seeds/--num_seeds select among prefixed reports; they do not "
161 "apply to two individual reports"
162 )
163 return args
164
165
166 def main() -> None:
167 args = parse_args()
168
169 labels = args.labels or [
170 (path or prefix).name.removesuffix(".pkl")
171 for path, prefix in (
172 (args.report_a_path, args.report_a_path_prefix),
173 (args.report_b_path, args.report_b_path_prefix),
174 )
175 ]
176 reports_a = collect_reports(args.report_a_path, args.report_a_path_prefix)
177 reports_b = collect_reports(args.report_b_path, args.report_b_path_prefix)
178 report_a_paths, report_b_paths, seeds = pair_reports(
179 reports_a, reports_b, labels, args.seeds
180 )
181
182 compare_reports(report_a_paths, report_b_paths, labels, seeds)
183
184
185 if __name__ == "__main__":
186 main()