forked from mateoespinosa/cem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcub_emb_size_ablation.py
329 lines (308 loc) · 11 KB
/
cub_emb_size_ablation.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import argparse
import copy
import joblib
import numpy as np
import os
import torch
from cem.data.CUB200.cub_loader import load_data, find_class_imbalance
from pathlib import Path
from pytorch_lightning import seed_everything
import cem.data.CUB200.cub_loader as cub_data_module
import cem.train.training as training
import cem.train.utils as utils
def main(
rerun=False,
result_dir='results/cub_emb_size_ablation/',
project_name='',
activation_freq=0,
num_workers=8,
single_frequency_epochs=0,
global_params=None,
):
seed_everything(42)
# parameters for data, model, and training
og_config = dict(
cv=5,
max_epochs=300,
patience=15,
batch_size=128,
num_workers=num_workers,
emb_size=16,
extra_dims=0,
concept_loss_weight=5,
learning_rate=0.01,
weight_decay=4e-05,
scheduler_step=20,
weight_loss=True,
c_extractor_arch="resnet34",
optimizer="sgd",
bool=False,
early_stopping_monitor="val_loss",
early_stopping_mode="min",
early_stopping_delta=0.0,
# By default we start with 25% of the concepts in the bottleneck
sampling_percent=0.25,
momentum=0.9,
shared_prob_gen=False,
sigmoidal_prob=False,
sigmoidal_embedding=False,
training_intervention_prob=0.0,
embedding_activation=None,
concat_prob=False,
)
utils.extend_with_global_params(og_config, global_params or [])
train_dl, val_dl, test_dl, imbalance, (n_concepts, n_tasks, _) = \
cub_data_module.generate_data(
config=og_config,
seed=42,
output_dataset_vars=True,
)
if result_dir and activation_freq:
# Then let's save the testing data for furter analysis later on
out_acts_save_dir = os.path.join(result_dir, "test_embedding_acts")
Path(out_acts_save_dir).mkdir(parents=True, exist_ok=True)
for (ds, name) in [
(test_dl, "test"),
(val_dl, "val"),
]:
x_total = []
y_total = []
c_total = []
for x, y, c in ds:
x_total.append(x.cpu().detach())
y_total.append(y.cpu().detach())
c_total.append(c.cpu().detach())
x_inputs = np.concatenate(x_total, axis=0)
print(f"x_{name}.shape =", x_inputs.shape)
np.save(os.path.join(out_acts_save_dir, f"x_{name}.npy"), x_inputs)
y_inputs = np.concatenate(y_total, axis=0)
print(f"y_{name}.shape =", y_inputs.shape)
np.save(os.path.join(out_acts_save_dir, f"y_{name}.npy"), y_inputs)
c_inputs = np.concatenate(c_total, axis=0)
print(f"c_{name}.shape =", c_inputs.shape)
np.save(os.path.join(out_acts_save_dir, f"c_{name}.npy"), c_inputs)
sample = next(iter(train_dl))
n_concepts, n_tasks = sample[2].shape[-1], 200
print("Training sample shape is:", sample[0].shape)
print("Training label shape is:", sample[1].shape)
print("Training concept shape is:", sample[2].shape)
os.makedirs(result_dir, exist_ok=True)
results = {}
for split in range(og_config["cv"]):
for emb_size in [1, 2, 4, 6, 8, 16, 32, 64]:
if emb_size not in results:
results[emb_size] = {}
if f'{split}' not in results[emb_size]:
results[emb_size][f'{split}'] = {}
print(
f'Experiment {split+1}/{og_config["cv"]} with emb_size',
emb_size,
)
# Trial period for mixture embedding model
config = copy.deepcopy(og_config)
config["architecture"] = "MixtureEmbModel"
config["extra_name"] = (
f"SharedProb_AdaptiveDropout_NoProbConcat_emb_size_{emb_size}"
)
config["shared_prob_gen"] = True
config["sigmoidal_prob"] = False
config["sigmoidal_embedding"] = False
config['training_intervention_prob'] = 0.25
config['concat_prob'] = False
config['emb_size'] = emb_size
config["embedding_activation"] = "leakyrelu"
mixed_emb_shared_prob_model, mixed_emb_shared_prob_test_results = \
training.train_model(
n_concepts=n_concepts,
n_tasks=n_tasks,
config=config,
train_dl=train_dl,
val_dl=val_dl,
test_dl=test_dl,
split=split,
result_dir=result_dir,
rerun=rerun,
project_name=project_name,
seed=split,
activation_freq=activation_freq,
single_frequency_epochs=single_frequency_epochs,
imbalance=imbalance,
)
training.update_statistics(
results[emb_size][f'{split}'],
config,
mixed_emb_shared_prob_model,
mixed_emb_shared_prob_test_results,
)
# Train fuzzy CBM with extra capacity
config = copy.deepcopy(og_config)
config["architecture"] = "ConceptBottleneckModel"
config["bool"] = False
config["extra_dims"] = (emb_size - 1) * n_concepts
config["extra_name"] = (
f"FuzzyExtraCapacity_Logit_emb_size_{emb_size}"
)
config["bottleneck_nonlinear"] = "leakyrelu"
config["sigmoidal_extra_capacity"] = False
config["sigmoidal_prob"] = False
config['emb_size'] = emb_size
extra_fuzzy_logit_model, extra_fuzzy_logit_test_results = \
training.train_model(
n_concepts=n_concepts,
n_tasks=n_tasks,
config=config,
train_dl=train_dl,
val_dl=val_dl,
test_dl=test_dl,
split=split,
result_dir=result_dir,
rerun=rerun,
project_name=project_name,
seed=split,
activation_freq=activation_freq,
single_frequency_epochs=single_frequency_epochs,
imbalance=imbalance,
)
training.update_statistics(
results[emb_size][f'{split}'],
config,
extra_fuzzy_logit_model,
extra_fuzzy_logit_test_results,
)
# train vanilla model with more capacity (i.e., no concept
# supervision) but with ReLU activation
config = copy.deepcopy(og_config)
config["architecture"] = "ConceptBottleneckModel"
config["extra_name"] = (
f"NoConceptSupervisionReLU_ExtraCapacity_emb_size_{emb_size}"
)
config["bool"] = False
config["extra_dims"] = (emb_size - 1) * n_concepts
config["bottleneck_nonlinear"] = "leakyrelu"
config["concept_loss_weight"] = 0
config['emb_size'] = emb_size
extra_vanilla_relu_model, extra_vanilla_relu_test_results = \
training.train_model(
n_concepts=n_concepts,
n_tasks=n_tasks,
config=config,
train_dl=train_dl,
val_dl=val_dl,
test_dl=test_dl,
split=split,
result_dir=result_dir,
rerun=rerun,
project_name=project_name,
seed=split,
activation_freq=activation_freq,
single_frequency_epochs=single_frequency_epochs,
imbalance=imbalance,
)
training.update_statistics(
results[emb_size][f'{split}'],
config,
extra_vanilla_relu_model,
extra_vanilla_relu_test_results,
)
# save results
joblib.dump(results, os.path.join(result_dir, f'results.joblib'))
return results
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=(
'Runs concept embedding experiment in CUB dataset.'
),
)
parser.add_argument(
'--project_name',
default='',
help=(
"Project name used for Weights & Biases monitoring. If not "
"provided, then we will assume no W&B is used for logging."
),
metavar="name",
)
parser.add_argument(
'--output_dir',
'-o',
default='results/cub_emb_size_ablation/',
help=(
"directory where we will dump our experiment's results. If not "
"given, then we will use ./results/cub_emb_size_ablation/."
),
metavar="path",
)
parser.add_argument(
'--rerun',
'-r',
default=False,
action="store_true",
help=(
"If set, then we will force a rerun of the entire experiment "
"even if valid results are found in the provided output "
"directory. Note that this may overwrite and previous results, "
"so use with care."
),
)
parser.add_argument(
'--activation_freq',
default=0,
help=(
'how frequently, in terms of epochs, should we store the '
'embedding activations for our validation set. By default we will '
'not store any activations.'
),
metavar='N',
type=int,
)
parser.add_argument(
'--single_frequency_epochs',
default=0,
help=(
'how frequently, in terms of epochs, should we store the '
'embedding activations for our validation set. By default we will '
'not store any activations.'
),
metavar='N',
type=int,
)
parser.add_argument(
'--num_workers',
default=8,
help=(
'number of workers used for data feeders. Do not use more workers '
'than cores in the machine.'
),
metavar='N',
type=int,
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
default=False,
help="starts debug mode in our program.",
)
parser.add_argument(
'-p',
'--param',
action='append',
nargs=2,
metavar=('param_name=value'),
help=(
'Allows the passing of a config param that will overwrite '
'anything passed as part of the config file itself.'
),
default=[],
)
args = parser.parse_args()
main(
rerun=args.rerun,
result_dir=args.output_dir,
project_name=args.project_name,
activation_freq=args.activation_freq,
num_workers=args.num_workers,
single_frequency_epochs=args.single_frequency_epochs,
global_params=args.param,
)
# hyperparameter_sweep()