Git Product home page Git Product logo

odin's Introduction

ODIN: Out-of-Distribution Detector for Neural Networks

This is a PyTorch implementation for detecting out-of-distribution examples in neural networks. The method is described in the paper Enhancing The Reliability of Out-of-distribution Image Detection in Neural Networks by S. Liang, Yixuan Li and R. Srikant. The method reduces the false positive rate from the baseline 34.7% to 4.3% on the DenseNet (applied to CIFAR-10) when the true positive rate is 95%.

Experimental Results

We used two neural network architectures, DenseNet-BC and Wide ResNet. The PyTorch implementation of DenseNet-BC is provided by Andreas Veit and Brandon Amos. The PyTorch implementation of Wide ResNet is provided by Sergey Zagoruyko. The experimental results are shown as follows. The definition of each metric can be found in the paper. performance

Pre-trained Models

We provide four pre-trained neural networks: (1) two DenseNet-BC networks trained on CIFAR-10 and CIFAR-100 respectively; (2) two Wide ResNet networks trained on CIFAR-10 and CIFAR-100 respectively. The test error rates are given by:

Architecture CIFAR-10 CIFAR-100
DenseNet-BC 4.81 22.37
Wide ResNet 3.71 19.86

Running the code

Dependencies

  • CUDA 8.0

  • PyTorch

  • Anaconda2 or 3

  • At least three GPU

    Note: Reproducing results of DenseNet-BC only requires one GPU, but reproducing results of Wide ResNet requires three GPUs. Single GPU version for Wide ResNet will be released soon in the future.

Downloading Out-of-Distribtion Datasets

We provide download links of five out-of-distributin datasets:

Here is an example code of downloading Tiny-ImageNet (crop) dataset. In the root directory, run

mkdir data
cd data
wget https://www.dropbox.com/s/avgm2u562itwpkl/Imagenet.tar.gz
tar -xvzf Imagenet.tar.gz
cd ..

Downloading Neural Network Models

We provide download links of four pre-trained models.

Here is an example code of downloading DenseNet-BC trained on CIFAR-10. In the root directory, run

mkdir models
cd models
wget https://www.dropbox.com/s/wr4kjintq1tmorr/densenet10.pth.tar.gz
tar -xvzf densenet10.pth.tar.gz
cd ..

Running

Here is an example code reproducing the results of DenseNet-BC trained on CIFAR-10 where TinyImageNet (crop) is the out-of-distribution dataset. The temperature is set as 1000, and perturbation magnitude is set as 0.0014. In the root directory, run

cd code
# model: DenseNet-BC, in-distribution: CIFAR-10, out-distribution: TinyImageNet (crop)
# magnitude: 0.0014, temperature 1000, gpu: 0
python main.py --nn densenet10 --out_dataset Imagenet --magnitude 0.0014 --temperature 1000 --gpu 0

Note: Please choose arguments according to the following.

args

  • args.nn: the arguments of neural networks are shown as follows

    Nerual Network Models args.nn
    DenseNet-BC trained on CIFAR-10 densenet10
    DenseNet-BC trained on CIFAR-100 densenet100
  • args.out_dataset: the arguments of out-of-distribution datasets are shown as follows

    Out-of-Distribution Datasets args.out_dataset
    Tiny-ImageNet (crop) Imagenet
    Tiny-ImageNet (resize) Imagenet_resize
    LSUN (crop) LSUN
    LSUN (resize) LSUN_resize
    iSUN iSUN
    Uniform random noise Uniform
    Gaussian random noise Gaussian
  • args.magnitude: the optimal noise magnitude can be found below. In practice, the optimal choices of noise magnitude are model-specific and need to be tuned accordingly.

    Out-of-Distribution Datasets densenet10 densenet100 wideresnet10 wideresnet100
    Tiny-ImageNet (crop) 0.0014 0.0014 0.0005 0.0028
    Tiny-ImageNet (resize) 0.0014 0.0028 0.0011 0.0028
    LSUN (crop) 0 0.0028 0 0.0048
    LSUN (resize) 0.0014 0.0028 0.0006 0.002
    iSUN 0.0014 0.0028 0.0008 0.0028
    Uniform random noise 0.0014 0.0028 0.0014 0.0028
    Gaussian random noise 0.0014 0.0028 0.0014 0.0028
  • args.temperature: temperature is set to 1000 in all cases.

  • args.gpu: make sure you use the following gpu when running the code:

    Neural Network Models args.gpu
    densenet10 0
    densenet100 0
    wideresnet10 1
    wideresnet100 2

Outputs

Here is an example of output.

Neural network architecture:          DenseNet-BC-100
In-distribution dataset:                     CIFAR-10
Out-of-distribution dataset:     Tiny-ImageNet (crop)

                          Baseline         Our Method
FPR at TPR 95%:              34.8%               4.3%
Detection error:              9.9%               4.6%
AUROC:                       95.3%              99.1%
AUPR In:                     96.4%              99.2%
AUPR Out:                    93.8%              99.1%

License

Please refer to the LICENSE.

odin's People

Contributors

yixuanli 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  avatar

odin's Issues

AUPR calculation

In https://github.com/facebookresearch/odin/blob/master/code/calMetric.py#L180 precision and recall is computed as:

    tp = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
    fp = np.sum(np.sum(Y1 >= delta)) / np.float(len(Y1))
    if tp + fp == 0: 
        continue
    precision = tp / (tp + fp)
    recall = tp

But, values tp and fp are TPR and FPR which means that precision is computed as TPR/(TPR + FPR) which seems to be not the same as TP/(TP + FP).
Although, those values will coincide when the number of negatives equals the number of positive samples, which is true for all reported datasets, except for iSUN, am I right?

Where is the optimizer used?

I see the optimizer1 has been defined in cal.py here

optimizer1 = optim.SGD(net1.parameters(), lr = 0, momentum = 0)

But I didn't see it ever being used! Am I missing something? please let me know !

Thanks

about datasets

the download links of the datasets are not available,can you supply new links,thx!

About computing FPR at 95% TPR used in calMetric.py

Hi, thank you so much for your work. I would like to understand better on your tpr95 method.

Q1. For obtaining the results of FPR at 95% TPR, I would like to understand why the values of start and end were different when comparing your algorithm vs the baseline method, which uses an end of 0.1 and 0.01 for CIFAR10 and CIFAR100 respectively?

def tpr95(name):
...
    # calculate baseline
    if name == "CIFAR-10": 
	start = 0.1
	end = 1 
    if name == "CIFAR-100": 
	start = 0.01
	end = 1    
...
    # calculate our algorithm
...
    if name == "CIFAR-10": 
	start = 0.1
	end = 0.12 
    if name == "CIFAR-100": 
	start = 0.01
	end = 0.0104    

Q2. For the same function, how are the values "if tpr <= 0.9505 and tpr >= 0.9495:" chosen for 95% TPR? As in is it based on 4 decimal places or some other approach. I experimented with other datasets and models, and found that there were zerodivision errors errors if the tpr was slightly above 0.9505 (like 0.9545 for example)

Thank you so much!

Two TabErrors

Unfortunately, Python 3 treats tab errors as syntax errors...

flake8 testing of https://github.com/facebookresearch/odin on Python 3.6.3

$ _flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./code/cal.py:71:104: E999 TabError: inconsistent use of tabs and spaces in indentation
	testset = torchvision.datasets.CIFAR10(root='../data', train=False, download=True, transform=transform)
                                                                                                       ^
./code/calMetric.py:37:12: E999 TabError: inconsistent use of tabs and spaces in indentation
	start = 0.1
           ^
2     E999 TabError: inconsistent use of tabs and spaces in indentation
2

About Input Processing

Hi, @YixuanLi
I have a question about x˜ = x − εsign(-∇x log Syˆ(x; T)).
The loss (-∇x log Syˆ(x; T)) after sign function will get number (-1, 0, 1).
image

If all the image(OOD or ID) will get the same perturbation?

Thank you.
Jacky

About Input Processing code

In the paper, the equation of input preprocessing( eq(2) in paper) is x˜ = x − εsign(−∇x log Syˆ(x; T)).

This equation is introduced in your repository, https://github.com/facebookresearch/odin/blob/master/code/calData.py#L72

tempInputs = torch.add(inputs.data,  -noiseMagnitude1, gradient)

However, the gradient = torch.ge(inputs.grad.data, 0) in your line 65 represents sign(∇x log Syˆ(x; T))
In fact, I think that your code is to implement x˜ = x − εsign(∇x log Syˆ(x; T)) not x˜ = x − εsign(-∇x log Syˆ(x; T))

What's your opinion?

About the parameters δ, T, ε

Hi, The parameters are tuned by using ood val data. Can we generalize the tuned parameters for the other ood data? Or when we have new ood samples do we have to tune the parameters again?

Also, I am curious about the possible usage of this technique on signal data like radar signals.

Thanks

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.