forked from BerasiDavide/vlm_image_compositionality
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification_optim.py
More file actions
109 lines (96 loc) · 3.54 KB
/
Copy pathclassification_optim.py
File metadata and controls
109 lines (96 loc) · 3.54 KB
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
'''
Script to perform compositional classification using CLIP Weights (CW) with optimal temperature as the noise distribution.
'''
import os
from argparse import Namespace
from tqdm import tqdm
import argparse
import json
import subprocess
import numpy as np
from collections import defaultdict
from datasets.composition_dataset import CompositionDataset
from datasets.read_datasets import DATASET_PATHS
from classification import main
def run_main_with_optimal_T(config: argparse.Namespace, verbose=True):
# Perform grid search on validation set to select the optimal temperature value
TT = np.linspace(0, 0.3, 31)[1:] # grid of temperature values
objective = 'auc'
T_optim, objective_optim = 0, 0
for T in tqdm(TT, "Searching for optimal T"):
config_T = Namespace(
open_world= config.open_world,
dataset= config.dataset,
experiment_name=f'{config.experiment_name}_CW_{T:.4f}',
model_architecture= config.model_architecture,
model_pretraining= config.model_pretraining,
n_images= config.n_images,
n_exp= config.n_exp,
modality_IW= config.modality_IW,
result_path= None,
test_phase="val"
)
result = main(config_T, verbose=False)
obj = result[objective]
if obj > objective_optim:
T_optim, objective_optim = T, obj
# Perform experiment with optimal T value
config_optim = Namespace(
open_world= config.open_world,
dataset= config.dataset,
experiment_name=f'{config.experiment_name}_CW_{T_optim:.4f}',
model_architecture= config.model_architecture,
model_pretraining= config.model_pretraining,
n_images= config.n_images,
n_exp= config.n_exp,
modality_IW= config.modality_IW,
result_path= config.result_path,
test_phase=config.test_phase
)
print(f"*** Running experiment with optimal T = {T_optim:.4f} ***")
main(config_optim, verbose=verbose)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
help="name of the dataset",
type=str, required=True)
parser.add_argument(
"--model_architecture",
help="clip model architecture",
type=str, default="ViT-L-14")
parser.add_argument(
"--model_pretraining",
help="clip model pretraining set",
type=str, default="openai")
parser.add_argument(
"--experiment_name",
help="name of the experiment",
type=str)
parser.add_argument(
"--modality_IW",
help="modality considerer for ideal words",
choices=['text', 'valid text', 'image'],
type=str, default=None)
parser.add_argument(
"--n_images",
help="limit the number of images per pair in IW computation with image modality",
type=int, default=None)
parser.add_argument(
"--open_world",
help="evaluate on open world setup",
action="store_true")
parser.add_argument(
"--n_exp",
help="number of times the experiment is repeated. >1 makes sense only if modality_IW='image' and n_image!=None",
default=1, type=int)
parser.add_argument(
"--test_phase",
help="test or val",
default="test", type=str)
parser.add_argument(
"--result_path",
help="path to json file. Result is saved here.",
type=str, default=None)
config = parser.parse_args()
run_main_with_optimal_T(config, verbose=True)