File size: 11,530 Bytes
26b362d | 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 | #!/usr/bin/env python3
"""
VLMEvalKit evaluation script for JDH_ALLDOC_qa dataset.
Evaluates model's ability to extract specific entry values from all medicaldocuments.
"""
import os
import re
import json
import numpy as np
from pathlib import Path
from vlmeval.dataset.image_base import ImageBaseDataset
from vlmeval.dataset.image_vqa import CustomVQADataset
from vlmeval.smp import load, dump, d2df, LMUDataRoot
from vlmeval.dataset.utils import build_judge, DEBUG_MESSAGE
from vlmeval.utils import track_progress_rich
def get_llm_judgment(pred_value, label_value, question, model):
"""Use LLM to judge if two values are equivalent."""
try:
prompt = f"""请判断以下两个答案是否在主要内容上一致:
问题:{question}
预测答案:{pred_value}
标准答案:{label_value}
如果答案在主要内容上完全一致,请输出1.0。
如果答案在主要内容上不一致,请输出0.0。
如果答案在主要内容上部分一致,请输出0-1之间的分数。
你可以推理思考,请将最终评分输出在<score>标签中。
"""
response = model.generate(prompt)
# First try to extract score from <score> tags
score_pattern = r"<score>\s*(\d+(?:\.\d+)?)\s*</score>"
score_matches = re.findall(score_pattern, response, re.IGNORECASE)
if score_matches:
score = float(score_matches[0])
# Ensure score is in 0-1 range, then convert to 0-100
score = max(0.0, min(1.0, score)) * 100
return score, response
# If no <score> tags found, look for numeric values in the response
# Try to find the last numeric value (likely the final score)
numeric_matches = re.findall(r"\d+(?:\.\d+)?", response)
if numeric_matches:
# Get the last numeric value as it's likely the final score
last_score = float(numeric_matches[-1])
# If the score is already in 0-100 range, use it as is
if 0 <= last_score <= 100:
return last_score, response
# If the score is in 0-1 range, convert to 0-100
elif 0 <= last_score <= 1:
return last_score * 100, response
# If score is outside expected ranges, default to 0
else:
return 0.0, response
# If no numeric values found, return 0
return 0.0, response
except Exception as e:
# Return 0 score with error information in response
error_response = f"Error in score extraction: {str(e)}"
return 0.0, error_response
def alldoc_qa_auxeval(model, line):
"""Auxiliary evaluation function for entry QA."""
try:
# Extract fields
question = str(line.get("question", ""))
answer = str(line.get("answer", ""))
prediction = str(line.get("prediction", ""))
assert model and model.working()
score, log = get_llm_judgment(prediction, answer, question, model)
# Create detailed log
log_message = f"问题:{question}\n预测:{prediction}\n标准答案:{answer}\n评判结果:{log}\n得分:{score}"
return {
"score": score,
"log": log_message,
}
except Exception as e:
return {
"score": 0.0,
"log": f"Evaluation error: {str(e)}",
}
class JDH_ALLDOC_qa:
def load_data(self, dataset):
"""Load custom dataset."""
data_path = os.path.join(LMUDataRoot(), f"{dataset}.tsv")
return load(data_path)
def build_prompt(self, line):
"""Build prompt for the model."""
msgs = ImageBaseDataset.build_prompt(self, line)
# Add system prompt at the beginning
# Use custom system prompt if provided in the line, otherwise use default
if isinstance(line, int):
line_data = self.data.iloc[line]
else:
line_data = line
if "system_prompt" in line_data and line_data["system_prompt"] and str(line_data["system_prompt"]).strip():
system_prompt = str(line_data["system_prompt"]).strip()
else:
system_prompt = "你是医学文档解析专家"
msgs.insert(0, dict(type="text", value=system_prompt))
return msgs
@classmethod
def evaluate(cls, eval_file, **judge_kwargs):
"""Evaluate entry QA dataset"""
import os.path as osp
# Setup file paths
suffix = eval_file.split(".")[-1]
model_name = judge_kwargs.get("model", "gpt-4o")
storage = eval_file.replace(f".{suffix}", f"_{model_name}.xlsx")
tmp_file = eval_file.replace(f".{suffix}", f"_{model_name}.pkl")
nproc = judge_kwargs.pop("nproc", 4)
if not osp.exists(storage):
# Load data
data = load(eval_file)
assert "answer" in data and "prediction" in data
# Convert to string
data["prediction"] = [str(x) for x in data["prediction"]]
data["answer"] = [str(x) for x in data["answer"]]
# Build judge model (optional for this task)
model = None
if judge_kwargs:
try:
model = build_judge(**judge_kwargs)
if not model.working():
model = None
print("Judge model not working, using rule-based evaluation only")
except:
model = None
print("Could not build judge model, using rule-based evaluation only")
# Prepare evaluation data
lt = len(data)
lines = [data.iloc[i] for i in range(lt)]
tups = [(model, line) for line in lines]
indices = [i for i in range(lt)]
# Load existing results if available
ans = {}
if osp.exists(tmp_file):
ans = load(tmp_file)
# Filter out already processed items
tups = [x for x, i in zip(tups, indices) if i not in ans]
indices = [i for i in indices if i not in ans]
# Process remaining items
if len(indices):
new_results = track_progress_rich(
alldoc_qa_auxeval,
tups,
nproc=nproc,
chunksize=nproc,
keys=indices,
save=tmp_file,
)
ans = load(tmp_file)
for k, v in zip(indices, new_results):
assert k in ans
# Verify consistency of cached vs newly computed results
assert ans[k]["log"] == v["log"] and ans[k]["score"] == v["score"]
# Collect results
data["score"] = [ans[i]["score"] for i in range(lt)]
data["log"] = [ans[i]["log"] for i in range(lt)]
# Add class_name and department fields from original data
data["class_name"] = [data.iloc[i].get("doc_type", "unknown") for i in range(lt)]
data["department"] = [data.iloc[i].get("dept_name", "unknown") for i in range(lt)]
# Save detailed results
dump(data, storage)
# Load results and calculate statistics
data = load(storage)
# Overall statistics
total_samples = len(data)
overall_score = data["score"].mean() if total_samples > 0 else 0.0
# Class name statistics
class_name_scores = {}
class_name_counts = {}
for class_name in set(data.get("class_name", ["unknown"] * total_samples)):
mask = [cn == class_name for cn in data.get("class_name", ["unknown"] * total_samples)]
class_samples = sum(mask)
if class_samples > 0:
class_scores = [score for score, m in zip(data["score"], mask) if m]
class_name_counts[class_name] = class_samples
class_name_scores[class_name] = np.mean(class_scores)
# Department statistics
department_scores = {}
department_counts = {}
for department in set(data.get("department", ["unknown"] * total_samples)):
mask = [dept == department for dept in data.get("department", ["unknown"] * total_samples)]
dept_samples = sum(mask)
if dept_samples > 0:
dept_scores = [score for score, m in zip(data["score"], mask) if m]
department_counts[department] = dept_samples
department_scores[department] = np.mean(dept_scores)
# Find samples with lowest scores (most difficult)
low_score_samples = [(i, data.iloc[i]["score"]) for i in range(total_samples)]
difficult_samples = sorted(low_score_samples, key=lambda x: x[1])[:10]
# Format results
results = {
"Overall": round(overall_score, 2),
"Total_Samples": total_samples,
}
# Add class name results
for class_name in sorted(class_name_counts.keys()):
results[f"{class_name}_score"] = round(class_name_scores[class_name], 2)
results[f"{class_name}_count"] = class_name_counts[class_name]
# Add department results
for department in sorted(department_counts.keys()):
results[f"{department}_score"] = round(department_scores[department], 2)
results[f"{department}_count"] = department_counts[department]
# Add difficult samples information
results["Most_Difficult_Samples"] = ", ".join([f"Sample_{i}({score:.1f})" for i, score in difficult_samples])
# Calculate score distribution
score_bins = [0, 50, 80, 100]
score_distribution = np.histogram(data["score"], bins=score_bins)[0]
results["Score_0_50"] = int(score_distribution[0])
results["Score_50_80"] = int(score_distribution[1])
results["Score_80_100"] = int(score_distribution[2])
# Convert to DataFrame and save
ret = d2df(results).round(2)
score_pth = storage.replace(".xlsx", "_score.csv")
dump(ret, score_pth)
# Also save a detailed error analysis
error_analysis_path = storage.replace(".xlsx", "_errors.txt")
with open(error_analysis_path, "w", encoding="utf-8") as f:
f.write("=== Error Analysis ===\n\n")
# Find samples with score < 50
errors = [(i, row) for i, row in data.iterrows() if row["score"] < 50]
f.write(f"Total Errors (score < 50): {len(errors)}\n\n")
for idx, (i, row) in enumerate(errors[:20]): # Show first 20 errors
f.write(f"Error {idx+1}:\n")
f.write(f" Class: {row.get('class_name', 'unknown')}\n")
f.write(f" Department: {row.get('department', 'unknown')}\n")
f.write(f" Question: {row.get('question', '')}\n")
f.write(f" Answer: {row.get('answer', '')}\n")
f.write(f" Prediction: {row.get('prediction', '')}\n")
f.write(f" Score: {row['score']}\n")
f.write("-" * 50 + "\n")
return ret
class GMD_simpleQA(JDH_ALLDOC_qa):
pass
class GMD_complexQA(JDH_ALLDOC_qa):
pass
# Override the default dataset class methods
CustomVQADataset.load_data = JDH_ALLDOC_qa.load_data
CustomVQADataset.build_prompt = JDH_ALLDOC_qa.build_prompt
CustomVQADataset.evaluate = JDH_ALLDOC_qa.evaluate
|