File size: 5,170 Bytes
234f610 | 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 | """Simple retrieval-style reference baseline for the image generation benchmark.
The baseline uses only the packaged metadata and retrieves the nearest training
example by RGB distance for every test sample.
"""
import argparse
import json
import math
from pathlib import Path
HF_ROOT = Path(__file__).resolve().parents[1]
def parse_args():
parser = argparse.ArgumentParser(
description="Run a nearest-RGB retrieval baseline on the image generation split."
)
parser.add_argument(
"--hf-root",
default=str(HF_ROOT),
help="Path to the exported huggingface package root.",
)
parser.add_argument(
"--output",
help="Optional JSON output path for the retrieval summary.",
)
parser.add_argument(
"--save-retrievals",
help="Optional JSONL output path for per-sample retrieval results.",
)
parser.add_argument(
"--compact",
action="store_true",
help="Write compact JSON instead of pretty JSON.",
)
return parser.parse_args()
def load_json(path: Path):
with open(path, "r", encoding="utf-8") as handle:
return json.load(handle)
def dump_json(value, compact: bool):
if compact:
return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
return json.dumps(value, ensure_ascii=False, indent=2)
def write_text(text: str, output_path: str | None):
if output_path:
path = Path(output_path)
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
return
print(text)
def rgb_distance(left, right):
return math.sqrt(
(left[0] - right[0]) ** 2
+ (left[1] - right[1]) ** 2
+ (left[2] - right[2]) ** 2
)
def nearest_neighbor(sample, train_samples):
best_match = None
best_distance = None
for candidate in train_samples:
distance = rgb_distance(sample["color_rgb"], candidate["color_rgb"])
if best_distance is None or distance < best_distance:
best_distance = distance
best_match = candidate
return best_match, best_distance
def safe_rate(correct: int, total: int):
if total == 0:
return None
return correct / total
def main():
args = parse_args()
hf_root = Path(args.hf_root)
train_metadata = load_json(hf_root / "image_generation" / "train" / "metadata.json")
test_metadata = load_json(hf_root / "image_generation" / "test" / "metadata.json")
train_samples = list(train_metadata.values())
test_samples = list(test_metadata.values())
retrievals = []
total_distance = 0.0
transparency_correct = 0
transparency_total = 0
surface_correct = 0
surface_total = 0
color_family_correct = 0
color_family_total = 0
for sample in test_samples:
match, distance = nearest_neighbor(sample, train_samples)
total_distance += distance
retrieval = {
"test_id": sample["id"],
"retrieved_train_id": match["id"],
"distance": distance,
"test_image_path": sample["image_path"],
"retrieved_image_path": match["image_path"],
}
retrievals.append(retrieval)
if sample.get("transparency") is not None and match.get("transparency") is not None:
transparency_total += 1
transparency_correct += int(sample["transparency"] == match["transparency"])
if sample.get("surface") is not None and match.get("surface") is not None:
surface_total += 1
surface_correct += int(sample["surface"] == match["surface"])
if sample.get("color_family") is not None and match.get("color_family") is not None:
color_family_total += 1
color_family_correct += int(sample["color_family"] == match["color_family"])
summary = {
"benchmark": "image_generation",
"baseline": "nearest_train_rgb_retrieval",
"train_samples": len(train_samples),
"test_samples": len(test_samples),
"mean_retrieval_distance": total_distance / len(test_samples),
"label_agreement": {
"transparency": {
"evaluated_samples": transparency_total,
"accuracy": safe_rate(transparency_correct, transparency_total),
},
"surface": {
"evaluated_samples": surface_total,
"accuracy": safe_rate(surface_correct, surface_total),
},
"color_family": {
"evaluated_samples": color_family_total,
"accuracy": safe_rate(color_family_correct, color_family_total),
},
},
}
if args.save_retrievals:
jsonl = "\n".join(json.dumps(item, ensure_ascii=False) for item in retrievals)
write_text(jsonl, args.save_retrievals)
write_text(dump_json(summary, args.compact), args.output)
if __name__ == "__main__":
main() |