agentleFS
open-edge-platformopen-edge-platform/skills/.agents/skills/anomalib-training/SKILL.md

anomalib-training

Skill8 starsChanged 8 days agoApache-2.0View on GitHub

Trains an anomalib model on a dataset via the Python API or CLI, including training on a custom folder-structured dataset with the Folder datamodule. Use when writing or debugging an anomalib training script/command, choosing Engine/Trainer arguments, or wiring a model + datamodule together. Do not use for adding a new model or datamodule from scratch (see anomalib-adding-a-model / anomalib-adding-a-datamodule), or for the tiled-ensemble or benchmarking pipelines (see anomalib-tiled-ensemble / anomalib-benchmarking).

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If model/datamodule are omitted from test/predict, the engine reuses the ones passed to fit.

    line 36
  2. Very specific:

    Use Folder whenever your data is laid out as root/normal_dir/*, root/abnormal_dir/*, and optionally root/mask_dir/* (segmentation masks) — …

    line 40
  3. Very specific:

    If you only have normal training images and unlabeled/normal-only test images, omit abnormal_dir and mask_dir — Folder will still produce a …

    line 63
  4. Very specific:

    Not every model trains via gradient descent — training-free models (e.g.

    line 105

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Training a Model on a Dataset

Training in anomalib always goes through anomalib.engine.Engine, which wraps a Lightning Trainer.

Python API — standard benchmark dataset (MVTecAD)

from anomalib.data import MVTecAD
from anomalib.models import Patchcore
from anomalib.engine import Engine

datamodule = MVTecAD(root="./datasets/MVTecAD", category="bottle", train_batch_size=32)
model = Patchcore()
engine = Engine()                      # any Lightning Trainer kwarg can go here
engine.fit(model=model, datamodule=datamodule)
results = engine.test(model=model, datamodule=datamodule)

Engine(**kwargs) forwards unknown kwargs straight to the underlying Lightning Trainer (accelerator, devices, strategy, max_epochs, logger, callbacks, enable_checkpointing, val_check_interval, barebones, ...) — there is no separate "Trainer config object" to build.

Key Engine methods: fit(model, datamodule=...), train(...) (fit + test in one call), test(model=None, datamodule=...), predict(model=None, datamodule=..., dataset=..., data_path=...). If model/datamodule are omitted from test/predict, the engine reuses the ones passed to fit.

Python API — custom data with the Folder datamodule

Use Folder whenever your data is laid out as root/normal_dir/*, root/abnormal_dir/*, and optionally root/mask_dir/* (segmentation masks) — no new dataset code needed:

from anomalib.data import Folder
from anomalib.models import Padim
from anomalib.engine import Engine

datamodule = Folder(
    name="custom",             # required — used as the datamodule's display name
    root="./datasets/custom",
    normal_dir="good",         # required
    abnormal_dir="defect",     # optional: enables anomalous test/eval samples
    mask_dir="mask",           # optional: enables pixel-level (segmentation) evaluation
    train_batch_size=32,
    eval_batch_size=32,
    num_workers=8,
)
model = Padim()
engine = Engine()
engine.fit(model=model, datamodule=datamodule)

If you only have normal training images and unlabeled/normal-only test images, omit abnormal_dir and mask_dir — Folder will still produce a valid train/test split via test_split_ratio (fraction of normal training images held out for testing). Use normal_test_dir if you have a separate directory of normal images specifically for the test set.

CLI

# Standard dataset, defaults
anomalib train --model Patchcore --data anomalib.data.MVTecAD

# Override a datamodule field
anomalib train --model Patchcore --data anomalib.data.MVTecAD --data.category transistor

# Override a trainer field (use a gradient-trained model like Stfpm where max_epochs is meaningful)
anomalib train --model anomalib.models.Stfpm --data anomalib.data.MVTecAD --trainer.max_epochs 3

# Custom Folder dataset from the CLI
anomalib train --model Padim --data anomalib.data.Folder \
  --data.name custom --data.root ./datasets/custom \
  --data.normal_dir good --data.abnormal_dir defect

# From a config file (jsonargparse; combine with any of the above overrides)
anomalib train --config path/to/config.yaml

CLI wiring lives in src/anomalib/cli/cli.py (AnomalibCLI); model/data classes are exposed as jsonargparse subclass arguments, so --model/--data accept either a short name (Padim) or a full class path (anomalib.models.Padim, anomalib.data.Folder).

Choosing accelerator / devices

Pass standard Lightning kwargs to Engine(...): accelerator="gpu"|"cpu"|"xpu", devices=1. For Intel XPU specifically, use SingleXPUStrategy/XPUAccelerator from anomalib.engine:

from anomalib.engine import Engine, SingleXPUStrategy, XPUAccelerator
engine = Engine(strategy=SingleXPUStrategy(), accelerator=XPUAccelerator())

Gotchas

  • Not every model trains via gradient descent — training-free models (e.g. Padim, Patchcore) still go through engine.fit(...); Engine/Trainer handles the single "epoch" needed to build their memory bank. You don't need special-case code for this. Note that these models override max_epochs via their trainer_arguments property (e.g. max_epochs=1), so any max_epochs you pass to Engine will be overwritten for such models.
  • Folder's mask_dir is what switches evaluation from image-level (classification) to pixel-level (segmentation) metrics — only pass it if you actually have per-pixel ground-truth masks.
  • Results (checkpoints, logs, images) are written under Engine's default_root_dir ("results" by default), nested by model/datamodule/category — check there first when debugging a run.

Reviewer / self-check

  • Engine(...) receives Trainer overrides as plain kwargs, not a hand-built Trainer object.
  • Folder-dataset training specifies normal_dir and, if applicable, abnormal_dir/mask_dir matching the actual on-disk layout.
  • CLI invocations use anomalib.data.<Class> / anomalib.models.<Class> paths that are actually exported (see anomalib-adding-a-model / anomalib-adding-a-datamodule for how exports work).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.