Any-to-Any
Bagel
Safetensors
bagel
HuggingSelf commited on
Commit
c58f857
·
verified ·
1 Parent(s): 0950d0c

Add Unified-Bench old protocol bundle: unified_bench_old_protocol_0p8346/scripts/reproduce_apr4_unified_bagel_score.py

Browse files
unified_bench_old_protocol_0p8346/scripts/reproduce_apr4_unified_bagel_score.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ import modal
8
+
9
+ from config import DATASET_CACHE_PATH, MODEL_CACHE_PATH, OUTPUT_APR4_PATH
10
+ from images import get_image
11
+ from volumes import dataset_volume, model_volume, output_apr4_volume
12
+
13
+
14
+ RUN_NAME = "bagel"
15
+ REF_DIR = f"{DATASET_CACHE_PATH}/unified_bench/Image"
16
+ GEN_DIR = f"{OUTPUT_APR4_PATH}/unified_bench/{RUN_NAME}/images"
17
+
18
+ image = (
19
+ get_image("unified_bench")
20
+ .add_local_dir("eval", remote_path="/workspace/eval")
21
+ .add_local_dir("src", remote_path="/workspace/src")
22
+ )
23
+ app = modal.App("umm-apr4-bagel-unified-reproduce")
24
+
25
+
26
+ def _image_files(path: str) -> list[str]:
27
+ exts = (".png", ".jpg", ".jpeg", ".webp", ".bmp")
28
+ return sorted(
29
+ [str(p) for p in Path(path).iterdir() if p.is_file() and p.suffix.lower() in exts],
30
+ key=lambda s: int(Path(s).stem) if Path(s).stem.isdigit() else Path(s).stem,
31
+ )
32
+
33
+
34
+ @app.function(
35
+ image=image,
36
+ volumes={
37
+ MODEL_CACHE_PATH: model_volume,
38
+ DATASET_CACHE_PATH: dataset_volume,
39
+ OUTPUT_APR4_PATH: output_apr4_volume,
40
+ },
41
+ gpu="A100-40GB",
42
+ timeout=7200,
43
+ )
44
+ def reproduce() -> dict[str, Any]:
45
+ import sys
46
+ import torch
47
+
48
+ sys.path.insert(0, "/workspace/eval/generation/unified_bench")
49
+ from scorers import load_scorer
50
+
51
+ refs = _image_files(REF_DIR)
52
+ gens = _image_files(GEN_DIR)
53
+ if len(refs) != len(gens):
54
+ raise RuntimeError(f"ref/gen count mismatch: refs={len(refs)} gens={len(gens)}")
55
+
56
+ model_paths = {
57
+ "clip": f"{MODEL_CACHE_PATH}/unified_bench/clip-vit-large-patch14",
58
+ "dinov2": f"{MODEL_CACHE_PATH}/unified_bench/dinov2-large",
59
+ "dinov3": f"{MODEL_CACHE_PATH}/unified_bench/dinov3-vitl16",
60
+ "longclip": f"{MODEL_CACHE_PATH}/unified_bench/longclip-L.pt",
61
+ }
62
+ result: dict[str, Any] = {
63
+ "run_name": RUN_NAME,
64
+ "ref_dir": REF_DIR,
65
+ "gen_dir": GEN_DIR,
66
+ "count": len(refs),
67
+ "model_paths": model_paths,
68
+ "averages_by_model": {},
69
+ "statistics_by_model": {},
70
+ "sanity": {},
71
+ }
72
+
73
+ for name in ("clip", "dinov2", "dinov3", "longclip"):
74
+ scorer = load_scorer(name, model_path=model_paths[name], device="cuda")
75
+ values = []
76
+ per_image = {}
77
+ for ref, gen in zip(refs, gens):
78
+ score = float(scorer.score_pair(ref, gen))
79
+ values.append(score)
80
+ per_image[Path(gen).name] = score
81
+ result["averages_by_model"][name] = sum(values) / len(values)
82
+ result["statistics_by_model"][name] = {
83
+ "total_images": len(values),
84
+ "valid_pairs": len(values),
85
+ "average_similarity": sum(values) / len(values),
86
+ "min_similarity": min(values),
87
+ "max_similarity": max(values),
88
+ }
89
+ result.setdefault("similarities_by_model", {})[name] = per_image
90
+
91
+ if name == "dinov3":
92
+ sample_n = min(12, len(refs))
93
+ ref_shift = [
94
+ float(scorer.score_pair(refs[i], refs[(i + 1) % sample_n]))
95
+ for i in range(sample_n)
96
+ ]
97
+ result["sanity"]["dinov3_ref_shift_12_avg"] = sum(ref_shift) / len(ref_shift)
98
+ result["sanity"]["dinov3_ref_shift_12_first"] = ref_shift[:5]
99
+
100
+ del scorer
101
+ if torch.cuda.is_available():
102
+ torch.cuda.empty_cache()
103
+
104
+ avgs = result["averages_by_model"]
105
+ result["overall_average"] = sum(avgs.values()) / len(avgs)
106
+ result["summary"] = {
107
+ "overall_average": result["overall_average"],
108
+ "averages_by_model": avgs,
109
+ }
110
+ print(json.dumps(result, indent=2, ensure_ascii=False), flush=True)
111
+ return result
112
+
113
+
114
+ @app.local_entrypoint()
115
+ def main() -> None:
116
+ print(json.dumps(reproduce.remote(), indent=2, ensure_ascii=False))