-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_matrix.py
161 lines (126 loc) · 5.29 KB
/
result_matrix.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
import re
import os
import json
from glob import glob
from tqdm import tqdm
import torch
import joblib
import numpy as np
import matplotlib.pyplot as plt
from utils.func import calculate_frechet_distance, read_data, softmax
from utils.visualize import draw_task, plot_two_rel
from utils.config import MODEL_LIST, TASK_INFO, ALL_DATASETS
from dataset import build_dataset
output_path = "./output"
# Validate the results
gpt_samples = {
"SEED_2": 2606,
"MME": 1000,
"MMBench_CN": 1994,
"MMBench_EN": 1994,
"MMMU": 900,
"CMMMU": 573,
"ScienceQA": 1467,
"CVBench": 400
}
bar = tqdm(MODEL_LIST)
for model in bar:
bar.set_description(model["model_path"])
for task in TASK_INFO:
file_list = glob(f"{output_path}/{model['store_model_path']}/{task['dataset']}_HS/*.pth")
if model["model_name"] in ["Gemini", "GPT4"]:
if task["dataset"] in gpt_samples:
assert len(file_list) == gpt_samples[task["dataset"]], f"Dataset: {task['dataset']}, Model: {model['model_name']} Expected: {gpt_samples[task['dataset']]} Got: {len(file_list)}"
else:
assert len(file_list) == task["sub_sampling"], f"Dataset: {task['dataset']}, Model: {model['model_name']} Expected: {task['sub_sampling']} Got: {len(file_list)}"
else:
if task["sub_sampling"] is not None:
assert len(file_list) == task["sub_sampling"]
else:
assert len(file_list) == task["num_samples"]
for file in file_list:
ins = torch.load(file)
ins2 = torch.load(file.replace(model['store_model_path'], "BLIP2-opt-2.7B"))
for key in ["question", "label"]:
if isinstance(ins[key], list):
ins[key] = sorted(ins[key])
ins2[key] = sorted(ins2[key])
if ins[key] != ins2[key]:
print(f"Key: {key} not equal for {file}")
print(ins)
print(ins2)
raise ValueError("Key not equal")
# Main result summary
with open(f"{output_path}/result_summary.json", "r") as f:
result_summary = json.load(f)
for model_info in MODEL_LIST:
model_name = model_info['store_model_path']
print(f"Processing {model_name}")
task_data_all = result_summary.get(model_name, {})
exisiting_tasks = set(re.findall(r'\((.*?)\)', key)[0] for key in task_data_all.keys())
for task_i in TASK_INFO:
if task_i['dataset'] in exisiting_tasks:
continue
print("-- Update task: ", task_i['dataset'])
task_data = read_data(model_name, task_i['dataset'], output_path, "avg")
task_data_all.update(task_data)
result_summary.update({model_name: task_data_all})
with open(f"{output_path}/result_summary.json", "w") as f:
json.dump(result_summary, f, indent=4)
# Dataset representation
for dataset in tqdm(ALL_DATASETS, position=1):
task_name, bench_name = re.match(r"(.*) \((.*)\)", dataset).groups()
output_file = f"{output_path}/dataset_representation/{task_name}_{bench_name}.pkl"
if os.path.exists(output_file):
continue
files = glob(f"{output_path}/LLaVA-7B/{bench_name}_HS/*.pth")
data = []
for file in tqdm(files, position=0):
ins = torch.load(file)
if ins["category"] != task_name:
continue
for key, value in ins.items():
if isinstance(value, torch.Tensor):
ins[key] = value.cpu().numpy()
data.append(ins)
print(len(data))
joblib.dump(data, output_file)
# Dataset representation from CLIP
for dataset in tqdm(ALL_DATASETS, position=1):
task_name, bench_name = re.match(r"(.*) \((.*)\)", dataset).groups()
output_file = f"{output_path}/dataset_representation_clip/{task_name}_{bench_name}.pkl"
if os.path.exists(output_file):
continue
files = glob(f"{output_path}/CLIP_embed/{bench_name}_HS/*.pth")
data = []
for file in tqdm(files, position=0):
ins = torch.load(file)
if ins["category"] != task_name:
continue
for key, value in ins.items():
if isinstance(value, torch.Tensor):
ins[key] = value.cpu().numpy()
data.append(ins)
print(len(data))
joblib.dump(data, output_file)
# All result tensor
all_result_summary_file = f"{output_path}/all_result_summary.json"
if not os.path.exists(all_result_summary_file):
with open(all_result_summary_file, "w") as f:
json.dump({}, f, indent=4)
with open(all_result_summary_file, "r") as f:
result_summary = json.load(f)
for model_info in MODEL_LIST:
model_name = model_info['store_model_path']
print(f"Processing {model_name}")
task_data_all = result_summary.get(model_name, {})
exisiting_tasks = set(re.findall(r'\((.*?)\)', key)[0] for key in task_data_all.keys())
for task_i in TASK_INFO:
if task_i['dataset'] in exisiting_tasks:
continue
print("-- Update task: ", task_i['dataset'])
task_data = read_data(model_name, task_i['dataset'], output_path, task_i["evaluation"])
task_data_all.update(task_data)
result_summary.update({model_name: task_data_all})
with open(all_result_summary_file, "w") as f:
json.dump(result_summary, f, indent=4)