Anomalib:在Linux服务器上安装使用Anomalib 2.1.0
Anomalib:在Linux服务器上安装使用Anomalib 2.1.0
- 前言
- 环境要求
- 相关介绍
- 安装使用Anomalib 2.1.0
- 下载Anomalib 2.1.0项目
- Linux
- requirements.txt
- 安装环境命令
- 准备数据集(这里以官方数据集为例)
- 使用Anomalib 2.1.0进行训练
- 使用Anomalib 2.1.0进行推理
- 常见问题
- ollama报错
- huggingface报错
- 参考
前言
- 由于本人水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏、人工智能混合编程实践专栏或我的个人主页查看
- 人工智能混合编程实践:C++调用Python ONNX进行YOLOv8推理
- 人工智能混合编程实践:C++调用封装好的DLL进行YOLOv8实例分割
- 人工智能混合编程实践:C++调用Python ONNX进行图像超分重建
- 人工智能混合编程实践:C++调用Python AgentOCR进行文本识别
- 通过计算实例简单地理解PatchCore异常检测
- Python将YOLO格式实例分割数据集转换为COCO格式实例分割数据集
- YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
- 基于DETR的人脸伪装检测
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)
- YOLOv5:TensorRT加速YOLOv5模型推理
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
- YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
- 使用Kaggle GPU资源免费体验Stable Diffusion开源项目
- Stable Diffusion:在服务器上部署使用Stable Diffusion WebUI进行AI绘图(v2.0)
- Stable Diffusion:使用自己的数据集微调训练LoRA模型(v2.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
相关介绍
- 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 2.1.0项目
- 官方源地址:https://github.com/open-edge-platform/anomalib.git
Linux
git clone -b v2.1.0 https://github.com/open-edge-platform/anomalib.git
cd anomalib/
requirements.txt
# requirements.txt
pandas==2.2.3
sphinx-autodoc-typehints==2.5.0
pytest-cov==6.0.0
openai==1.56.2
freia==0.2
pytest-timeout==2.4.0
lightning-utilities==0.15.2
pillow==11.3.0
pre-commit==4.0.1
transformers==4.46.3
omegaconf==2.3.0
nncf==2.14.0
pytest-sugar==1.0.0
gradio==5.8.0
myst-parser==4.0.0
ollama==0.4.0
mlflow==2.18.0
open-clip-torch==2.24.0
pandoc==2.4
tox==4.23.2
pytest-json-report
einops==0.8.1
sphinx-design==0.6.1
matplotlib==3.10.5
onnx==1.17.0
scikit-learn==1.7.1
av==14.0.0
gitpython==3.1.43
coverage==7.6.8
sphinx-book-theme==1.1.3
pytest-mock==3.14.0
pytest-xdist==3.6.1
tensorboard==2.18.0
nbsphinx==0.9.5
rich-argparse==1.7.1
jsonargparse==4.40.2
opencv-python==4.10.0.84
scikit-image==0.25.2
ipykernel==6.29.5
openvino==2024.5.0
pytest==8.3.4
notebook==7.2.2
wandb==0.15.9
numpy==1.26.3
kornia==0.8.1
docstring-parser==0.17.0
tifffile==2025.5.10
lightning==2.5.3
ipywidgets==8.1.5
sphinx==8.1.3
torchmetrics==1.8.0
rich==14.1.0
comet-ml==3.47.4
python-dotenv==1.0.1
sphinx-copybutton==0.5.2
timm==1.0.19# torch 2.4.0+cu118
# torchaudio 2.4.0+cu118
# torchmetrics 1.8.1
# torchvision 0.19.0+cu118
# pytorch-triton-xpu
# pytorch-triton-rocm
# commitizen
安装环境命令
# CUDA 11.8
pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu118
# linux
# pip install torch-2.4.0+cu118-cp310-cp310-linux_x86_64.whl -i https://mirrors.aliyun.com/pypi/simple
# pip install torchvision-0.19.0+cu118-cp310-cp310-linux_x86_64.whl -i https://mirrors.aliyun.com/pypi/simple
# pip install torchaudio-2.4.0+cu118-cp310-cp310-linux_x86_64.whl -i https://mirrors.aliyun.com/pypi/simplepip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simplepip install -e . -i https://mirrors.aliyun.com/pypi/simpleanomalib install -v
如果没有报错,则安装成功。
准备数据集(这里以官方数据集为例)
使用Anomalib 2.1.0进行训练
from anomalib.data import MVTecAD
from anomalib.engine import Engine
from anomalib.models import Patchcore# 1. Basic Usage
# Initialize with default settings
model = Patchcore()# 2. Custom Configuration
# Configure model parameters
model = Patchcore(backbone="wide_resnet50_2", # Feature extraction backbonelayers=["layer2", "layer3"], # Layers to extract features frompre_trained=True, # Use pretrained weightsnum_neighbors=9, # Number of nearest neighbors
)# 3. Training Pipeline
# Set up the complete training pipeline
datamodule = MVTecAD(root="./datasets/MVTecAD",category="bottle",train_batch_size=32,eval_batch_size=32, # Important for feature extraction
)# Initialize training engine with specific settings
engine = Engine(max_epochs=1, # Patchcore typically needs only one epochaccelerator="auto", # Automatically detect GPU/CPUdevices=1, # Number of devices to use
)# Train the model
engine.fit(model=model,datamodule=datamodule,
)
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%|█████████████████████████████████████████████| 21401/21401 [00:36<00:00, 580.21it/s]
Epoch 0: 100%|█████████████████████████████████████████████████████████████████████████| 7/7 [00:40<00:00, 0.17it/s]`Trainer.fit` stopped: `max_epochs=1` reached.
Epoch 0: 100%|█████████████████████████████████████████████████████████████████████████| 7/7 [00:41<00:00, 0.17it/s]
使用Anomalib 2.1.0进行推理
# 1. Import required modules
from pathlib import Pathfrom 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/MVTecAD/bottle/test"),image_size=(256, 256),
)# 4. Get predictions
predictions = engine.predict(model=model,dataset=dataset,ckpt_path="./results/Patchcore/MVTecAD/bottle/v1/weights/lightning/model.ckpt",
)# 5. Access the results
if predictions is not None:for prediction in predictions:image_path = prediction.image_pathanomaly_map = prediction.anomaly_map # Pixel-level anomaly heatmappred_label = prediction.pred_label # Image-level label (0: normal, 1: anomalous)pred_score = prediction.pred_score # Image-level anomaly score
$ 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/MVTecAD/bottle/v1/weights/lightning/model.ckpt
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3]
Loaded model weights from the checkpoint at results/Patchcore/MVTecAD/bottle/v1/weights/lightning/model.ckpt
Field 'gt_mask' is None in item. Skipping visualization.
Predicting DataLoader 0: 100%|████████████████████████████████████████████████████████████████████████| 83/83 [00:11<00:00, 7.06it/s]
常见问题
ollama报错
ImportError: cannot import name 'Image' from 'ollama' (/home/pc/anaconda3/envs/anomalib_env/lib/python3.10/site-packages/ollama/__init__.py)
暂时未找到原因,和很好的解决方法。
目前,可以将ollama卸载,可以解决此报错
huggingface报错
huggingface_hub.errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.
用国内镜像加速下载Hugging Face 的国内镜像站(如 hf-mirror.com)可解决网络问题。
export HF_ENDPOINT="https://hf-mirror.com"
参考
[1] Anomalib官方文档:https://anomalib.readthedocs.io/en/v2.1.0/
[2] https://github.com/open-edge-platform/anomalib.git
- 由于本人水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏、人工智能混合编程实践专栏或我的个人主页查看
- 人工智能混合编程实践:C++调用Python ONNX进行YOLOv8推理
- 人工智能混合编程实践:C++调用封装好的DLL进行YOLOv8实例分割
- 人工智能混合编程实践:C++调用Python ONNX进行图像超分重建
- 人工智能混合编程实践:C++调用Python AgentOCR进行文本识别
- 通过计算实例简单地理解PatchCore异常检测
- Python将YOLO格式实例分割数据集转换为COCO格式实例分割数据集
- YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
- 基于DETR的人脸伪装检测
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)
- YOLOv5:TensorRT加速YOLOv5模型推理
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
- YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
- 使用Kaggle GPU资源免费体验Stable Diffusion开源项目
- Stable Diffusion:在服务器上部署使用Stable Diffusion WebUI进行AI绘图(v2.0)
- Stable Diffusion:使用自己的数据集微调训练LoRA模型(v2.0)