Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
143 lines (114 loc) · 4.58 KB
/
Copy pathevaluate.py
File metadata and controls
143 lines (114 loc) · 4.58 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
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
import sys
import argparse
import os
import yaml
import contextlib
from tools.read_yaml import *
sys.path.append(os.getcwd())
from benchmarks.base_eval_dataset import load_dataset
from file_utils.counterfactual_file_manage import CounterFactualFileManager
from file_utils.result_file_manage import ResultFileManager
from models.base_model import AVAILABLE_MODELS, load_model
from transformers import set_seed
set_seed(555)
def get_info(info):
if "name" not in info:
raise ValueError("Model name is not specified.")
name = info["name"]
# info.pop("name")
return name, info
def load_models(model_infos):
for model_info in model_infos:
name, info = get_info(model_info)
model = load_model(name, info)
yield model
def load_datasets(dataset_infos):
for dataset_info in dataset_infos:
name, info = get_info(dataset_info)
dataset = load_dataset(name, info)
yield dataset
class DualOutput:
def __init__(self, file, stdout):
self.file = file
self.stdout = stdout
def write(self, data):
self.file.write(data)
self.stdout.write(data)
def flush(self):
self.file.flush()
self.stdout.flush()
if __name__ == "__main__":
args = argparse.ArgumentParser()
args.add_argument(
"--models",
type=str,
nargs="?",
help="Specify model names as comma separated values.",
default='llama_adapter,uniter,uniter_large',
)
args.add_argument(
"--datasets",
type=str,
nargs="?",
help="Specify dataset names as comma separated values.",
default='vqa2,mscoco',
)
args.add_argument(
"--counterfactual",
action="store_true",
default=False,
)
args.add_argument(
"--contradiction_threshold",
type=float,
default=0.8,
)
args.add_argument(
"--clip_lower_threshold",
type=float,
default=0.2,
)
args.add_argument(
"--clip_upper_threshold",
type=float,
default=0.8,
)
args.add_argument(
"--max_new_tokens",
type=int,
default=2048,
)
args.add_argument(
"--temperature",
type=float,
default=0.0,
)
phrased_args = args.parse_args()
model_names = phrased_args.models.split(",")
model_infos = [{"name": name, "counterfactual": phrased_args.counterfactual, "temperature": phrased_args.temperature, "max_new_tokens": phrased_args.max_new_tokens} for name in model_names]
dataset_infos = [{"name": dataset_name, "counterfactual": phrased_args.counterfactual, "counterfactual_path": get_counterfactual_folder(), "contradiction_threshold": phrased_args.contradiction_threshold, "clip_lower_threshold": phrased_args.clip_lower_threshold, "clip_upper_threshold": phrased_args.clip_upper_threshold} for dataset_name in phrased_args.datasets.split(",")]
if not os.path.exists(get_log_folder()):
os.makedirs(get_log_folder())
if not os.path.exists(get_counterfactual_folder()):
os.makedirs(get_counterfactual_folder())
is_counterfactual = phrased_args.counterfactual
for model_info in model_infos:
print("\nMODEL INFO:", model_info)
print("-" * 80)
dataset_count = 0
for dataset_info in dataset_infos:
dataset_name, _dataset_info = get_info(dataset_info)
counterfactual_file_manager = CounterFactualFileManager(model_info["name"], dataset_name, contradiction_threshold=phrased_args.contradiction_threshold, clip_lower_threshold=phrased_args.clip_lower_threshold, clip_upper_threshold=phrased_args.clip_upper_threshold, evaluate=True) if is_counterfactual else None
result_file_manager = ResultFileManager(model_info["name"], dataset_name, is_counterfactual)
model = load_model(model_info["name"], model_info)
dataset = load_dataset(dataset_name, _dataset_info)
dataset_count += 1
print('MODEL:', model.name, 'TEMPERATURE:', model.temperature, 'MAX_NEW_TOKENS:', model.max_new_tokens, 'CONTRADICTION:', dataset.contradiction_threshold, 'CLIP:', dataset.clip_lower_threshold, dataset.clip_upper_threshold)
print(f"\nDATASET: {dataset.name}")
print("-" * 20)
dataset.evaluate(model, counterfactual_file_manager, result_file_manager) # Assuming this function now prints results directly.
print()
print("-" * 80)
print(f"Total Datasets Evaluated: {dataset_count}\n")
print("=" * 80)
# python evaluate.py --models otter_image --datasets mmbench