Anomalib:使用Anomalib 2.1.0训练自己的数据集进行异常检测
- 前言
- 环境要求
- 相关介绍
- 安装Anomalib 2.1.0
- 训练自己的数据集进行异常检测
- 准备自己数据集
- 使用Anomalib 2.1.0进行训练
- 使用Anomalib 2.1.0进行推理
- 参考
前言
- 由于本人水平有限,难免出现错漏,敬请批评改正。
环境要求
Package Version Build Editable project location
---------------------------------- -------------- ----- ----------------------------------------------
about-time 4.2.1
absl-py 2.3.1
accessible-pygments 0.0.5
aiofiles 23.2.1
aiohappyeyeballs 2.6.1
aiohttp 3.12.15
aiosignal 1.4.0
alabaster 1.0.0
alembic 1.16.4
alive-progress 3.3.0
annotated-types 0.7.0
anomalib 2.2.0.dev0
antlr4-python3-runtime 4.9.3
anyio 4.10.0
appdirs 1.4.4
argcomplete 3.6.2
argon2-cffi 25.1.0
argon2-cffi-bindings 25.1.0
arrow 1.3.0
asttokens 3.0.0
async-lru 2.0.5
async-timeout 5.0.1
attrs 25.3.0
autograd 1.8.0
av 14.0.0
babel 2.17.0
beautifulsoup4 4.13.4
bleach 6.2.0
blinker 1.9.0
cachetools 5.5.2
certifi 2025.8.3
cffi 1.17.1
cfgv 3.4.0
chardet 5.2.0
charset-normalizer 3.4.3
click 8.2.1
cloudpickle 3.1.1
cma 4.3.0
colorama 0.4.6
comet-ml 3.47.4
comm 0.2.3
commitizen 4.8.3
configobj 5.0.9
contourpy 1.3.2
coverage 7.6.8
cycler 0.12.1
databricks-sdk 0.63.0
debugpy 1.8.16
decli 0.6.3
decorator 5.2.1
defusedxml 0.7.1
Deprecated 1.2.18
dill 0.4.0
distlib 0.4.0
distro 1.9.0
docker 7.1.0
docker-pycreds 0.4.0
docstring_parser 0.17.0
docutils 0.21.2
dulwich 0.24.1
einops 0.8.1
everett 3.1.0
exceptiongroup 1.3.0
execnet 2.1.1
executing 2.2.0
fastapi 0.116.1
fastjsonschema 2.21.2
ffmpy 0.6.1
filelock 3.19.1
Flask 3.1.2
fonttools 4.59.1
fqdn 1.5.1
FrEIA 0.2
frozenlist 1.7.0
fsspec 2025.7.0
ftfy 6.3.1
gitdb 4.0.12
GitPython 3.1.43
google-auth 2.40.3
gradio 5.8.0
gradio_client 1.5.1
graphemeu 0.7.2
graphene 3.4.3
graphql-core 3.2.6
graphql-relay 3.2.0
greenlet 3.2.4
grpcio 1.74.0
gunicorn 23.0.0
h11 0.16.0
hf-xet 1.1.7
httpcore 1.0.9
httpx 0.25.2
huggingface-hub 0.34.4
identify 2.6.13
idna 3.10
imageio 2.37.0
imagesize 1.4.1
importlib_metadata 8.7.0
importlib_resources 6.5.2
iniconfig 2.1.0
ipykernel 6.29.5
ipython 8.37.0
ipywidgets 8.1.5
isoduration 20.11.0
itsdangerous 2.2.0
jedi 0.19.2
Jinja2 3.1.6
jiter 0.10.0
joblib 1.5.1
json5 0.12.1
jsonargparse 4.40.2
jsonpointer 3.0.0
jsonschema 4.25.1
jsonschema-specifications 2025.4.1
jstyleson 0.0.2
jupyter_client 8.6.3
jupyter_core 5.8.1
jupyter-events 0.12.0
jupyter-lsp 2.2.6
jupyter_server 2.16.0
jupyter_server_terminals 0.5.3
jupyterlab 4.2.7
jupyterlab_pygments 0.3.0
jupyterlab_server 2.27.3
jupyterlab_widgets 3.0.15
kiwisolver 1.4.9
kornia 0.8.1
kornia_rs 0.1.9
lark 1.2.2
lazy_loader 0.4
lightning 2.5.3
lightning-utilities 0.15.2
linkify-it-py 2.0.3
Mako 1.3.10
Markdown 3.8.2
markdown-it-py 3.0.0
MarkupSafe 2.1.5
matplotlib 3.10.5
matplotlib-inline 0.1.7
mdit-py-plugins 0.5.0
mdurl 0.1.2
mistune 3.1.3
mlflow 2.18.0
mlflow-skinny 2.18.0
mpmath 1.3.0
multidict 6.6.4
myst-parser 4.0.0
natsort 8.4.0
nbclient 0.10.2
nbconvert 7.16.6
nbformat 5.10.4
nbsphinx 0.9.5
nest-asyncio 1.6.0
networkx 3.3
ninja 1.11.1.4
nncf 2.14.0
nodeenv 1.9.1
notebook 7.2.2
notebook_shim 0.2.4
numpy 1.26.3
nvidia-cublas-cu11 11.11.3.6
nvidia-cuda-cupti-cu11 11.8.87
nvidia-cuda-nvrtc-cu11 11.8.89
nvidia-cuda-runtime-cu11 11.8.89
nvidia-cudnn-cu11 9.1.0.70
nvidia-cufft-cu11 10.9.0.58
nvidia-curand-cu11 10.3.0.86
nvidia-cusolver-cu11 11.4.1.48
nvidia-cusparse-cu11 11.7.5.86
nvidia-nccl-cu11 2.20.5
nvidia-nvtx-cu11 11.8.86
omegaconf 2.3.0
onnx 1.17.0
open-clip-torch 2.24.0
openai 1.56.2
opencv-python 4.10.0.84
opentelemetry-api 1.36.0
opentelemetry-sdk 1.36.0
opentelemetry-semantic-conventions 0.57b0
openvino 2024.5.0
openvino-telemetry 2025.2.0
orjson 3.11.2
overrides 7.7.0
packaging 24.2
pandas 2.2.3
pandoc 2.4
pandocfilters 1.5.1
parso 0.8.4
pathtools 0.1.2
pexpect 4.9.0
pillow 11.3.0
pip 25.1
platformdirs 4.3.8
pluggy 1.6.0
plumbum 1.9.0
ply 3.11
pre_commit 4.0.1
prometheus_client 0.22.1
prompt_toolkit 3.0.51
propcache 0.3.2
protobuf 4.25.8
psutil 7.0.0
ptyprocess 0.7.0
pure_eval 0.2.3
pyarrow 18.1.0
pyasn1 0.6.1
pyasn1_modules 0.4.2
pycparser 2.22
pydantic 2.11.7
pydantic_core 2.33.2
pydata-sphinx-theme 0.16.1
pydot 2.0.0
pydub 0.25.1
Pygments 2.19.2
pymoo 0.6.1.5
pyparsing 3.2.3
pyproject-api 1.9.0
pytest 8.3.4
pytest-cov 6.0.0
pytest-json-report 1.5.0
pytest-metadata 3.1.1
pytest-mock 3.14.0
pytest-sugar 1.0.0
pytest-timeout 2.4.0
pytest-xdist 3.6.1
python-box 6.1.0
python-dateutil 2.9.0.post0
python-dotenv 1.0.1
python-json-logger 3.3.0
python-multipart 0.0.20
pytorch-lightning 2.5.3
pytorch-triton-rocm 3.4.0
pytorch-triton-xpu 3.4.0
pytz 2025.2
PyYAML 6.0.2
pyzmq 27.0.1
questionary 2.1.0
referencing 0.36.2
regex 2025.7.33
requests 2.32.4
requests-toolbelt 1.0.0
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rfc3987-syntax 1.1.0
rich 14.1.0
rich-argparse 1.7.1
rpds-py 0.27.0
rsa 4.9.1
ruff 0.12.9
safehttpx 0.1.6
safetensors 0.6.2
scikit-image 0.25.2
scikit-learn 1.7.1
scipy 1.15.3
semantic-version 2.10.0
Send2Trash 1.8.3
sentencepiece 0.2.1
sentry-sdk 2.35.0
setproctitle 1.3.6
setuptools 78.1.1
shellingham 1.5.4
simplejson 3.20.1
six 1.17.0
smmap 5.0.2
sniffio 1.3.1
snowballstemmer 3.0.1
soupsieve 2.7
Sphinx 8.1.3
sphinx-autodoc-typehints 2.5.0
sphinx-book-theme 1.1.3
sphinx-copybutton 0.5.2
sphinx_design 0.6.1
sphinxcontrib-applehelp 2.0.0
sphinxcontrib-devhelp 2.0.0
sphinxcontrib-htmlhelp 2.1.0
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 2.0.0
sphinxcontrib-serializinghtml 2.0.0
SQLAlchemy 2.0.43
sqlparse 0.5.3
stack-data 0.6.3
starlette 0.47.2
sympy 1.14.0
tabulate 0.9.0
tensorboard 2.18.0
tensorboard-data-server 0.7.2
termcolor 3.1.0
terminado 0.18.1
threadpoolctl 3.6.0
tifffile 2025.5.10
timm 1.0.19
tinycss2 1.4.0
tokenizers 0.20.3
tomli 2.2.1
tomlkit 0.13.3
torch 2.4.0+cu118
torchaudio 2.4.0+cu118
torchmetrics 1.8.0
torchvision 0.19.0+cu118
tornado 6.5.2
tox 4.23.2
tqdm 4.67.1
traitlets 5.14.3
transformers 4.46.3
triton 3.0.0 1
typer 0.16.1
types-python-dateutil 2.9.0.20250809
typeshed_client 2.8.2
typing_extensions 4.14.1
typing-inspection 0.4.1
tzdata 2025.2
uc-micro-py 1.0.3
uri-template 1.3.0
urllib3 2.5.0
uvicorn 0.35.0
virtualenv 20.34.0
wandb 0.15.9
wcwidth 0.2.13
webcolors 24.11.1
webencodings 0.5.1
websocket-client 1.8.0
websockets 14.2
Werkzeug 3.1.3
wheel 0.45.1
widgetsnbextension 4.0.14
wrapt 1.17.3
wurlitzer 3.1.1
yarl 1.20.1
zipp 3.23.0
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
相关介绍
- Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
- PyTorch 是一个深度学习框架,封装好了很多网络和深度学习相关的工具方便我们调用,而不用我们一个个去单独写了。它分为 CPU 和 GPU 版本,其他框架还有 TensorFlow、Caffe 等。PyTorch 是由 Facebook 人工智能研究院(FAIR)基于 Torch 推出的,它是一个基于 Python 的可续计算包,提供两个高级功能:1、具有强大的 GPU 加速的张量计算(如 NumPy);2、构建深度神经网络时的自动微分机制。
- Anomalib是一个深刻的学习库,旨在收集国家的技术异常检测的算法为基准,在公共和私人数据集。 Anomalib提供了若干准备使用的实现的异常检测算法中描述的最近的文献,以及一套工具,便于发展和执行情况的定制模型。 该图书馆拥有的强烈关注在视觉异常检测,其目的算法以检测和/或进行本地化的异常现象在图片或视频数据集。
- 关键特性
- 简单和模块化API和CLI培训、推断、制定基准,并hyperparameter优化。
- 最大的公共集合的使用准备好的深入学习的异常检测算法和基准数据集。
- 闪电基础的模式实现减少代码和限制的执行努力方面裸露的必需品。
- 大多数模型可以出口到OpenVINO中间表示(IR)加速推理在英特尔的硬件。
- 一个设定的推断工具,用于快速和容易部署的标准或定义的异常检测模型。
- Anomalib官方文档:https://anomalib.readthedocs.io/en/v2.1.0/
安装Anomalib 2.1.0
- 请查阅Anomalib:在Linux服务器上安装使用Anomalib 2.1.0,这里不再赘述。(Windows的安装方法,与此类似。)
训练自己的数据集进行异常检测
准备自己数据集
使用Anomalib 2.1.0进行训练
from anomalib.data import MVTecAD
from anomalib.engine import Engine
from anomalib.models import Patchcore
from pathlib import Path
from anomalib.data import Folder
# 1. Basic Usage
# Initialize with default settings
model = Patchcore()
# 2. Custom Configuration
# Configure model parameters
model = Patchcore(
backbone="wide_resnet50_2", # Feature extraction backbone
layers=["layer2", "layer3"], # Layers to extract features from
pre_trained=True, # Use pretrained weights
num_neighbors=9, # Number of nearest neighbors
)
# 3. Training Pipeline
# Default structure expects:
# - train/good: Normal (good) training images
# - test/good: Normal test images
# - test/defect: Anomalous test images
datamodule = Folder(
name="my_dataset",
root=Path("./datasets/my_dataset"),
normal_dir="good", # Subfolder containing normal images
abnormal_dir="defect", # Subfolder containing anomalous images
)
# Initialize training engine with specific settings
engine = Engine(
max_epochs=1, # Patchcore typically needs only one epoch
accelerator="auto", # Automatically detect GPU/CPU
devices=1, # Number of devices to use
)
# Train the model
engine.fit(
model=model,
datamodule=datamodule,
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3]
| Name | Type | Params | Mode
----------------------------------------------------------
0 | pre_processor | PreProcessor | 0 | train
1 | post_processor | PostProcessor | 0 | train
2 | evaluator | Evaluator | 0 | train
3 | model | PatchcoreModel | 24.9 M | train
----------------------------------------------------------
24.9 M Trainable params
0 Non-trainable params
24.9 M Total params
99.450 Total estimated model params size (MB)
19 Modules in train mode
174 Modules in eval mode
Selecting Coreset Indices.: 100%|████████████████████████████████████████████████████████████| 1638/1638 [00:02<00:00, 697.09it/s]
Epoch 0: 100%|██████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:04<00:00, 0.25it/s]`Trainer.fit` stopped: `max_epochs=1` reached.
Epoch 0: 100%|██████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:04<00:00, 0.22it/s]
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
使用Anomalib 2.1.0进行推理
# 1. Import required modules
from pathlib import Path
from anomalib.data import PredictDataset
from anomalib.engine import Engine
from anomalib.models import Patchcore
# 2. Initialize the model and load weights
model = Patchcore()
engine = Engine()
# 3. Prepare test data
# You can use a single image or a folder of images
dataset = PredictDataset(
path=Path("./datasets/my_dataset/defect/"),
image_size=(256, 256), # h,w,
)
# 4. Get predictions
predictions = engine.predict(
model=model,
dataset=dataset,
ckpt_path="./results/Patchcore/my_dataset/v0/weights/lightning/model.ckpt",
)
# 5. Access the results
if predictions is not None:
for prediction in predictions:
image_path = prediction.image_path
anomaly_map = prediction.anomaly_map # Pixel-level anomaly heatmap
pred_label = prediction.pred_label # Image-level label (0: normal, 1: anomalous)
pred_score = prediction.pred_score # Image-level anomaly score
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
$ python patchcore_inference_20250820.py
GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
Restoring states from the checkpoint path at results/Patchcore/my_datas et/v0/weights/lightning/model.ckpt
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3]
Loaded model weights from the checkpoint at results/Patchcore/my_datas et/v0/weights/lightning/model.ckpt
Field 'gt_mask' is None in item. Skipping visualization.
Predicting DataLoader 0: 100%|██████████████████████████████████████████████████████████| 8/8 [00:01<00:00, 5.53it/s]
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
参考
[1] Anomalib官方文档:https://anomalib.readthedocs.io/en/v2.1.0/
[2] https://github.com/open-edge-platform/anomalib.git
所有评论(0)