PatchCore-Anomaly-Detection

80 downloads 0 likes on ModelScope updated

Model card

PatchCore 异常检测模型

基于 PatchCore (CVPR 2022) 的工业异常检测预训练模型,支持 MVTec AD 和 VisA 数据集。

模型信息

项目 内容
方法 PatchCore
骨干网络 WideResNet-50-2
特征层 layer2 + layer3
输入尺寸 256 × 256
框架 PyTorch

支持的类别

MVTec AD (15 类)

类别 AUROC F1
bottle 0.960 0.945
cable 0.991 0.942
capsule 0.943 0.836
carpet 0.992 0.956
grid 0.913 0.912
hazelnut 1.000 0.979
leather 1.000 0.984
metal_nut 0.980 0.938
pill 0.964 0.917
screw 0.819 0.649
tile 1.000 1.000
toothbrush 0.850 0.821
transistor 0.988 0.894
wood 0.984 0.952
zipper 0.747 0.742

平均 AUROC: 0.942

VisA (12 类)

类别 AUROC F1
candle 0.926 0.678
capsules 0.590 0.246
cashew 0.927 0.825
chewinggum 0.985 0.946
fryum 0.905 0.814
macaroni1 0.860 0.560
macaroni2 0.693 0.229
pcb1 0.961 0.810
pcb2 0.902 0.732
pcb3 0.918 0.750
pcb4 0.987 0.911
pipe_fryum 0.974 0.921

平均 AUROC: 0.886

使用方法

1. 安装依赖

pip install torch torchvision safetensors numpy scikit-learn tqdm Pillow

2. 下载模型

from modelscope import snapshot_download

model_dir = snapshot_download('Brilliantccc/PatchCore-Anomaly-Detection')

3. 推理示例

import torch
import json
from PIL import Image
import torchvision.transforms as transforms
from models.patchcore import PatchCore

# 加载模型
model = PatchCore.load('mvtec/bottle/patchcore_model.pth', device='cuda')

# 加载阈值
with open('mvtec/bottle/config.json') as f:
    threshold = json.load(f)['threshold']

# 预处理
transform = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

image = transform(Image.open('test.jpg').convert('RGB')).unsqueeze(0).cuda()

# 推理
with torch.no_grad():
    scores, _ = model.compute_anomaly_scores(image)

print(f'异常分数: {scores.item():.4f}')
print(f'是否异常: {scores.item() > threshold}')

模型文件说明

mvtec/bottle/
├── config.json                # 配置(含阈值)
├── patchcore_model.pth        # 模型参数
└── memory_bank.safetensors    # 记忆库

引用

@inproceedings{roth2022towards,
  title={Towards total recall in industrial anomaly detection},
  author={Roth, Karsten and Pemula, Latha and Zepeda, Joaquin and Sch{\"o}lkopf, Bernhard and Brox, Thomas and Gehler, Peter},
  booktitle={CVPR},
  year={2022}
}