-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
192 lines (167 loc) · 4.34 KB
/
config.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
import ast
_VALID_DATASETS = {"fashion-mnist", "bios", "ravdess", "facet"}
_LOADER_KW = {
"train_batch_size",
"valid_batch_size",
"test_batch_size",
"calib_batch_size",
"calib_val_batch_size",
"n_calib",
"n_test",
"n_calib_val", # HPO for conformal
"n_train",
"n_val",
"m",
"model_checkpoint",
"save_model_ckpt",
}
_MODEL_KW = {
"optimizer",
"lr",
"epochs",
"model_size",
"model_checkpoint",
"save_model_ckpt",
}
def get_config(dataset):
dataset = dataset.lower()
assert dataset in _VALID_DATASETS, f"Unknown dataset {dataset}"
base_config = CFG_MAP["base"]()
dataset_config = CFG_MAP[dataset]()
return {
"dataset": dataset,
**base_config,
**dataset_config, # dataset unpacked last, so overwrites base if there are duplicates
}
def get_base_config():
# Shared config applicable to all datasets
cfg = {
"seed": 0, # random seed for reproducibility
"alpha": 0.1, # conformal error tolerance rate
"test_batch_size": 256,
"calib_batch_size": 256,
"m": 10,
"data_root": "data/",
"logdir_root": "logs/",
"k": 3,
"score_fn": "raps", # hinge, raps, saps
"hpo_iterations": 50,
"verbose": False,
"model_checkpoint": None,
"save_model_ckpt": False,
}
return cfg
def get_fmnist_config():
# Add dataset-specific config parameters as required
cfg = {
"train_batch_size": 256,
"valid_batch_size": 256,
"n_calib": 2000,
"n_test": 8000,
"optimizer": "adam",
"lr": 0.001,
"epochs": 2,
"h_params_raps": {
"T": 1.0,
"kreg": 3,
"lamda": 0.5,
},
"h_params_saps": {
"T": 1.0,
"lamda": 0.5,
},
"h_params_hinge": {"T": 1.0},
}
return cfg
def get_ravdess_config():
# Add dataset-specific config parameters as required
cfg = {
"alpha": 0.1,
"n_calib": 0.5833333,
"n_calib_val": 0.16666666,
"n_test": 0.25,
"test_batch_size": 64,
"calib_val_batch_size": 64,
"calib_batch_size": 64,
"m": 8,
"data_root": "data/RAVDESS/",
"h_params_raps": {
"T": 0.5,
"kreg": 2,
"lamda": 2.0,
},
"h_params_saps": {
"T": 1.0,
"lamda": 0.5,
},
"h_params_hinge": {"T": 1.0},
}
return cfg
def get_biosbias_config():
# Add dataset-specific config parameters as required
cfg = {
"train_batch_size": 512,
"test_batch_size": 512,
"calib_batch_size": 512,
"n_calib": 10000,
"n_test": 2000,
"n_calib_val": 5000,
"n_train": 50000,
"n_val": 5000,
"data_root": "data/BiosBias/",
"optimizer": "adam",
"score_fn": "saps",
"lr": 0.005,
"epochs": 15,
"h_params_raps": {
"T": 0.2,
"kreg": 3,
"lamda": 0.5,
},
"h_params_saps": {
"T": 1.0,
"lamda": 0.5,
},
"h_params_hinge": {"T": 1.0},
}
return cfg
def get_facet_config():
# Add dataset-specific config parameters as required
cfg = {
"n_calib": 4000,
"n_calib_val": -1, # use half of remaining examples for HPO
"n_test": -1, # use other half of remaining examples for test
"m": 20,
"data_root": "data/facet/",
"model_size": "ViT-L/14",
"h_params_raps": {
"T": 1.0,
"kreg": 3,
"lamda": 0.5,
},
"h_params_saps": {
"T": 1.0,
"lamda": 0.5,
},
"h_params_hinge": {
"T": 1.0
}
}
return cfg
CFG_MAP = {
"base": get_base_config,
"fashion-mnist": get_fmnist_config,
"ravdess": get_ravdess_config,
"bios": get_biosbias_config,
"facet": get_facet_config,
}
def parse_config_arg(key_value):
assert "=" in key_value, "Must specify config items with format `key=value`"
k, v = key_value.split("=", maxsplit=1)
assert k, "Config item can't have empty key"
assert v, "Config item can't have empty value"
try:
v = ast.literal_eval(v)
except ValueError:
v = str(v)
return k, v