Logical anomaly detection asks whether an image breaks a rule about its arrangement, such as the wrong number of pushpins in a box, a missing label, or a part in the wrong place. In these cases every small patch of the image can look perfectly normal. The best unsupervised detectors for this problem squeeze the whole image into a compact global summary and learn what normal summaries look like. That squeezing is also their weak point. When the summary is built by averaging over space, a component covering one percent of the image moves the summary by roughly one percent. A missing small part therefore barely registers, and the problem gets worse as camera resolution drops or downsampling shrinks the part. The authors of Contextual Affinity Distillation for Image Anomaly Detection name this directly as their open limitation: their global branch is insensitive to very small-scale logical anomalies.
We propose to replace area-weighted compression with count-preserving compression. A small network predicts an importance map, and during training it is pushed so that each component's importance integrates to about one, whether the component covers four pixels or four thousand. The global summary then becomes a sum of roughly one descriptor per component. Removing or adding a tiny part changes it about as much as removing or adding a large one. Component masks are needed only to supervise the importance map during training; at test time no detector or segmenter runs. The method borrows the idea of learnable importance-weighted pooling from multi-scale detection and the density-map view of counting. It applies both to the compression step of unsupervised logical anomaly models, a setting where neither has been tried.
Core Claim
Let $f_i$ be the backbone feature at spatial location $i$, and let $w_i \ge 0$ be the predicted importance at $i$. We form the global code as an unnormalized weighted sum rather than a mean:
$$z = \sum_i w_i f_i .$$
During training only, we take pseudo-component masks $\{M_c\}$, where $M_c$ is the set of locations belonging to component $c$, from an off-the-shelf segmenter, as SALAD already does with SAM-HQ and DINO pseudo-labels. We then add a mass loss:
$$\mathcal{L}_{\text{mass}} = \sum_c \Big(\sum_{i \in M_c} w_i - 1\Big)^2 + \lambda \sum_{i \notin \cup_c M_c} w_i^2 ,$$
where the first term gives each component unit total importance and the second, weighted by $\lambda$, suppresses background. Write $S(a)$ for the expected change in $z$ when a component of area $a$ is removed. Under mean pooling, $S(a)$ grows in proportion to $a$, because each location contributes $1/A$ of the code for an image of area $A$. Under the mass loss, $S(a)$ should be roughly flat, because each component contributes one unit of weight regardless of its size.
The claim is testable without any downstream detector. If it holds, logical anomalies involving the smallest components should gain the most detection accuracy, and that gain should grow as input resolution is lowered.
Two design choices follow from the multi-scale detection literature. First, Finding Tiny Faces shows that objects only a few pixels wide carry no usable internal texture and must be recognized from their surroundings. The importance predictor is therefore given a wide, dilated receptive field rather than a pointwise projection. Second, the weights are computed on a stride-8 feature map rather than the coarsest one, so that a tiny part has not already vanished before importance is assigned. LIP: Local Importance-Based Pooling supplies the template for the importance network and its stabilizers. LIP learns importance maps before downsampling and raised small-object AP on COCO for Faster R-CNN from 21.9% to 24.0%. But LIP normalizes weights locally and is trained by a supervised classification or detection loss. We instead normalize per component and train with an explicit counting constraint, inside a model that never sees anomaly labels.
Research Program
The module is a drop-in replacement for the global compression step of existing two-branch logical anomaly detectors. These models pair a local branch for scratches and dents with a global branch for arrangement. We will insert it into three hosts.
The first host is the Global Context Condensing Block of DSKD (Contextual Affinity Distillation for Image Anomaly Detection). That block compresses features into a one-dimensional bottleneck and trains a global student with a contextual affinity loss. The second host is the global autoencoder of ULSAD (Revisiting Deep Feature Reconstruction for Logical and Structural Industrial Anomaly Detection), which matches self-attention maps between original and reconstructed features from two mid-level ResNet layers. The third host is the global branch of SALAD (SALAD -- Semantics-Aware Logical Anomaly Detection), currently the strongest reported system on the benchmark.
For the two hosts that operate on token sets, we keep a small set of count-preserving tokens instead of one vector. Each token is a weighted sum under a soft assignment of locations, so that relations between components remain representable. These are exactly the "signposts" DSKD models. The training objective is the host's own objective plus $\mathcal{L}_{\text{mass}}$. No synthetic anomalies are required. Optionally, we can add a feature-space augmentation that deletes or duplicates masked components, to check whether explicit sensitivity training adds anything on top of the mass constraint.
The work involves three analyses beyond accuracy. The first is to measure $S(a)$ empirically: we delete components from normal test images at the feature level and plot the change in the global code against component area, for mean pooling, LIP-style pooling, and our pooling. The second is to stratify every logical anomaly in MVTec LOCO AD by the ground-truth mask area of the defect region. We split them into terciles by footprint, so that headline AUROC cannot hide behavior on tiny parts. The third is to sweep input resolution downward, turning spatial resolution degradation into a controlled variable rather than an uncontrolled nuisance.
Baselines
The naive reference is each host model unchanged, at its published input resolution and with its original mean or condensing compression. This tells us whether the module helps at all.
The strongest existing method is SALAD, which reports a mean AUROC of 96.1% on MVTec LOCO AD (SALAD). It counts implicitly through a composition branch built on segmentation pseudo-labels, and it is the system a reviewer would expect us to beat. Because that figure is close to the ceiling, we do not expect to raise the overall mean much. Beating SALAD will mean improving logical-anomaly AUROC on the smallest-footprint tercile and under reduced input resolution, by more than the spread across five seeds, while the overall mean stays within seed noise.
For the lighter hosts, the reported reference points are as follows. DSKD reports mean AUROC 0.840, logical-only AUROC 0.812, and sPRO 0.730 (Contextual Affinity Distillation for Image Anomaly Detection). ULSAD reports 84.1% image AUROC at 33.42 images per second and 2.17 GB on an A100 (Revisiting Deep Feature Reconstruction for Logical and Structural Industrial Anomaly Detection). We will rerun all hosts ourselves with identical backbones, input resolution, training epochs, and an equal hyperparameter search budget. We compare against our reruns and cite the published numbers only to confirm the reruns are faithful.
The object-centric competitor is ObjectCore (ObjectCore), which reports 80.8% image AUROC in the 4-shot setting on LOCO and 96.5% on CAD-SD. It finds parts explicitly with a finetuned GroundingDINO and SAM, then matches them to a memory bank with the Hungarian algorithm. We run it in its few-shot protocol, and also with full training data where feasible. We report its accuracy per footprint tercile together with its latency.
For cross-scale distillation, we adapt the multi-branch idea of Multi-Scale Cross Distillation for Object Detection in Aerial Images. That work trains shared-weight branches at 0.5×, 1.0×, and 1.5× input and distills into the 1.0× branch, improving RoI Transformer by 6.50 mAP on DOTA-v1.0. In our adaptation, a 2× resolution copy of the host's global branch distills its code into the 1× branch.
Two ablations isolate the key ingredient. In the first, we keep the importance network but drop $\mathcal{L}_{\text{mass}}$, so the network is trained only by the host objective; this is essentially LIP transplanted. In the second, we keep the mass loss but compute weights on the stride-32 map instead of stride 8. Simple sum pooling and generalized-mean pooling serve as parameter-free controls. All variants are evaluated at matched throughput.
Why not just…?
Why not just feed the existing model higher-resolution images, or test at several scales? This is the most serious alternative, and we treat it as one. If a 2× input fixes tiny-part logical anomalies for an affordable cost, the module has little purpose. The evidence against it is partly about cost: multi-scale testing raised latency up to sevenfold in Multi-Scale Cross Distillation for Object Detection in Aerial Images, and inspection lines run at tens of images per second. The other part is structural. Higher resolution makes the tiny part larger in pixels but not larger as a fraction of the image, so mean pooling still weights it by area. We will settle the question with accuracy-versus-throughput curves: each host at 1×, 1.5×, and 2× input against our module at 1×. The proposal is justified only if our 1× point dominates the curve on the smallest-footprint tercile.
Why not just detect and count parts explicitly, as ObjectCore, LogiCode, or SALAD's composition branch do? Explicit counting is exactly right when the detector sees the parts. The file documents where it does not. ObjectCore depends on GroundingDINO pseudo-detections that fail on tiny or indistinct parts and pays a Hungarian matching cost (ObjectCore). LogiCode reports 98.9% accuracy on LOCO, but it is bounded by its counting and locating APIs and has a first-pass code success rate near 60%. Our method still uses a segmenter, but only to supervise training. If the segmenter misses some tiny parts in some training images, the importance network can still generalize from the parts it did see. At test time no detector runs at all. The footprint-stratified comparison against ObjectCore and SALAD, combined with the resolution sweep where detector recall should fall first, will show whether learning to count beats calling a counter.
Why not just use sum, max, or generalized-mean pooling, or plain LIP? Max pooling cannot tell two pins from three. Sum pooling counts only if background features happen to be near zero, which nothing enforces. Plain LIP learns importance from whatever loss it is given, and an unsupervised reconstruction or distillation loss gives it no reason to favor tiny parts. These are cheap controls rather than arguments. If unconstrained learned importance matches our results, the mass loss is unnecessary and we will report that.
Why not just distill from a high-resolution branch? Cross-scale distillation is a plausible fix and may be complementary, so we run it both as a baseline and combined with our module. Distillation passes along whatever the high-resolution teacher's mean-pooled code encodes, and that code is still area-weighted. We therefore predict that distillation helps under resolution degradation but not on small footprints at native resolution. That split is the observation that would separate the two mechanisms.
Proposed Experiments
The main benchmark is MVTec LOCO AD, reporting image AUROC split into logical and structural anomalies and sPRO for localization. The logical anomalies are additionally stratified into footprint terciles from ground-truth mask area. We run at native resolution and at progressively downsampled inputs, with short side halved and then quartered. VisA, whose multi-instance categories such as capsules and macaroni stress counting, and MVTec AD serve as checks that structural detection is not harmed. CAD-SD tests the few-shot regime against ObjectCore.
Throughput and GPU memory are reported for every configuration on the same hardware, alongside ULSAD's published 33.42 images per second as a sanity anchor. Every result is the mean over five seeds with the spread reported.
The decisive checks are three. First, the measured sensitivity curve $S(a)$ must be visibly flatter under the mass loss than under mean pooling or plain LIP; if it is not, the mechanism is wrong regardless of accuracy. Second, the accuracy gain must concentrate in the smallest-footprint tercile and grow as resolution falls. A uniform gain across terciles would suggest generic regularization rather than count preservation. Third, at matched throughput, the 1× module must beat the 2× input baseline and the cross-scale distillation baseline on small footprints, without lowering structural AUROC.
If the first check passes but the third fails, the honest conclusion is that higher resolution is the cheaper fix, and we will say so.
References
- Contextual Affinity Distillation for Image Anomaly Detection
- SALAD -- Semantics-Aware Logical Anomaly Detection
- Finding Tiny Faces
- LIP: Local Importance-Based Pooling
- Revisiting Deep Feature Reconstruction for Logical and Structural Industrial Anomaly Detection
- ObjectCore - Efficient Few-shot Logical Anomaly Detection using Object Representations
- Multi-Scale Cross Distillation for Object Detection in Aerial Images
- LogiCode: An LLM-Driven Framework for Logical Anomaly Detection