Computer Vision

Get Started with Anomalib: 5-Step Setup Guide (2025)

Ready to master industrial anomaly detection? Our 2025 guide walks you through setting up Anomalib in 5 easy steps, from installation to your first training.

D

Dr. Adrian Vance

A Computer Vision researcher specializing in industrial automation and deep learning applications.

7 min read23 views

In the world of manufacturing and quality control, spotting a tiny defect can be like finding a needle in a haystack. Traditional methods often fail or require massive, meticulously labeled datasets of every possible flaw. What if you could teach a machine what a perfect product looks like and have it flag anything that deviates, even flaws it has never seen before? This is the power of unsupervised anomaly detection, and Anomalib is your key to unlocking it.

Developed by Intel, Anomalib is a deep learning library that has rapidly become the go-to toolkit for anomaly detection tasks. It offers a comprehensive collection of state-of-the-art algorithms, a standardized benchmarking framework, and an intuitive interface that makes it accessible to both researchers and practitioners. Whether you're inspecting circuit boards, textiles, or medical scans, Anomalib provides the tools to build a robust visual inspection system.

This guide will cut through the complexity and provide a clear, five-step path to get you up and running with Anomalib in 2025. We'll go from a blank slate to training your first model and interpreting the results. Let's get started!

Step 1: Setting Up Your Python Environment

Before we install any packages, it's crucial to create an isolated virtual environment. This prevents conflicts with other projects and system-level Python packages. It's a professional habit that will save you countless headaches down the road. You can use either Python's built-in venv or Conda.

Option A: Using venv (Recommended for most users)

venv is included with Python 3.3+. Open your terminal and run the following commands:

# Create a new virtual environment named 'anomalib_env'
python -m venv anomalib_env

# Activate the environment
# On Windows:
anomalib_env\Scripts\activate

# On macOS/Linux:
source anomalib_env/bin/activate

You'll know it's active when you see (anomalib_env) at the beginning of your terminal prompt.

Option B: Using Conda

If you're a data scientist, you might prefer Conda. It's excellent for managing complex dependencies, especially for GPU-accelerated libraries like PyTorch.

# Create a new conda environment with a specific Python version
conda create -n anomalib_env python=3.10

# Activate the environment
conda activate anomalib_env

With your environment activated, you're ready to install Anomalib.

Step 2: Installing the Anomalib Library

You have two primary ways to install Anomalib: from the Python Package Index (PyPI) for stability or from the source code on GitHub for the latest features.

Installation Method Command Best For
From PyPI (Stable) pip install anomalib Beginners and production use where stability is key.
From Source (Latest) git clone https://github.com/openvinotoolkit/anomalib.git
cd anomalib
pip install -e .
Developers who want the newest features or need to modify the library's code.

For this guide, let's stick with the stable PyPI version. It's the most straightforward way to get started.

Advertisement
pip install anomalib

This command will install Anomalib along with its core dependencies, including PyTorch. If you have a CUDA-enabled GPU (which is highly recommended for faster training), ensure you have a compatible version of PyTorch installed. You can check the official PyTorch website for the correct installation command.

Step 3: Understanding Datasets and Configs

Anomalib is designed to be data-centric and configuration-driven. Before you can train a model, you need to tell it what data to use and how to behave. This is handled through a dataset folder and a YAML configuration file.

The Dataset Structure

Anomalib expects a specific folder structure for your data. It's simple and intuitive. For a standard unsupervised task, you only need to provide "good" (normal) images for training. The testing set contains both normal images and anomalous ones, organized into subfolders.

Here's the structure for the famous MVTec AD dataset, which Anomalib supports out-of-the-box:

mvtec/
└── bottle/
    ├── train/
    │   └── good/
    │       ├── 000.png
    │       ├── 001.png
    │       └── ...
    ├── test/
    │   ├── good/
    │   │   ├── 000.png
    │   │   └── ...
    │   ├── broken_large/
    │   │   ├── 000.png
    │   │   └── ...
    │   └── contamination/
    │       ├── 000.png
    │       └── ...
    └── ground_truth/
        └── broken_large/
            ├── 000_mask.png
            └── ...

The key takeaway for your own custom dataset is this: place all your normal training images in a train/good folder. Your testing images go into test/good for normal samples and test/<anomaly_name> for defective samples.

The Configuration (YAML) Files

Instead of passing dozens of arguments through the command line, Anomalib uses .yaml files to define an experiment. These files are the control panel for your training job. If you installed from source, you'll find them in the configs/ directory. If not, you can find them in the GitHub repository.

Let's look at a snippet from a typical config file, like patchcore.yaml:

# src/anomalib/models/patchcore/config.yaml

dataset:
  name: mvtec
  format: mvtec
  path: ./datasets/mvtec
  category: bottle
  image_size: 256

model:
  name: patchcore
  backbone: wide_resnet50
  layers:
    - layer2
    - layer3
  coreset_sampling_ratio: 0.1

trainer:
  max_epochs: 1
  # ... other trainer settings

This file clearly defines the dataset path, the model to use (PatchCore), its specific parameters, and the training settings. You can easily duplicate and modify these files to experiment with different models or datasets.

Step 4: Running Your First Training Job

Now for the exciting part! With our environment and understanding of the structure, we can launch a training job. Anomalib provides a simple command-line interface (CLI) for this.

First, you'll need a config file and a dataset. For this example, let's assume you've downloaded the MVTec `bottle` dataset and placed it in a `datasets/mvtec` folder. You also need the `patchcore.yaml` config file.

The command to start training is:

anomalib train --model patchcore --data.path ./datasets/mvtec --data.category bottle

Alternatively, if you are using a custom YAML file:

anomalib train --config my_patchcore_config.yaml

The system will spring to life! You'll see a progress bar as the model trains and evaluates. Once finished, all the results, including model weights, logs, and performance metrics, will be saved in a structured directory, typically under results/<model_name>/<dataset_category>/.

A Quick Note: Which Model to Choose?

Anomalib offers over 15 models. Which one is right for you? Here’s a very brief comparison of three popular choices:

  • PaDiM: Fast to train and offers good performance. A great starting point.
  • PatchCore: Often provides state-of-the-art accuracy, but can be memory-intensive with large datasets. Excellent for high-performance needs.
  • FastFlow: A normalizing flow-based model that is very fast during inference, making it suitable for real-time applications.

Start with PaDiM or PatchCore, and explore from there based on your specific accuracy and speed requirements.

Step 5: Inference and Visualizing Results

Training a model is only half the battle. The real value comes from using it to detect anomalies in new, unseen images. Anomalib has a dedicated CLI for inference.

You'll need the path to your trained model's weights (the .ckpt or .pt file) and the input image or folder you want to inspect.

anomalib infer --weights results/patchcore/mvtec/bottle/v1/weights/torch/model.pt \
             --input path/to/your/new_images/ \
             --output results/inference/

This command will run each image through the model and generate outputs. The most important output is the anomaly map, a heatmap that highlights where the model detected a deviation from the norm. Bright, hot areas on the map indicate likely anomalies.

You will find these visualized images in the specified --output directory. By inspecting these heatmaps, you can not only tell if an image is anomalous but also where the defect is located. This visual feedback is invaluable for quality control operators and for debugging your model's performance.

Conclusion: Your Journey Has Just Begun

Congratulations! You've successfully navigated the five core steps to get started with Anomalib: setting up your environment, installing the library, understanding its data and config structure, training a model, and running inference.

You now have a powerful tool at your disposal for tackling complex visual inspection challenges. The real journey starts now. I encourage you to experiment with your own custom datasets, try different models from the library, and dive into the configuration files to fine-tune your results. The world of unsupervised anomaly detection is vast, and with Anomalib, you're well-equipped to explore it.

Tags

You May Also Like