-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_glocal_probing.py
626 lines (588 loc) · 19.9 KB
/
main_glocal_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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import argparse
import itertools
import os
import pickle
import warnings
from collections import defaultdict
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,
LearningRateMonitor,
ModelCheckpoint,
)
from sklearn.model_selection import KFold
from thingsvision import get_extractor
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
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(
"--imagenet_root",
type=str,
help="path/to/imagenet/data/folder",
default="/home/space/datasets/imagenet/2012/",
)
aa("--dataset", type=str, help="Which dataset to use", default="things")
aa("--model", type=str)
aa(
"--model_dict_path",
type=str,
default="/home/space/datasets/things/model_dict_all.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("--optim", type=str, default="Adam", choices=["Adam", "AdamW", "SGD"])
aa(
"--learning_rates",
type=float,
default=1e-3,
nargs="+",
metavar="eta",
choices=[1e-1, 1e-2, 1e-3, 1e-4, 1e-5],
)
aa(
"--regularization",
type=str,
default="l2",
choices=["l2", "eye"],
help="What kind of regularization to be applied",
)
aa(
"--lmbdas",
type=float,
default=1e-3,
nargs="+",
help="Relative contribution of the l2 or identity regularization penality",
choices=[1.0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5],
)
aa(
"--alphas",
type=float,
default=1e-1,
nargs="+",
help="Relative contribution of the contrastive loss term",
)
aa(
"--taus",
type=float,
default=1,
nargs="+",
help="temperature value for contrastive learning objective",
choices=[1.0, 5e-1, 25e-2, 1e-1, 5e-2, 25e-3, 1e-2],
)
aa(
"--sigma",
type=float,
default=1e-3,
help="Scalar to scale a neural net's pre-transformed representation space prior to the optimization process",
choices=[1.0, 1e-1, 1e-2, 1e-3, 1e-4],
)
aa(
"--triplet_batch_size",
type=int,
default=256,
metavar="B_T",
help="Use 64 <= B <= 1024 and power of two for running optimization process on GPU",
choices=[64, 128, 256, 512, 1024],
)
aa(
"--contrastive_batch_sizes",
type=int,
default=1024,
nargs="+",
metavar="B_C",
help="Use 64 <= B <= 4096 and power of two for running optimization process on GPU",
choices=[64, 128, 256, 512, 1024, 2048, 4096],
)
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=20,
)
aa(
"--patience",
type=int,
help="number of checks with no improvement after which training will be stopped",
default=15,
)
aa("--device", type=str, default="gpu", 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 to use a bias in the linear probe",
)
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 get_combination(
etas: List[float],
lambdas: List[float],
alphas: List[float],
taus: List[float],
contrastive_batch_sizes: List[int],
) -> List[Tuple[float, float, float, float, int]]:
combs = []
combs.extend(
list(itertools.product(etas, lambdas, alphas, taus, contrastive_batch_sizes))
)
return combs[int(os.environ["SLURM_ARRAY_TASK_ID"])]
def create_optimization_config(
args,
eta: float,
lmbda: float,
alpha: float,
tau: float,
contrastive_batch_size: int,
out_path: str,
) -> Dict[str, Any]:
"""Create frozen config dict for optimization hyperparameters."""
optim_cfg = dict()
optim_cfg["optim"] = args.optim
optim_cfg["reg"] = args.regularization
optim_cfg["lr"] = eta
optim_cfg["lmbda"] = lmbda
optim_cfg["alpha"] = alpha
optim_cfg["tau"] = tau
optim_cfg["contrastive_batch_size"] = contrastive_batch_size
optim_cfg["triplet_batch_size"] = args.triplet_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)
optim_cfg["sigma"] = args.sigma
optim_cfg["out_path"] = out_path
return optim_cfg
def create_model_config(args) -> Dict[str, Any]:
"""Create frozen config dict for optimization hyperparameters."""
model_cfg = dict()
model_config = utils.evaluation.load_model_config(args.model_dict_path)
model_cfg["model"] = args.model
model_cfg["module"] = model_config[args.model][args.module]["module_name"]
model_cfg["source"] = args.source
model_cfg["device"] = "cuda" if args.device == "gpu" else args.device
return model_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_temperature(
model_config, model: List[str], module: str, objective: str = "cosine"
) -> List[str]:
"""Get optimal temperature values for all models."""
try:
temp = model_config[model][module]["temperature"][objective]
except KeyError:
temp = 1.0
warnings.warn(
f"\nMissing temperature value for {model} and {module} layer.\nSetting temperature value to 1.\n"
)
return temp
def get_batches(
dataset: Tensor, batch_size: int, train: bool, num_workers: int = 0
) -> Iterator:
batches = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=True if train else False,
num_workers=num_workers,
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_overall_loss",
dirpath=optim_cfg["ckptdir"],
filename="ooo-finetuning-epoch{epoch:02d}-val_overall_loss{val/overall_loss:.2f}",
auto_insert_metric_name=False,
every_n_epochs=steps,
)
early_stopping = EarlyStopping(
monitor="val_overall_loss",
min_delta=1e-4,
mode="min",
patience=optim_cfg["patience"],
verbose=True,
check_finite=True,
)
lr_monitor = LearningRateMonitor(logging_interval="step")
callbacks = [checkpoint_callback, early_stopping, lr_monitor]
return callbacks
def get_mean_cv_performances(cv_results: Dict[str, List[float]]) -> Dict[str, float]:
return {metric: np.mean(folds) for metric, folds in cv_results.items()}
def make_results_df(
columns: List[str],
probing_performances: Dict[str, float],
ooo_choices: Array,
model_name: str,
module_name: str,
source: str,
optim_cfg: Dict[str, Any],
) -> 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_performances["test_acc"]
probing_results_current_run["cross-entropy-overall"] = probing_performances[
"test_overall_loss"
]
probing_results_current_run["triplet-loss"] = probing_performances[
"test_triplet_loss"
]
probing_results_current_run["locality-loss"] = probing_performances[
"test_contrastive_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["reg"] = optim_cfg["reg"]
probing_results_current_run["optim"] = optim_cfg["optim"].lower()
probing_results_current_run["lr"] = optim_cfg["lr"]
probing_results_current_run["alpha"] = optim_cfg["alpha"]
probing_results_current_run["lmbda"] = optim_cfg["lmbda"]
probing_results_current_run["tau"] = optim_cfg["tau"]
probing_results_current_run["sigma"] = optim_cfg["sigma"]
probing_results_current_run["bias"] = optim_cfg["use_bias"]
probing_results_current_run["contrastive_batch_size"] = optim_cfg[
"contrastive_batch_size"
]
probing_results_current_run["triplet_batch_size"] = optim_cfg["triplet_batch_size"]
probing_results_current_run["contrastive"] = True
return probing_results_current_run
def save_results(
args,
optim_cfg: Dict[str, Any],
probing_performances: Dict[str, 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_performances=probing_performances,
ooo_choices=ooo_choices,
model_name=args.model,
module_name=args.module,
source=args.source,
optim_cfg=optim_cfg,
)
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-overall",
"triplet-loss",
"locality-loss",
# "choices",
"module",
"family",
"source",
"reg",
"optim",
"lr",
"alpha",
"lambda",
"tau",
"sigma",
"bias",
"contrastive_batch_size",
"triplet_batch_size",
"contrastive",
]
probing_results = make_results_df(
columns=columns,
probing_performances=probing_performances,
ooo_choices=ooo_choices,
model_name=args.model,
module_name=args.module,
source=args.source,
optim_cfg=optim_cfg,
)
probing_results.to_pickle(os.path.join(out_path, "probing_results.pkl"))
def load_extractor(model_cfg: Dict[str, str]) -> Any:
model_name = model_cfg["model"]
if model_name.startswith("OpenCLIP"):
name, variant, data = model_name.split("_")
model_params = dict(variant=variant, dataset=data)
elif model_name.startswith("clip"):
name, variant = model_name.split("_")
model_params = dict(variant=variant)
else:
name = model_name
model_params = None
extractor = get_extractor(
model_name=name,
source=model_cfg["source"],
device=model_cfg["device"],
pretrained=True,
model_parameters=model_params,
)
return extractor
def run(
features: Array,
imagenet_root: str,
data_root: str,
model_cfg: Dict[str, str],
optim_cfg: Dict[str, Any],
n_objects: int,
device: str,
rnd_seed: int,
num_processes: int,
) -> Tuple[Dict[str, List[float]], Array]:
"""Run optimization process."""
callbacks = get_callbacks(optim_cfg)
extractor = load_extractor(model_cfg)
imagenet_train_set = ImageFolder(
os.path.join(imagenet_root, "train_set"),
extractor.get_transformations(resize_dim=256, crop_dim=224),
)
imagenet_val_set = ImageFolder(
os.path.join(imagenet_root, "val_set"),
extractor.get_transformations(resize_dim=256, crop_dim=224),
)
triplets = utils.probing.load_triplets(data_root)
things_mean = features.mean()
things_std = features.std()
features = (
features - things_mean
) / things_std # subtract global mean and normalize by standard deviation of feature matrix
optim_cfg["things_mean"] = things_mean
optim_cfg["things_std"] = things_std
objects = np.arange(n_objects)
# For glocal optimization, we don't need to perform k-Fold cross-validation (we can simply set k=4 or 5)
kf = KFold(n_splits=3, random_state=rnd_seed, shuffle=True)
cv_results = defaultdict(list)
ooo_choices = []
for train_idx, _ in tqdm(kf.split(objects), 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_things = get_batches(
dataset=train_triplets,
batch_size=optim_cfg["triplet_batch_size"],
train=True,
num_workers=0,
)
train_batches_imagenet = get_batches(
dataset=imagenet_train_set,
batch_size=optim_cfg["contrastive_batch_size"],
train=True,
num_workers=8,
)
val_batches_things = get_batches(
dataset=val_triplets,
batch_size=optim_cfg["triplet_batch_size"],
train=False,
num_workers=0,
)
val_batches_imagenet = get_batches(
dataset=imagenet_val_set,
batch_size=optim_cfg["contrastive_batch_size"],
train=True,
num_workers=8,
)
train_batches = utils.probing.ZippedBatchLoader(
batches_i=train_batches_things,
batches_j=train_batches_imagenet,
num_workers=8,
)
val_batches = utils.probing.ZippedBatchLoader(
batches_i=val_batches_things,
batches_j=val_batches_imagenet,
num_workers=num_processes,
)
glocal_probe = utils.probing.GlocalProbe(
features=features,
optim_cfg=optim_cfg,
model_cfg=model_cfg,
extractor=extractor,
)
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(glocal_probe, train_batches, val_batches)
test_performances = trainer.test(
glocal_probe,
dataloaders=val_batches,
)
predictions = trainer.predict(glocal_probe, dataloaders=val_batches_things)
predictions = torch.cat(predictions, dim=0).tolist()
ooo_choices.append(predictions)
for metric, performance in test_performances[0].items():
cv_results[metric].append(performance)
break
transformation = {}
transformation["weights"] = glocal_probe.transform_w.data.detach().cpu().numpy()
if optim_cfg["use_bias"]:
transformation["bias"] = glocal_probe.transform_b.data.detach().cpu().numpy()
ooo_choices = np.concatenate(ooo_choices)
return ooo_choices, cv_results, transformation, things_mean, things_std
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]
eta, lmbda, alpha, tau, contrastive_batch_size = get_combination(
etas=args.learning_rates,
lambdas=args.lmbdas,
alphas=args.alphas,
taus=args.taus,
contrastive_batch_sizes=args.contrastive_batch_sizes,
)
out_path = os.path.join(
args.probing_root,
"results",
args.source,
args.model,
args.module,
args.optim.lower(),
str(args.eta),
str(lmbda),
str(alpha),
str(tau),
str(contrastive_batch_size),
)
if not os.path.exists(out_path):
os.makedirs(out_path, exist_ok=True)
optim_cfg = create_optimization_config(
args=args,
eta=eta,
lmbda=lmbda,
alpha=alpha,
tau=tau,
contrastive_batch_size=contrastive_batch_size,
out_path=out_path,
)
model_cfg = create_model_config(args)
ooo_choices, cv_results, transform, things_mean, things_std = run(
features=model_features,
imagenet_root=args.imagenet_root,
data_root=args.data_root,
model_cfg=model_cfg,
optim_cfg=optim_cfg,
n_objects=args.n_objects,
device=args.device,
rnd_seed=args.rnd_seed,
num_processes=args.num_processes,
)
probing_performances = get_mean_cv_performances(cv_results)
save_results(
args=args,
optim_cfg=optim_cfg,
probing_performances=probing_performances,
ooo_choices=ooo_choices,
)
if optim_cfg["use_bias"]:
with open(os.path.join(out_path, "transform.npz"), "wb") as f:
np.savez_compressed(
file=f,
weights=transform["weights"],
bias=transform["bias"],
mean=things_mean,
std=things_std,
)
else:
with open(os.path.join(out_path, "transform.npz"), "wb") as f:
np.savez_compressed(
file=f,
weights=transform["weights"],
mean=things_mean,
std=things_std,
)