Download src/extract_features_v801.py from sneo/v802-classifier: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/sneo/v802-classifier/resolve/main/src/extract_features_v801.py
- Command line
-
hf download hf://sneo/v802-classifier/src/extract_features_v801.py
-
curl -L -o extract_features_v801.py https://huggingface.co/sneo/v802-classifier/resolve/main/src/extract_features_v801.py
10.1 kB
| #!/usr/bin/env python3 | |
| """Extract resumable multi-view DINOv3 descriptors for the v801 probe. | |
| The legacy extractor immediately reduces four views to an element-wise mean | |
| and maximum. v801 keeps each view and adds patch maximum/std descriptors so | |
| that model selection can decide how much spatially local information to keep. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import fcntl | |
| import hashlib | |
| import json | |
| import os | |
| import platform | |
| import tempfile | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| from transformers import AutoImageProcessor, AutoModel | |
| from dataset_utils import load_dataset | |
| from extract_features import file_sha256, four_views | |
| FEATURE_SCHEMA = "dinov3_four_view_cls_patchmean_patchmax_patchstd_v801" | |
| DESCRIPTORS = ("cls", "patch_mean", "patch_max", "patch_std") | |
| def atomic_savez(path: Path, **arrays: np.ndarray) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| descriptor, temporary = tempfile.mkstemp( | |
| prefix=f".{path.name}.", suffix=".tmp.npz", dir=path.parent | |
| ) | |
| os.close(descriptor) | |
| temporary_path = Path(temporary) | |
| try: | |
| np.savez_compressed(temporary_path, **arrays) | |
| os.replace(temporary_path, path) | |
| finally: | |
| temporary_path.unlink(missing_ok=True) | |
| def atomic_write_json(path: Path, value: dict[str, object]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| descriptor, temporary = tempfile.mkstemp( | |
| prefix=f".{path.name}.", suffix=".tmp", dir=path.parent | |
| ) | |
| temporary_path = Path(temporary) | |
| try: | |
| with os.fdopen(descriptor, "w", encoding="utf-8") as handle: | |
| json.dump(value, handle, ensure_ascii=False, indent=2) | |
| handle.write("\n") | |
| handle.flush() | |
| os.fsync(handle.fileno()) | |
| os.replace(temporary_path, path) | |
| finally: | |
| temporary_path.unlink(missing_ok=True) | |
| def cache_identity(dataset: object, model_path: Path) -> tuple[str, dict[str, str], str]: | |
| model_digests = { | |
| name: file_sha256(model_path / name) | |
| for name in ("model.safetensors", "config.json", "preprocessor_config.json") | |
| if (model_path / name).is_file() | |
| } | |
| if "model.safetensors" not in model_digests: | |
| raise FileNotFoundError(f"missing model weights under {model_path}") | |
| source = hashlib.sha256() | |
| source.update(FEATURE_SCHEMA.encode()) | |
| source.update(json.dumps(dataset.classes, ensure_ascii=False).encode()) | |
| source.update(np.asarray(dataset.labels, dtype=np.uint8).tobytes()) | |
| for name, path in zip(dataset.file_names, dataset.image_paths): | |
| source.update(str(name).encode()) | |
| source.update(bytes.fromhex(file_sha256(path))) | |
| source_digest = source.hexdigest() | |
| identity = hashlib.sha256() | |
| identity.update(FEATURE_SCHEMA.encode()) | |
| identity.update(json.dumps(model_digests, sort_keys=True).encode()) | |
| identity.update(source_digest.encode()) | |
| return identity.hexdigest(), model_digests, source_digest | |
| def legacy_features(descriptors: np.ndarray) -> np.ndarray: | |
| """Reconstruct the v800 4096-D representation from richer descriptors.""" | |
| cls_patch = descriptors[:, :, :2].reshape(len(descriptors), 4, -1) | |
| vectors = np.concatenate((cls_patch.mean(axis=1), cls_patch.max(axis=1)), axis=1) | |
| norms = np.linalg.norm(vectors, axis=1, keepdims=True) | |
| return (vectors / np.maximum(norms, 1e-12)).astype(np.float32) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--dataset", type=Path, required=True) | |
| parser.add_argument("--model", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--batch-size", type=int, default=2) | |
| parser.add_argument("--threads", type=int, default=4) | |
| parser.add_argument("--num-shards", type=int, default=1) | |
| parser.add_argument("--shard-index", type=int, default=0) | |
| args = parser.parse_args() | |
| if args.num_shards < 1 or not 0 <= args.shard_index < args.num_shards: | |
| raise ValueError("shard-index must be in [0, num-shards)") | |
| dataset = load_dataset(args.dataset) | |
| torch.set_num_threads(args.threads) | |
| processor = AutoImageProcessor.from_pretrained(args.model, local_files_only=True) | |
| model = AutoModel.from_pretrained(args.model, local_files_only=True).eval() | |
| register_tokens = int(model.config.num_register_tokens) | |
| hidden_size = int(model.config.hidden_size) | |
| started = time.time() | |
| key, model_digests, source_digest = cache_identity(dataset, args.model) | |
| parts_dir = args.output.parent / f"{args.output.stem}.parts.{key[:16]}" | |
| parts_dir.mkdir(parents=True, exist_ok=True) | |
| starts = list(range(0, len(dataset.image_paths), args.batch_size)) | |
| assigned_starts = [ | |
| start for batch_index, start in enumerate(starts) | |
| if batch_index % args.num_shards == args.shard_index | |
| ] | |
| for start in assigned_starts: | |
| paths = dataset.image_paths[start : start + args.batch_size] | |
| end = start + len(paths) | |
| part_path = parts_dir / f"{start:04d}-{end:04d}.npz" | |
| if part_path.is_file(): | |
| with np.load(part_path) as part: | |
| if str(part["cache_key"]) != key: | |
| raise ValueError(f"cached provenance mismatch: {part_path}") | |
| if not np.array_equal(part["file_names"], dataset.file_names[start:end]): | |
| raise ValueError(f"cached filename mismatch: {part_path}") | |
| expected = (len(paths), 4, len(DESCRIPTORS), hidden_size) | |
| if part["view_descriptors"].shape != expected: | |
| raise ValueError(f"cached descriptor shape mismatch: {part_path}") | |
| print(f"DINOv3 v801 {end}/{len(dataset.image_paths)} (cached)", flush=True) | |
| continue | |
| views: list[Image.Image] = [] | |
| for path in paths: | |
| with Image.open(path) as image: | |
| views.extend(four_views(image)) | |
| inputs = processor( | |
| images=views, return_tensors="pt", do_resize=False, do_center_crop=False | |
| ) | |
| with torch.inference_mode(): | |
| hidden = model(**inputs).last_hidden_state | |
| patches = hidden[:, 1 + register_tokens :] | |
| descriptors = torch.stack( | |
| ( | |
| hidden[:, 0], | |
| patches.mean(dim=1), | |
| patches.amax(dim=1), | |
| patches.std(dim=1, unbiased=False), | |
| ), | |
| dim=1, | |
| ).reshape(len(paths), 4, len(DESCRIPTORS), hidden_size) | |
| atomic_savez( | |
| part_path, | |
| cache_key=np.asarray(key), | |
| file_names=dataset.file_names[start:end], | |
| view_descriptors=descriptors.cpu().numpy().astype(np.float32), | |
| ) | |
| print(f"DINOv3 v801 {end}/{len(dataset.image_paths)}", flush=True) | |
| missing = [] | |
| for start in starts: | |
| end = min(start + args.batch_size, len(dataset.image_paths)) | |
| if not (parts_dir / f"{start:04d}-{end:04d}.npz").is_file(): | |
| missing.append(start) | |
| if missing: | |
| print( | |
| f"Shard {args.shard_index}/{args.num_shards} complete; " | |
| f"waiting for {len(missing)} parts.", | |
| flush=True, | |
| ) | |
| return | |
| lock_path = args.output.with_suffix(args.output.suffix + ".lock") | |
| lock_path.parent.mkdir(parents=True, exist_ok=True) | |
| with lock_path.open("w") as lock: | |
| fcntl.flock(lock, fcntl.LOCK_EX) | |
| blocks = [] | |
| for start in starts: | |
| end = min(start + args.batch_size, len(dataset.image_paths)) | |
| part_path = parts_dir / f"{start:04d}-{end:04d}.npz" | |
| with np.load(part_path) as part: | |
| if str(part["cache_key"]) != key: | |
| raise ValueError(f"cached provenance mismatch: {part_path}") | |
| if not np.array_equal(part["file_names"], dataset.file_names[start:end]): | |
| raise ValueError(f"cached filename mismatch: {part_path}") | |
| blocks.append(part["view_descriptors"].astype(np.float32)) | |
| descriptors = np.concatenate(blocks) | |
| expected = (len(dataset.image_paths), 4, len(DESCRIPTORS), hidden_size) | |
| if descriptors.shape != expected or not np.isfinite(descriptors).all(): | |
| raise ValueError(f"unexpected or non-finite descriptor tensor: {descriptors.shape}") | |
| base = legacy_features(descriptors) | |
| atomic_savez( | |
| args.output, | |
| file_names=dataset.file_names, | |
| features=base, | |
| view_descriptors=descriptors, | |
| labels=dataset.labels, | |
| classes=np.asarray(dataset.classes), | |
| cache_key=np.asarray(key), | |
| feature_schema=np.asarray(FEATURE_SCHEMA), | |
| descriptor_names=np.asarray(DESCRIPTORS), | |
| source_digest=np.asarray(source_digest), | |
| model_file_sha256_json=np.asarray(json.dumps(model_digests, sort_keys=True)), | |
| ) | |
| metadata = { | |
| "feature_type": FEATURE_SCHEMA, | |
| "cache_key": key, | |
| "source_digest": source_digest, | |
| "model_path": str(args.model.resolve()), | |
| "model_sha256": model_digests["model.safetensors"], | |
| "model_file_sha256": model_digests, | |
| "samples": len(dataset.image_paths), | |
| "legacy_feature_shape": list(base.shape), | |
| "view_descriptor_shape": list(descriptors.shape), | |
| "descriptor_names": list(DESCRIPTORS), | |
| "hidden_size": hidden_size, | |
| "num_register_tokens": register_tokens, | |
| "patch_size": int(model.config.patch_size), | |
| "batch_size": args.batch_size, | |
| "threads": args.threads, | |
| "num_shards": args.num_shards, | |
| "seconds": round(time.time() - started, 3), | |
| "python": platform.python_version(), | |
| "torch": torch.__version__, | |
| } | |
| atomic_write_json(args.output.with_suffix(".json"), metadata) | |
| print(json.dumps(metadata, ensure_ascii=False, indent=2), flush=True) | |
| if __name__ == "__main__": | |
| main() | |