Home

Awesome

CutPaste

CutPaste: image from paper CutPaste: image from paper

Unofficial implementation of Google's "CutPaste: Self-Supervised Learning for Anomaly Detection and Localization" in PyTorch

Installation

To rerun experiments or try on your own dataset, first clone the repository and install requirements.txt.

$ git clone https://github.com/LilitYolyan/CutPaste.git
$ cd CutPaste
$ pip install -r requirements.txt

Self-supervised training

Run train.py to train self-supervised model on MVTec dataset

For 3 way classification head

$ python train.py --dataset_path path/to/your/dataset/ --num_class 3

For binary classification head

$ python train.py --dataset_path path/to/your/dataset/ --num_class 2

For feature extractor any torchvision model can be used. For example to use EfficientNet(B4)

$ python train.py --dataset_path path/to/your/dataset/ --encoder efficientnet_b4

To track training process with TensorBoard

tensorboard --logdir logdirs

Anomaly Detection

To run anomaly detection for MVTec with Gaussian Density Estimator

$ python anomaly_detection.py --checkpoint path/to/your/weights --data path/to/mvtec

TODO

Any contribution is appreciated!

Experiment Results

For more experiment results go to "experiments.md"

To train self-supervised model we used same hyperparameters as was used in paper:

HyperparameterValue
Number of epochs265
Batch size32
Learning rate0.03
Input size256

AUC comparison of our code and paper results

Defect NameCutPaste binary (ours)CutPaste binary (paper's)CutPaste 3way (ours)CutPaste 3way (paper's)
tile84.195.978.993.4
wood89.594.989.298.6
pill88.793.478.792.4
leather98.799.784.8100.0
hazelnut98.891.380.897.3
screw89.254.456.686.3
cable83.387.775.793.1
toothbrush94.799.278.698.3
capsule80.287.970.896.2
carpet57.967.926.193.1
zipper99.599.485.799.4
metal_nut91.596.889.799.3
bottle98.599.275.798.3
grid99.999.973.099.9
transistor84.496.485.595.5

ROC curves using embeddings from binary classification for self-supervised learning

<p float="left"> <img src="/experiments/roc_binary/bottle.jpg" width="260" /> <img src="experiments/roc_binary/cable.jpg" width="260" /> <img src="experiments/roc_binary/capsule.jpg" width="260" /> </p> <p float="left"> <img src="/experiments/roc_binary/carpet.jpg" width="260" /> <img src="experiments/roc_binary/grid.jpg" width="260" /> <img src="experiments/roc_binary/hazelnut.jpg" width="260" /> </p> <p float="left"> <img src="/experiments/roc_binary/leather.jpg" width="260" /> <img src="experiments/roc_binary/metal_nut.jpg" width="260" /> <img src="experiments/roc_binary/pill.jpg" width="260" /> </p> <p float="left"> <img src="experiments/roc_binary/toothbrush.jpg" width="260" /> <img src="/experiments/roc_binary/screw.jpg" width="260" /> <img src="experiments/roc_binary/tile.jpg" width="260" /> </p> <p float="left"> <img src="experiments/roc_binary/zipper.jpg" width="260" /> <img src="experiments/roc_binary/wood.jpg" width="260" /> <img src="/experiments/roc_binary/transistor.jpg" width="260" /> </p>

t-SNE visualisation of embeddings

<p float="left"> <img src="experiments/tsne_3way/grid_tsne.jpg" width="400" /> <img src="experiments/tsne_3way/wood_tsne.jpg" width="400" /> </p>