Download binding_clip/evaluate.py from Kiriti-Ray/binding-clip: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/Kiriti-Ray/binding-clip/resolve/main/binding_clip/evaluate.py
- Command line
-
hf download hf://Kiriti-Ray/binding-clip/binding_clip/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/Kiriti-Ray/binding-clip/resolve/main/binding_clip/evaluate.py
2.98 kB
| """Evaluate a set of checkpoints and compute paired scene-bootstrap differences.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from torch.utils.data import DataLoader | |
| from .data import CoGenT | |
| from .model import MODES, load_model | |
| from .train import bootstrap_clustered, evaluate, resolve_device, set_seed | |
| def deltas(first, second): | |
| first_correct, cross, scenes = first | |
| second_correct, second_cross, second_scenes = second | |
| if not (np.array_equal(cross, second_cross) and np.array_equal(scenes, second_scenes)): | |
| raise ValueError("Bootstrap comparisons must use the same examples in the same order") | |
| differences = { | |
| "color_swap": second_correct[:, 0].astype(float) - first_correct[:, 0], | |
| "spatial_flip": second_correct[:, 1].astype(float) - first_correct[:, 1], | |
| "joint": second_correct.all(axis=1).astype(float) - first_correct.all(axis=1), | |
| } | |
| return { | |
| key: { | |
| "all": bootstrap_clustered(difference, scenes), | |
| "cross_shape": bootstrap_clustered(difference[cross], scenes[cross]), | |
| } | |
| for key, difference in differences.items() | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("data", type=Path) | |
| parser.add_argument("checkpoints", type=Path) | |
| parser.add_argument("--split", choices=("valA", "valB"), default="valB") | |
| parser.add_argument("--device", choices=("auto", "cpu", "mps", "cuda"), default="auto") | |
| parser.add_argument("--batch-size", type=int, default=48) | |
| parser.add_argument("--output", type=Path) | |
| args = parser.parse_args() | |
| if args.batch_size < 1: | |
| parser.error("batch size must be positive") | |
| set_seed(42) | |
| device = resolve_device(args.device) | |
| metrics = {} | |
| examples = {} | |
| datasets = {} | |
| for mode in MODES: | |
| checkpoint = args.checkpoints / mode | |
| if not checkpoint.is_dir(): | |
| continue | |
| model = load_model(checkpoint, device) | |
| resolution = model.config.resolution | |
| if resolution not in datasets: | |
| datasets[resolution] = CoGenT(args.data, args.split, resolution) | |
| loader = DataLoader(datasets[resolution], batch_size=args.batch_size) | |
| metrics[mode], examples[mode] = evaluate(model, loader, device, confidence=True, return_examples=True) | |
| if not metrics: | |
| parser.error("no checkpoint subfolders found") | |
| report = { | |
| "split": args.split, | |
| "metrics": metrics, | |
| "paired_deltas": { | |
| f"{second}_minus_{first}": deltas(examples[first], examples[second]) | |
| for first, second in (("global", "unary"), ("global", "pair"), ("unary", "pair"), ("pair", "factorized")) | |
| if first in examples and second in examples | |
| }, | |
| } | |
| output = json.dumps(report, indent=2) + "\n" | |
| if args.output: | |
| args.output.write_text(output) | |
| else: | |
| print(output, end="") | |
| if __name__ == "__main__": | |
| main() | |