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

Overview

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

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

  • Self-supervised model
  • Gaussian Density Estimator
  • EfficientNet Implementation
  • Localization

Any contribution is appreciated!

Comments
  • Bug in anomaly_detection.py

    Bug in anomaly_detection.py

    Hi~

    The GDE model should be built with pure embeddings of normal samples. However, in anomaly_detection.py, you build GDE model with embeddings of training samples. Some of these training samples have been transformed by CutPaste, which should be considered as anomalies. So I wonder whether there are some problems.

    opened by zhiyuanyou 10
  • About the train.py

    About the train.py

    hello! Thank you for your code contribution!I follow the setup instruction to run train.py , But I encountered the following problems:

    $ python train.py --dataset_path CutPaste-Li/mvtec/bottle --num_class 3

    Missing logger folder: tb_logs/exp1 Traceback (most recent call last): File "train.py", line 103, in model = CutPaste(hparams = args) File "train.py", line 19, in init self.model = CutPasteNet(encoder = hparams.encoder, pretrained = hparams.pretrained, dims = hparams.dims, num_class = hparams.num_class) File "/home/lwz/CutPaste-Li/model.py", line 49, in init super().init(encoder, pretrained, dims, num_class) File "/home/lwz/CutPaste-Li/model.py", line 23, in init self.out = nn.Linear(dims[-1], num_class) File "/home/lwz/.conda/envs/cutpasteli/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 85, in init self.weight = Parameter(torch.empty((out_features, in_features), **factory_kwargs)) TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of:

    • (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
    • (tuple of ints size, *, torch.memory_format memory_format, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

    I am really confused about that, could you please give me some advice? Is this a problem with the installation package version?

    opened by EricLee0224 3
  • Dataset organization

    Dataset organization

    Hello, First of all, thank you for your neat work! I managed to run your code on the bottle class of the Mvtec dataset by running the command:

    python train.py --dataset_path /home/username1/data/datasets/mvtec/bottle/train --num_class 3
    

    However when I tried to run the anomaly_detection.py, using the following command:

    python anomaly_detection.py --checkpoint ./tb_logs/exp1/version_3/checkpoints --data /data/username1/datasets/mvtec/bottle/test
    

    I did not obtain any results, the problem is that the all_checkpoint list in the main part of the anomaly_detection.py contains only the checkpoint I specified in the command above, which makes me wonder whether I am doing something wrong when specifying the command above and also I wonder whether you have a different dataset organization on your side. Because for now, I am having the following organization (the original):

    mvtec
    │   readme.
    │   license.txt    
    │
    └─── bottle
    │   │   readme.txt
    │   │   lisence.txt
    │   │
    │   └─── ground_truth
    │   |   └─── broken_large
    │   |   └─── broken_small   
    │   |   └─── contamination
    │   |
    |   └─── train
    |   |     └─── good
    |   |       |  000.png
    |   |       | ...
    │   |
    |   └─── test
    |   |     └─── broken_large
    |   |     | |  000.png  
    |   |     | |  ...     
    |   |     └───   broken_small
    |   |     | |  000.png    
    |   |     | |  ...
    |   |     └───   contamination
    |   |     | |   000.png
    |   |     | |  ...   
    |   |     └───   good
    |   |     | |  000.png 
    |   |     | | ...
    └─── cable
    | ...
    └─── zipper
    

    Can you please tell me whether you used a different organization? Because when I look at mvtec_anomaly_detection function I see that you build a path using the defect name + "train" and you do the same for "test". Thank you for your help.

    opened by nesrnesr 2
  • Problem with Dataset

    Problem with Dataset

    Hello, First of all, thank you for your neat work! I managed to run your code on the bottle class of the Mvtec dataset by running the command:

    python train.py --dataset_path /home/username1/data/datasets/mvtec/bottle/train --num_class 3
    

    However when I tried to run the anomaly_detection.py, using the following command:

    python anomaly_detection.py --checkpoint ./tb_logs/exp1/version_3/checkpoints --data /data/username1/datasets/mvtec/bottle/test
    

    I did not obtain any results, the problem is that the all_checkpoint list in the main part of the anomaly_detection.py contains only the checkpoint I specified in the command above, which makes me wonder whether I am doing something wrong when specifying the command above and also I wonder whether you have a different dataset organization on your side. Because for now, I am having the following organization (the original):

    mvtec
    │   readme.
    │   license.txt    
    │
    └─── bottle
    │   │   readme.txt
    │   │   lisence.txt
    │   │
    │   └─── ground_truth
    │   |   └─── broken_large
    │   |   └─── broken_small   
    │   |   └─── contamination
    │   |
    |   └─── train
    |   |     └─── good
    |   |       |  000.png
    |   |       | ...
    │   |
    |   └─── test
    |   |     └─── broken_large
    |   |     | |  000.png  
    |   |     | |  ...     
    |   |     └───   broken_small
    |   |     | |  000.png    
    |   |     | |  ...
    |   |     └───   contamination
    |   |     | |   000.png
    |   |     | |  ...   
    |   |     └───   good
    |   |     | |  000.png 
    |   |     | | ...
    └─── cable
    | ...
    └─── zipper
    

    Can you please tell me whether you used a different organization? Because when I look at mvtec_anomaly_detection function I see that you build a path using the defect name + "train" and you do the same for "test". Thank you for your help.

    Hello, How did you solve this problem? did you change the folders?

    Originally posted by @farzadips in https://github.com/LilitYolyan/CutPaste/issues/14#issuecomment-1079054692

    opened by farzadips 1
  • Scheduler,  model, batch size

    Scheduler, model, batch size

    This pull request contains following changes: 1.Scheduler is added to training loop 2.Encoder freezing 3.batch size is changed to be user defined argument

    opened by AnnaManasyan 0
  • localization.py

    localization.py

    Thank you very much for your contribution to the code, the code is very well written, I have a few questions and confusion? I hope you can find time to reply,I got the original heat map。 b2aa6725bb32fe23f72ce5cfa96a487 After the heat map and the original map are superimposed with a certain weight, is 06b5754453bfc7a5b83af560f71d807 I'm confused why? Which layer is the network of feature extraction, is the result of that layer

    opened by wfz2200220105 2
  • Bug in localization

    Bug in localization

    Hello, can you help: heatmap_on_image() in the final line of code of localization.py has 3 arguments, but while defining it, there were only 2 arguments. Also visualize_heatmap() is not defined Thanks

    opened by Chichiviriche 7
  • Getting problem when using ResNet50

    Getting problem when using ResNet50

    Getting problems on this.

        return torch._C._nn.linear(input, weight, bias)
    RuntimeError: mat1 and mat2 shapes cannot be multiplied (12x2048 and 512x512)
    

    I am getting this error with the ff command

    python train.py --dataset_path ../data/train --encoder resnet50 --pretrained --num_gpus 1
    

    My ../data/train is just composed of jpg images.

    I would just want to do self-supervised pretraining without annotations/labels.

    Seems like it has soemthing to do with this dims

        def __init__(self, encoder='resnet18', pretrained=True, dims=[512, 512, 512, 512, 512, 512, 512, 512, 128], num_class=3):
    

    What would be the dims for resnet50?

    opened by sarmientoj24 1
  • bug in Localization

    bug in Localization

    hi, great work. There seems to be a bug in image_patches = unfold(image).squeeze(0).reshape(-1, 3, *self.kernel_dim ) it's not unfolding correctly.

    This worked for me image_patches = unfold(image).squeeze(0).permute(1,0).reshape(-1, 3, *self.kernel_dim )

    btw, KDE is used for anomaly detection in the code, but in my experiments it's not necessary and slow. A simple Gaussian estimation using sklearn's mixture of gaussian estimator is fast and working fine. Just a suggestion.

    opened by yurenzhang 2
  • Bug of localization in train.py

    Bug of localization in train.py

    Hi,

    In train.py, there is a localization option shown like, parser.add_argument('--localization', default='False', choices=('True', 'False'), help='If True train on (64,64) cropped patches') However, if 'False' actually means True because 'False' is seen as string. So, the localization option is always True.

    opened by zhiyuanyou 2
Owner
Lilit Yolyan
AI Enthusiast, PhD student, Computer Vision Specialist
Lilit Yolyan
Unofficial implementation of Google's FNet: Mixing Tokens with Fourier Transforms

FNet: Mixing Tokens with Fourier Transforms Pytorch implementation of Fnet : Mixing Tokens with Fourier Transforms. Citation: @misc{leethorp2021fnet,

Rishikesh (ऋषिकेश) 218 Jan 5, 2023
Red Team tool for exfiltrating files from a target's Google Drive that you have access to, via Google's API.

GD-Thief Red Team tool for exfiltrating files from a target's Google Drive that you(the attacker) has access to, via the Google Drive API. This includ

Antonio Piazza 39 Dec 27, 2022
A large dataset of 100k Google Satellite and matching Map images, resembling pix2pix's Google Maps dataset.

Larger Google Sat2Map dataset This dataset extends the aerial ⟷ Maps dataset used in pix2pix (Isola et al., CVPR17). The provide script download_sat2m

null 34 Dec 28, 2022
Google-drive-to-sqlite - Create a SQLite database containing metadata from Google Drive

google-drive-to-sqlite Create a SQLite database containing metadata from Google

Simon Willison 140 Dec 4, 2022
This is an unofficial PyTorch implementation of Meta Pseudo Labels

This is an unofficial PyTorch implementation of Meta Pseudo Labels. The official Tensorflow implementation is here.

Jungdae Kim 320 Jan 8, 2023
An unofficial PyTorch implementation of a federated learning algorithm, FedAvg.

Federated Averaging (FedAvg) in PyTorch An unofficial implementation of FederatedAveraging (or FedAvg) algorithm proposed in the paper Communication-E

Seok-Ju Hahn 123 Jan 6, 2023
Unofficial PyTorch implementation of Attention Free Transformer (AFT) layers by Apple Inc.

aft-pytorch Unofficial PyTorch implementation of Attention Free Transformer's layers by Zhai, et al. [abs, pdf] from Apple Inc. Installation You can i

Rishabh Anand 184 Dec 12, 2022
Unofficial PyTorch implementation of Neural Additive Models (NAM) by Agarwal, et al.

nam-pytorch Unofficial PyTorch implementation of Neural Additive Models (NAM) by Agarwal, et al. [abs, pdf] Installation You can access nam-pytorch vi

Rishabh Anand 11 Mar 14, 2022
Unofficial implementation of Alias-Free Generative Adversarial Networks. (https://arxiv.org/abs/2106.12423) in PyTorch

alias-free-gan-pytorch Unofficial implementation of Alias-Free Generative Adversarial Networks. (https://arxiv.org/abs/2106.12423) This implementation

Kim Seonghyeon 502 Jan 3, 2023
Unofficial Pytorch Implementation of WaveGrad2

WaveGrad 2 — Unofficial PyTorch Implementation WaveGrad 2: Iterative Refinement for Text-to-Speech Synthesis Unofficial PyTorch+Lightning Implementati

MINDs Lab 104 Nov 29, 2022
The author's officially unofficial PyTorch BigGAN implementation.

BigGAN-PyTorch The author's officially unofficial PyTorch BigGAN implementation. This repo contains code for 4-8 GPU training of BigGANs from Large Sc

Andy Brock 2.6k Jan 2, 2023
Unofficial PyTorch Implementation of UnivNet: A Neural Vocoder with Multi-Resolution Spectrogram Discriminators for High-Fidelity Waveform Generation

UnivNet UnivNet: A Neural Vocoder with Multi-Resolution Spectrogram Discriminators for High-Fidelity Waveform Generation This is an unofficial PyTorch

MINDs Lab 170 Jan 4, 2023
StarGAN-ZSVC: Unofficial PyTorch Implementation

This repository is an unofficial PyTorch implementation of StarGAN-ZSVC by Matthew Baas and Herman Kamper. This repository provides both model architectures and the code to inference or train them.

Jirayu Burapacheep 11 Aug 28, 2022
Unofficial pytorch implementation for Self-critical Sequence Training for Image Captioning. and others.

An Image Captioning codebase This is a codebase for image captioning research. It supports: Self critical training from Self-critical Sequence Trainin

Ruotian(RT) Luo 906 Jan 3, 2023
Unofficial PyTorch Implementation of UnivNet: A Neural Vocoder with Multi-Resolution Spectrogram Discriminators for High-Fidelity Waveform Generation

UnivNet UnivNet: A Neural Vocoder with Multi-Resolution Spectrogram Discriminators for High-Fidelity Waveform Generation This is an unofficial PyTorch

MINDs Lab 54 Aug 30, 2021
Unofficial PyTorch implementation of Fastformer based on paper "Fastformer: Additive Attention Can Be All You Need"."

Fastformer-PyTorch Unofficial PyTorch implementation of Fastformer based on paper Fastformer: Additive Attention Can Be All You Need. Usage : import t

Hong-Jia Chen 126 Dec 6, 2022
Unofficial pytorch implementation of paper "One-Shot Free-View Neural Talking-Head Synthesis for Video Conferencing"

One-Shot Free-View Neural Talking Head Synthesis Unofficial pytorch implementation of paper "One-Shot Free-View Neural Talking-Head Synthesis for Vide

ZLH 406 Dec 23, 2022
Unofficial Pytorch Lightning implementation of Contrastive Syn-to-Real Generalization (ICLR, 2021)

Unofficial Pytorch Lightning implementation of Contrastive Syn-to-Real Generalization (ICLR, 2021)

Gyeongjae Choi 17 Sep 23, 2021
Unofficial PyTorch implementation of MobileViT based on paper "MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer".

MobileViT RegNet Unofficial PyTorch implementation of MobileViT based on paper MOBILEVIT: LIGHT-WEIGHT, GENERAL-PURPOSE, AND MOBILE-FRIENDLY VISION TR

Hong-Jia Chen 91 Dec 2, 2022