Anomaly / Defect Detection (Anomalib) - User Manual
Dragonfly Prototype Apps · Anomaly / Defect Detection (Anomalib)...
Version 1.0 · 2026-07-09
English Manual
Contents
1. Overview
2. Use Cases
3. Installation & Enabling
4. Runtime Environment & First-Run Setup
5. Interface Guide
6. Step-by-Step Usage
7. Parameter Reference
8. Outputs
9. FAQ & Troubleshooting
10. Notes & Known Limitations
11. References
1. Overview
Anomaly / Defect Detection (Anomalib) is a Dragonfly plugin for unsupervised anomaly / defect detection. The key idea: you only need a batch of defect-free ("normal") images to train a model — no defect annotations required. Once trained, you apply the model to a test Channel: the plugin computes a per-slice anomaly heatmap (higher = more anomalous) and publishes it back as a new heatmap Channel, then thresholds it into a defect MultiROI ready for measurement and visualization.
The underlying engine is the open-source Anomalib library (built on PyTorch / PyTorch-Lightning). Three anomaly-detection models are offered: PatchCore, PaDiM, and FastFlow. PatchCore and PaDiM are memory-bank / feature-distribution models that fit in a single forward pass; FastFlow is a normalizing-flow model trained by gradient descent over Epochs.
The plugin is primarily for 2D images; a 3D Channel is processed slice/slab-wise: each z-slice is treated as an independent image, its anomaly map computed, then stacked back onto the source grid. All heavy computation (PyTorch CUDA + Anomalib) runs in the plugin's own isolated virtual environment (venv) subprocess, talking to the panel via file-based JSON IPC, and never in Dragonfly's bundled Python — so it neither pollutes nor slows down Dragonfly itself.
On Dragonfly 2027.1 (Python 3.14) the bundled Python cannot build this environment: install a python.org Python (3.12 or 3.13) and enter it in the panel's Base Python field; Dragonfly 2025.1 needs no separate Python.
Licensing note: Anomalib, PyTorch, torchvision and the rest are open-source libraries installed into the isolated environment by Setup Environment over the internet. The plugin bundles none of them and bundles no pretrained weights — pretrained backbone weights are downloaded automatically by Anomalib inside that environment at first train/apply. Please observe the licenses of each upstream library and its weights.
2. Use Cases
This plugin is especially useful when normal samples are plentiful but defect examples are rare — you cannot collect enough labeled examples of every defect, but you have many good (in-spec) parts. Train on the good parts, and the model automatically locates regions that deviate from "normal". Typical applications:
- Industrial and materials CT / microscopy images: cracks, pores, inclusions, or foreign objects;
- Castings and additive-manufactured (3D-printed) parts: surface or internal defects;
- Semiconductor, electronics, and surface-quality inspection: deviation detection;
- Any quality-inspection task where defects are rare but good parts abound — train a "normality" model on the good parts, then flag anomalous regions.
An unsupervised method flags everything that deviates from normal — this can be a real defect or merely a rare-but-valid structural variation. Combine threshold tuning with visual review to confirm results.
3. Installation & Enabling
1. Download the Prototype Apps online package from ImageFly and extract it. Close Dragonfly, run install_prototype_apps.exe and select the Dragonfly version to install into.
2. Start Dragonfly, open Prototype Apps ▸ App Store and search for Anomaly / Defect Detection (Anomalib). If an Enable button is shown, click it and follow the sign-in, download or installation prompts.
3. Restart Dragonfly when prompted, then open Anomaly / Defect Detection (Anomalib) from the Prototype Apps menu. Return to App Store to enable or disable it later.
4. Runtime Environment & First-Run Setup
The plugin uses a venv-in-code isolation model: the heavy ML dependencies are never installed into Dragonfly. Before first use you must build this environment on the Setup tab.
4.1 What Setup Environment does
Clicking Setup Environment (build venv + install torch + anomalib) on the Setup tab will:
1. Create a dedicated venv for the plugin. On Dragonfly 2027.1, set Base Python to a python.org Python 3.12 or 3.13 executable. On 2025.1, you can leave it blank to use the bundled Python.
2. Upgrade pip / setuptools / wheel inside the venv;
3. Install torch + torchvision from the CUDA wheel index (default https://download.pytorch.org/whl/cu124);
4. Install the rest of the stack (anomalib + numpy, etc.);
5. Run an import smoke test reporting the torch version, whether CUDA is available, and the GPU name, then fill the venv python path back into the panel.
The venv is created in a venv\ sub-folder inside the installed plugin code directory (%LOCALAPPDATA%\comet\<Dragonfly version>\pythonUserExtensions\GenericMenuItems\Anomalib\venv). On success, the venv python path shows in the "Anomalib venv python" field and is persisted.
4.2 Download size & hardware requirements
The first Setup needs internet and downloads torch CUDA wheels + anomalib, totaling several GB and taking a few minutes. Training / applying needs an NVIDIA GPU; a CPU can run inference (apply) only, but slowly.
Pretrained weights are not bundled: the pretrained backbone weights Anomalib uses (e.g. the PatchCore / PaDiM feature extractor, FastFlow's timm backbone) are downloaded lazily inside that venv at first train/apply — so the first train/apply also needs internet.
4.3 Fallbacks when setup fails
- On Dragonfly 2027.1, set Base Python to a python.org Python 3.12 or 3.13 executable. On 2025.1, you can leave it blank to use the bundled Python.
- Torch CUDA wheel index field: if your driver targets a different CUDA version, change the URL to match (e.g. replace
cu124withcu121). - venv rebuild: if a previous venv was only half-built (has python.exe but no working pip), clicking Setup Environment again rebuilds it automatically — no manual cleanup needed.
- Run mode: offers
windows(default, shipped) andwsl; only windows is supported in the shipped build.
5. Interface Guide
The top of the panel shows a blue note summarizing the train-then-apply flow. Below it are three tabs Setup / Train / Apply; at the very bottom sit a green result summary line and a read-only log view that streams progress and messages.
5.1 Setup tab
Control | Type | Description |
Anomalib venv python | Text field | The venv's python path; filled automatically by Setup Environment — usually no need to edit. |
Base Python (build) | Text field | On Dragonfly 2027.1, set Base Python to a python.org Python 3.12 or 3.13 executable. On 2025.1, you can leave it blank to use the bundled Python. |
Run mode | Dropdown | Run mode; windows (default) / wsl. Only windows is supported. |
Torch CUDA wheel index | Text field | The CUDA wheel index for torch; default https://download.pytorch.org/whl/cu124. |
Setup Environment | Button | Builds the venv and installs torch + torchvision + anomalib. |
5.2 Train tab
Control | Type | Default / Range | Description |
Output folder | Text + Browse | C:\AnomalibWorkspace | Workspace output folder; intermediates and the model folder are saved here. |
Refresh channels | Button | - | Rescan the current scene's Channels and refresh the dropdowns. |
Normal-image Channel | Dropdown | (Channels in scene) | Select the defect-free (normal) image Channel to train on. |
Model | Dropdown | PatchCore | Anomaly model: PatchCore / PaDiM / FastFlow. |
Image size (px) | Spin box | 256 (range 64-1024, step 32) | The side length images are resized to for training / inference. |
Epochs (FastFlow only; 0 = default) | Spin box | 0 (0-500; 0 shows as default) | Training epochs; only effective for FastFlow — disabled for other models. |
Train | Button | - | Start training; on completion the model folder is pre-filled on the Apply tab. |
5.3 Apply tab
Control | Type | Default | Description |
Model folder | Text + Browse | (auto-filled after Train) | The trained model folder produced by Train. |
Model (must match Train) | Dropdown | PatchCore | Model used for apply; must match the one used for training. |
Test Channel | Dropdown + Refresh | (Channels in scene) | Select the test Channel to inspect. |
Threshold mode | Dropdown | auto | Threshold mode: auto (F1-optimal) / manual (fixed value). |
Manual threshold (0-1) | Spin box | 0.5 (0.000-1.000, step 0.05) | Manual threshold; enabled only in manual mode. |
Heatmap title | Text field | (blank = Anomaly_heatmap) | Title of the output heatmap Channel. |
Defect ROI title | Text field | (blank = Anomaly_defects) | Title of the output defect MultiROI. |
Apply -> heatmap Channel + defect MultiROI | Button | - | Apply the model and publish the heatmap Channel and defect MultiROI. |
6. Step-by-Step Usage
Open Setup and click Execute All Steps with Current Settings after configuring the relevant tabs. Set up the environment first is unchecked by default. Select it for a first run that needs to build the environment.
Training is selected by default. Choose the defect-free training data and output workspace in Train, then select the Channel to inspect in Apply. The trained model is passed to Apply automatically. To use an existing model, clear Train a new model first and set Model folder in Apply.
6.1 Train an anomaly model
Input required: a Channel containing only defect-free (normal) images. A 2D Channel is one image; a 3D Channel treats each z-slice as one normal image.
1. Open the panel, go to the Setup tab, and on first use click Setup Environment and wait for the environment to build (see Chapter 4).
2. Switch to the Train tab and set the Output folder (workspace).
3. Click Refresh channels and pick your normal-image Channel in Normal-image Channel.
4. Choose a Model (default PatchCore); adjust Image size if needed; if you chose FastFlow, set Epochs.
5. Click Train. The plugin exports the normal Channel to an (n,y,x) image stack and hands it to the venv runner to fit the model; the log shows progress (epoch, AUROC, F1).
6. On completion the result line reads "Trained." and the trained model folder is pre-filled on the Apply tab, with the model selector kept in sync.
6.2 Apply the model to detect defects
Input required: a test Channel to inspect, and a model folder produced by Train.
1. Switch to the Apply tab, confirm Model folder points to the trained model folder (usually already filled), and that Model matches the training model.
2. Click Refresh and pick the Channel to inspect in Test Channel.
3. Choose a Threshold mode: auto lets the model pick the F1-optimal threshold; manual lets you enter a fixed 0-1 value in Manual threshold.
4. Optionally set Heatmap title and Defect ROI title (blank uses the default names).
5. Click Apply. The plugin exports the test Channel, runs inference slice by slice in the venv, normalizes the anomaly map to [0,1], and thresholds it into a binary mask.
6. On completion the plugin publishes an anomaly heatmap Channel and a defect MultiROI into the scene, and the result line reports the threshold used and the number of defect slices.
7. Parameter Reference
Parameter | Default | Description |
Base Python (build) | Blank on 2025.1. External Python path on 2027.1. | Base interpreter that builds the venv; must be CPython 3.9+ with stdlib venv + pip. |
Torch CUDA wheel index | https://download.pytorch.org/whl/cu124 | The CUDA wheel index for torch / torchvision; should match your driver's CUDA version. |
Run mode | windows | Run mode; only windows is supported (wsl is a reserved option). |
Output folder | C:\AnomalibWorkspace | Workspace for training / applying; holds intermediates and the model folder. |
Model | PatchCore | Anomaly model: PatchCore / PaDiM (single-pass fit) or FastFlow (epoch-based). |
Image size (px) | 256 | The side length images are resized to for training / inference; range 64-1024, step 32. |
Epochs | 0 (default) | Training epochs; only FastFlow uses it, 0 means default (FastFlow's default is ~50); other models always fit in one pass. |
Threshold mode | auto | auto = use the model's fitted F1-optimal threshold; manual = use the value below. |
Manual threshold (0-1) | 0.5 | Manual threshold in the normalized anomaly-score space [0,1]; score >= threshold is a defect. Enabled only in manual mode. |
Heatmap title | Anomaly_heatmap | Title of the output heatmap Channel (default if left blank). |
Defect ROI title | Anomaly_defects | Title of the output defect MultiROI (default if left blank). |
The threshold acts on the anomaly score normalized to [0,1] (0 = most normal, 1 = most anomalous), so a manual threshold should also be in 0-1; values outside that range are clamped to [0,1].
8. Outputs
After a successful Apply, the plugin creates two kinds of objects in the Dragonfly scene (on the source Channel's grid / spacing / origin):
- Anomaly heatmap Channel (default title
Anomaly_heatmap): a per-slice anomaly-score map normalized to [0,1] — higher = more anomalous. It can be overlaid in 2D / 3D views or displayed with a color LUT to visualize the anomaly distribution. - Defect MultiROI (default title
Anomaly_defects): the binary defect mask from thresholding the heatmap, marking regions judged anomalous. Ready for measurement (area / volume / count) and visualization.
The result summary line reports the threshold used and "defect slices / total slices". All intermediate files (the exported image stack, heatmap and mask .npy files, logs) are saved under timestamped apply_* / train_* sub-folders in the Output folder, along with the model_* model folder.
The Train output is a model_<name> folder containing the fitted checkpoint plus an anomalib_model.json recording the model name; that folder is the "Model folder" the Apply tab needs.
9. FAQ & Troubleshooting
Q: Clicking Train / Apply says "venv not set. Run Setup Environment first."
A: The isolated environment hasn't been built. Go to the Setup tab, click Setup Environment, and wait for it to finish — the venv python path fills in automatically — then train / apply.
Q: Setup Environment fails, saying venv creation failed or there is no venv module.
A: The chosen base Python lacks stdlib venv or pip. Enter another CPython 3.9+ in Base Python (build) (e.g. C:\Python312\python.exe) and retry; if a venv was half-built, clicking the button again rebuilds it automatically.
Q: torch install fails, or it reports CUDA is not available.
A: Confirm the machine has an NVIDIA GPU with a working driver, and set Torch CUDA wheel index to match your driver's CUDA version (e.g. cu121 / cu124). A CPU can run inference only (slowly). The first train / apply also needs internet to download pretrained weights.
Q: I don't see my Channel in the Train / Apply dropdown.
A: Click Refresh channels / Refresh to rescan the current scene, and confirm the Channel is loaded. Train needs a normal (defect-free) image Channel; Apply needs the test Channel to inspect.
Q: Too many false positives / too few detections — how do I tune it?
A: Switch Threshold mode to manual and adjust Manual threshold: raising it reduces false positives (marks fewer regions), lowering it is more sensitive (marks more). You can also try a different model or a larger Image size.
Q: Apply reports "apply finished but no anomaly heatmap was produced".
A: The runner produced no heatmap. Check that Model folder is a valid trained-model folder, that Model matches the training model, and read the log for the detailed error (e.g. missing input, CUDA out of memory).
10. Notes & Known Limitations
- Nature of unsupervised detection: the model only learns "what is normal"; anything deviating from the normal distribution is flagged — real defects and rare-but-valid variations alike. Combine thresholds with visual review.
- Primarily 2D: a 3D Channel is processed slice by slice and stacked back, without modeling 3D context across slices.
- Model must match: the Model selected for Apply must match the one used to train, otherwise the checkpoint cannot load correctly.
- Epochs affect FastFlow only: PatchCore / PaDiM fit in a single pass; setting epochs has no effect (the field is auto-disabled).
- Hardware & VRAM: training / inference needs an NVIDIA GPU; PatchCore memory banks can be large, so watch VRAM for big images or datasets; Apply infers one slice at a time to bound memory.
- First-run internet: the first Setup and the first train / apply both require internet (to download dependencies and pretrained weights).
- Anomalib is version-sensitive: Anomalib's API varies across versions and requirements do not pin an exact version; if upstream changes cause issues, re-run Setup Environment.
11. References
- Anomalib (anomaly detection library): https://github.com/openvinotoolkit/anomalib
- PyTorch: https://pytorch.org
- PyTorch CUDA wheel index: https://download.pytorch.org/whl/cu124
- PatchCore: Roth et al., "Towards Total Recall in Industrial Anomaly Detection" (2022)
- PaDiM: Defard et al., "PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization" (2020)
- FastFlow: Yu et al., "FastFlow: Unsupervised Anomaly Detection and Localization via 2D Normalizing Flows" (2021)
The UI controls, parameter defaults, and workflow described in this manual reflect the plugin's current implementation.