DOI: 10.3390/electronics15215109 ISSN: 2079-9292

PC-Reg: Modeling Spatial Dependencies for Logical Anomaly Detection

Sergio Villanueva López, Emilio Soria-Olivas, Manuel Sánchez-Montañés

In industrial visual inspection, individual components may look normal even when their arrangement or quantity is incorrect. For example, a cable may be plugged into the wrong slot of a connector, although every part in the image is intact. These logical anomalies are difficult for methods such as PatchCore, which score image regions independently, while the vision-language pipelines that can reason about them are slow and costly to deploy on a production line. In this work, we present Positional Contextual Regression (PC-Reg), which is a method that learns from defect-free images how each region relates to its surroundings, without gradient-based training. A frozen vision backbone provides the patch features, and a closed-form ridge regression at each grid position predicts every patch feature from its neighbors; the Mahalanobis distance of the prediction residual, calibrated per position, measures how far a region departs from its expected context. The attention of the backbone weights the neighbors, and a complementary score captures the content of the four image quadrants. With one fixed configuration across 32 categories of MVTec LOCO AD, MVTec AD, and VisA, the combined method, PC-RegDual, reaches 85.7%, 98.8%, and 92.6% AUROC (87.5%, 99.0%, and 93.5% with a larger backbone). On the same features, it outperforms a nearest-neighbor baseline that follows PatchCore on MVTec LOCO AD by 5.3 percentage points overall and by 14.6 percentage points on logical anomalies, and it outperforms PaDiM and a mean-subtraction baseline on all three benchmarks. Diagnostic feature-space tests show that PC-Reg responds to broken spatial relationships that independent patch scoring misses, and an evaluation on production-line images (BTAD) gives results comparable to the best baselines. On a consumer GPU, fitting takes about 5 s per category (72 s on one CPU core) and inference 55 ms per image, with deterministic output and no external services, so new products can be set up in minutes on local hardware.

More from our Archive