|
| 1 | +"""Create a side-by-side table comparing the results of PTLFlow with those reported in the original papers. |
| 2 | +
|
| 3 | +This script only evaluates results of models that provide the "things" pretrained models. |
| 4 | +
|
| 5 | +Tha parsing of this script is tightly connected to how the results are output by validate.py. |
| 6 | +""" |
| 7 | + |
| 8 | +# ============================================================================= |
| 9 | +# Copyright 2024 Henrique Morimitsu |
| 10 | +# |
| 11 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | +# you may not use this file except in compliance with the License. |
| 13 | +# You may obtain a copy of the License at |
| 14 | +# |
| 15 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +# |
| 17 | +# Unless required by applicable law or agreed to in writing, software |
| 18 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | +# See the License for the specific language governing permissions and |
| 21 | +# limitations under the License. |
| 22 | +# ============================================================================= |
| 23 | + |
| 24 | +import argparse |
| 25 | +import math |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +from loguru import logger |
| 29 | +import pandas as pd |
| 30 | + |
| 31 | +PAPER_VAL_COLS = { |
| 32 | + "model": ("Model", "model"), |
| 33 | + "sclean": ("S.clean", "sintel-clean-val/epe"), |
| 34 | + "sfinal": ("S.final", "sintel-final-val/epe"), |
| 35 | + "k15epe": ("K15-epe", "kitti-2015-val/epe"), |
| 36 | + "k15fl": ("K15-fl", "kitti-2015-val/flall"), |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def _init_parser() -> argparse.ArgumentParser: |
| 41 | + parser = argparse.ArgumentParser() |
| 42 | + parser.add_argument( |
| 43 | + "--paper_results_path", |
| 44 | + type=str, |
| 45 | + default=str(Path("docs/source/results/paper_results_things.csv")), |
| 46 | + help=("Path to the csv file containing the results from the papers."), |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--validate_results_path", |
| 50 | + type=str, |
| 51 | + default=str(Path("docs/source/results/metrics_all_things.csv")), |
| 52 | + help=( |
| 53 | + "Path to the csv file containing the results obtained by the validate script." |
| 54 | + ), |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--output_dir", |
| 58 | + type=str, |
| 59 | + default=str(Path("outputs/metrics")), |
| 60 | + help=("Path to the directory where the outputs will be saved."), |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--add_delta", |
| 64 | + action="store_true", |
| 65 | + help=( |
| 66 | + "If set, adds one more column showing the difference between paper and validation results." |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + return parser |
| 71 | + |
| 72 | + |
| 73 | +def save_results(args: argparse.Namespace) -> None: |
| 74 | + paper_df = pd.read_csv(args.paper_results_path) |
| 75 | + val_df = pd.read_csv(args.validate_results_path) |
| 76 | + paper_df["model"] = paper_df[PAPER_VAL_COLS["model"][0]] |
| 77 | + val_df["model"] = val_df[PAPER_VAL_COLS["model"][1]] |
| 78 | + df = pd.merge(val_df, paper_df, "left", "model") |
| 79 | + |
| 80 | + compare_cols = ["ptlflow", "paper"] |
| 81 | + if args.add_delta: |
| 82 | + compare_cols.append("delta") |
| 83 | + |
| 84 | + out_dict = {"model": ["", ""]} |
| 85 | + for name in list(PAPER_VAL_COLS.keys())[1:]: |
| 86 | + for ic, col in enumerate(compare_cols): |
| 87 | + out_dict[f"{name}-{col}"] = [name if ic == 0 else "", col] |
| 88 | + |
| 89 | + for _, row in df.iterrows(): |
| 90 | + out_dict["model"].append(row["model"]) |
| 91 | + for key in list(PAPER_VAL_COLS.keys())[1:]: |
| 92 | + paper_col_name = PAPER_VAL_COLS[key][0] |
| 93 | + paper_res = float(row[paper_col_name]) |
| 94 | + val_col_name = PAPER_VAL_COLS[key][1] |
| 95 | + val_res = float(row[val_col_name]) |
| 96 | + res_list = [val_res, paper_res] |
| 97 | + |
| 98 | + if args.add_delta: |
| 99 | + delta = val_res - paper_res |
| 100 | + res_list.append(delta) |
| 101 | + |
| 102 | + for name, res in zip(compare_cols, res_list): |
| 103 | + out_dict[f"{key}-{name}"].append( |
| 104 | + "" if (math.isinf(res) or math.isnan(res)) else f"{res:.3f}" |
| 105 | + ) |
| 106 | + |
| 107 | + out_df = pd.DataFrame(out_dict) |
| 108 | + Path(args.output_dir).mkdir(parents=True, exist_ok=True) |
| 109 | + output_path = Path(args.output_dir) / "paper_ptlflow_metrics.csv" |
| 110 | + out_df.to_csv(output_path, index=False, header=False) |
| 111 | + logger.info("Results saved to: {}", output_path) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + parser = _init_parser() |
| 116 | + args = parser.parse_args() |
| 117 | + save_results(args) |
0 commit comments