-
Notifications
You must be signed in to change notification settings - Fork 288
/
list_top_hparams.py
152 lines (129 loc) · 5.31 KB
/
list_top_hparams.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Example usage:
python -u -m domainbed.scripts.list_top_hparams \
--input_dir domainbed/misc/test_sweep_data --algorithm ERM \
--dataset VLCS --test_env 0
"""
import collections
import argparse
import functools
import glob
import pickle
import itertools
import json
import os
import random
import sys
import numpy as np
import tqdm
from domainbed import datasets
from domainbed import algorithms
from domainbed.lib import misc, reporting
from domainbed import model_selection
from domainbed.lib.query import Q
import warnings
def todo_rename(records, selection_method, latex):
grouped_records = reporting.get_grouped_records(records).map(lambda group:
{ **group, "sweep_acc": selection_method.sweep_acc(group["records"]) }
).filter(lambda g: g["sweep_acc"] is not None)
# read algorithm names and sort (predefined order)
alg_names = Q(records).select("args.algorithm").unique()
alg_names = ([n for n in algorithms.ALGORITHMS if n in alg_names] +
[n for n in alg_names if n not in algorithms.ALGORITHMS])
# read dataset names and sort (lexicographic order)
dataset_names = Q(records).select("args.dataset").unique().sorted()
dataset_names = [d for d in datasets.DATASETS if d in dataset_names]
for dataset in dataset_names:
if latex:
print()
print("\\subsubsection{{{}}}".format(dataset))
test_envs = range(datasets.num_environments(dataset))
table = [[None for _ in [*test_envs, "Avg"]] for _ in alg_names]
for i, algorithm in enumerate(alg_names):
means = []
for j, test_env in enumerate(test_envs):
trial_accs = (grouped_records
.filter_equals(
"dataset, algorithm, test_env",
(dataset, algorithm, test_env)
).select("sweep_acc"))
mean, err, table[i][j] = format_mean(trial_accs, latex)
means.append(mean)
if None in means:
table[i][-1] = "X"
else:
table[i][-1] = "{:.1f}".format(sum(means) / len(means))
col_labels = [
"Algorithm",
*datasets.get_dataset_class(dataset).ENVIRONMENTS,
"Avg"
]
header_text = (f"Dataset: {dataset}, "
f"model selection method: {selection_method.name}")
print_table(table, header_text, alg_names, list(col_labels),
colwidth=20, latex=latex)
# Print an "averages" table
if latex:
print()
print("\\subsubsection{Averages}")
table = [[None for _ in [*dataset_names, "Avg"]] for _ in alg_names]
for i, algorithm in enumerate(alg_names):
means = []
for j, dataset in enumerate(dataset_names):
trial_averages = (grouped_records
.filter_equals("algorithm, dataset", (algorithm, dataset))
.group("trial_seed")
.map(lambda trial_seed, group:
group.select("sweep_acc").mean()
)
)
mean, err, table[i][j] = format_mean(trial_averages, latex)
means.append(mean)
if None in means:
table[i][-1] = "X"
else:
table[i][-1] = "{:.1f}".format(sum(means) / len(means))
col_labels = ["Algorithm", *dataset_names, "Avg"]
header_text = f"Averages, model selection method: {selection_method.name}"
print_table(table, header_text, alg_names, col_labels, colwidth=25,
latex=latex)
if __name__ == "__main__":
np.set_printoptions(suppress=True)
parser = argparse.ArgumentParser(
description="Domain generalization testbed")
parser.add_argument("--input_dir", required=True)
parser.add_argument('--dataset', required=True)
parser.add_argument('--algorithm', required=True)
parser.add_argument('--test_env', type=int, required=True)
args = parser.parse_args()
records = reporting.load_records(args.input_dir)
print("Total records:", len(records))
records = reporting.get_grouped_records(records)
records = records.filter(
lambda r:
r['dataset'] == args.dataset and
r['algorithm'] == args.algorithm and
r['test_env'] == args.test_env
)
SELECTION_METHODS = [
model_selection.IIDAccuracySelectionMethod,
model_selection.LeaveOneOutSelectionMethod,
model_selection.OracleSelectionMethod,
]
for selection_method in SELECTION_METHODS:
print(f'Model selection: {selection_method.name}')
for group in records:
print(f"trial_seed: {group['trial_seed']}")
best_hparams = selection_method.hparams_accs(group['records'])
for run_acc, hparam_records in best_hparams:
print(f"\t{run_acc}")
for r in hparam_records:
assert(r['hparams'] == hparam_records[0]['hparams'])
print("\t\thparams:")
for k, v in sorted(hparam_records[0]['hparams'].items()):
print('\t\t\t{}: {}'.format(k, v))
print("\t\toutput_dirs:")
output_dirs = hparam_records.select('args.output_dir').unique()
for output_dir in output_dirs:
print(f"\t\t\t{output_dir}")