Skip to content

Conversation

@mzweilin
Copy link
Contributor

@mzweilin mzweilin commented Mar 30, 2023

What does this PR do?

This PR adds a modality-aware composer Composer.

Type of change

Please check all relevant options.

  • Improvement (non-breaking)
  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing

Please describe the tests that you ran to verify your changes. Consider listing any relevant details of your test configuration.

  • pytest tests/test_composer.py

Before submitting

  • The title is self-explanatory and the description concisely explains the PR
  • My PR does only one thing, instead of bundling different changes together
  • I list all the breaking changes introduced by this pull request
  • I have commented my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have run pre-commit hooks with pre-commit run -a command without errors

Did you have fun?

Make sure you had fun coding 🙃

@mzweilin mzweilin requested a review from dxoigmn March 30, 2023 18:56
Copy link
Member

@dxoigmn dxoigmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments.

Comment on lines 14 to 15
__all__ = ["Additive", "Overlay", "ModalityComposer"]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if exporting these is smart given their generic names? I wouldn't mind exporting (by default) ModalityComposer though. If they were named AdditiveComposer then the possibility of a naming conflict is low. But mart.attack.Additive feels a little too generic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Removed.

**kwargs,
) -> torch.Tensor | tuple:
# Bypass batch-aware in Composer.__call__(), because we have the recursive self.compose().
input_adv = self.compose(perturbation, input=input, target=target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect, at a minimum, that these arguments (perturbation, input, and target) would have the same types as in compose since you're directly passing them to compose?

My suspicion is that some functionality probably needs to move to Composer.__call__ while dict functionality should be in ModalityComposer.__call__?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it will be less confusing if we follow the implementation in Enforcer.

Comment on lines 68 to 84
class ModalityComposer(Composer):
"""A modality-aware composer.
Example usage: `ModalityComposer(rgb=Overlay(), depth=Additive())`. Note that
`ModalityComposer(default=Additive())` is equivalent with `Additive()`.
"""

def __init__(self, **modality_composers):
self.modality_composers = modality_composers

def __call__(
self,
perturbation: torch.Tensor | tuple,
*,
input: torch.Tensor | tuple,
target: torch.Tensor | dict[str, Any] | tuple,
**kwargs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModalityComposer feels like it can just wholesale replace Composer? If so, you should just rename this as Composer and get rid of ModalityComposer. The other option is to just have ModalityComposer only take dicts so that it specialized in modalities, which we encode as dicts (as the name suggests).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revised the implementation following the structure in Enforcer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shouldn't merge this PR.

I will try to implement a shared modality-aware mechanism in Perturber for initializer gradient_modifier projector composer and optim_params.

@mzweilin
Copy link
Contributor Author

Abandoned in favor of #115

@mzweilin mzweilin closed this Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants