Download src/experiment_v802_probe.py from sneo/v802-classifier: direct link, hf CLI and curl.
- Browser
- Download file 4.83 kB
-
https://huggingface.co/sneo/v802-classifier/resolve/main/src/experiment_v802_probe.py
- Command line
-
hf download hf://sneo/v802-classifier/src/experiment_v802_probe.py
-
curl -L -o experiment_v802_probe.py https://huggingface.co/sneo/v802-classifier/resolve/main/src/experiment_v802_probe.py
4.83 kB
| #!/usr/bin/env python3 | |
| """Logistic probes on v802 pooled descriptors (native-aspect DINOv3 / SigLIP2).""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| from train_v801 import Spec, binary_probabilities, fit_binary, oof_probabilities | |
| from v802_common import ( | |
| CHECK_SEEDS, DEV_SEEDS, ROOT, dump, evaluate, load_data, make_folds, summarize, v801_oof, | |
| ) | |
| TRIALS = ROOT / "artifacts" / "models" / "v802_trials" | |
| DINO_STATS = {"cls": 0, "reg": 1, "mean": 2, "max": 3, "std": 4} | |
| SIGLIP_STATS = {"emb": 0, "mean": 1, "max": 2, "std": 3} | |
| def l2(x: np.ndarray) -> np.ndarray: | |
| return x / np.maximum(np.linalg.norm(x, axis=-1, keepdims=True), 1e-12) | |
| def pooled_features(feature_dir: Path, layers: list[int] | None, stats: list[str]) -> np.ndarray: | |
| """Concatenate per-block L2-normalized descriptors; averages with a sibling -flip dir if given.""" | |
| meta = json.loads((feature_dir / "meta.json").read_text()) | |
| pooled = np.load(feature_dir / "pooled.npy") | |
| if pooled.ndim == 4: # DINOv3: (N, L, S, D) | |
| slots = [meta["pooled_layers"].index(layer) for layer in layers] | |
| blocks = [pooled[:, s, DINO_STATS[k]] for s in slots for k in stats] | |
| else: # SigLIP2: (N, S, D) | |
| blocks = [pooled[:, SIGLIP_STATS[k]] for k in stats] | |
| return l2(np.concatenate([l2(b) for b in blocks], axis=1)) | |
| def aug_oof_probabilities(views, y, anchor_idx, target_idx, folds, spec: Spec) -> np.ndarray: | |
| """Train on every view (e.g. raw + flipped) as separate samples; average view probabilities.""" | |
| result = np.zeros((len(target_idx), y.shape[1])) | |
| for train_local, valid_local in folds: | |
| train_idx = np.concatenate((anchor_idx, target_idx[train_local])) | |
| x_train = np.concatenate([v[train_idx] for v in views]) | |
| y_train = np.tile(y[train_idx], (len(views), 1)) | |
| probe = fit_binary(x_train, y_train, spec, np.ones(len(x_train))) | |
| valid_idx = target_idx[valid_local] | |
| result[valid_local] = np.mean([binary_probabilities(probe, v[valid_idx]) for v in views], axis=0) | |
| return result | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--features", type=Path, nargs="+", required=True, | |
| help="feature dirs; multiple dirs (e.g. raw + flip) are averaged") | |
| parser.add_argument("--aug-features", type=Path, nargs="*", default=[], | |
| help="extra view dirs used as training augmentation + probability TTA") | |
| parser.add_argument("--layers", type=int, nargs="+", default=[24]) | |
| parser.add_argument("--stats", nargs="+", default=["cls", "mean"]) | |
| parser.add_argument("--c", type=float, nargs="+", default=[0.001, 0.003, 0.01, 0.03]) | |
| parser.add_argument("--name", required=True) | |
| parser.add_argument("--seeds", choices=("dev", "check", "all"), default="dev") | |
| args = parser.parse_args() | |
| started = time.time() | |
| meta = json.loads((args.features[0] / "meta.json").read_text()) | |
| data = load_data(meta["file_names"]) | |
| x = l2(np.mean([pooled_features(d, args.layers, args.stats) for d in args.features], axis=0)) | |
| views = [x] + [pooled_features(d, args.layers, args.stats) for d in args.aug_features] | |
| seeds = {"dev": DEV_SEEDS, "check": CHECK_SEEDS, "all": DEV_SEEDS + CHECK_SEEDS}[args.seeds] | |
| report = {"name": args.name, "features": [str(d) for d in args.features], | |
| "aug_features": [str(d) for d in args.aug_features], "layers": args.layers, | |
| "stats": args.stats, "dimension": int(x.shape[1]), "seeds": list(seeds), "by_c": {}} | |
| for c in args.c: | |
| rows = {"probe": [], "mix": []} | |
| for seed in seeds: | |
| folds = make_folds(data, seed) | |
| spec = Spec("v802probe", c, class_weight=None) | |
| probs = ( | |
| aug_oof_probabilities(views, data.y, data.anchor_idx, data.target_idx, folds, spec) | |
| if args.aug_features else | |
| oof_probabilities(x, data.y, data.origins, data.anchor_idx, data.target_idx, folds, spec) | |
| ) | |
| base = v801_oof(data, seed, TRIALS / "cache") | |
| v801 = 0.8 * base["legacy"] + 0.2 * base["knn"] | |
| rows["probe"].append(evaluate(data, probs, folds)) | |
| rows["mix"].append(evaluate(data, 0.5 * probs + 0.5 * v801, folds)) | |
| (TRIALS / "oof").mkdir(parents=True, exist_ok=True) | |
| np.savez(TRIALS / "oof" / f"{args.name}_c{c}_seed{seed}.npz", | |
| names=data.names[data.target_idx], probs=probs) | |
| report["by_c"][str(c)] = {k: summarize(v) for k, v in rows.items()} | |
| print(c, json.dumps(report["by_c"][str(c)]), flush=True) | |
| report["seconds"] = round(time.time() - started, 1) | |
| dump(TRIALS / f"{args.name}.json", report) | |
| if __name__ == "__main__": | |
| main() | |