forked from haitongli/knowledge-distillation-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesize_results.py
62 lines (45 loc) · 1.99 KB
/
synthesize_results.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
"""Aggregates results from the metrics_eval_best_weights.json in a parent folder"""
import argparse
import json
import os
from tabulate import tabulate
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', default='experiments',
help='Directory containing results of experiments')
def aggregate_metrics(parent_dir, metrics):
"""Aggregate the metrics of all experiments in folder `parent_dir`.
Assumes that `parent_dir` contains multiple experiments, with their results stored in
`parent_dir/subdir/metrics_dev.json`
Args:
parent_dir: (string) path to directory containing experiments results
metrics: (dict) subdir -> {'accuracy': ..., ...}
"""
# Get the metrics for the folder if it has results from an experiment
metrics_file = os.path.join(parent_dir, 'metrics_val_best_weights.json')
if os.path.isfile(metrics_file):
with open(metrics_file, 'r') as f:
metrics[parent_dir] = json.load(f)
# Check every subdirectory of parent_dir
for subdir in os.listdir(parent_dir):
if not os.path.isdir(os.path.join(parent_dir, subdir)):
continue
else:
aggregate_metrics(os.path.join(parent_dir, subdir), metrics)
def metrics_to_table(metrics):
# Get the headers from the first subdir. Assumes everything has the same metrics
headers = metrics[list(metrics.keys())[0]].keys()
table = [[subdir] + [values[h] for h in headers] for subdir, values in metrics.items()]
res = tabulate(table, headers, tablefmt='pipe')
return res
if __name__ == "__main__":
args = parser.parse_args()
# Aggregate metrics from args.parent_dir directory
metrics = dict()
aggregate_metrics(args.parent_dir, metrics)
table = metrics_to_table(metrics)
# Display the table to terminal
print(table)
# Save results in parent_dir/results.md
save_file = os.path.join(args.parent_dir, "results.md")
with open(save_file, 'w') as f:
f.write(table)