-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvg_bon.py
More file actions
385 lines (329 loc) · 13.3 KB
/
Copy pathvg_bon.py
File metadata and controls
385 lines (329 loc) · 13.3 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
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
import argparse
import json
from pathlib import Path
from src.tools.logger import setup_logger
from src.evaluation.lm_prediction_call import (
LHRSLM,
VHM,
GeoChatLM,
LMDeployLM,
SkysenseGPTLM,
VLLMLM,
)
import torch
from tqdm import tqdm
import numpy as np
from src.model.qwen_reward import Qwen2Reward
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
from collections import defaultdict
from qwen_vl_utils import process_vision_info
import re
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
logger = setup_logger(__name__)
MODEL_TYPE_MAP = {
"lhrs": LHRSLM,
"vhm": VHM,
"skysensegpt": SkysenseGPTLM,
"geochat": GeoChatLM,
"lmdeploy": LMDeployLM,
"vllm": VLLMLM,
}
LHRS_TYPE_MAP = {
"1": "identity",
"2": "color",
"3": "orientation",
"4": "shape",
"5": "quantity",
"6": "area",
"7": "distance",
"8": "resolution",
"9": "modality",
"10": "location",
"11": "reasoning",
}
def convt_qa(conversations, model):
values = [conversation["value"] for conversation in conversations]
query = values[0]
answer = values[1]
vg_prefix = getattr(model, "vg_prefix", "")
vg_suffix = getattr(model, "vg_suffix", "")
query = vg_prefix + " " + query + " " + vg_suffix
return query, answer
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_type", type=str, required=True)
parser.add_argument("--model_path", type=str, required=True)
parser.add_argument("--model_name", type=str, required=True)
parser.add_argument("--reward_model_path", type=str, required=True)
parser.add_argument("--reward_model_type", type=str, default="Qwen2Reward")
parser.add_argument("--eval_image_root", type=str, required=True)
parser.add_argument("--eval_target_file", type=str, required=True)
parser.add_argument("--sample_num", type=int, default=10)
parser.add_argument("--temperature", type=float, default=0.85)
parser.add_argument("--top_p", type=float, default=0.95)
parser.add_argument("--output_path", type=str, required=True)
parser.add_argument("--policy_model_cuda_id", type=int, default=0)
parser.add_argument("--reward_model_cuda_id", type=int, default=1)
parser.add_argument("--reasoning_config", type=str, default=None)
args = parser.parse_args()
return args
def build_reward_model(path, args):
if args.reward_model_type == "Qwen2Reward":
reward_model = Qwen2Reward.from_pretrained(path)
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
elif args.reward_model_type == "majority_voting":
return None, None
else:
reward_model = CLIPModel.from_pretrained(path)
processor = CLIPProcessor.from_pretrained(path)
processor.tokenizer.truncation_side = "left"
reward_model.to(f"cuda:{args.reward_model_cuda_id}")
reward_model.eval()
for name, param in reward_model.named_parameters():
param.requires_grad = False
return reward_model, processor
def extract_answer_bbox(answer):
pattern = r"\[(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\]"
matches = re.findall(pattern, answer)
coords = [[float(x) for x in match] for match in matches]
return coords
def intersection_geo(box1, box2):
x_min1, y_min1, x_max1, y_max1 = box1
x_min2, y_min2, x_max2, y_max2 = box2
x_min_int = max(x_min1, x_min2)
y_min_int = max(y_min1, y_min2)
x_max_int = min(x_max1, x_max2)
y_max_int = min(y_max1, y_max2)
return x_min_int, y_min_int, x_max_int, y_max_int
def calculate_area(box):
x_min1, y_min1, x_max1, y_max1 = box
area_box1 = (x_max1 - x_min1) * (y_max1 - y_min1)
return area_box1
def calculate_iou(box1, box2):
x_min1, y_min1, x_max1, y_max1 = box1
x_min2, y_min2, x_max2, y_max2 = box2
x_min_int, y_min_int, x_max_int, y_max_int = intersection_geo(box1, box2)
if x_min_int >= x_max_int or y_min_int >= y_max_int:
return 0.0
area_int = (x_max_int - x_min_int) * (y_max_int - y_min_int)
area_box1 = (x_max1 - x_min1) * (y_max1 - y_min1)
area_box2 = (x_max2 - x_min2) * (y_max2 - y_min2)
iou = area_int / (area_box1 + area_box2 - area_int)
return iou
def main(args):
jsonl_path = Path(args.output_path) / f"{args.model_name}_bon.jsonl"
jsonl_path.parent.mkdir(parents=True, exist_ok=True)
json_path = Path(args.output_path) / f"{args.model_name}_bon.json"
json_path.parent.mkdir(parents=True, exist_ok=True)
policy_model = MODEL_TYPE_MAP[args.model_type](
model_path=args.model_path,
model_name=args.model_name,
temperature=args.temperature,
top_p=args.top_p,
beam_size=1,
do_sample=True,
use_cache=True,
dtype="float16",
device=f"cuda:{args.policy_model_cuda_id}",
max_new_tokens=50,
reasoning_config=args.reasoning_config,
)
reward_model, processor = build_reward_model(args.reward_model_path, args)
eval_data = json.load(open(args.eval_target_file, "r"))
eval_data = eval_data[:1000]
final_dict = defaultdict(list)
image_root = Path(args.eval_image_root)
for idx, anns in tqdm(enumerate(eval_data), total=len(eval_data)):
image_name = anns["image"]
image_name = image_root / image_name
w, h = Image.open(image_name).size
question, answer = convt_qa(anns["conversations"], policy_model)
question = question.replace("<image>\n", "")
outputs = policy_model.generate_n(
question, image_name, n_samples=args.sample_num
)
if args.reasoning_config is not None:
outputs = [
output.split("<answer>")[1].split("</answer>")[0].strip()
for output in outputs
]
new_outputs = []
pased_result = []
for output in outputs:
parsed_output = policy_model.extract_bbox(output)
if parsed_output is not None:
pased_result.append(parsed_output)
parsed_str = ""
for idx, box in enumerate(parsed_output):
if not isinstance(box, list) or len(box) < 4:
parsed_str = output
break
else:
parsed_str += f"[{box[0]},{box[1]},{box[2]},{box[3]}]"
if idx != len(parsed_output) - 1:
parsed_str += ","
new_outputs.append(parsed_str)
else:
pased_result.append(None)
new_outputs.append(output)
outputs = new_outputs
if args.reward_model_type == "Qwen2Reward":
messages = []
for output in outputs:
message = [
{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image",
"image": f"file://{image_name}",
},
],
},
{"role": "assistant", "content": output},
]
messages.append(message)
if len(messages) > 3:
# single gpu only support 5 samples, otherwise will out of memory
total_samples = len(messages)
wrap_messages = []
for i in range(0, total_samples, 3):
wrap_messages.append(messages[i : i + 3])
else:
wrap_messages = [messages]
all_scores = []
for sub_messages in wrap_messages:
reward_texts = [
processor.apply_chat_template(
message, tokenize=False, add_generation_prompt=False
)
for message in sub_messages
]
image_inputs, video_inputs = process_vision_info(sub_messages)
reward_inputs = processor(
text=reward_texts,
images=image_inputs,
videos=video_inputs,
return_tensors="pt",
padding=True,
truncation=True,
max_length=16384,
)
reward_inputs = reward_inputs.to(f"cuda:{args.reward_model_cuda_id}")
with torch.inference_mode() and torch.autocast(
device_type="cuda", dtype=torch.float16
):
reward_scores = (
reward_model(**reward_inputs)
.values.detach()
.cpu()
.numpy()
.tolist()
)
all_scores.extend(reward_scores)
elif args.reward_model_type == "majority_voting":
# get the output that appears most frequently
from collections import Counter
output_counts = Counter(outputs)
most_common_output = output_counts.most_common(1)
if most_common_output:
chose_answer = most_common_output[0][0]
chose_bbox = pased_result[outputs.index(chose_answer)]
else:
messages = []
for output in outputs:
message = question + "\n" + output
messages.append(message)
wrap_messages = [messages]
all_scores = []
for sub_messages in wrap_messages:
image = Image.open(image_name)
inputs = processor(
images=image,
text=sub_messages,
return_tensors="pt",
padding=True,
truncation=True,
)
inputs = inputs.to(f"cuda:{args.reward_model_cuda_id}")
with torch.inference_mode() and torch.autocast(
device_type="cuda", dtype=torch.float16
):
reward_scores = (
reward_model(**inputs)
.logits_per_image.softmax(dim=-1)
.cpu()
.numpy()
.tolist()
)
all_scores.extend(reward_scores)
if args.reward_model_type != "majority_voting":
max_score_index = np.argmax(all_scores)
chose_answer = outputs[max_score_index]
chose_bbox = pased_result[max_score_index]
result_dict = {
"filename": str(image_name.name),
"query": question,
"answer": answer,
"pred": chose_answer,
}
# write to jsonl
with open(jsonl_path, "a") as f:
f.write(json.dumps(result_dict) + "\n")
prediction = chose_answer.strip()
answer = answer.strip()
answer_bbox = extract_answer_bbox(answer)
pred_bbox_ori = chose_bbox
if answer_bbox is not None and pred_bbox_ori is not None:
for answer, pred in zip(answer_bbox, pred_bbox_ori):
if answer and pred and len(pred) > 0 and len(answer) > 0:
if policy_model.bbox_normalize_bound is not None:
pred_bbox = [
float(pred[0] * w / policy_model.bbox_normalize_bound),
float(pred[1] * h / policy_model.bbox_normalize_bound),
float(pred[2] * w / policy_model.bbox_normalize_bound),
float(pred[3] * h / policy_model.bbox_normalize_bound),
]
else:
pred_bbox = pred
try:
iou = calculate_iou(answer, pred_bbox)
except Exception as e:
iou = 0
if iou >= 0.5:
score = 1
else:
score = 0
else:
pred = None
score = 0
final_dict["score"].append(score)
final_dict["image_name"].append(image_name)
final_dict["question"].append(question)
final_dict["answer"].append(answer)
final_dict["prediction"].append(prediction)
final_dict["answer_bbox"].append(str(answer_bbox))
final_dict["pred_bbox"].append(str(pred_bbox_ori))
final_dict["iou"].append(iou)
else:
for answer in answer_bbox:
final_dict["score"].append(0)
final_dict["image_name"].append(image_name)
final_dict["question"].append(question)
final_dict["answer"].append(answer)
final_dict["prediction"].append(prediction)
final_dict["answer_bbox"].append(str(answer_bbox))
final_dict["iou"].append(0)
avg_score = sum(final_dict["score"]) / len(final_dict["score"])
perf_dict = {"accuracy": avg_score}
logger.info(f"accuracy: {avg_score}")
json.dump(perf_dict, open(json_path, "w"))
if __name__ == "__main__":
args = get_args()
output_path = Path(args.output_path)
output_path.mkdir(parents=True, exist_ok=True)
setup_logger(__name__, output_path, rank=0)
main(args)