Unofficial pytorch implementation of Fourier Heat Map proposed in 'A Fourier Perspective on Model Robustness in Computer Vision' [Yin+, NeurIPS2019]
This is an unofficial pytorch implementation of Fourier Heat Map which is proposed in the paper, A Fourier Perspective on Model Robustness in Computer Vision [Yin+, NeurIPS2019].
Fourier Heat Map allows to investigate the sensitivity of CNNs to high and low frequency corruptions via a perturbation analysis in the Fourier domain.
We release v0.2.0. API is renewed and some useful libraries (e.g. hydra) are added.
Previous version is still available as v0.1.0.
Docker is supported. Now, you can evaluate Fourier Heat Map on the Docker container.
This library requires following as a pre-requisite.
Note that I run the code with Ubuntu 20, Pytorch 1.8.1, CUDA 11.0.
This repo uses poetry as a package manager.
The following code will install all necessary libraries under .venv/
.
$ git clone [email protected]:gatheluck/FourierHeatmap.git
$ cd FourierHeatmap
$ pip install poetry # If you haven't installed poetry yet.
$ poetry install
This codes expect datasets exist under data/
. For example, if you want to evaluate Fourier Heat Map for ImageNet, please set up like follows:
FourierHeatmap
├── data
│ └── imagenet
│ ├── train/
│ └── val/
The script fhmap/fourier/basis.py
generates Fourier base functions. For example:
$ poetry run python fhmap/fourier/basis.py
will generate 31x31 2D Fourier basis and save as an image under outputs/basis.png
. The generated image should be like follows.
The script fhmap/apps/eval_fhmap.py
eveluate Fourier Heat Map for a model. For example:
$ poetry run python fhmap/apps/eval_fhmap.py dataset=cifar10 arch=resnet56 weightpath=[PYTORCH_MODEL_WEIGHT_PATH] eps=4.0
will generate 31x31 Fourier Heat Map for ResNet56 on CIFAR-10 dataset and save as an image under outputs/eval_fhmap/
. The generated image should be like follows.
Note that the L2 norm size (=eps) of Fourier basis use in original paper is following:
dataset | eps |
---|---|
CIFAR-10 | 4.0 |
ImageNet | 15.7 |
If you want to evaluate Fourier Heat Map on your custom dataset, please refer follwing instraction.
Implement YourCustomDatasetStats
class:
YourCustomDatasetStats
class should inherit from original DatasetStats
class in factory/dataset
module and also shoud be placed in factory/dataset
module.Cifar10Stats
class in factory/dataset
module.Implement YourCustomDataModule
class:
YourCustomDataModule
class should inherit from BaseDataModule
class in factory/dataset
module and also shoud be placed in factory/dataset
module.Cifar10DataModule
class in factory/dataset
module.Implement YourCustomDatasetConfig
class:
YourCustomDatasetConfig
class should inherit from DatasetConfig
class in schema/dataset
module and also shoud be placed in schema/dataset
module. Please add YourCustomDatasetConfig
to schema/__init__
.Cifar10Config
class in schema/dataset
module.Add option for your custom dataset:
ConfigStore
class by adding a follwing line to apps/eval_fhmap
.s.store(group="dataset", name="yourcustomdataset", node=schema.YourCustomDatasetConfig)
Now, you will be able to call your custom dataset like following.
$ poetry run python fhmap/apps/eval_fhmap.py dataset=yourcustomdataset arch=resnet50 weightpath=[PYTORCH_MODEL_WEIGHT_PATH] eps=4.0
If you want to evaluate Fourier Heat Map on your custom architecture (model), please refer follwing instraction.
Implement YourCustomArch
class:
torch.nn.module
.factory/archs/resnet
module.Implement YourCustomArchConfig
class:
YourCustomArchConfig
class should inherit from ArchConfig
class in schema/arch
module and also shoud be placed in schema/arch
module. Please add YourCustomArchConfig
to schema/__init__
.Resnet56Config
class in schema/arch
module.Resnet50Config
class in schema/arch
module.Add option for your custom architecture:
ConfigStore
class by adding a follwing line to apps/eval_fhmap
.s.store(group="arch", name="yourcustomarch", node=schema.YourCustomArchConfig)
Now, you will be able to call your custom arch like following.
$ poetry run python fhmap/apps/eval_fhmap.py dataset=cifar10 arch=yourcustomarch weightpath=[PYTORCH_MODEL_WEIGHT_PATH] eps=4.0
In order to use FourierHeatmap throgh docker, please install Docker with NVIDIA Container Toolkit beforehand. For detail, please refere official installation guide.
If nvidia-smi
is able to run through docker like following, it is successfully installed.
$ sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi
Tue Apr 27 06:46:09 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.102.04 Driver Version: 450.102.04 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce GTX 1080 Off | 00000000:01:00.0 On | N/A |
| N/A 56C P0 42W / N/A | 1809MiB / 8114MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
We use environmental variables to specify the arguments. The variables that can be specified and their meanings are as follows:
name | optional | default | description |
---|---|---|---|
HOST_DATADIR | False | Path to the directory where the dataset is located in the host. | |
HOST_OUTPUTSDIR | False | Path to the directory where the output will be located in the host. | |
HOST_WEIGHTDIR | False | Path to the directory where the pretrained wight is located in the host. | |
WEIGHTFILE | False | File name of the pretrained wight. | |
ARCH | True | resnet56 | Name of the architecture. |
BATCH_SIZE | True | 512 | Size of batch. |
DATASET | True | cifar10 | Name of dataset. |
EPS | True | 4.0 | L2 norm size of Fourier basis. |
IGNORE_EDGE_SIZE | True | 0 | Size of the edge to ignore. |
NUM_SAMPLES | True | -1 | Number of samples used from dataset. If -1, use all samples. |
NVIDIA_VISIBLE_DEVICES | True | 0 | Device number (or list of number) visible from CUDA. |
For example:
$ export HOST_DATADIR=[DATASET_DIRECTORY_PATH]
$ export HOST_OUTPUTSDIR=[OUTPUTS_DIRECTORY_PATH]
$ export HOST_WEIGHTDIR=[WEIGHT_DIRECTORY_PATH]
$ export WEIGHTFILE=[PYTORCH_MODEL_FILE]
$ cd provision/docker
$ sudo -E docker-compose up # -E option is needed to inherit environment variables.
will generate 31x31 Fourier Heat Map for ResNet56 on CIFAR-10 dataset and save as an image under OUTPUTS_DIRECTORY_PATH/eval_fhmap/
.