File size: 4,670 Bytes
efe1bd4 | 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 | import os
import json
import base64
import re
import asyncio
import aiofiles
from tqdm.asyncio import tqdm_asyncio
from openai import AsyncOpenAI
Test_Model = "Qwen2.5-VL-7B" # 模型名称
# ===== 配置项 =====
TEST_JSON_PATH = "/code/CogReasoner/Test/VisualWebBench_Action_Prediction_281.json" # 测试集 JSON 路径
MODEL_NAME = "qwen2vl" # 使用的模型名称
MAX_SAMPLE = 281 # 测试样本数
MAX_CONCURRENT_REQUESTS = 5 # 最大并发数
ACCURACY_PRINT_INTERVAL = 10 # 每多少步打印一次准确率
OUTPUT_JSON_PATH = f"/code/CogReasoner/Code/Evalaute/Result/Test-{Test_Model}-VisualWebBench_Action_Prediction_281.json" # 推理结果保存路径
# ===== 初始化 OpenAI 客户端(对接 vLLM API) =====
client = AsyncOpenAI(
api_key="EMPTY",
base_url="http://localhost:8080/v1",
)
# ===== 提取模型输出的选项,如 G、A、B等 =====
def extract_answer_letter(text):
match = re.search(r"\b([A-H])\b", text.strip(), re.IGNORECASE)
if match:
return match.group(1).upper()
return None
# ===== 异步处理单个样本 =====
async def process_item(index, item, sem, stats):
async with sem:
image_path = item["images"][0]
gt_answer = item["messages"][-1]["content"].strip().upper()
prompt = item["messages"][0]["content"]
# 编码图像
async with aiofiles.open(image_path, "rb") as f:
content = await f.read()
encoded_image = base64.b64encode(content).decode("utf-8")
image_data_uri = f"data:image;base64,{encoded_image}"
try:
# 推理请求
response = await client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": image_data_uri}},
{
"type": "text",
"text": prompt + "Directly give the answer letter (A, B, C, D, E, F, G, H) without any explanation.",
},
],
},
],
temperature=0.1,
top_p=0.95,
max_tokens=2048,
)
pred_text = response.choices[0].message.content.strip()
except Exception as e:
pred_text = f"[ERROR] {str(e)}"
pred_answer = extract_answer_letter(pred_text)
match = pred_answer == gt_answer
stats["total"] += 1
stats["correct"] += int(match)
if stats["total"] % ACCURACY_PRINT_INTERVAL == 0:
acc = stats["correct"] / stats["total"] * 100
print(f"\n📊 Step {stats['total']}: Accuracy = {acc:.2f}%\n")
return {
"image": image_path,
"ground_truth": gt_answer,
"prediction": pred_answer,
"match": match,
"raw_model_output": pred_text
}
# ===== 主函数 =====
async def main():
with open(TEST_JSON_PATH, "r", encoding="utf-8") as f:
test_data = json.load(f)[:MAX_SAMPLE]
sem = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
stats = {"total": 0, "correct": 0}
tasks = [process_item(i, item, sem, stats) for i, item in enumerate(test_data)]
print(f"\n🚀 Starting evaluation of {len(tasks)} samples...\n")
results = await tqdm_asyncio.gather(*tasks)
accuracy = stats["correct"] / stats["total"] * 100
errors = [r for r in results if not r["match"]]
# 写入输出
output = {
"metrics": {
"total": stats["total"],
"correct": stats["correct"],
"accuracy": accuracy
},
"errors": errors
}
with open(OUTPUT_JSON_PATH, "w", encoding="utf-8") as f:
json.dump(output, f, indent=2, ensure_ascii=False)
# 控制台输出摘要
print(f"\n✅ Evaluation Complete")
print(f"🎯 Accuracy: {accuracy:.2f}%")
print(f"📁 Results saved to: {OUTPUT_JSON_PATH}")
print("\n❌ Sample Errors (up to 5):")
for r in errors[:5]:
print(f"- Image : {r['image']}")
print(f" Ground Truth : {r['ground_truth']}")
print(f" Prediction : {r['prediction']}")
print(f" Raw Output : {r['raw_model_output']}\n")
await client.close() # ✅ 释放连接池
# ===== 启动入口 =====
if __name__ == "__main__":
asyncio.run(main())
sys.exit(0) # ✅ 强制退出,防止异步底层未回收导致挂起
|