Visual Anomaly Detection
Module: Computer Vision | Difficulty: Intermediate
Anomaly Score
where is a pretrained feature extractor and are normal reference features.
PatchCore
Uses coreset subsampling of nearest-neighbor features.
Feature Embedding Distance
Student-Teacher Distillation
import torch
import torch.nn as nn
import torchvision.models as models
class PatchCore:
def __init__(self, backbone='resnet18', num_features=256):
model = models.__dict__[backbone](pretrained=True)
self.feature_extractor = nn.Sequential(*list(model.children())[:-2]).eval()
self.memory_bank = []
def fit(self, normal_loader):
for images in normal_loader:
with torch.no_grad():
feats = self.feature_extractor(images)
B, C, H, W = feats.shape
feats = feats.permute(0, 2, 3, 1).reshape(-1, C)
self.memory_bank.append(feats)
self.memory_bank = torch.cat(self.memory_bank)
def score(self, images):
with torch.no_grad():
feats = self.feature_extractor(images)
B, C, H, W = feats.shape
feats = feats.permute(0, 2, 3, 1).reshape(-1, C)
dists = torch.cdist(feats, self.memory_bank)
min_dists, _ = dists.min(dim=1)
scores = min_dists.reshape(B, H, W)
return scores
Key Takeaways
- Unsupervised anomaly detection requires only normal training data
- PatchCore achieves strong performance with nearest-neighbor search
- Industrial inspection demands low false-positive rates