Machine Learning

Build a Powerful Detector with Anomalib: 2025 Tutorial

Ready to build powerful anomaly detectors in 2025? This step-by-step tutorial guides you through using Anomalib to train and deploy advanced models. Get started!

D

David Chen

Senior Machine Learning Engineer specializing in computer vision and industrial AI solutions.

8 min read16 views

Ever stared at a production line, wondering how you could automatically spot that one tiny, misaligned screw or a subtle crack in a product? Welcome to the world of anomaly detection. In 2025, this isn't just a niche task for PhDs—it's an accessible, powerful tool for developers and engineers, thanks to libraries like Anomalib.

What Exactly is Anomalib?

Anomalib is a deep learning library that packs a serious punch. Developed by Intel and part of the OpenVINO™ toolkit ecosystem, it's designed specifically for benchmarking, developing, and deploying anomaly detection algorithms. Think of it as a comprehensive toolbox filled with state-of-the-art models, ready to be used with just a few lines of code or a simple command.

Its core philosophy is to make unsupervised anomaly detection incredibly straightforward. In most real-world scenarios, you have plenty of examples of what's "normal" but very few (or no) examples of what's "abnormal." Anomalib thrives in this environment. You train it exclusively on normal images, and it learns to identify anything that deviates from that baseline as an anomaly.

Why is Anomalib the Go-To Choice in 2025?

While anomaly detection isn't new, Anomalib's relevance has only grown. Here’s why it's a top contender in 2025:

  • State-of-the-Art Algorithms: It provides ready-to-use implementations of leading research papers like PatchCore, PaDiM, and CFLOW. You don't need to spend weeks implementing them from scratch.
  • Modularity and Flexibility: It's built on PyTorch Lightning, making it highly modular. You can easily swap datasets, models, or even individual components to create custom solutions.
  • Effortless Deployment: With built-in support for OpenVINO export, you can optimize your trained models for high-performance inference on a wide range of hardware, from CPUs to specialized accelerators.
  • Active Community & Development: The library is consistently updated with new models and features, backed by a strong community and Intel's support.

Setting Up Your Anomaly Detection Lab

Getting started is surprisingly simple. Let's get your environment ready in just a few minutes.

Prerequisites

Ensure you have Python 3.8 or newer and pip installed. It's highly recommended to work within a virtual environment to avoid package conflicts.

# Create a virtual environment python -m venv anomalib_env # Activate it # On Windows: .\anomalib_env\Scripts\activate # On macOS/Linux: source anomalib_env/bin/activate 

Installation

Advertisement

With your virtual environment active, you can install Anomalib directly from PyPI:

pip install anomalib[full] 

We use [full] to install all the extra dependencies, including those for the command-line interface (CLI) and logging tools, which we'll be using in this tutorial. That's it! You're ready to build.

Choosing Your Weapon: Which Anomalib Model is Right for You?

Anomalib isn't a one-size-fits-all solution; it’s a collection of specialized tools. Choosing the right model is crucial for getting the best performance. PatchCore is often the best starting point due to its excellent accuracy, but here's a quick comparison to guide your choice:

Model Name Key Strength Ideal Use Case Computational Cost
PatchCore Highest Accuracy & SOTA Performance Complex textures where subtle defects are critical (e.g., fabric, wood grain). High (Inference can be slower)
PaDiM Great Balance of Speed & Accuracy General-purpose industrial inspection where inference time matters. Medium
CFLOW Pixel-Perfect Localization When you need precise segmentation of the anomalous region. High (Training can be long)
FastFlow Extremely Fast Inference Real-time applications on edge devices where speed is the top priority. Low

Your First Anomaly Detector: A Step-by-Step Tutorial

Let's build a detector to spot defects on leather, using the MVTec AD dataset. We'll use Anomalib's powerful command-line interface (CLI) for a code-free experience.

Step 1: The Dataset

Anomalib's CLI can automatically download and prepare certain datasets, including MVTec. This makes getting started a breeze. We'll specify this in our configuration file.

Step 2: Create a Configuration File

Anomalib's behavior is controlled by a YAML configuration file. It's clean, readable, and easy to modify. Create a file named patchcore_leather.yaml and paste the following content into it:

# patchcore_leather.yaml dataset: name: mvtec category: leather format: mvtec path: ./datasets/MVTec task: segmentation train_batch_size: 32 eval_batch_size: 32 num_workers: 8 model: name: patchcore layers: - layer1 - layer2 - layer3 backbone: resnet18 pre_trained: true project: path: ./results seed: 42 logging: logger: [] # Disables all loggers for this example. For production, use 'wandb' or 'tensorboard'. accelerator: auto # auto will select GPU if available, otherwise CPU metrics: image: - F1Score - AUROC pixel: - F1Score - AUROC visualization: show_images: False # Set to True to display images live during testing mode: full 

This file tells Anomalib to:
- Use the 'leather' category from the MVTec dataset.
- Train a patchcore model with a ResNet18 backbone.
- Save the results (trained model, visualizations) into a folder named ./results.

Step 3: Train the Model

Now for the magic. Open your terminal in the same directory as your YAML file and run:

anomalib train --config patchcore_leather.yaml 

Anomalib will now download the dataset (if it's not already there), build the PatchCore model, and start training. You'll see a progress bar as it learns what "normal" leather looks like. On a modern GPU, this will take just a few minutes.

Step 4: Inference and Visualization

Once training is complete, the trained model and results are saved in the ./results/patchcore/mvtec/leather/ directory. Let's test it on some new images. Run the inference command:

anomalib infer --config patchcore_leather.yaml --weight_path ./results/patchcore/mvtec/leather/latest/weights/lightning/model.ckpt --input ./datasets/MVTec/leather/test/poke --output ./inference_results 

This command takes our trained model (specified by --weight_path) and runs it on a folder of defective test images (--input). The visual results will be saved in the ./inference_results folder.

When you open that folder, you'll find images with heatmaps overlaid. The bright, hot-colored areas precisely highlight the detected anomalies—the pokes and cuts that deviate from the normal leather texture. You've just built a state-of-the-art visual anomaly detector without writing a single line of Python!

Key Takeaways & Best Practices

As you move from this tutorial to your own projects, keep these points in mind:

  • Garbage In, Garbage Out: The single most important factor for success is a clean training dataset. Ensure your "normal" samples are truly free of anomalies and representative of your process.
  • Start with PatchCore: When in doubt, start with PatchCore. Its performance is a fantastic baseline. If it's too slow for your application, then explore faster models like PaDiM or FastFlow.
  • Leverage the CLI for Speed: The CLI is perfect for rapid prototyping and benchmarking. You can test multiple models and configurations on your dataset in a single afternoon.
  • Use the API for Integration: For production systems, use the Python API. It gives you fine-grained control to integrate Anomalib's models into your existing applications.

Wrapping Up: Your Journey into Anomaly Detection

You've seen how incredibly accessible and powerful anomaly detection has become. With Anomalib, you've gone from setup to a fully trained, high-performance detector in just a few steps. The days of needing massive, labeled datasets for every little defect are over.

The real power lies in applying this to your own unique problems. Whether it's monitoring manufacturing lines, checking medical scans, or ensuring infrastructure safety, the principles are the same. So take what you've learned, experiment with different models and datasets, and start building intelligent systems that can see the unseen.

Tags

You May Also Like