Giter VIP home page Giter VIP logo

anomaly-detection-patchsvdd-pytorch's Introduction

Patch SVDD

Patch SVDD for Image anomaly detection. Paper: https://arxiv.org/abs/2006.16067 (published in ACCV 2020).

An input image and a generated anomaly map for 'wood' class.

wood

Compatibility

The code runs on python 3.7, pytorch 1.3.1, and torchvision 0.4.2.

Installation

Step 1. Install libraries.

Step 2. Download MVTec AD dataset.

  • Download MVTec AD dataset: Download
  • Untar the 'mvtec_anomaly_detection.tar.xz' file.

Code examples

Step 1. Set the DATASET_PATH variable.

Set the DATASET_PATH to the root path of the downloaded MVTec AD dataset.

Step 2. Train Patch SVDD.

python main_train.py --obj=bottle --lr=1e-4 --lambda_value=1e-3 --D=64
  • obj denotes the name of the class out of 15 MVTec AD classes.
  • lr denotes the learning rate of Adam optimizer.
  • lambda_value denotes the value of 'lambda' in Eq. 6 of the paper.
  • D denotes the number of embedding dimension (default to 64).

Step 3. Evaluate the trained encoder.

python main_evaluate.py --obj=bottle
  • obj denotes the name of the class.

The script loads the trained encoder saved in ckpts/ directory. Note that the same evaluation procedure is performed at every training epoch in Step 2.

For a quick evaluation, trained encoders for cable and wood classes are included. Training (Step 2) can be skipped for those classes.

Step 4. Obtain anomaly maps.

python main_visualize.py --obj=bottle
  • obj denotes the name of the class.

The script generates and saves anomaly maps for all the test images in the obj class. The genereated maps are saved in anomaly_maps/obj directory.

anomaly-detection-patchsvdd-pytorch's People

Contributors

nuclearboy95 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

anomaly-detection-patchsvdd-pytorch's Issues

SSIM AE

Hi, wondering have you tested the SSIM-AE's performance in terms of detection?

Question on SVDD' loss

Hello,

Thank you for this insightful paper. I have a question regarding the SVDD' loss, equation (4) in your paper.

Lsvdd' ~ || f(p) - f(p') ||

What prevents your network for not returning zeros regardless p or p' (since there are no 'negative samples' in the loss) ?
In your code, the loss tend to converge to zero eventually.

How to distinguish good and bad cases?

Hello, author,
Thank you for your greate work!
I have one question when trying "python main_visualize.py --obj=bottle"
The good case visualize the following picture:
n077
It looks like all red,
In parellel the bad cases always visualize part of red in result picture as following:
n037

So I 'd like to know how to distinguish good and bad cases?
Thank you so much!

Inference on the custom dataset

Thanks for your works! I want to convert defect images to binary maps. My custom dataset only include positive samples (just like good in mvtecad dataset) and images with different defects.
How can I inference on my custom dataset without training? That seems like ground truth is required.
Thank you!

How can i visualize the output result?

It seems that only anomaly maps are visualized with the existing main_visualize.py code.

I want to check the output image that the network generates.

Doesn't anyone know?

For data custom dataset, Loss function does not converge.

I succeeded in learning with MVTech data.
After that, I want to learn with custom data.
I created a file structure like MVTech to train custom data.
I'm currently training wall images.
I'm going to learn 500 images of the wall.
python main_train.py --obj=wallcrack --lr=1e-4 --lambda_value=1e-3 --D=64 --epoch=500 used the command
However, the Loss value does not converge as shown below.
image

I would appreciate it if you could give me a Hyperparameter or some tips.

The picture below is an example of the image I want to learn.
Example)
image
image
image
image
image

Any hints on why train raises "ValueError: Only one class present in y_true. ROC AUC score is not defined in that case."?

Dear all, I'm trying to train PatchSVDD with my own dataset (aerial crop images). I structured the images in the MvTec Ad format, the train folder has 117 "good" images and the test folder has 3 classes (good, road and earth).

However, when I launch the train, I always get a "ValueError: Only one class present in y_true. ROC AUC score is not defined in that case." error right on the first epoch.

I tried both png and jpg file formats, and all images are 640x640 pixels (like in the example below).

If I use one of the MvTec examples (carpet, for example) the train steps perform normally.

Do you have any hints on how to solve this? Thank you very much!

téléchargement (2)

time consumption

great job! and can u tell me the time consumption per image during inferring, 256*256 image for example. thanks!

A trick to accelerate convergence

Training with the default config, it can hardly works and tends to learn trivial solution. However, it only needs to modify a small part, introduce negative examples on svdd loss (just extract different patches), now it can converge very quickly and achieve the effect quickly.

How can I tell if an object has a defect?

The visualization script shows a heat map that identifies the location of defects. However, in the test I detect the complete hazelnut, there are also parts marked in red. How should I determine what pictures are GOOD?

Unable to reproduce results for 'leather'

Hello, thank you for the great work and release of trained model weights.

I have managed to run the evaluation script for the wood model and get around 0.95 Image AUROC and 0.92 pixel AUROC, similar to reported results in paper.

However, for the 'leather' model, I only get about 0.501 image AUROC and 0.773 pixel AUROC when using your trained weights.
The reported numbers are based on det_64 and seg_64.

Do I need to put any specific settings to reproduce the results? Thanks

Performances of some classes drop when training more epochs

I don't quite understand why the performances of Carpet, Grid, Tile, and Screw classes getting worse and worse during training. The best performances of them almost all appear in the first epoch, and the Grid class even lasts at AUROC=0.5 for both detection and segmentation.
Does anyone knows why would this happen?

image

occlusion 검출에 대하여..

안녕하세요.

훌륭한 논문과 코드 잘 보았습니다.

해당 논문을 활용하여 순수한 얼굴 이미지와 손으로 가려진 얼굴이미지, 두개의 클래스로 나누어 학습을 진행해보고자 합니다.

드리고 싶은 질문은 두가지 입니다.

  1. 이미지 환경이 제한되어있는 기존의 anomaly detection데이터셋과 달리 다양한 얼굴이 입력으로 들어오는 지금의 경우에도 잘 작동할거라고 보시는지?

  2. ValueError: Found input variables with inconsistent numbers of samples: [16121856, 5373952]이러한 에러가 발생하는데 이는
    _ranking.py에서
    y_type = type_of_target(y_true, input_name="y_true")
    if not (y_type == "binary" or (y_type == "multiclass" and pos_label is not None)):
    raise ValueError("{0} format is not supported".format(y_type))
    check_consistent_length(y_true, y_score, sample_weight)
    y_true = column_or_1d(y_true)
    y_score = column_or_1d(y_score)
    assert_all_finite(y_true)
    assert_all_finite(y_score

y_true 와 y_score의 갯수가 달라서 발생하는 듯합니다. 그 이유를 혹시 아실까요?

긴글 읽어주셔서 감사합니다.

inspection.py 관련하여 문의드립니다.

안녕하세요?

inspection.py 관련하여 문의드립니다.

def eval_encoder_NN_multiK(enc, obj):
x_tr = mvtecad.get_x_standardized(obj, mode='train')
x_te = mvtecad.get_x_standardized(obj, mode='test')

embs64_tr = infer(x_tr, enc, K=64, S=16)
embs64_te = infer(x_te, enc, K=64, S=16)

x_tr = mvtecad.get_x_standardized(obj, mode='train')
x_te = mvtecad.get_x_standardized(obj, mode='test')

embs32_tr = infer(x_tr, enc.enc, K=32, S=4)
embs32_te = infer(x_te, enc.enc, K=32, S=4)

embs64 = embs64_tr, embs64_te
embs32 = embs32_tr, embs32_te

return eval_embeddings_NN_multiK(obj, embs64, embs32)

해당 코드에서, x_tr와 x_te를 두 번 부르셨는데, 혹시 굳이 두 번 불러야하는 이유가 있을까요?

제 custom dataset의 크기가 큰 상황이여서, 이쪽 코드에서 시간이 많이 들어가는 것 같아 두 번째 x_tr와 x_te를 빼도 괜찮을지 몰라서 문의드립니다.

How to reproduce the reported results?

Currently, with the default configs, we can't reach the reported results on some MVTec data categories in the paper. Would you possibly share the trained models or the best training parameters? Thanks.

About epoch value

Thanks for providing great code.

Is the epoch value different for each item? Or is it a same epoch value?
For example, is the bottle 10 times and the screw 20 times different?
If all epoch values come from the same epoch value, how many times is the performance presented in this paper obtained by running the epoch?

Thank you.

for windows anaconda users, a silly way to solve the problem of not being able to install 'ngtpy' under windows

Just edit inspection.py, change the 'ngt' method to 'kdt', I don’t know what will happen, but the program is running... :)
Is there any problem to do like this? @nuclearboy95

def eval_embeddings_NN_multiK(obj, embs64, embs32, NN=1):
emb_tr, emb_te = embs64
maps_64 = measure_emb_NN(emb_te, emb_tr, method='kdt', NN=NN)
maps_64 = distribute_scores(maps_64, (256, 256), K=64, S=16)
det_64, seg_64 = assess_anomaly_maps(obj, maps_64)

emb_tr, emb_te = embs32

maps_32 = measure_emb_NN(emb_te, emb_tr, method='ngt', NN=NN)

maps_32 = measure_emb_NN(emb_te, emb_tr, method='kdt', NN=NN)
maps_32 = distribute_scores(maps_32, (256, 256), K=32, S=4)
det_32, seg_32 = assess_anomaly_maps(obj, maps_32)

maps_sum = maps_64 + maps_32
det_sum, seg_sum = assess_anomaly_maps(obj, maps_sum)

maps_mult = maps_64 * maps_32
det_mult, seg_mult = assess_anomaly_maps(obj, maps_mult)

return {
    'det_64': det_64,
    'seg_64': seg_64,

    'det_32': det_32,
    'seg_32': seg_32,

    'det_sum': det_sum,
    'seg_sum': seg_sum,

    'det_mult': det_mult,
    'seg_mult': seg_mult,

    'maps_64': maps_64,
    'maps_32': maps_32,
    'maps_sum': maps_sum,
    'maps_mult': maps_mult,
}

Evaluate model problem

Thanks for your great work.
In main_evaluated.py the result is shown as
print( f'| K64 | Det: {det_64:.3f} Seg:{seg_64:.3f} | K32 | Det: {det_32:.3f} Seg:{seg_32:.3f} | sum | Det: {det_sum:.3f} Seg:{seg_sum:.3f} | mult | Det: {det_mult:.3f} Seg:{seg_mult:.3f} ({obj})')
However, in the paper, you report Random Encoder, Raw Patch and L2-AE. Is there some relationship between them?

AUC unusally high during 1st few epochs

I am training custom object class using my own dataset converted into MvTec Ad format. I am using small lambda value in the results below but its still the same for large lambda values.
Screenshot from 2021-03-17 11-58-40
Epochs: 300
Embedding dimension: 64

Can anyone tell me why this is happening?
I have also trained 'screw` class and the AUC is high only on 1st epoch and then it goes down drastically.

Over-sensitive

Hello everyone,

Thank you very much for the interesting work!

I trained the algorithm on a custom dataset containing mostly sensory and texture-like data.
The results contain many falsely identified anomalies (see second images) in normal data. Do you have any suggestion on preventing this over-sensitive behavior?
Thanks in advance!

image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.