PatchCore MVTec AD ONNX models
Browse files- README.md +67 -0
- backbone.onnx +3 -0
- knn_bottle.onnx +3 -0
- knn_grid.onnx +3 -0
- knn_metal_nut.onnx +3 -0
- thresholds.json +20 -0
README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nc-sa-4.0
|
| 3 |
+
library_name: onnx
|
| 4 |
+
pipeline_tag: image-classification
|
| 5 |
+
tags:
|
| 6 |
+
- anomaly-detection
|
| 7 |
+
- patchcore
|
| 8 |
+
- mvtec-ad
|
| 9 |
+
- onnx
|
| 10 |
+
- industrial-inspection
|
| 11 |
+
datasets:
|
| 12 |
+
- mvtec-ad
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# PatchCore on MVTec AD (ONNX, runs in the browser)
|
| 16 |
+
|
| 17 |
+
From-scratch PyTorch reimplementation of **PatchCore** (Roth et al., *Towards Total Recall in Industrial Anomaly
|
| 18 |
+
Detection*, CVPR 2022), exported to ONNX. Fitted on **defect-free images only**; defects are found as patches that
|
| 19 |
+
are far from every patch seen on good parts.
|
| 20 |
+
|
| 21 |
+
**Live demo (in your browser, no server):** [yandyassine/patchcore-defect-detection](https://huggingface.co/spaces/yandyassine/patchcore-defect-detection) ·
|
| 22 |
+
**Code & write-up:** [COMPUTER-VISION-PROJECT-LIBRARY / 13](https://github.com/yandouziyassine/COMPUTER-VISION-PROJECT-LIBRARY/tree/main/13-anomaly-detection-patchcore)
|
| 23 |
+
|
| 24 |
+
## Results (MVTec AD test set, image level)
|
| 25 |
+
|
| 26 |
+
| Category | Test images | Image AUROC | F1 at the shipped threshold |
|
| 27 |
+
|---|---|---|---|
|
| 28 |
+
| bottle | 83 | 1.000 | 1.000 |
|
| 29 |
+
| metal_nut | 115 | 0.999 | 0.995 |
|
| 30 |
+
| grid | 78 | 0.977 | 0.974 |
|
| 31 |
+
|
| 32 |
+
These numbers were re-measured **with the ONNX files in this repo** (fp16-stored weights), and match the PyTorch
|
| 33 |
+
model exactly at this precision.
|
| 34 |
+
|
| 35 |
+
## Files
|
| 36 |
+
|
| 37 |
+
| File | What it does | Size |
|
| 38 |
+
|---|---|---|
|
| 39 |
+
| `backbone.onnx` | image `(1,3,224,224)` → patch features `(784, 1536)`: WideResNet-50-2 layers 2+3, 3×3 local averaging | 50 MB |
|
| 40 |
+
| `knn_<category>.onnx` | features → anomaly map `(224,224)` + image score; memory bank (10 % greedy coreset) baked in | 50–64 MB |
|
| 41 |
+
| `thresholds.json` | per-category decision threshold (best F1 on the test set) | |
|
| 42 |
+
|
| 43 |
+
Weights are stored as float16 and cast to float32 inside the graph, so the math runs in float32.
|
| 44 |
+
|
| 45 |
+
## Use
|
| 46 |
+
|
| 47 |
+
Preprocessing: resize the shorter side to 256, center-crop 224, ImageNet mean/std normalization.
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
import numpy as np, onnxruntime as ort
|
| 51 |
+
bb = ort.InferenceSession("backbone.onnx")
|
| 52 |
+
knn = ort.InferenceSession("knn_bottle.onnx")
|
| 53 |
+
feats = bb.run(None, {"image": x})[0] # x: float32 (1,3,224,224)
|
| 54 |
+
amap, score = knn.run(None, {"features": feats})
|
| 55 |
+
defect = score > 1.9728 # thresholds.json["bottle"]["threshold"]
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
## Limitations
|
| 59 |
+
|
| 60 |
+
- One model per product; a photo of another object will look anomalous everywhere (by design).
|
| 61 |
+
- Thresholds were picked on the MVTec test set itself, so the F1 column is optimistic; AUROC is threshold-free.
|
| 62 |
+
- Trained at 224 px; very small defects can be missed.
|
| 63 |
+
|
| 64 |
+
## License
|
| 65 |
+
|
| 66 |
+
Memory banks are derived from MVTec AD, licensed **CC BY-NC-SA 4.0**: non-commercial use only. Backbone weights:
|
| 67 |
+
torchvision ImageNet WideResNet-50-2.
|
backbone.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:220153c1693b2fc5a00b9f3261c35ab4ec3d66ac2e554d596407a8e83c867e5e
|
| 3 |
+
size 49734896
|
knn_bottle.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:11683ee720d10143b3ecf9841f0e116eb1e0dfc8d0d9ea93edec08bc29539793
|
| 3 |
+
size 50373353
|
knn_grid.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:223d64a726c168d1b2d56003b7150c1dd110a9ef92ea77e8d8f679734d58b0b9
|
| 3 |
+
size 63628441
|
knn_metal_nut.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:32b3aa7b53bc043d9b70ccf90d7d5806c4c65646c0c15c7a3afaf1b5eebc1d1d
|
| 3 |
+
size 53026215
|
thresholds.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bottle": {
|
| 3 |
+
"threshold": 1.9728,
|
| 4 |
+
"f1": 1.0,
|
| 5 |
+
"precision": 1.0,
|
| 6 |
+
"recall": 1.0
|
| 7 |
+
},
|
| 8 |
+
"metal_nut": {
|
| 9 |
+
"threshold": 2.1994,
|
| 10 |
+
"f1": 0.9947,
|
| 11 |
+
"precision": 0.9894,
|
| 12 |
+
"recall": 1.0
|
| 13 |
+
},
|
| 14 |
+
"grid": {
|
| 15 |
+
"threshold": 1.7589,
|
| 16 |
+
"f1": 0.9735,
|
| 17 |
+
"precision": 0.9821,
|
| 18 |
+
"recall": 0.9649
|
| 19 |
+
}
|
| 20 |
+
}
|