-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_llava_vqa.py
More file actions
302 lines (254 loc) · 12.1 KB
/
eval_llava_vqa.py
File metadata and controls
302 lines (254 loc) · 12.1 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
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
import os
import json
import argparse
import torch
import torch.distributed as dist
from tqdm import tqdm
from PIL import Image
from datetime import datetime
from torchvision import transforms
import logging
from downstream.llava.model.builder import load_pretrained_model
from downstream.llava.utils import disable_torch_init
from downstream.llava.constants import IMAGE_TOKEN_INDEX
from downstream.llava.conversation import conv_templates
from downstream.llava.mm_utils import tokenizer_image_token
from perf.profiling import ProfileModelMemory
class RelativePathFormatter(logging.Formatter):
def __init__(self, rank, fmt=None, datefmt=None, style='%', validate=True):
super().__init__(fmt, datefmt, style, validate)
self.rank = rank
def format(self, record):
run_dir = os.getcwd()
record.rank = self.rank
record.relativepath = os.path.relpath(os.path.abspath(record.pathname), run_dir)
return super().format(record)
def setup_logger(output_dir, rank):
os.makedirs(output_dir, exist_ok=True)
log_path = os.path.join(output_dir, f"eval_rank{rank}.log")
formatter = RelativePathFormatter(
rank,
fmt='[RANK %(rank)d] %(asctime)s - %(relativepath)s:%(lineno)d - [%(levelname)s] - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
fh = logging.FileHandler(log_path)
fh.setFormatter(formatter)
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger = logging.getLogger("eval")
logger.setLevel(logging.INFO)
logger.handlers = [ch, fh]
return logger
def normalize_answer(s):
import re, string
def remove_articles(text): return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text): return ' '.join(text.split())
def remove_punctuation(text): return ''.join(ch for ch in text if ch not in set(string.punctuation))
def lower(text): return text.lower()
text = lower(s)
text = white_space_fix(remove_articles(remove_punctuation(text)))
return "yes" if text.startswith("yes") else "no" if text.startswith("no") else text
def vqa_soft_accuracy(prediction, answers):
prediction = normalize_answer(prediction)
answers = [normalize_answer(ans) for ans in answers]
return min(1.0, answers.count(prediction) / 3.0)
def ddp_scatter(data, rank, world_size):
chunk_size = len(data) // world_size
remainder = len(data) % world_size
start = rank * chunk_size + min(rank, remainder)
end = start + chunk_size + (1 if rank < remainder else 0)
return data[start:end]
@torch.inference_mode()
def generate_batch(logger, model, tokenizer, image_processor, image_paths, prompts, device, use_random_image=False):
if use_random_image:
sample_img = image_processor.preprocess(Image.new("RGB", (512, 512)), return_tensors="pt")["pixel_values"][0]
img_shape = sample_img.shape
image_tensor_batch = torch.randn((len(prompts), *img_shape), device=device, dtype=model.get_vision_tower().dtype)
else:
images = [Image.open(p).convert("RGB") for p in image_paths]
image_tensors = [image_processor.preprocess(img, return_tensors="pt")["pixel_values"][0] for img in images]
image_tensor_batch = torch.stack(image_tensors).to(device, dtype=model.get_vision_tower().dtype)
input_ids_batch = [tokenizer_image_token(p, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt") for p in prompts]
tokenizer.pad_token_id = tokenizer.eos_token_id
input_ids_batch = torch.nn.utils.rnn.pad_sequence(input_ids_batch, batch_first=True, padding_value=tokenizer.pad_token_id)
attention_mask = (input_ids_batch != tokenizer.pad_token_id).long().to(device)
input_ids_batch = input_ids_batch.to(device)
with ProfileModelMemory(model, logger):
output_ids = model.generate(
inputs=input_ids_batch,
attention_mask=attention_mask,
images=image_tensor_batch,
do_sample=False,
temperature=0.0,
max_new_tokens=8,
pad_token_id=tokenizer.pad_token_id
)
torch.cuda.empty_cache()
return [tokenizer.decode(ids, skip_special_tokens=True).strip() for ids in output_ids]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", required=True)
parser.add_argument("--projector_weight", required=True)
parser.add_argument("--image_root", required=True)
parser.add_argument("--output_dir", required=True)
parser.add_argument("--max_samples", type=int, default=2000)
parser.add_argument("--batch_size", type=int, default=200)
parser.add_argument("--use_unibind", action='store_true', default=True, help="Use Unibind for encoder")
parser.add_argument(
"--unibind_pretrain_weights",
type=str,
default="./ckpts/pretrained_weights_flash_atten_image_patchs.pt",
help="Path to UniBind pretrain weights (must match LoRA base used in attacks)"
)
args = parser.parse_args()
rank = int(os.environ.get("LOCAL_RANK", "0"))
torch.cuda.set_device(rank)
torch.distributed.init_process_group("nccl", device_id=rank)
device = torch.device("cuda", rank)
world_size = dist.get_world_size()
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
root_dir = os.path.join(args.output_dir, timestamp)
model_tags = ["unibind", "robustbind2", "robustbind4"]
# model_tags = ["unibind"]
settings = [
{
"name": "clean",
"val_json_template": "datasets/VQA2/val_data.json",
"use_random_image": False
},
{
"name": "random",
"val_json_template": "datasets/VQA2/val_data.json",
"use_random_image": True
},
{
"name": "eps2",
"val_json_template": "datasets/VQA2/val_data_adv_eps2_{model_tag}.json",
"use_random_image": False
},
{
"name": "eps4",
"val_json_template": "datasets/VQA2/val_data_adv_eps4_{model_tag}.json",
"use_random_image": False
},
]
lora_weights_map = {
"unibind": None,
"robustbind2": "./ckpts/vision_eps2_lora_weights.pt",
"robustbind4": "./ckpts/vision_eps4_lora_weights.pt",
}
# Dictionary to store all results for CSV generation
all_accuracies = {}
for setting in settings:
setting_name = setting["name"]
val_json_template = setting["val_json_template"]
use_random = setting["use_random_image"]
for model_tag in model_tags:
if setting_name == "random" and model_tag != "unibind":
continue
val_json = val_json_template.format(model_tag=model_tag)
model_out_dir = os.path.join(root_dir, setting_name, model_tag)
os.makedirs(model_out_dir, exist_ok=True)
logger = setup_logger(model_out_dir, rank)
logger.info(f"CLI Arguments: {json.dumps(vars(args), indent=2)}")
logger.info(f"🧪 Evaluating {model_tag.upper()} [{setting_name}]")
with open(val_json) as f:
data = json.load(f)
if args.max_samples:
data = data[:args.max_samples]
data = ddp_scatter(data, rank, world_size)
logger.info(f"📊 Rank {rank} processing {len(data)} samples...")
disable_torch_init()
# Log model configuration for transparency
logger.info(
json.dumps(
{
"model_tag": model_tag,
"setting": setting_name,
"use_unibind": args.use_unibind,
"unibind_pretrain_weights": args.unibind_pretrain_weights,
"projector_weight": args.projector_weight,
"use_lora": lora_weights_map[model_tag] is not None,
"lora_weights": lora_weights_map[model_tag],
"lora_rank": 4,
"lora_alpha": 8,
},
indent=2,
)
)
tokenizer, model, image_processor, _ = load_pretrained_model(
model_path=args.model_path,
model_name=args.model_path,
model_base=None,
torch_dtype=torch.float16,
device=device,
device_map=None,
use_unibind=args.use_unibind,
unibind_pretrain_weights=args.unibind_pretrain_weights,
projector_weights_path=args.projector_weight,
unibind_use_lora=lora_weights_map[model_tag] is not None,
unibind_lora_weights=lora_weights_map[model_tag],
freeze_projector=True,
freeze_unibind=True,
unibind_lora_rank=4,
unibind_lora_alpha=8
)
model = model.to(device)
results = []
for i in tqdm(range(0, len(data), args.batch_size), disable=(rank != 0)):
batch = data[i:i + args.batch_size]
image_paths = [os.path.join(args.image_root, item["image"]) for item in batch]
questions = [item["question"] for item in batch]
answers = [item.get("answers", []) for item in batch]
prompts = []
for q in questions:
conv = conv_templates["llava_v1"].copy()
conv.append_message(conv.roles[0], f"<image>\nQuestion: {q}\nAnswer:")
conv.append_message(conv.roles[1], None)
prompts.append(conv.get_prompt())
preds = [normalize_answer(p) for p in generate_batch(
logger, model, tokenizer, image_processor,
image_paths, prompts, device,
use_random_image=use_random
)]
accs = [vqa_soft_accuracy(p, a) for p, a in zip(preds, answers)]
for item, pred, acc in zip(batch, preds, accs):
results.append({
"image_id": item["image_id"],
"question_id": item["question_id"],
"question": item["question"],
"predicted_answer": pred,
"vqa_soft_accuracy": acc
})
gathered = [None for _ in range(world_size)]
torch.distributed.all_gather_object(gathered, results)
if rank == 0:
all_results = [r for sublist in gathered for r in sublist]
acc_mean = sum(r["vqa_soft_accuracy"] for r in all_results) / len(all_results)
output_json = os.path.join(model_out_dir, "vqa_results.json")
with open(output_json, "w") as f:
json.dump(all_results, f, indent=2)
logger.info(f"✅ Saved {len(all_results)} VQA results to {output_json}")
logger.info(f"📊 Final VQA Soft Accuracy: {acc_mean:.3f}")
# Store accuracy in memory for CSV generation
all_accuracies[(setting_name, model_tag)] = (acc_mean, val_json)
torch.distributed.barrier()
if rank == 0:
# Write consolidated CSV results from in-memory data
epsilon_map = {"clean": "None", "random": "None", "eps2": "2/255", "eps4": "4/255"}
csv_path = os.path.join(root_dir, "vqa_results_summary.csv")
with open(csv_path, "w") as csv_file:
csv_file.write("Model,Setting,Epsilon,Accuracy,DataFile\n")
for setting in settings:
setting_name = setting["name"]
for model_tag in model_tags:
if setting_name == "random" and model_tag != "unibind":
continue
key = (setting_name, model_tag)
if key in all_accuracies:
acc_mean, val_json = all_accuracies[key]
csv_file.write(f"{model_tag},{setting_name},{epsilon_map[setting_name]},{acc_mean:.4f},{val_json}\n")
print(f"\n✅ Consolidated CSV results saved to: {csv_path}")
dist.destroy_process_group()
if __name__ == "__main__":
main()