"""PatchCore-style per-view anomaly scorer (CPU-friendly). Backbone: torchvision resnet18 (ImageNet). Patch features = concat of layer2 (32x32x128) and layer3 (upsampled 16x16x256 -> 32x32), followed by 3x3 neighborhood aggregation, giving 1024 patch embeddings of dim 384 per view. Memory bank per category with random coreset subsampling (2K vectors). Per-view score = max over patches of the (smoothed) kNN distance map. """ from __future__ import annotations import os import random import numpy as np import torch import torch.nn.functional as F import torchvision.models as tvm from PIL import Image from scipy.ndimage import gaussian_filter IMAGENET_MEAN = (0.485, 0.456, 0.406) IMAGENET_STD = (0.229, 0.224, 0.225) GRID = 32 # 256 input -> layer2 32x32 BACKBONES = { "resnet18": (tvm.resnet18, tvm.ResNet18_Weights.IMAGENET1K_V1, 384), "wide_resnet50_2": (tvm.wide_resnet50_2, tvm.Wide_ResNet50_2_Weights.IMAGENET1K_V1, 1536), } def feat_dim(backbone): return BACKBONES[backbone][2] class PatchScorer: def __init__(self, device: str = "cpu", seed: int = 0, backbone: str = "resnet18"): self.device = device self.seed = seed self.backbone = backbone ctor, weights, dim = BACKBONES[backbone] FEAT_DIM = dim self.feat_dim = dim model = ctor(weights=weights) self.backbone_module = model.to(device).eval() self._feats = {} model.layer2.register_forward_hook(self._hook("l2")) model.layer3.register_forward_hook(self._hook("l3")) self.bank = None # np.ndarray [N, dim] float32 self._nn = None def _hook(self, name): def fn(_m, _i, out): self._feats[name] = out return fn def _preprocess(self, imgs): x = torch.stack([torch.from_numpy(np.asarray(im, dtype=np.float32) / 255.0) for im in imgs]) x = x.permute(0, 3, 1, 2) mean = torch.tensor(IMAGENET_MEAN).view(1, 3, 1, 1) std = torch.tensor(IMAGENET_STD).view(1, 3, 1, 1) return (x - mean) / std @torch.no_grad() def patch_embeddings(self, imgs) -> np.ndarray: """imgs: list of PIL/np RGB 256x256 -> [n, 1024, feat_dim] float32.""" x = self._preprocess(imgs).to(self.device) self.backbone_module(x) l2 = self._feats["l2"] # [n,C2,32,32] l3 = F.interpolate(self._feats["l3"], size=(GRID, GRID), mode="bilinear", align_corners=False) f = torch.cat([l2, l3], dim=1) # [n,feat_dim,32,32] f = F.avg_pool2d(f, kernel_size=3, stride=1, padding=1) # neighborhood-aware f = f.permute(0, 2, 3, 1).reshape(-1, self.feat_dim) return f.cpu().numpy().astype(np.float32) # ---------- memory bank ---------- def build_bank(self, image_paths, patches_per_image=120, bank_size=4096, batch=16, log=print): rng = random.Random(self.seed) pool = [] for i in range(0, len(image_paths), batch): imgs = [Image.open(p).convert("RGB") for p in image_paths[i:i + batch]] feats = self.patch_embeddings(imgs) # [b*1024, 384] n_img = len(imgs) f = feats.reshape(n_img, GRID * GRID, FEAT_DIM) for j in range(n_img): idx = rng.sample(range(GRID * GRID), patches_per_image) pool.append(f[j, idx]) if len(pool) * patches_per_image > 300_000: keep = rng.sample(range(len(pool)), 150_000 // patches_per_image) pool = [pool[k] for k in keep] pool = np.concatenate(pool, axis=0) if len(pool) > bank_size: sel = rng.sample(range(len(pool)), bank_size) pool = pool[sel] self.bank = pool.astype(np.float32) def score_images(self, image_paths, batch=16): """Return (scores [n], maps [n, GRID, GRID] float16) for image paths.""" imgs = [Image.open(p).convert("RGB") for p in image_paths] return self.score_arrays(imgs, batch=batch) def score_arrays(self, imgs, batch=16): """imgs: list of HxWx3 np.uint8 arrays. Returns (scores, maps).""" from sklearn.neighbors import NearestNeighbors if self._nn is None: self._nn = NearestNeighbors(n_neighbors=1, algorithm="brute", n_jobs=2) self._nn.fit(self.bank) scores, maps = [], [] for i in range(0, len(imgs), batch): feats = self.patch_embeddings(imgs[i:i + batch]) n_img = len(imgs[i:i + batch]) dist, _ = self._nn.kneighbors(feats, return_distance=True) d = dist[:, 0].reshape(n_img, GRID, GRID).astype(np.float32) for j in range(n_img): sm = gaussian_filter(d[j], sigma=2.0) maps.append(sm.astype(np.float16)) scores.append(float(sm.max())) return np.array(scores, dtype=np.float32), np.stack(maps) def save(self, path): os.makedirs(os.path.dirname(path), exist_ok=True) np.save(path, self.bank) def load(self, path): self.bank = np.load(path) self._nn = None