generated from ashleve/lightning-hydra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Move RobustBench and Carla stuff from mart
to examples
#183
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57c90bf
Move RobustBench stuff to examples outside the MART package.
mzweilin 576fa01
Update README.
mzweilin 4940717
Move Carla and RobustBench stuff into examples.
mzweilin 08e83cb
Update README.
mzweilin cb5978f
Update README.
mzweilin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,65 +46,8 @@ debug: ## Enter debugging mode with pdb, an example. | |
# | ||
python -m pdb -m mart experiment=CIFAR10_CNN debug=default | ||
|
||
.PHONY: cifar_attack | ||
cifar_attack: ## Evaluate adversarial robustness of a CIFAR-10 model from robustbench, expect 0.6171875 | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=gpu \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_autoattack \ | ||
+model.test_sequence.seq005=input_adv_test \ | ||
model.test_sequence.seq010.preprocessor=["input_adv_test"] | ||
|
||
.PHONY: cifar_train | ||
cifar_train: ## Adversarial training for a CIFAR-10 model. | ||
python -m mart experiment=CIFAR10_CNN_Adv \ | ||
fit=true \ | ||
trainer=gpu | ||
|
||
|
||
# Download and extract dataset of carla_over_obj_det | ||
CARLA_OVERHEAD_DATASET_TRAIN ?= data/carla_over_obj_det/train/kwcoco_annotations.json | ||
CARLA_OVERHEAD_DATASET_DEV ?= data/carla_over_obj_det/dev/kwcoco_annotations.json | ||
|
||
data/carla_over_obj_det/carla_over_od_dev_1.0.0.tar.gz: | ||
mkdir -p $(@D) | ||
wget -O $@ https://armory-public-data.s3.us-east-2.amazonaws.com/carla/carla_over_od_dev_1.0.0.tar.gz | ||
|
||
$(CARLA_OVERHEAD_DATASET_DEV): data/carla_over_obj_det/carla_over_od_dev_1.0.0.tar.gz | ||
tar -zxf $< -C data/carla_over_obj_det | ||
|
||
data/carla_over_obj_det/carla_over_od_train_val_1.0.0.tar.gz: | ||
mkdir -p $(@D) | ||
wget -O $@ https://armory-public-data.s3.us-east-2.amazonaws.com/carla/carla_over_od_train_val_1.0.0.tar.gz | ||
|
||
$(CARLA_OVERHEAD_DATASET_TRAIN): data/carla_over_obj_det/carla_over_od_train_val_1.0.0.tar.gz | ||
tar -zxf $< -C data/carla_over_obj_det | ||
|
||
|
||
.PHONY: carla_train | ||
carla_train: $(CARLA_OVERHEAD_DATASET_TRAIN) $(CARLA_OVERHEAD_DATASET_DEV) ## Train Faster R-CNN with the CarlaOverObjDet dataset from Armory. | ||
python -m mart \ | ||
experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
trainer.precision=16 \ | ||
fit=true \ | ||
tags=["regular_training","backbone_ImageNetPretrained"] \ | ||
|
||
|
||
# You need to specify weights of target model in [model.modules.losses_and_detections.model.weights_fpath]. | ||
.PHONY: carla_attack | ||
carla_attack: $(CARLA_OVERHEAD_DATASET_TRAIN) $(CARLA_OVERHEAD_DATASET_DEV) ## Evaluate adversarial robustness of a pretrained model. | ||
python -m mart \ | ||
experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
fit=false \ | ||
model.modules.losses_and_detections.model.weights_fpath=null \ | ||
[email protected]_adv_test=object_detection_mask_adversary \ | ||
model.modules.input_adv_test.optimizer.lr=5 \ | ||
model.modules.input_adv_test.max_iters=50 \ | ||
+model.test_sequence.seq001.input_adv_test._call_with_args_=[input,target] \ | ||
+model.test_sequence.seq001.input_adv_test.model=model \ | ||
+model.test_sequence.seq001.input_adv_test.step=step \ | ||
model.test_sequence.seq010.preprocessor=[input_adv_test] \ | ||
tags=["MaskPGD50_LR5"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,41 +68,12 @@ pre-commit install | |
|
||
## How to run | ||
|
||
The [configs](/configs) folder is required to run the toolkit. You can evaluate adversarial robustness of pretrained models with chosen experiment configuration from [configs/experiment/](configs/experiment/). Feel free to override any parameter from command line. Run `make` to learn more tasks pre-defined in [Makefile](Makefile). | ||
The toolkit comes with built-in experiment configurations in [mart/configs](mart/configs). | ||
|
||
```bash | ||
# run on CPU | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=default \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_eps8_pgd10_step1 | ||
|
||
# run on GPU | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=gpu \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_eps8_pgd10_step1 | ||
|
||
# train on 1 GPU | ||
python -m mart experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
task_name=1GPU_ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
fit=true | ||
|
||
# train on multiple GPUs using Distributed Data Parallel | ||
python -m mart experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
task_name=2GPUs_ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
fit=true \ | ||
trainer=ddp \ | ||
trainer.devices=2 \ | ||
datamodule.ims_per_batch=4 \ | ||
model.optimizer.lr=0.025 \ | ||
trainer.max_steps=5244 | ||
``` | ||
For example, you can run a fast adversarial training experiment on CIFAR-10 with `python -m mart experiment=CIFAR10_CNN_Adv`. | ||
Running on GPU will make it even faster `CUDA_VISIBLE_DEVICES=0 python -m mart experiment=CIFAR10_CNN_Adv trainer=gpu trainer.precision=16`. | ||
|
||
You can also install the repository as a package, then run `python -m mart` from anywhere with your own `configs` folder. | ||
You can see other examples in [examples](/examples). | ||
|
||
## Acknowledgements | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# How to use Modular Adversarial Robustness Toolkit | ||
|
||
We provide examples on how to use the toolkit in your project. | ||
|
||
A typical procedure is | ||
|
||
1. Install the toolkit as a Python package `pip install https://github.com/IntelLabs/MART/archive/refs/heads/main.zip`; | ||
2. Create a `configs` folder; | ||
3. Add your configurations in `configs`; | ||
4. Run experiments at the folder that contains `configs`. | ||
|
||
The toolkit searches configurations in the order of `./configs` and `mart.configs`. | ||
Local configurations in `./configs` precede those built-in configurations in `mart/configs`. | ||
|
||
You can find specific examples in sub-folders. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Download and extract dataset of carla_over_obj_det | ||
CARLA_OVERHEAD_DATASET_TRAIN ?= data/carla_over_obj_det/train/kwcoco_annotations.json | ||
CARLA_OVERHEAD_DATASET_DEV ?= data/carla_over_obj_det/dev/kwcoco_annotations.json | ||
|
||
data/carla_over_obj_det/carla_over_od_dev_2.0.0.tar.gz: | ||
mkdir -p $(@D) | ||
wget -O $@ https://armory-public-data.s3.us-east-2.amazonaws.com/carla/carla_over_od_dev_2.0.0.tar.gz | ||
|
||
$(CARLA_OVERHEAD_DATASET_DEV): data/carla_over_obj_det/carla_over_od_dev_2.0.0.tar.gz | ||
tar -zxf $< -C data/carla_over_obj_det | ||
|
||
data/carla_over_obj_det/carla_over_od_train_val_1.0.0.tar.gz: | ||
mkdir -p $(@D) | ||
wget -O $@ https://armory-public-data.s3.us-east-2.amazonaws.com/carla/carla_over_od_train_val_1.0.0.tar.gz | ||
|
||
$(CARLA_OVERHEAD_DATASET_TRAIN): data/carla_over_obj_det/carla_over_od_train_val_1.0.0.tar.gz | ||
tar -zxf $< -C data/carla_over_obj_det | ||
|
||
|
||
.PHONY: carla_train | ||
carla_train: $(CARLA_OVERHEAD_DATASET_TRAIN) $(CARLA_OVERHEAD_DATASET_DEV) ## Train Faster R-CNN with the CarlaOverObjDet dataset from Armory. | ||
python -m mart \ | ||
experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
trainer.precision=16 \ | ||
fit=true \ | ||
tags=["regular_training","backbone_ImageNetPretrained"] \ | ||
|
||
|
||
# You need to specify weights of target model in [model.modules.losses_and_detections.model.weights_fpath]. | ||
.PHONY: carla_attack | ||
carla_attack: $(CARLA_OVERHEAD_DATASET_TRAIN) $(CARLA_OVERHEAD_DATASET_DEV) ## Evaluate adversarial robustness of a pretrained model. | ||
python -m mart \ | ||
experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
fit=false \ | ||
model.modules.losses_and_detections.model.weights_fpath=null \ | ||
[email protected]_adv_test=object_detection_mask_adversary \ | ||
model.modules.input_adv_test.optimizer.lr=5 \ | ||
model.modules.input_adv_test.max_iters=50 \ | ||
+model.test_sequence.seq001.input_adv_test._call_with_args_=[input,target] \ | ||
+model.test_sequence.seq001.input_adv_test.model=model \ | ||
+model.test_sequence.seq001.input_adv_test.step=step \ | ||
model.test_sequence.seq010.preprocessor=[input_adv_test] \ | ||
# tags=["MaskPGD50_LR5"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Introduction | ||
|
||
This example shows how to use MART to train an object detection model on the Carla overhead dataset. | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
```bash | ||
# train on 1 GPU | ||
CUDA_VISIBLE_DEVICES=0 \ | ||
python -m mart experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
task_name=1GPU_ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
trainer=gpu \ | ||
fit=true | ||
|
||
# train on multiple GPUs using Distributed Data Parallel | ||
CUDA_VISIBLE_DEVICES=0,1 \ | ||
python -m mart experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
task_name=2GPUs_ArmoryCarlaOverObjDet_TorchvisionFasterRCNN \ | ||
fit=true \ | ||
trainer=ddp \ | ||
trainer.devices=2 \ | ||
datamodule.ims_per_batch=4 \ | ||
model.optimizer.lr=0.025 \ | ||
trainer.max_steps=5244 | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mart @ git+https://github.com/IntelLabs/MART.git |
38 changes: 38 additions & 0 deletions
38
examples/carla_overhead_object_detection/tests/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# Copyright (C) 2022 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
import pyrootutils | ||
import pytest | ||
from hydra import compose, initialize | ||
from hydra.core.global_hydra import GlobalHydra | ||
from omegaconf import DictConfig | ||
|
||
root = Path(os.getcwd()) | ||
pyrootutils.set_root(path=root, dotenv=True, pythonpath=True) | ||
|
||
experiments_names = [ | ||
"ArmoryCarlaOverObjDet_TorchvisionFasterRCNN", | ||
] | ||
|
||
|
||
# Loads the configuration file from a given experiment | ||
def get_cfg(experiment): | ||
with initialize(version_base="1.2", config_path="../configs"): | ||
params = "experiment=" + experiment | ||
cfg = compose(config_name="lightning.yaml", return_hydra_config=True, overrides=[params]) | ||
return cfg | ||
|
||
|
||
@pytest.fixture(scope="function", params=experiments_names) | ||
def cfg_experiment(request) -> DictConfig: | ||
cfg = get_cfg(request.param) | ||
|
||
yield cfg | ||
|
||
GlobalHydra.instance().clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../tests/helpers/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../tests/test_configs.py |
69 changes: 69 additions & 0 deletions
69
examples/carla_overhead_object_detection/tests/test_experiments.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
from typing import Dict | ||
|
||
import pytest | ||
from hydra.core.global_hydra import GlobalHydra | ||
|
||
from tests.helpers.dataset_generator import FakeCOCODataset | ||
from tests.helpers.run_if import RunIf | ||
from tests.helpers.run_sh_command import run_sh_command | ||
|
||
module = "mart" | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def carla_cfg(tmp_path) -> Dict: | ||
# Generate fake CARLA dataset on disk at tmp_path | ||
dataset = FakeCOCODataset(tmp_path, config=carla_ds, name="carla_over_obj_det") | ||
dataset.generate(num_images=2, num_annotations_per_image=2) | ||
|
||
cfg = { | ||
"trainer": [ | ||
"++trainer.fast_dev_run=3", | ||
], | ||
"datamodel": [ | ||
"++paths.data_dir=" + str(tmp_path), | ||
"datamodule.num_workers=0", | ||
], | ||
} | ||
yield cfg | ||
|
||
GlobalHydra.instance().clear() | ||
|
||
|
||
carla_ds = { | ||
"train": { | ||
"folder": "train", | ||
"modalities": ["rgb"], | ||
"ann_folder": "train", | ||
"ann_file": "kwcoco_annotations.json", | ||
}, | ||
"val": { | ||
"folder": "val", | ||
"modalities": ["rgb"], | ||
"ann_folder": "val", | ||
"ann_file": "kwcoco_annotations.json", | ||
}, | ||
"test": { | ||
"folder": "dev", | ||
"modalities": ["foreground_mask", "rgb"], | ||
"ann_folder": "dev", | ||
"ann_file": "kwcoco_annotations.json", | ||
}, | ||
} | ||
|
||
|
||
@RunIf(sh=True) | ||
@pytest.mark.slow | ||
def test_armory_carla_fasterrcnn_experiment(carla_cfg, tmp_path): | ||
"""Test Armory CARLA TorchVision FasterRCNN experiment.""" | ||
overrides = carla_cfg["trainer"] + carla_cfg["datamodel"] | ||
command = [ | ||
"-m", | ||
module, | ||
"experiment=ArmoryCarlaOverObjDet_TorchvisionFasterRCNN", | ||
"[email protected]_adv_test=object_detection_mask_adversary", | ||
"hydra.sweep.dir=" + str(tmp_path), | ||
"optimized_metric=training/loss_objectness", | ||
] + overrides | ||
run_sh_command(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Introduction | ||
|
||
This example shows how to use MART to evaluate adversarial robustness of models from RobustBench. | ||
|
||
Note that the attack algorithm here is not optimal, just for the demonstration purpose. | ||
|
||
The `requirements.txt` contains dependency of MART and RobustBench. | ||
|
||
The `./configs` folder contains configurations of the target model `classifier_robustbench` and the MART experiment `CIFAR10_RobustBench`. | ||
|
||
The configuration files in `./configs` precedes those in `mart.configs` (MART's built-in configs). | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## How to run | ||
|
||
```bash | ||
# run on CPU | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=default \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_eps8_pgd10_step1 | ||
|
||
# run on GPU | ||
CUDA_VISIBLE_DEVICES=0 \ | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=gpu \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_eps8_pgd10_step1 \ | ||
+model.test_sequence.seq005=input_adv_test \ | ||
model.test_sequence.seq010.preprocessor=["input_adv_test"] | ||
|
||
# Evaluate with AutoAttack, expect 0.6171875 | ||
CUDA_VISIBLE_DEVICES=0 \ | ||
python -m mart experiment=CIFAR10_RobustBench \ | ||
trainer=gpu \ | ||
fit=false \ | ||
+trainer.limit_test_batches=1 \ | ||
[email protected]_adv_test=classification_autoattack \ | ||
+model.test_sequence.seq005=input_adv_test \ | ||
model.test_sequence.seq010.preprocessor=["input_adv_test"] | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mart @ git+https://github.com/IntelLabs/MART.git | ||
robustbench @ git+https://github.com/RobustBench/robustbench.git@9a590683b7daecf963244dea402529f0d728c727 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../tests/helpers/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should tag a version to install. Otherwise main will continually change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added instructions on installing a released version.
This is a general README for all examples and they may depend on different versions in the future.