Skip to content
/ OAL Public

[ICMR 2025] Enhancing OOD Detection Using Latent Diffusion

License

Notifications You must be signed in to change notification settings

HengGao12/OAL

Enhancing OOD Detection Using Latent Diffusion

Heng Gao, Jun Li
Fudan University


Usage

Installation

conda create -n openood python=3.8
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117
wget https://codeload.github.com/Vastlab/libMR/zip/refs/heads/master
cd python
pip install -r requirements.txt
cd ../
pip install .
cd ../
git clone https://github.com/Jingkang50/OpenOOD.git
cd OpenOOD
pip install -e .
pip install timm

In order to better adapt to the OpenOOD framework, we changed the vision_transformer.py in the Pytorch-Image-Models library as follows:

...
    def forward(self, x, return_feature):
        x = self.forward_features(x)
        x, pre_logits = self.forward_head(x)
        if return_feature:
            return x, pre_logits  
        else:
            return x
   
    def get_fc(self):
        fc = self.head
        return fc.weight.cpu().detach().numpy(), fc.bias.cpu().detach().numpy()

    def get_fc_layer(self):
        return self.head
...

Remark. All environment settings are listed in requirements.txt.

Data Preparation

Our codebase accesses the datasets from ./data/ and pretrained models from ./results/checkpoints/ . One can download the datasets via running ./scripts/download/download.py.

├── ...
├── data
│   ├── benchmark_imglist
│   ├── images_classic
│   └── images_largescale
├── openood
├── results
│   ├── checkpoints
│   └── ...
├── scripts
├── main.py
├── ...

Outlier Synthesis and Training

To synthesize OOD samples using Stable Diffusion, please refer to DreamOOD code repository for installation. Note that, here we use xformers==0.0.13. The synthesized data and token embeddings can be downloaded from this link. The following commands are the stepwise synthesis of OOD samples.

Get Token Embeddings

python outlier_generation/scripts/get_token_embed.py

Get ID embeddings from Transformers

# CIFAR-10
python scripts/get_id_features_cifar10.py
# CIFAR-100
python scripts/get_id_features_cifar100.py

Sample OOD data using k-NN distance search

# CIFAR-10
python scripts/get_embed_cifar10.py
# CIFAR-100
python scripts/get_embed_cifar100.py

Outlier Synthesis using Stable Diffusion

bash outlier_generation/generate_outliers_in_pixel_space.sh

OAL Training

Teacher Model Pretrained Weights

Here is the pretrained weights of teacher models for Knowledge Distillation.

In-Distribution Dataset Download Links
CIFAR-10 OneDrive
CIFAR-100 OneDrive
# Trained by OAL on CIAFR-10
bash scripts/basics/cifar10/train_cifar10_oal.sh
# Trained by OAL on CIFAR-100
bash scripts/basics/cifar100/train_cifar100_oal.sh
OOD Testing
# Test on CIFAR-100 using EBO score
bash scripts/ood/ebo/cifar100_test_ood_ebo.sh

# Test on CIFAR-10 using EBO score
bash scripts/ood/ebo/cifar10_test_ood_ebo.sh

Before running this command, please load the pretrained models for testing.

Benchmark Evaluation Results

img|center
img|center

The Leaderboard link is: https://zjysteven.github.io/OpenOOD/

Acknowledgments

OAL is developed based on OpenOOD, Pytorch-Image-Models and DreamOOD. Thanks to their great works.

About

[ICMR 2025] Enhancing OOD Detection Using Latent Diffusion

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published