The 2025 Anomalib Guide: 7 Secrets for Top Performance
Unlock peak performance in your anomaly detection projects. Discover 7 expert secrets for mastering Anomalib in 2025, from custom features to model distillation.
Dr. Alistair Finch
Principal AI Scientist specializing in industrial computer vision and deep learning.
Anomalib is an incredible library, a true powerhouse for anyone working on visual anomaly detection. But if you're still relying on the default configurations, you're likely leaving a significant amount of performance on the table. To truly excel and push your models from "good" to "state-of-the-art," you need to dig a little deeper.
That's what this guide is all about. We're moving beyond the basics to uncover the seven secrets that separate the beginners from the pros. Let's get your models to top performance in 2025.
Secret 1: Go Beyond Default Hyperparameter Tuning
Running Anomalib's built-in hyperparameter optimization (HPO) is a great first step, but the default search spaces are often too broad or conservative. True mastery lies in defining intelligent search spaces based on the model you're using. Instead of letting it search blindly, guide it.
Integrate tools like Optuna or Ray Tune directly for more control. For a model like PatchCore
, don't just tune the backbone. Focus on what makes it unique:
- Co-reset Subsampling Percentage: The
coreset_sampling_ratio
is critical. Instead of the default0.1
, try a targeted search between0.01
and0.2
. A smaller ratio can speed things up, but a larger one might capture more variance. - Number of Neighbors: The
n_neighbors
parameter for the K-Nearest Neighbors search is vital for scoring. The default is 9, but the optimal value is highly dataset-dependent. A search space like[3, 5, 7, 9, 11]
is a good starting point.
Pro Tip: Use Optuna's pruners to automatically stop unpromising trials early. This can reduce your HPO time from days to hours, allowing for more extensive and refined searches.
Secret 2: Unleash Power with Custom Feature Extractors
Most Anomalib models default to a backbone pre-trained on ImageNet (e.g., ResNet, Wide-ResNet). This is a fantastic general-purpose feature extractor, but it's not a silver bullet. If your data is highly specialized—like medical imagery, metallic surfaces, or textiles—ImageNet features might miss subtle, domain-specific patterns.
The Domain Shift Problem
ImageNet contains photos of cats, dogs, and cars. Your factory floor has images of brushed aluminum with microscopic scratches. The features that define a "cat" are very different from those that define a "flawless surface."
The secret is to use a custom feature extractor. You have two main options:
- Self-Supervised Pre-training: Train a backbone on your own large, unlabeled dataset of normal images using a self-supervised method like SimCLR or DINO. This teaches the model what "normal" looks like in your specific domain.
- Use a Domain-Specific Pre-trained Model: If available, use a model already pre-trained on a similar domain. For example, a backbone trained on satellite imagery will be better for aerial inspection tasks.
In Anomalib, you can specify a different backbone easily in the model.backbone
config. The performance gain from a domain-aligned feature extractor can be the single biggest improvement you make.
Secret 3: Master Multi-Scale Features for Robustness
Anomalies aren't one-size-fits-all. A tiny discoloration is a low-level texture anomaly, while a missing component is a high-level structural anomaly. To detect both, your model needs to "see" at multiple scales.
Models like PaDiM and PatchCore support using features from multiple layers of the backbone network. Early layers capture fine-grained details (color, texture), while deeper layers capture more abstract, semantic information (shapes, object parts).
In your config.yaml
, you can specify which layers to use:
model:
layers: ["layer1", "layer2", "layer3"]
By combining features from different layers, you create a richer, multi-scale representation of normalcy. This makes your model far more robust to different types of defects. Experimenting with different layer combinations is key to finding the sweet spot for your specific problem.
Choosing the Right Model: A Quick Comparison
With multiple models available, it can be tough to choose. Here’s a quick-glance table to help you decide based on your priorities.
Model | Key Strength | Inference Speed | Memory Usage | Best For... |
---|---|---|---|---|
PatchCore | Highest Accuracy (SOTA) | Slow to Moderate | High (stores memory bank) | Offline analysis or when top accuracy is non-negotiable. |
PaDiM | Good Balance | Moderate | Moderate | A great all-rounder for many industrial applications. |
CFA | Good Segmentation | Fast | Low | Situations needing precise defect localization without a huge memory footprint. |
FastFlow | Extremely Fast | Very Fast | Low | Real-time applications on edge devices where latency is critical. |
Secret 4: Get Creative with Synthetic Anomaly Generation
Most anomaly detection is a one-class problem: you only train on normal data. But what if you could teach the model what an anomaly might look like without needing real defective samples? Enter synthetic anomaly generation.
Anomalib has powerful tools for this, most notably the CutPaste augmentation. Instead of just rotating or flipping images, you can:
- Cut a random patch from one normal image and paste it onto another.
- Apply aggressive, unrealistic augmentations (e.g., solarization, extreme color jitter) to a patch and paste it back.
This forces the model to learn a tighter, more precise boundary of what is "normal." It becomes sensitive not just to foreign objects but also to textural and structural inconsistencies. This is a powerful way to boost performance when you have very few or no real anomaly examples.
Secret 5: Fine-Tune for Pixel-Perfect Segmentation
Getting a high image-level AUROC score is great, but in the real world, you often need to know where the anomaly is. This means you need a clean, accurate pixel-level segmentation mask.
Many users overlook the settings that directly control the quality of the anomaly map. Here are two to focus on:
- Normalization Method: In the
normalization
section of your config, you can choose betweenmin_max
andcdf
. Whilemin_max
is simple,cdf
(Cumulative Distribution Function) often produces much cleaner anomaly maps with better contrast, as it re-distributes the anomaly scores based on their statistical distribution. This can be the difference between a noisy, unusable map and a crisp, actionable one. - Gaussian Blur: A small amount of Gaussian blur (
filter_size
,sigma
) applied to the anomaly map can smooth out high-frequency noise and connect disparate anomalous regions, leading to a more coherent final mask.
Don't just chase the image-level score. Spend time visualizing and tuning the parameters that affect the pixel-level output. That's what delivers real-world value.
Secret 6: Distill Models for Real-World Edge Deployment
You've trained a massive, high-performing PatchCore model that gets 99.8% AUROC. Fantastic! But it's too slow and memory-hungry to run on your factory's edge device. Is it all for nothing?
No. The secret is knowledge distillation. Anomalib supports this out of the box. The process involves:
- Training a Teacher: Train your best, largest model (e.g., a Wide-ResNet-101 PatchCore). This is your "teacher."
- Training a Student: Choose a much lighter model (e.g., a ResNet-18 PaDiM or a smaller PatchCore). This is your "student."
- Distillation: Train the student model not just on the data, but also to mimic the feature representations of the teacher model. The student learns to be as "smart" as the teacher, but in a much more compact form.
The result is a small, fast model that retains a huge portion of the teacher's accuracy. You get the best of both worlds: high performance and deployment feasibility.
Secret 7: The Final 10% - Nail Your Post-Processing
Your model outputs a raw anomaly map, which is just a grayscale image of scores. The final step of turning this into a binary decision (anomaly or not) is post-processing, and it's critically important.
Thresholding and Morphology
Anomalib's threshold
settings are your best friend here.
- Adaptive vs. Manual Thresholding: A manual, global threshold is simple but brittle. If lighting conditions change, it can fail. Adaptive thresholding (
method: adaptive
in the config) is far more robust as it calculates a threshold based on the image's own content. This should be your default choice. - Morphological Operations: After thresholding, you might have a noisy mask with small, isolated "hot" pixels. Use morphological operations like opening (erosion followed by dilation) to remove this noise and closing (dilation followed by erosion) to fill small holes in larger detected regions. This can dramatically improve the reliability of your system's final output.
Key Takeaways for Top Performance
To elevate your Anomalib projects, remember to:
- Tune Smart: Use targeted HPO search spaces for specific model parameters.
- Specialize Your Features: Swap ImageNet backbones for custom or self-supervised ones.
- See at All Scales: Combine features from multiple layers to detect diverse anomaly types.
- Synthesize Defects: Use CutPaste to create more robust one-class models.
- Focus on the Mask: Tune normalization and blurring for better pixel-level segmentation.
- Distill for the Edge: Use knowledge distillation to create fast, deployable models.
- Clean Up the Output: Master adaptive thresholding and morphological operations.
By incorporating these seven secrets into your workflow, you'll move from being a user of Anomalib to a master, capable of building robust, high-performance visual inspection systems for any challenge.