-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_probing.py
413 lines (384 loc) · 13.1 KB
/
main_probing.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import argparse
import os
import pickle
from typing import Any, Callable, Dict, Iterator, List, Tuple
import numpy as np
import pandas as pd
import torch
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from sklearn.model_selection import KFold
from torch.utils.data import DataLoader
from tqdm import tqdm
import utils
Array = np.ndarray
Tensor = torch.Tensor
FrozenDict = Any
def parseargs():
parser = argparse.ArgumentParser()
def aa(*args, **kwargs):
parser.add_argument(*args, **kwargs)
aa("--data_root", type=str, help="path/to/things")
aa("--dataset", type=str, help="Which dataset to use", default="things")
aa("--model", type=str)
aa(
"--model_dict_path",
type=str,
default="./datasets/things/model_dict.json",
help="Path to the model_dict.json",
)
aa(
"--module",
type=str,
default="penultimate",
help="neural network module for which to learn a linear transform",
choices=["penultimate", "logits"],
)
aa(
"--source",
type=str,
default="torchvision",
choices=[
"google",
"loss",
"custom",
"ssl",
"imagenet",
"torchvision",
"vit_same",
"vit_best",
],
)
aa(
"--n_objects",
type=int,
help="Number of object categories in the data",
default=1854,
)
aa(
"--n_folds",
type=int,
default=3,
choices=[2, 3, 4, 5],
help="Number of folds in k-fold cross-validation.",
)
aa("--optim", type=str, default="Adam", choices=["Adam", "AdamW", "SGD"])
aa("--learning_rate", type=float, default=1e-3)
aa(
"--lmbda",
type=float,
default=1e-3,
help="Relative contribution of the regularization term",
choices=[1.0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5],
)
aa(
"--batch_size",
type=int,
default=256,
help="Use power of 2 for running optimization on GPU",
choices=[64, 128, 256, 512, 1024],
)
aa(
"--epochs",
type=int,
help="Maximum number of epochs to perform finetuning",
default=100,
)
aa(
"--burnin",
type=int,
help="Minimum number of epochs to perform finetuning",
default=10,
)
aa(
"--patience",
type=int,
help="number of checks with no improvement after which training will be stopped",
default=10,
)
aa("--device", type=str, default="cpu", choices=["cpu", "gpu"])
aa(
"--num_processes",
type=int,
default=4,
help="Number of devices to use for performing distributed training on CPU",
)
aa(
"--use_bias",
action="store_true",
help="whether or not to use a bias for the naive transform",
)
aa("--probing_root", type=str, help="path/to/probing")
aa("--log_dir", type=str, help="directory to checkpoint transformations")
aa("--rnd_seed", type=int, default=42, help="random seed for reproducibility")
args = parser.parse_args()
return args
def create_optimization_config(args) -> Tuple[FrozenDict, FrozenDict]:
"""Create frozen config dict for optimization hyperparameters."""
optim_cfg = dict()
optim_cfg["optim"] = args.optim
optim_cfg["lr"] = args.learning_rate
optim_cfg["lmbda"] = args.lmbda
optim_cfg["n_folds"] = args.n_folds
optim_cfg["batch_size"] = args.batch_size
optim_cfg["max_epochs"] = args.epochs
optim_cfg["min_epochs"] = args.burnin
optim_cfg["patience"] = args.patience
optim_cfg["use_bias"] = args.use_bias
optim_cfg["ckptdir"] = os.path.join(args.log_dir, args.model, args.module)
return optim_cfg
def load_features(probing_root: str, subfolder: str = "embeddings") -> Dict[str, Array]:
"""Load features for THINGS objects from disk."""
with open(os.path.join(probing_root, subfolder, "features.pkl"), "rb") as f:
features = pickle.load(f)
return features
def get_batches(triplets: Tensor, batch_size: int, train: bool) -> Iterator:
batches = DataLoader(
dataset=triplets,
batch_size=batch_size,
shuffle=True if train else False,
num_workers=0,
drop_last=False,
pin_memory=True if train else False,
)
return batches
def get_callbacks(optim_cfg: FrozenDict, steps: int = 20) -> List[Callable]:
if not os.path.exists(optim_cfg["ckptdir"]):
os.makedirs(optim_cfg["ckptdir"])
print("\nCreating directory for checkpointing...\n")
checkpoint_callback = ModelCheckpoint(
monitor="val_loss",
dirpath=optim_cfg["ckptdir"],
filename="ooo-finetuning-epoch{epoch:02d}-val_loss{val/loss:.2f}",
auto_insert_metric_name=False,
every_n_epochs=steps,
)
early_stopping = EarlyStopping(
monitor="val_loss",
min_delta=1e-4,
mode="min",
patience=optim_cfg["patience"],
verbose=True,
check_finite=True,
)
callbacks = [checkpoint_callback, early_stopping]
return callbacks
def get_mean_cv_acc(
cv_results: Dict[str, List[float]], metric: str = "test_acc"
) -> float:
avg_val_acc = np.mean([vals[0][metric] for vals in cv_results.values()])
return avg_val_acc
def get_mean_cv_loss(
cv_results: Dict[str, List[float]], metric: str = "test_loss"
) -> float:
avg_val_loss = np.mean([vals[0][metric] for vals in cv_results.values()])
return avg_val_loss
def make_results_df(
columns: List[str],
probing_acc: float,
probing_loss: float,
ooo_choices: Array,
model_name: str,
module_name: str,
source: str,
lmbda: float,
optim: str,
lr: float,
n_folds: int,
bias: bool,
) -> pd.DataFrame:
probing_results_current_run = pd.DataFrame(index=range(1), columns=columns)
probing_results_current_run["model"] = model_name
probing_results_current_run["probing"] = probing_acc
probing_results_current_run["cross-entropy"] = probing_loss
# probing_results_current_run["choices"] = [ooo_choices]
probing_results_current_run["module"] = module_name
probing_results_current_run["family"] = utils.analyses.get_family_name(model_name)
probing_results_current_run["source"] = source
probing_results_current_run["l2_reg"] = lmbda
probing_results_current_run["optim"] = optim.lower()
probing_results_current_run["lr"] = lr
probing_results_current_run["n_folds"] = n_folds
probing_results_current_run["bias"] = bias
return probing_results_current_run
def save_results(
args, probing_acc: float, probing_loss: float, ooo_choices: Array
) -> None:
out_path = os.path.join(args.probing_root, "results")
if not os.path.exists(out_path):
print("\nCreating results directory...\n")
os.makedirs(out_path)
if os.path.isfile(os.path.join(out_path, "probing_results.pkl")):
print(
"\nFile for probing results exists.\nConcatenating current results with existing results file...\n"
)
probing_results_overall = pd.read_pickle(
os.path.join(out_path, "probing_results.pkl")
)
probing_results_current_run = make_results_df(
columns=probing_results_overall.columns.values,
probing_acc=probing_acc,
probing_loss=probing_loss,
ooo_choices=ooo_choices,
model_name=args.model,
module_name=args.module,
source=args.source,
lmbda=args.lmbda,
optim=args.optim,
lr=args.learning_rate,
n_folds=args.n_folds,
bias=args.use_bias,
)
probing_results = pd.concat(
[probing_results_overall, probing_results_current_run],
axis=0,
ignore_index=True,
)
probing_results.to_pickle(os.path.join(out_path, "probing_results.pkl"))
else:
print("\nCreating file for probing results...\n")
columns = [
"model",
"probing",
"cross-entropy",
# "choices",
"module",
"family",
"source",
"l2_reg",
"optim",
"lr",
"n_folds",
"bias",
]
probing_results = make_results_df(
columns=columns,
probing_acc=probing_acc,
probing_loss=probing_loss,
ooo_choices=ooo_choices,
model_name=args.model,
module_name=args.module,
source=args.source,
lmbda=args.lmbda,
optim=args.optim,
lr=args.learning_rate,
n_folds=args.n_folds,
bias=args.use_bias,
)
probing_results.to_pickle(os.path.join(out_path, "probing_results.pkl"))
def run(
features: Array,
data_root: str,
n_objects: int,
device: str,
optim_cfg: FrozenDict,
rnd_seed: int,
num_processes: int,
) -> Tuple[Dict[str, List[float]], Array]:
"""Run optimization process."""
callbacks = get_callbacks(optim_cfg)
triplets = utils.probing.load_triplets(data_root)
# features -= features.mean(axis=0) # center input features
# features = utils.probing.standardize(features) # z-transform / standardize input features
features = (
features - features.mean()
) / features.std() # subtract mean and normalize by standard deviation
optim_cfg["sigma"] = 1e-3
objects = np.arange(n_objects)
# Perform k-fold cross-validation with k = 3 or k = 4
kf = KFold(n_splits=optim_cfg["n_folds"], random_state=rnd_seed, shuffle=True)
cv_results = {}
ooo_choices = []
for k, (train_idx, _) in tqdm(enumerate(kf.split(objects), start=1), desc="Fold"):
train_objects = objects[train_idx]
# partition triplets into disjoint object sets
triplet_partitioning = utils.probing.partition_triplets(
triplets=triplets,
train_objects=train_objects,
)
train_triplets = utils.probing.TripletData(
triplets=triplet_partitioning["train"],
n_objects=n_objects,
)
val_triplets = utils.probing.TripletData(
triplets=triplet_partitioning["val"],
n_objects=n_objects,
)
train_batches = get_batches(
triplets=train_triplets,
batch_size=optim_cfg["batch_size"],
train=True,
)
val_batches = get_batches(
triplets=val_triplets,
batch_size=optim_cfg["batch_size"],
train=False,
)
linear_probe = utils.probing.Linear(
features=features,
optim_cfg=optim_cfg,
)
trainer = Trainer(
accelerator=device,
callbacks=callbacks,
# strategy="ddp_spawn" if device == "cpu" else None,
strategy="ddp",
max_epochs=optim_cfg["max_epochs"],
min_epochs=optim_cfg["min_epochs"],
devices=num_processes if device == "cpu" else "auto",
enable_progress_bar=True,
gradient_clip_val=1.0,
gradient_clip_algorithm="norm",
)
trainer.fit(linear_probe, train_batches, val_batches)
val_performance = trainer.test(
linear_probe,
dataloaders=val_batches,
)
predictions = trainer.predict(linear_probe, dataloaders=val_batches)
predictions = torch.cat(predictions, dim=0).tolist()
ooo_choices.append(predictions)
cv_results[f"fold_{k:02d}"] = val_performance
transformation = linear_probe.transform_w.data.detach().cpu().numpy()
if optim_cfg["use_bias"]:
bias = linear_probe.transform_b.data.detach().cpu().numpy()
transformation = np.concatenate((transformation, bias[:, None]), axis=1)
ooo_choices = np.concatenate(ooo_choices)
return ooo_choices, cv_results, transformation
if __name__ == "__main__":
# parse arguments
args = parseargs()
# seed everything for reproducibility of results
seed_everything(args.rnd_seed, workers=True)
features = load_features(args.probing_root)
model_features = features[args.source][args.model][args.module]
optim_cfg = create_optimization_config(args)
ooo_choices, cv_results, transform = run(
features=model_features,
data_root=args.data_root,
n_objects=args.n_objects,
device=args.device,
optim_cfg=optim_cfg,
rnd_seed=args.rnd_seed,
num_processes=args.num_processes,
)
avg_cv_acc = get_mean_cv_acc(cv_results)
avg_cv_loss = get_mean_cv_loss(cv_results)
save_results(
args, probing_acc=avg_cv_acc, probing_loss=avg_cv_loss, ooo_choices=ooo_choices
)
out_path = os.path.join(
args.probing_root,
"results",
args.source,
args.model,
args.module,
str(args.n_folds),
str(args.lmbda),
args.optim.lower(),
str(args.learning_rate),
)
if not os.path.exists(out_path):
os.makedirs(out_path, exist_ok=True)
with open(os.path.join(out_path, "transform.npy"), "wb") as f:
np.save(file=f, arr=transform)