Skip to content

Commit fd13947

Browse files
📚 fix(docs): fixed the "Anomalib in 15 Minutes" train and inference code (#2904)
* docs: fixed the 'Anomalib in 15 Minutes' train and inference code to match 2.1.0 Signed-off-by: FedericoDeBona <federicodebona@hotmail.it> * Update examples/api/01_getting_started/basic_openvino_inference.py Co-authored-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com> Signed-off-by: Federico <36895247+FedericoDeBona@users.noreply.github.com> * Update examples/api/01_getting_started/basic_torch_inference.py Co-authored-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com> Signed-off-by: Federico <36895247+FedericoDeBona@users.noreply.github.com> --------- Signed-off-by: FedericoDeBona <federicodebona@hotmail.it> Signed-off-by: Federico <36895247+FedericoDeBona@users.noreply.github.com> Co-authored-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com>
1 parent f6ec1c5 commit fd13947

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

‎docs/source/markdown/get_started/anomalib.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interfaces, and might be easier for those who would like to use anomalib off-the
5252

5353
```{literalinclude} ../../../../examples/api/01_getting_started/basic_training.py
5454
:language: python
55-
:lines: 10-34
55+
:lines: 10-53
5656
```
5757

5858
:::
@@ -61,6 +61,7 @@ interfaces, and might be easier for those who would like to use anomalib off-the
6161

6262
```{literalinclude} ../../../../examples/cli/01_getting_started/basic_training.sh
6363
:language: bash
64+
:lines: 10-33
6465
```
6566

6667
:::
@@ -81,6 +82,7 @@ Anomalib includes multiple inferencing scripts, including Torch, Lightning, Grad
8182

8283
```{literalinclude} ../../../../examples/api/01_getting_started/basic_inference.py
8384
:language: python
85+
:lines: 10-40
8486
```
8587

8688
:::
@@ -104,8 +106,9 @@ Anomalib includes multiple inferencing scripts, including Torch, Lightning, Grad
104106
:::{tab-item} API
105107
:sync: label-1
106108

107-
```{code-block} python
108-
109+
```{literalinclude} ../../../../examples/api/01_getting_started/basic_torch_inference.py
110+
:language: python
111+
:lines: 10-11
109112
```
110113

111114
:::
@@ -129,8 +132,9 @@ Anomalib includes multiple inferencing scripts, including Torch, Lightning, Grad
129132
:::{tab-item} API
130133
:sync: label-1
131134

132-
```{code-block} python
133-
135+
```{literalinclude} ../../../../examples/api/01_getting_started/basic_openvino_inference.py
136+
:language: python
137+
:lines: 10-28
134138
```
135139

136140
:::

‎examples/api/01_getting_started/basic_inference.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88
"""
99

1010
# 1. Import required modules
11-
from pathlib import Path
12-
1311
from anomalib.data import PredictDataset
1412
from anomalib.engine import Engine
15-
from anomalib.models import EfficientAd
13+
from anomalib.models import Patchcore
1614

1715
# 2. Initialize the model and load weights
18-
model = EfficientAd()
16+
model = Patchcore()
1917
engine = Engine()
2018

2119
# 3. Prepare test data
2220
# You can use a single image or a folder of images
2321
dataset = PredictDataset(
24-
path=Path("path/to/test/images"),
22+
path="path/to/test/images",
2523
image_size=(256, 256),
2624
)
2725

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Getting Started with Anomalib Inference using the Python API.
5+
6+
This example shows how to perform inference on a trained model
7+
using the Anomalib Python API.
8+
"""
9+
10+
# 1. Import required modules
11+
from anomalib.deploy import OpenVINOInferencer
12+
13+
# 2. Initialize the inferencer
14+
inferencer = OpenVINOInferencer(
15+
path="/path/to/openvino/model.bin",
16+
)
17+
18+
# 4. Get predictions
19+
predictions = inferencer.predict(
20+
image="/path/to/image.png",
21+
)
22+
23+
# 5. Access the results
24+
if predictions is not None:
25+
for prediction in predictions:
26+
anomaly_map = prediction.anomaly_map # Pixel-level anomaly heatmap
27+
pred_label = prediction.pred_label # Image-level label (0: normal, 1: anomalous)
28+
pred_score = prediction.pred_score # Image-level anomaly score
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Getting Started with Anomalib Inference using the Python API.
5+
6+
This example shows how to perform inference on a trained model
7+
using the Anomalib Python API.
8+
"""
9+
10+
# TorchInferencer is a legacy inferencer. Consider using Engine.predict() instead,
11+
# which provides a more modern and feature-rich interface for model inference.

‎examples/api/01_getting_started/basic_training.py‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
# 1. Import required modules
1111
from anomalib.data import MVTecAD
12+
from anomalib.deploy import ExportType
1213
from anomalib.engine import Engine
13-
from anomalib.models import EfficientAd
14+
from anomalib.models import Patchcore
1415

1516
# 2. Create a dataset
1617
# MVTecAD is a popular dataset for anomaly detection
@@ -19,15 +20,29 @@
1920
category="bottle", # MVTec category to use
2021
train_batch_size=32, # Number of images per training batch
2122
eval_batch_size=32, # Number of images per validation/test batch
22-
num_workers=8, # Number of parallel processes for data loading
2323
)
2424

2525
# 3. Initialize the model
26-
# EfficientAd is a good default choice for beginners
27-
model = EfficientAd()
26+
# Patchcore is a good choice for beginners
27+
model = Patchcore(
28+
num_neighbors=6, # Override default model settings
29+
)
2830

2931
# 4. Create the training engine
30-
engine = Engine(max_epochs=10) # Train for 10 epochs
32+
engine = Engine(
33+
max_epochs=1, # Override default trainer settings
34+
)
3135

3236
# 5. Train the model
37+
# This produces a lightning model (.ckpt)
3338
engine.fit(datamodule=datamodule, model=model)
39+
40+
# 6. Test the model performance
41+
test_results = engine.test(datamodule=datamodule, model=model)
42+
43+
# 7. Export the model
44+
# Different formats are available: Torch, OpenVINO, ONNX
45+
engine.export(
46+
model=model,
47+
export_type=ExportType.OPENVINO,
48+
)

‎examples/cli/01_getting_started/basic_training.sh‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88
# This example shows the basic steps to train an anomaly detection model.
99

1010
# 1. Basic Training
11-
# Train a model using default configuration (recommended for beginners)
11+
# Train and test a model using default configuration on MVTecAD bottle (default category)
1212
echo "Training with default configuration..."
13-
anomalib train --model efficient_ad
13+
anomalib train --model Patchcore --data anomalib.data.MVTecAD
1414

1515
# 2. Training with Basic Customization
16-
# Customize basic parameters like batch size and epochs
16+
# Customize basic parameters like batch size and epochs.
17+
# For example `EfficientAd` requires a train batch size of 1
1718
echo -e "\nTraining with custom parameters..."
18-
anomalib train --model efficient_ad \
19-
--data.train_batch_size 32 \
20-
--trainer.max_epochs 10
19+
anomalib train --model EfficientAd --data anomalib.data.MVTecAD \
20+
--data.category hazelnut \
21+
--data.train_batch_size 1 \
22+
--trainer.max_epochs 200
2123

22-
# 3. Using a Different Dataset
23-
# Train on a specific category of MVTecAD dataset
24-
echo -e "\nTraining on MVTecAD bottle category..."
25-
anomalib train --model efficient_ad \
26-
--data.category bottle
24+
# 3. Train with config file
25+
# Train with a custom config file
26+
echo -e "\nTraining with config file..."
27+
anomalib train --config path/to/config.yaml
28+
29+
# 4. Export a trained model into OpenVINO
30+
echo .e "\nExporting model into OpenVINO..."
31+
anomalib export --model Patchcore \
32+
--ckpt_path /path/to/model.ckpt \
33+
--export_type OPENVINO

0 commit comments

Comments
 (0)