Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of Anomalib adversary #256

Merged
merged 40 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ece832f
Add batch_c15n for [0,1] image input and imagenet-normalized input.
mzweilin May 2, 2024
726a0af
Turn off inference mode before creating perturbations.
mzweilin May 2, 2024
b0c3079
Switch to training mode before running LightningModule.training_step().
mzweilin May 2, 2024
eba3c2b
Add utils for config instantiation.
mzweilin May 2, 2024
dd10c79
Add mart.utils.Get() to extract a value from kwargs dict.
mzweilin May 2, 2024
43d63f5
Upgrade Lightning from 2.0 to 2.1.
mzweilin May 2, 2024
f4f1f1a
Move most dependencies to optional groups.
mzweilin May 2, 2024
a18b691
Avoid importing everything in mart.
mzweilin May 2, 2024
6c8a99d
Make a separate naming space mart.transforms.objdet for object detect…
mzweilin May 2, 2024
e0dc984
Comment
mzweilin May 14, 2024
b51d3be
Merge branch 'main' into mzweilin/instantiate_config
mzweilin May 15, 2024
f4e9acc
Clean up.
mzweilin May 15, 2024
b283bf1
Move to mart.nn.Get().
mzweilin May 15, 2024
976f77e
Merge branch 'mzweilin/instantiate_config' into mzweilin/get_value_fr…
mzweilin May 15, 2024
4c0de4f
Merge branch 'mzweilin/get_value_from_kwargs' into mzweilin/upgrade_l…
mzweilin May 15, 2024
b96a99a
Revert changes in transforms.
mzweilin May 15, 2024
89eea77
Revert changes in __init__.py
mzweilin May 15, 2024
9491cc4
Revert changes in pyproject.toml.
mzweilin May 15, 2024
ec63b73
Skip object detection tests if pycocotools is not installed.
mzweilin May 15, 2024
e063944
Simplify dependency with newer lightning 2.1+.
mzweilin May 15, 2024
63286d0
Clean up comments.
mzweilin May 15, 2024
d725ea3
Remove the version constraint on rich, due to the conflict with Anoma…
mzweilin May 15, 2024
8d19d9b
Make a package anomalib_adversary
mzweilin May 15, 2024
94d4d44
Add two model configs for Anomalib.
mzweilin May 15, 2024
b85aaf1
Add README.
mzweilin May 15, 2024
3c0f172
Update README
mzweilin May 15, 2024
101940d
Replace Anomalib config files with command lines.
mzweilin May 16, 2024
d71cfd5
Merge branch 'main' into mzweilin/example_anomalib_adversary
mzweilin May 17, 2024
ed21be6
Add an empty configs folder, because mart.generate_config requires a …
mzweilin May 17, 2024
f74c293
Point dependency to MART@main.
mzweilin May 17, 2024
061b502
Parameterize module step function in AdversaryConnector
dxoigmn May 18, 2024
ceb7ccb
HACK: Gain function that clones it target
dxoigmn May 18, 2024
310186e
Update README with WinClip example
dxoigmn May 18, 2024
187ac53
Update MART dependency.
mzweilin Jun 5, 2024
e0117ae
Mordenize packaging with pyproject.toml.
mzweilin Jun 5, 2024
3b016d0
Revert "Parameterize module step function in AdversaryConnector"
mzweilin Jun 5, 2024
4fb0690
Revert "HACK: Gain function that clones it target"
mzweilin Jun 5, 2024
7500539
Revert "Update README with WinClip example"
mzweilin Jun 5, 2024
6cc6df6
Update README.
mzweilin Jun 5, 2024
2621460
Add a comma to make consistent lines.
mzweilin Jun 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/anomalib_adversary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
results
datasets
96 changes: 96 additions & 0 deletions examples/anomalib_adversary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Adversarial Robustness of Anomaly Detection

This project demonstrates how to generate adversarial examples against anomaly detection models in [Anomalib](https://github.com/openvinotoolkit/anomalib).

## Installation

Anomalib requires Python 3.10+.

```sh
pip install -e .
```

## Experiment

0. \[Optional\] Soft link the existing datasets folder from Anomalib if you have downloaded datasets before with Anomalib.

```sh
ln -s {PATH_TO_ANOMALIB_REPO}/datasets .
```

1. Train a model. We add an EarlyStopping callback in command line.

```sh
CUDA_VISIBLE_DEVICES=0 anomalib train \
--data anomalib.data.MVTec \
--data.category transistor \
--model Stfpm \
--trainer.callbacks lightning.pytorch.callbacks.EarlyStopping \
--trainer.callbacks.patience 5 \
--trainer.callbacks.monitor pixel_AUROC \
--trainer.callbacks.mode max
```

2. Evaluate the trained model without adversary as baseline.

```sh
CUDA_VISIBLE_DEVICES=0 anomalib test \
--data anomalib.data.MVTec \
--data.category transistor \
--model Stfpm \
--ckpt_path=results/Stfpm/MVTec/transistor/latest/weights/lightning/model.ckpt
```

```console
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Test metric ┃ DataLoader 0 ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ image_AUROC │ 0.8733333349227905 │
│ image_F1Score │ 0.7945205569267273 │
│ pixel_AUROC │ 0.7860202789306641 │
│ pixel_F1Score │ 0.46384868025779724 │
└───────────────────────────┴───────────────────────────┘
```

3. Generate an adversary config file from MART.

```sh
python -m mart.generate_config \
--config_dir="configs" \
--export_node=callbacks.adversary_connector \
--resolve=True \
callbacks=adversary_connector \
batch_c15n@callbacks.adversary_connector.batch_c15n=dict_imagenet_normalized \
callbacks.adversary_connector.adversary=$\{attack\} \
+attack=classification_fgsm_linf \
~attack.gain \
+attack.gain._target_=mart.nn.Get \
+attack.gain.key=loss \
attack.objective=null \
attack.eps=10 \
attack.callbacks.progress_bar.enable=true \
> anomalib_fgsm_linf_10.yaml
```

4. Run attack. We add a MART callback that loads the attack config file we just generated [./anomalib_fgsm_linf_10.yaml](./anomalib_fgsm_linf_10.yaml).

```sh
CUDA_VISIBLE_DEVICES=0 anomalib test \
--data anomalib.data.MVTec \
--data.category transistor \
--model Stfpm \
--trainer.callbacks mart.utils.CallbackInstantiator \
--trainer.callbacks.cfg_path ./anomalib_fgsm_linf_10.yaml \
--ckpt_path=results/Stfpm/MVTec/transistor/latest/weights/lightning/model.ckpt
```

```console
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Test metric ┃ DataLoader 0 ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ image_AUROC │ 0.5979167222976685 │
│ image_F1Score │ 0.5714285969734192 │
│ pixel_AUROC │ 0.686780571937561 │
│ pixel_F1Score │ 0.0955422893166542 │
└───────────────────────────┴───────────────────────────┘
```
Empty file.
15 changes: 15 additions & 0 deletions examples/anomalib_adversary/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "anomalib_adversary"
version = "0.1.0a"
description = "Evaluating robustness of anomaly detection models in Anomalib."
authors = [
{name = "Intel Corporation"}
]

dependencies = [
"anomalib[full]==1.0.1",
"mart @ https://github.com/IntelLabs/MART/archive/refs/tags/v0.6.1.zip",
]

[tool.setuptools.packages.find]
include = ["anomalib_adversary*"]
Loading