DefectRAG / backend /vision.py
Wizzas's picture
Upload source code
2274b3a verified
Raw History Blame
12.1 kB
"""Frozen DINOv3 patch features + normal-only fitted nearest-neighbour banks.
This is a compact fallback detector, NOT Dinomaly and NOT a defect classifier.
"""
from pathlib import Path
import hashlib
import json
import time
import threading
import numpy as np
from PIL import Image, ImageFilter
import torch
import torch.nn.functional as F
from transformers import AutoModel, AutoImageProcessor
from .config import CATEGORIES
class ModelUnavailable(RuntimeError):
"""A model, memory bank or checkpoint is missing, does not match its checksum or cannot load."""
pass
def create_detector(settings):
"""Choose the detector for the configured profile.
"legacy" always uses the original DINOv3 detector. "dinov2_highres_v1" is the DINOv2
detector with the frozen encoder and random-sample banks. "dinov2_tuned_v1" is the DINOv2
detector with the fine-tuned encoder and coreset banks. "auto" uses the tuned profile when
models/highres_tuned/PROFILE.json exists and says "active", otherwise the high-resolution
profile when models/highres/PROFILE.json exists, otherwise legacy. Asking explicitly for a
profile that is not installed or not validated raises ModelUnavailable.
"""
mode = settings.profile
if mode not in ("auto", "legacy", "dinov2_highres_v1", "dinov2_tuned_v1"):
raise ModelUnavailable("Unknown detector profile")
if mode == "legacy":
return Detector(settings)
tuned_marker = settings.root / "models/highres_tuned/PROFILE.json"
tuned_active = tuned_marker.exists() and json.loads(tuned_marker.read_text()).get("status") == "active"
if mode == "dinov2_tuned_v1" or (mode == "auto" and tuned_active):
if not tuned_marker.exists():
raise ModelUnavailable("Requested fine-tuned profile is not installed")
if not tuned_active:
raise ModelUnavailable("Fine-tuned profile is not validated for activation")
from .highres import TunedDetector
return TunedDetector(settings)
marker = settings.root / "models/highres/PROFILE.json"
if mode == "auto" and not marker.exists():
return Detector(settings)
if not marker.exists():
raise ModelUnavailable("Requested high-resolution profile is not installed")
manifest = json.loads(marker.read_text())
if manifest.get("status") != "active":
raise ModelUnavailable("High-resolution profile is not validated for activation")
from .highres import HighResolutionDetector
return HighResolutionDetector(settings)
def sha256(path):
"""Return the SHA-256 hex digest of a file, read in 1 MiB blocks."""
digest = hashlib.sha256()
with open(path, "rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
class Encoder:
"""The frozen pretrained image encoder. Its weights are never trained or changed here.
Runs on the GPU in half precision when one is available, otherwise on the CPU.
"""
def __init__(self, source, device="auto"):
self.device = torch.device(("cuda" if torch.cuda.is_available() else "cpu") if device == "auto" else device)
torch.set_num_threads(min(4, torch.get_num_threads()))
self.processor = AutoImageProcessor.from_pretrained(str(source), local_files_only=True)
self.model = AutoModel.from_pretrained(str(source), local_files_only=True).eval().to(self.device)
self.model.requires_grad_(False)
if self.device.type == "cuda":
self.model.half()
self.dtype = next(self.model.parameters()).dtype
@torch.inference_mode()
def patches(self, image):
"""Turn an image into one unit-length feature vector per image patch.
Returns the patch features and the side length of the square patch grid. The class and
register tokens are skipped. Raises ModelUnavailable if the grid is not square.
"""
inputs = self.processor(images=image.convert("RGB"), return_tensors="pt")
inputs = {
k: v.to(self.device, dtype=self.dtype) if v.is_floating_point() else v.to(self.device)
for k, v in inputs.items()
}
output = self.model(**inputs).last_hidden_state
skip = 1 + self.model.config.num_register_tokens
patches = F.normalize(output[:, skip:].float(), dim=-1)[0]
side = int(patches.shape[0] ** 0.5)
if side * side != patches.shape[0]:
raise ModelUnavailable("Encoder returned a non-square spatial grid")
return patches, side
def distances(patches, bank):
"""For each patch, return its distance to the closest normal patch in the memory bank.
Features are unit length, so the largest dot product is the highest cosine similarity and
the distance is 1 minus it. The bank is processed in chunks to bound memory use.
"""
# Chunk bank to keep peak allocations bounded on CPU and GPU.
best = torch.full((len(patches),), -1.0, device=patches.device)
for chunk in bank.split(2048):
best = torch.maximum(best, (patches @ chunk.T).amax(dim=1))
return (1 - best).clamp_min(0)
def map_and_score(encoder, bank, image):
"""Compute the patch-distance map and the image-level anomaly score.
The score is the mean of the largest 1% of patch distances (at least one patch). Returns
the distance grid and the score.
"""
patches, side = encoder.patches(image)
raw = distances(patches, bank).reshape(side, side).cpu().numpy()
# Fixed before validation: mean of top 1% spatial patch distances.
score = float(np.sort(raw.ravel())[-max(1, int(np.ceil(raw.size * 0.01))) :].mean())
return raw, score
def display_map(raw, image, scale):
"""Draw the distance grid as a colour heatmap blended over the image (60% image, 40% heatmap).
"scale" is the distance value shown as the hottest colour. Returns a new image.
"""
intensity = Image.fromarray(np.clip(raw / max(scale, 1e-8) * 255, 0, 255).astype("uint8"))
intensity = intensity.resize(image.size, Image.Resampling.BILINEAR).filter(
ImageFilter.GaussianBlur(max(image.size) / 160)
)
t = np.asarray(intensity).astype("float32") / 255
colors = np.stack([np.clip(2 * t, 0, 1), np.clip(2 - abs(4 * t - 2), 0, 1), np.clip(2 - 2 * t, 0, 1)], axis=-1)
base = np.asarray(image.convert("RGB")).astype("float32") / 255
overlay = (base * 0.60 + colors * 0.40) * 255
return Image.fromarray(overlay.astype("uint8"))
class Detector:
"""The original (legacy) detector: frozen DINOv3 features compared with a per-category bank.
Banks are loaded on first use and cached. A lock lets only one prediction run at a time.
"""
profile_name = "legacy"
def __init__(self, settings):
self.settings = settings
self.encoder: Encoder | None = None
self.banks = {}
self.lock = threading.Lock()
def cards(self):
"""Return the saved model card of every category, or a stub marked unavailable if missing."""
cards = []
for category in CATEGORIES:
path = self.settings.root / "models" / f"{category}.json"
cards.append(json.loads(path.read_text()) if path.exists() else {"category": category, "available": False})
return cards
def load(self, category):
"""Return the model card and normal-patch bank for a category, loading them if needed.
Checks that the encoder and the bank match the checksums recorded in the card. Raises
ModelUnavailable if a file is missing or a checksum differs.
"""
if category not in CATEGORIES:
raise ValueError("Unsupported product category")
card_path = self.settings.root / "models" / f"{category}.json"
bank_path = self.settings.root / "models" / f"{category}.npz"
if not card_path.exists() or not bank_path.exists():
raise ModelUnavailable("No calibrated model for this category. Run the training pipeline first.")
card = json.loads(card_path.read_text())
if self.encoder is None:
try:
self.encoder = Encoder(self.settings.encoder, self.settings.device)
except Exception as exc:
raise ModelUnavailable(
"DINOv3 encoder could not load. Configure DEFECTRAG_ENCODER with an authorized local checkpoint."
) from exc
source = Path(self.settings.encoder)
if not source.is_dir():
from huggingface_hub import snapshot_download
source = Path(snapshot_download(self.settings.encoder, local_files_only=True))
fingerprint = sha256(source / "model.safetensors")
if fingerprint != card["encoder_sha256"]:
self.encoder = None
raise ModelUnavailable("Encoder checksum differs from the calibrated model")
encoder = self.encoder
if encoder is None:
raise ModelUnavailable("DINOv3 encoder did not initialize")
if category not in self.banks:
if sha256(bank_path) != card["bank_sha256"]:
raise ModelUnavailable("Normal memory bank checksum mismatch")
bank = np.load(bank_path, allow_pickle=False)["patches"].astype("float32")
self.banks[category] = F.normalize(torch.from_numpy(bank).to(encoder.device), dim=-1)
return card, self.banks[category]
def predict(self, image, category):
"""Score one image. Returns the result record and the heatmap image.
Verdict: "Uncertain" if the score is within the card's uncertainty margin of the
threshold, otherwise "Anomalous" if the score reaches the threshold, otherwise "Good".
"""
with self.lock:
start = time.perf_counter()
card, bank = self.load(category)
encoder = self.encoder
if encoder is None:
raise ModelUnavailable("DINOv3 encoder did not initialize")
if encoder.device.type == "cuda":
torch.cuda.reset_peak_memory_stats()
inference_start = time.perf_counter()
raw, score = map_and_score(encoder, bank, image)
if encoder.device.type == "cuda":
torch.cuda.synchronize()
elapsed = (time.perf_counter() - inference_start) * 1000
threshold, margin = card["threshold"], card["uncertainty_margin"]
verdict = (
"Uncertain" if abs(score - threshold) <= margin else ("Anomalous" if score >= threshold else "Good")
)
result = {
"category": category,
"verdict": verdict,
"score": score,
"threshold": threshold,
"uncertainty_margin": margin,
"defect_category": None,
"model_id": card["model_id"],
"inference_ms": round(elapsed, 2),
"total_model_ms": round((time.perf_counter() - start) * 1000, 2),
"peak_gpu_mb": round(torch.cuda.max_memory_allocated() / 1024**2, 2)
if encoder.device.type == "cuda"
else None,
"device": str(encoder.device),
"localization": "14 x 14 patch distances, interpolated; not a defect segmentation classifier",
"scope": "VisA benchmark appearance anomaly screening; category is user-selected and not verified by this model.",
"calibration": card["calibration"],
"model_sha256": card["bank_sha256"],
}
preview = image.copy()
preview.thumbnail((1000, 1000))
return result, display_map(raw, preview, card["heatmap_scale"])