-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_temporal_prediction.py
More file actions
336 lines (295 loc) · 12.2 KB
/
metrics_temporal_prediction.py
File metadata and controls
336 lines (295 loc) · 12.2 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
import os
import json
import base64
import time
import re
import logging
from openai import OpenAI
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
# import the generic extractor that returns (score, reasoning)
from metrics_common import extract_score_and_reason
from utils.prompts import (
prompt_consist_temporal,
prompt_instruction_temporal,
prompt_quality,
)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s")
logging.getLogger("openai").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--models",
type=str,
nargs="+",
default=["doubao", "gpt", "gemini"],
)
parser.add_argument("--category", type=str, default="count_change", help="category name")
args = parser.parse_args()
# Constants
BENCH_DIR = "KRIS_Bench"
RESULTS_DIR = "results"
MODELS = args.models
CATEGORIES = ["temporal_prediction"]
METRICS = ["consistency", "instruction_following", "image_quality"]
# Initialize OpenAI client
api_key = os.environ.get("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)
def encode_image_to_base64(image_path):
"""Encode image file to base64 string."""
try:
from PIL import Image
import io
# Open image and convert to JPEG format in memory
img = Image.open(image_path)
buffer = io.BytesIO()
img.convert('RGB').save(buffer, format='JPEG', quality=85)
buffer.seek(0)
# Encode the JPEG data to base64
return base64.b64encode(buffer.getvalue()).decode("utf-8")
except Exception as e:
logging.error(f"Error encoding image {image_path}: {e}")
return None
def evaluate_temporal_images(model_name, category, image_id, metrics=None):
"""
Evaluate a single temporal‐prediction case:
three reference frames + one predicted frame.
Returns a dict mapping:
metric -> score,
metric_reasoning -> reasoning text.
"""
if metrics is None:
metrics = METRICS
results = {}
# load annotation
annotation_path = os.path.join(BENCH_DIR, category, "annotation.json")
try:
with open(annotation_path, "r", encoding="utf-8") as af:
annotations = json.load(af)
except Exception as e:
logging.error(f"Error loading annotation file {annotation_path}: {e}")
return results
ann = annotations.get(str(image_id))
if not ann:
logging.error(f"Image ID {image_id} not found in annotations for {category}")
return results
# build file paths
refs = ann.get("ori_img", [])
ref_paths = [os.path.join(BENCH_DIR, category, fn) for fn in refs]
pred_path = os.path.join(RESULTS_DIR, model_name, category, f"{image_id}.jpg")
# check existence
for p in ref_paths:
if not os.path.exists(p):
logging.error(f"Reference image not found: {p}")
return results
if not os.path.exists(pred_path):
logging.error(f"Predicted image not found: {pred_path}")
return results
# encode images
ref_b64s = []
for p in ref_paths:
b64 = encode_image_to_base64(p)
if not b64:
logging.error(f"Failed to encode reference image: {p}")
return results
ref_b64s.append(b64)
pred_b64 = encode_image_to_base64(pred_path)
if not pred_b64:
logging.error(f"Failed to encode predicted image: {pred_path}")
return results
instruction = ann.get("ins_en", "")
# Determine the frame number of the predicted frame based on reference filenames
# Extract frame numbers from reference filenames
frame_numbers = []
for ref in refs:
match = re.search(r'(\d+)-(\d+)', ref)
if match:
frame_numbers.append(int(match.group(2)))
# Determine the predicted frame number
# Since we have 3 reference frames and need to predict 1 frame,
# the predicted frame is the one missing from frames 1-4
all_possible_frames = set([1, 2, 3, 4])
existing_frames = set(frame_numbers)
missing_frames = all_possible_frames - existing_frames
# There should be exactly one missing frame
if len(missing_frames) == 1:
pred_frame_num = list(missing_frames)[0]
else:
# Default to next frame if we can't determine
pred_frame_num = max(frame_numbers) + 1 if frame_numbers else 1
# for each metric, get both score and reasoning
for metric in metrics:
if metric == "consistency":
prompt = prompt_consist_temporal.format(N=pred_frame_num, instruct=instruction)
resp = evaluate_temporal_with_gpt(prompt, ref_b64s, pred_b64, frame_numbers, pred_frame_num)
score, reason = extract_score_and_reason(
resp,
score_key="consistency_score",
reason_fields=["reasoning", "reason"],
)
results["consistency_score"] = score
results["consistency_reasoning"] = reason
elif metric == "instruction_following":
prompt = prompt_instruction_temporal.format(N=pred_frame_num, instruct=instruction)
resp = evaluate_temporal_with_gpt(prompt, ref_b64s, pred_b64, frame_numbers, pred_frame_num)
score, reason = extract_score_and_reason(
resp,
score_key="instruction_score",
reason_fields=["reasoning", "reason"],
)
results["instruction_score"] = score
results["instruction_reasoning"] = reason
elif metric == "image_quality":
resp = evaluate_with_gpt(prompt_quality, None, pred_b64)
score, reason = extract_score_and_reason(
resp,
score_key="quality_score",
reason_fields=["reasoning", "reason"],
)
results["quality_score"] = score
results["quality_reasoning"] = reason
else:
logging.warning(f"Unknown metric: {metric}")
return results
def evaluate_temporal_with_gpt(prompt, reference_base64_list, predicted_base64, frame_numbers, pred_frame_num):
"""
Send reference frames + predicted frame to GPT and return its response.
Frames are sent in numerical order with the predicted frame inserted at its proper position.
"""
messages = [{"role": "user", "content": []}]
messages[0]["content"].append({"type": "text", "text": prompt})
# Create a combined list of all frames (reference + predicted)
all_frames = []
for i, b64 in enumerate(reference_base64_list):
all_frames.append((frame_numbers[i], b64, "Reference"))
# Add the predicted frame
all_frames.append((pred_frame_num, predicted_base64, "Generated"))
# Sort frames by frame number
all_frames.sort(key=lambda x: x[0])
# Add frames to the message in order
for frame_num, b64, frame_type in all_frames:
messages[0]["content"].append({"type": "text", "text": f"Frame {frame_num} ({frame_type}):"})
messages[0]["content"].append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}"}
})
for attempt in range(3):
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
stream=False,
max_tokens=1000
)
return response.choices[0].message.content
except Exception as e:
logging.error(f"GPT call failed (attempt {attempt+1}/3): {e}")
time.sleep(5)
return ""
def evaluate_with_gpt(prompt, original_base64=None, edited_base64=None):
"""
Send a single image to GPT for quality evaluation.
"""
messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
if edited_base64:
messages[0]["content"].append({"type": "text", "text": "This is the image to evaluate:"})
messages[0]["content"].append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{edited_base64}"}
})
for attempt in range(3):
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
stream=False,
max_tokens=1000
)
return response.choices[0].message.content
except Exception as e:
logging.error(f"GPT call failed (attempt {attempt+1}/3): {e}")
time.sleep(5)
return ""
def process_temporal_image_eval(model, category, image_id, metrics, annotations):
"""
Thread worker: evaluate and package results for one image.
"""
res = evaluate_temporal_images(model, category, image_id, metrics)
if not res:
return None
ann = annotations.get(str(image_id), {})
data = {
"instruction": ann.get("ins_en", ""),
"explain": ann.get("explain_en", ""),
**res
}
return image_id, data
def run_temporal_evaluation(models=None, categories=None, metrics=None, max_workers=8):
"""
Master loop: loads existing metrics, dispatches workers, saves updates.
"""
if models is None: models = MODELS
if categories is None: categories = CATEGORIES
if metrics is None: metrics = METRICS
# mapping of metric to expected result keys
expected_keys_map = {
"consistency": ["consistency_score"],
"instruction_following": ["instruction_score"],
"image_quality": ["quality_score"],
}
for model in models:
for category in tqdm(categories, desc=f"Evaluating {model}", unit="cat"):
# load annotations
ann_file = os.path.join(BENCH_DIR, category, "annotation.json")
try:
with open(ann_file, "r", encoding="utf-8") as af:
annotations = json.load(af)
image_ids = sorted(annotations.keys())
except Exception as e:
logging.error(f"Failed loading annotations: {e}")
continue
# prepare metrics file
out_dir = os.path.join(RESULTS_DIR, model, category)
os.makedirs(out_dir, exist_ok=True)
metrics_fp = os.path.join(out_dir, "metrics.json")
# Load existing metrics if present
try:
if os.path.isfile(metrics_fp):
with open(metrics_fp, "r", encoding="utf-8") as mf:
metrics_data = json.load(mf)
else:
metrics_data = {}
except Exception as e:
logging.error(f"Error loading existing metrics at {metrics_fp}: {e}")
metrics_data = {}
# Process all images regardless of whether they've been processed before
to_process = image_ids
if not to_process:
logging.info(f"No images to process for {model}/{category}.")
continue
# dispatch workers
with ThreadPoolExecutor(max_workers=max_workers) as executor:
fut_map = {
executor.submit(process_temporal_image_eval, model, category, img_id, metrics, annotations): img_id
for img_id in to_process
}
for fut in tqdm(as_completed(fut_map), total=len(fut_map),
desc=f"{model}/{category}", leave=False):
img_id = fut_map[fut]
try:
out = fut.result()
if out:
_id, data = out
metrics_data[_id] = data
except Exception as e:
logging.error(f"Failed processing {img_id}: {e}")
# save updated metrics
try:
with open(metrics_fp, "w", encoding="utf-8") as mf:
json.dump(metrics_data, mf, ensure_ascii=False, indent=2)
logging.info(f"Saved metrics to {metrics_fp}")
except Exception as e:
logging.error(f"Failed to save metrics: {e}")
if __name__ == "__main__":
run_temporal_evaluation(max_workers=15)