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

Generic Masked Patch Attack #1904

Merged
merged 31 commits into from
Aug 25, 2023
Merged

Conversation

jprokos26
Copy link
Contributor

@jprokos26 jprokos26 commented Mar 23, 2023

The goal of this PR is to implement a generic masked patch module to be integrated into existing attacks with minimal code change to the attack module

How to use this code

See the docs

To illustrate what this does see the example below:

Example Run

Attack kwargs from config
"attack": {
        "knowledge": "white",
        "kwargs": {
            "batch_size": 1,
            "learning_rate": 0.1,
            "max_iter": 2,
            "optimizer": "pgd",
            "targeted": false,
            "verbose": true,
            "patch_base_image": "https://github.com/jprokos26/armory/blob/public-base-images/armory/art_experimental/attacks/base_images/%5BCARLA_OD%5D_stable_diffusion_gemstones.png?raw=true",
            "patch_mask": {
                "shape": "grid",
                "shape_kwargs": {
                    "num_circles": 5,
                    "circle_radius": 0.1,
                    "circle_spacing": 2
                },
                "invert": false,
                "fill": "https://github.com/jprokos26/armory/blob/public-base-images/armory/art_experimental/attacks/base_images/white_w_black_text.png?raw=true"
            }
        },
        "module": "armory.art_experimental.attacks.carla_obj_det_adversarial_patch",
        "name": "CARLAAdversarialPatchPyTorch",
        "use_label": true
    },
Resultant image
image



@dataclass
class PatchMask:
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'm not sure if this file is the correct place for this class since we do not have a dedicated 'attack utils' file. Should this be moved to its own file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be moved to be within armory.utils

@jprokos26 jprokos26 marked this pull request as draft March 23, 2023 14:44
@jprokos26
Copy link
Contributor Author

@christopherwoodall Still WIP, not worth you trying to debug the current issue with patch_base_image but feel free to address other comments; will request review by EOW with fixes

@jprokos26 jprokos26 self-assigned this Apr 24, 2023
@jprokos26
Copy link
Contributor Author

@mwartell I am running into python package issues when installed not in editable mode but it works as expected when it is. Specifically armory cannot find armory.cli.tools after installation (failing here), I haven't had time to track down exactly why this is the case, I have tried relative and absolute imports to no avail, what might I be doing wrong? Do I need to make changes to our pyproject.toml to have it find the tools package properly?

@mwartell
Copy link
Collaborator

mwartell commented Jun 6, 2023

The culprit is an overzealous exclusion in pyproject.toml

I'm looking for the syntax to fix that, likely exclude = ["/tools"]

Hatch claims to use the gitignore pattern rules which is a good choice.

If there is a separator (/) at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.

Therefore

exclude = ["/tools"]

will exclude $TOP/tools but will allow $TOP/armory/cli/tools

Comment on lines 5 to 8
def generate_shapes(command_args, prog, description):
from armory.utils.shape_gen import ( # move to lazy load to avoid PIL import error
Shape,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a better way to handle this? In particular, Pillow is listed under engine and develop but when you install armory-testbed standard it is not included and will error when trying to run armory --help. This fixes that problem but creates another that if generate_shapes is called at runtime from an environment without PIL the error message will not be useful. However since this is strictly code that is called from either the cli in which the user will know how to fix the issue or from the carla_adversarial_patch_pytorch code which must be running with engine installed and PIL must also be installed I left it this way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We had wanted to make the armory launcher clean so that the host could have a thin (no ML framework install) loader that relied on the contents of the container for the heavy libraries). This was a big motivation for keeping the dependencies lean. Since Armory v23.x has dropped containers and the two-stage armory launch, and with Armory v0.x.x having an informal end-of-life in under 18 months, I think we just don't care that much any more.

Do you need PIL in pyproject dependencies? Then put it there, it isn't a big deal anymore. @christopherwoodall care to render an opinion?

Copy link
Contributor

Choose a reason for hiding this comment

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

Totally in agreement that all required libraries should be in the dependencies section.

Copy link
Contributor

@christopherwoodall christopherwoodall left a comment

Choose a reason for hiding this comment

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

Let's get those CI tests passing, otherwise, it looks good to me.

Comment on lines 5 to 8
def generate_shapes(command_args, prog, description):
from armory.utils.shape_gen import ( # move to lazy load to avoid PIL import error
Shape,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Totally in agreement that all required libraries should be in the dependencies section.

@jprokos26 jprokos26 changed the title (WIP) Generic Masked Patch Attack Generic Masked Patch Attack Aug 24, 2023
@jprokos26 jprokos26 marked this pull request as ready for review August 24, 2023 18:40
@christopherwoodall christopherwoodall self-requested a review August 24, 2023 20:03
Co-authored-by: Christopher Woodall <christopher.woodall@twosixtech.com>
@jprokos26 jprokos26 merged commit 4fc0d37 into twosixlabs:develop Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants