File size: 8,041 Bytes
ddd3b6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""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):
    pass


def create_detector(settings):
    marker=settings.root/'models/highres/PROFILE.json'
    mode=settings.profile
    if mode not in ('auto','legacy','dinov2_highres_v1'):raise ModelUnavailable('Unknown detector profile')
    if mode=='legacy' or (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):
    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:
    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):
        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):
    # 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):
    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 * .01))):].mean())
    return raw, score

def display_map(raw, image, scale):
    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 * .60 + colors * .40) * 255
    return Image.fromarray(overlay.astype('uint8'))

class Detector:
    profile_name='legacy'
    def __init__(self, settings):
        self.settings = settings
        self.encoder: Encoder | None = None
        self.banks = {}
        self.lock = threading.Lock()

    def cards(self):
        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):
        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):
        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'])