| """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() |