Code Monkey home page Code Monkey logo

deep_mahalanobis_detector's Introduction

A Simple Unified Framework for Detecting Out-of-Distribution Samples and Adversarial Attacks

This project is for the paper "A Simple Unified Framework for Detecting Out-of-Distribution Samples and Adversarial Attacks". Some codes are from odin-pytorch, LID, and adversarial_image_defenses.

Preliminaries

It is tested under Ubuntu Linux 16.04.1 and Python 3.6 environment, and requries Pytorch package to be installed:

Downloading Out-of-Distribtion Datasets

We use download links of two out-of-distributin datasets from odin-pytorch:

Please place them to ./data/.

Downloading Pre-trained Models

We provide six pre-trained neural networks (1) three DenseNets trained on CIFAR-10, CIFAR-100 and SVHN, where models trained on CIFAR-10 and CIFAR-100 are from odin-pytorch, and (2) three ResNets trained on CIFAR-10, CIFAR-100 and SVHN.

Please place them to ./pre_trained/.

Detecting Out-of-Distribution Samples (Baseline and ODIN)

# model: ResNet, in-distribution: CIFAR-10, gpu: 0
python OOD_Baseline_and_ODIN.py --dataset cifar10 --net_type resnet --gpu 0

Detecting Out-of-Distribution Samples (Mahalanobis detector)

1. Extract detection characteristics:

# model: ResNet, in-distribution: CIFAR-10, gpu: 0
python OOD_Generate_Mahalanobis.py --dataset cifar10 --net_type resnet --gpu 0

2. Train simple detectors:

# model: ResNet
python OOD_Regression_Mahalanobis.py --net_type resnet

Detecting Adversarial Samples (LID & Mahalanobis detector)

0. Generate adversarial samples:

# model: ResNet, in-distribution: CIFAR-10, adversarial attack: FGSM  gpu: 0
python ADV_Samples.py --dataset cifar10 --net_type resnet --adv_type FGSM --gpu 0

1. Extract detection characteristics:

# model: ResNet, in-distribution: CIFAR-10, adversarial attack: FGSM  gpu: 0
python ADV_Generate_LID_Mahalanobis.py --dataset cifar10 --net_type resnet --adv_type FGSM --gpu 0

2. Train simple detectors:

# model: ResNet
python ADV_Regression.py --net_type resnet

deep_mahalanobis_detector's People

Contributors

pokaxpoka 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

deep_mahalanobis_detector's Issues

Validation on adversarial samples for OOD detection

In the paper you mention you validate the hyperparameters for the input processing (the FGSM magnitude) and the feature ensemble using adversarial samples (right part of Table 2 in the paper). I think this validation makes more sense than validation using OOD samples, since as you say these samples are often inaccessible a priori.

I cannot seem to find the part in the code for this validation, and was just wondering specifically how you validate the FGSM magnitude when you use the adversarial samples, since the in-distribution samples will also be preprocessed with FGSM in the same way as the adversarial samples, correct? Then I guess the only difference between in-dist and adv samples is that the adv samples are processed with one extra FGSM optimization step?

If you could clarify or point me to the code section, that would be great.

BTW nice work!

How you select the random_noise_size?

Dear author,

May I know how you select the random_noise_size based on different network architecture and dataset?

I'm going to use another dataset, how should I set the random_noise_size, min_pixel and max_pixel?

Thank you very much!
YH

Interpretation of output file

Could you please add information to the readme about how to interpret the output files? It's not clear what the values in the numpy arrays refer to.

How to train a One-Class Logistic Regressor?

Hi, many thanks for your work.
Recently, we want to compare our proposed method with this work.
In our settings, the model has no access to the ood data,
therefore, can we train a One-Class logistic regressor with your proposed code?
looking forward to your reply, and many thanks for your creative works.

pretrained model

hi, the links of pretrained model u posted are invalid([ResNet on CIFAR-10]/ [ResNet on CIFAR-100] / [ResNet on SVHN])
can i trouble u to update it

AUROC with M(x) scores?

While reading the paper I struggle to understand the following:

how to compute a AUROC score using the M(x) distance score? If the ground truth is 1, for in-distribution, and 0, for out-of-distribution, how to compute a AUROC if M(x) is e.g. - 639.2 (i.e. not a distribution)?

Thanks for your help!

About The OOD Testing Set

Hi, thanks for you amazing work.
In the source code, I find that this work uses the ood data for training a logistic regression model.
I wonder if this is fair for comparision with other method such as softmax baseline and ODIN.
Looking forward to your reply!! Many Thanks.

Model training scripts

Would you consider adding the scripts you used to train the Resnet and Densenet models?

Release License of the repository

Should we assume that this repository copyright its hold by authors or authors kindly released it under some open source license like MIT ?

best regards

How to calculate tied covariance matrix

As per the formulae given in the paper

image

which is equalivalent to calculate the covaraince matrix for each class and then take the weighted average to get the tied covariance matrix. But in the code,

precision = []
for k in range(num_output):
X = 0
for i in range(num_classes):
if i == 0:
X = list_features[k][i] - sample_class_mean[k][i]
else:
X = torch.cat((X, list_features[k][i] - sample_class_mean[k][i]), 0)
# find inverse
group_lasso.fit(X.cpu().numpy())
temp_precision = group_lasso.precision_
temp_precision = torch.from_numpy(temp_precision).float().cuda()
precision.append(temp_precision)

you are using sklearn.covariance.EmpiricalCovariance for all of the data (see line 117 X) but as per formulae you calculate the covariance for each class and then take the average. So I feel that we should apply sklearn.covariance.EmpiricalCovariance per class and then take the sum.

Thanks,

Baselines comparison

You merged the in-distribution and out-of-distribution test set and split out new train/val/test set for LR based on Mahalanobis score. However, you don't do it in the same way for ODIN and temperature scaling. Is that fair? At least, I suppose you can use the same subset to report and compare AUC.

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.