Skip to content

Commit

Permalink
Visualize intermediate images of Composer (#244)
Browse files Browse the repository at this point in the history
* 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.

* Add a composer.visualize switch to see intermediate images.
  • Loading branch information
mzweilin authored Feb 4, 2024
1 parent 4dd867e commit eaac60c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mart/attack/composer/modular.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING, Any, Iterable

import torch
from torchvision.transforms.functional import to_pil_image

from mart.nn import SequentialDict

Expand All @@ -19,7 +20,7 @@


class Composer(torch.nn.Module):
def __init__(self, perturber: Perturber, modules, sequence) -> None:
def __init__(self, perturber: Perturber, modules, sequence, visualize: bool = False) -> None:
"""_summary_
Args:
Expand All @@ -34,6 +35,7 @@ def __init__(self, perturber: Perturber, modules, sequence) -> None:
if isinstance(sequence, dict):
sequence = [sequence[key] for key in sorted(sequence)]
self.functions = SequentialDict(modules, {"composer": sequence})
self.visualize = visualize

def configure_perturbation(self, input: torch.Tensor | Iterable[torch.Tensor]):
return self.perturber.configure_perturbation(input)
Expand Down Expand Up @@ -76,6 +78,12 @@ def _compose(
input=input, target=target, perturbation=perturbation, step="composer"
)

# Visualize intermediate images.
if self.visualize:
for key, value in output.items():
if isinstance(value, torch.Tensor):
to_pil_image(value / 255).save(f"{key}.png")

# SequentialDict returns a dictionary DotDict,
# but we only need the return value of the most recently executed module.
last_added_key = next(reversed(output))
Expand Down

0 comments on commit eaac60c

Please sign in to comment.