Skip to content

Commit 4dd867e

Browse files
authored
Simulation-in-loop adversary (#243)
* Create a folder for attack.composer. * Add composer modules for unbounded patch adversary. * Add config of Adam optimizer. * Add LoadCoords for patch adversary. * Add a config of unbounded patch adversary. * Add a datamodule config for carla patch adversary. * Fix the simple Linf projection. * Add composer module PertImageBase for Lp bounded patch adversary. * Add config of lp-bounded patch adversary. * Add a fake renderer composer module. * Teardown a test dataset gracefully for the rendering-in-loop adversary. * Add configs of simulation-in-loop adversary. * Add a datamodule config for CARLA patch rendering. * Update CarlaDataset config. * Add a composer.visualize switch to see intermediate images. * Revert "Teardown a test dataset gracefully for the rendering-in-loop adversary." This reverts commit a5ffef3. * Revert "Add a composer.visualize switch to see intermediate images." This reverts commit a17e224.
1 parent 3862abe commit 4dd867e

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

mart/attack/composer/patch.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"PertRectSize",
1515
"PertExtractRect",
1616
"PertRectPerspective",
17+
"FakeRenderer",
1718
"PertImageBase",
1819
]
1920

@@ -75,6 +76,29 @@ def forward(self, perturbation, input, coords):
7576
return perturbation
7677

7778

79+
class FakeRenderer(torch.nn.Module):
80+
"""Replace image with a re-rendered image, but keep the gradient on the perturbation."""
81+
82+
def forward(self, perturbation, input, renderer):
83+
"""Use the same perturbation and target.coordinates to re-render input in Simulation.
84+
85+
perturbation is the rectangular patch. or a masked frame, with rectangular coordinates
86+
in target, so we can extract the rectangle patch for rendering. input is the digitally
87+
composed frame. target should include everything that needs to re-render a frame with the
88+
perturbation.
89+
"""
90+
91+
with torch.no_grad():
92+
input_rendered = renderer(perturbation)
93+
input_rendered = input_rendered.clamp(0, 255)
94+
delta = input_rendered - input
95+
96+
# Fake differentiable rendering, or BPDA, but keep the mask constraint.
97+
input = input + delta
98+
99+
return input
100+
101+
78102
class PertImageBase(torch.nn.Module):
79103
"""Resize an image and add to perturbation."""
80104

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fake_renderer:
2+
_target_: mart.attack.composer.FakeRenderer
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defaults:
2+
- object_detection_lp_patch_adversary
3+
4+
composer:
5+
modules:
6+
fake_renderer:
7+
_target_: mart.attack.composer.FakeRenderer
8+
9+
sequence:
10+
seq060:
11+
# Ignore output from overlay.
12+
fake_renderer:
13+
["pert_image_base", "pert_rect_perspective", "target.renderer"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defaults:
2+
- object_detection_patch_adversary
3+
4+
composer:
5+
modules:
6+
fake_renderer:
7+
_target_: mart.attack.composer.FakeRenderer
8+
9+
sequence:
10+
seq060:
11+
# Ignore output from overlay.
12+
fake_renderer:
13+
["pert_extract_rect", "pert_rect_perspective", "target.renderer"]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defaults:
2+
- default.yaml
3+
4+
train_dataset: null
5+
6+
val_dataset: null
7+
8+
test_dataset:
9+
_target_: oscar_datagen_tools.dataset.dataset.CarlaDataset
10+
simulation_run: ???
11+
modality: "rgb"
12+
annFile: ${.simulation_run}/kwcoco_annotations.json
13+
num_insertion_ticks: 50
14+
transforms:
15+
_target_: mart.transforms.Compose
16+
transforms:
17+
- _target_: torchvision.transforms.ToTensor
18+
- _target_: mart.transforms.ConvertCocoPolysToMask
19+
- _target_: mart.transforms.LoadPerturbableMask
20+
perturb_mask_folder: ${....simulation_run}/foreground_mask/
21+
- _target_: mart.transforms.LoadCoords
22+
folder: ${....simulation_run}/patch_metadata/
23+
- _target_: mart.transforms.Denormalize
24+
center: 0
25+
scale: 255
26+
- _target_: torch.fake_quantize_per_tensor_affine
27+
_partial_: true
28+
# (x/1+0).round().clamp(0, 255) * 1
29+
scale: 1
30+
zero_point: 0
31+
quant_min: 0
32+
quant_max: 255
33+
34+
collate_fn:
35+
_target_: hydra.utils.get_method
36+
path: mart.datamodules.coco.collate_fn

0 commit comments

Comments
 (0)