Skip to content

Commit

Permalink
0.13.4 (#1124)
Browse files Browse the repository at this point in the history
* updating scenario configs for eval 3 (#1123)

* handling merge conflict

* bump version 0.13.3 --> 0.13.4

* log that random patch location is being used

Co-authored-by: Yusong Tan <ytan@mitre.org>

* small updates for Eval 3 (#1126)

* small updates for Eval 3

* updated kwargs and moved assert -> valueerror

* lint

Co-authored-by: David Slater <david.slater@twosixlabs.com>

Co-authored-by: Yusong Tan <ytan@mitre.org>
Co-authored-by: yusong-tan <59029053+yusong-tan@users.noreply.github.com>
Co-authored-by: David Slater <david.slater@twosixlabs.com>
  • Loading branch information
4 people authored Jul 14, 2021
1 parent c249287 commit 8eb10ac
Show file tree
Hide file tree
Showing 61 changed files with 1,148 additions and 237 deletions.
2 changes: 1 addition & 1 deletion armory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# Semantic Version
__version__ = "0.13.3"
__version__ = "0.13.4"


# Submodule imports
Expand Down
52 changes: 28 additions & 24 deletions armory/art_experimental/attacks/pgd_patch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import logging

from art.attacks.evasion import ProjectedGradientDescent
import numpy as np


logger = logging.getLogger(__name__)


class PGDPatch(ProjectedGradientDescent):
"""
Apply Masked PGD to image and video inputs,
Expand All @@ -15,15 +20,29 @@ def __init__(self, estimator, **kwargs):
def generate(self, x, y=None, **generate_kwargs):
video_input = generate_kwargs.get("video_input", False)

if "patch_ratio" in generate_kwargs:
patch_ratio = generate_kwargs["patch_ratio"]
patch_height = int(x.shape[-3] * patch_ratio ** 0.5)
patch_width = int(x.shape[-2] * patch_ratio ** 0.5)
elif "patch_height" in generate_kwargs and "patch_width" in generate_kwargs:
patch_height = generate_kwargs["patch_height"]
patch_width = generate_kwargs["patch_width"]
else:
raise ValueError(
"generate_kwargs did not define 'patch_ratio', or it did not define 'patch_height' and 'patch_width"
)

if "ymin" in generate_kwargs:
ymin = generate_kwargs["ymin"]
else:
raise ValueError("generate_kwargs did not define 'ymin'")
logger.info("Selecting random value for patch ymin coordinate.")
ymin = np.random.randint(int(x.shape[-3] - patch_height))

if "xmin" in generate_kwargs:
xmin = generate_kwargs["xmin"]
else:
raise ValueError("generate_kwargs did not define 'xmin'")
logger.info("Selecting random value for patch xmin coordinate.")
xmin = np.random.randint(int(x.shape[-2] - patch_width))

assert x.ndim in [
4,
Expand All @@ -36,27 +55,12 @@ def generate(self, x, y=None, **generate_kwargs):
channels = np.where(channels_mask)[0]

mask = np.zeros(shape=x.shape[1:], dtype=np.float32)
if "patch_ratio" in generate_kwargs:
patch_ratio = generate_kwargs["patch_ratio"]
ymax = ymin + int(x.shape[-3] * patch_ratio ** 0.5)
xmax = xmin + int(x.shape[-2] * patch_ratio ** 0.5)
if video_input:
mask[:, ymin:ymax, xmin:xmax, channels] = 1.0
else:
mask[ymin:ymax, xmin:xmax, channels] = 1.0
elif "patch_height" in generate_kwargs and "patch_width" in generate_kwargs:
patch_height = generate_kwargs["patch_height"]
patch_width = generate_kwargs["patch_width"]
if video_input:
mask[
:, ymin : ymin + patch_height, xmin : xmin + patch_width, channels
] = 1.0
else:
mask[
ymin : ymin + patch_height, xmin : xmin + patch_width, channels
] = 1.0

if video_input:
mask[
:, ymin : ymin + patch_height, xmin : xmin + patch_width, channels
] = 1.0
else:
raise ValueError(
"generate_kwargs did not define 'patch_ratio', or it did not define 'patch_height' and 'patch_width'"
)
mask[ymin : ymin + patch_height, xmin : xmin + patch_width, channels] = 1.0

return super().generate(x, y=y, mask=mask)
31 changes: 31 additions & 0 deletions armory/art_experimental/attacks/robust_dpatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

from art.attacks.evasion import RobustDPatch
import numpy as np


logger = logging.getLogger(__name__)


class RobustDPatch(RobustDPatch):
"""
Generate and apply patch
"""

def __init__(self, estimator, **kwargs):
# allows for random patch location
if "patch_location" not in kwargs:
self.random_location = True
else:
self.random_location = False
super().__init__(estimator=estimator, **kwargs)

def generate(self, x, y=None, **generate_kwargs):
if self.random_location:
logger.info("Selecting random coordinates for patch_location.")
self.patch_location = (
np.random.randint(int(x.shape[-3] - self.patch_shape[0])),
np.random.randint(int(x.shape[-2] - self.patch_shape[1])),
)
super().generate(x, y=y, **generate_kwargs)
return super().apply_patch(x)
42 changes: 42 additions & 0 deletions armory/art_experimental/attacks/video_frame_border.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from art.attacks.evasion import ProjectedGradientDescent
import numpy as np


class FrameBorderPatch(ProjectedGradientDescent):
"""
Apply Masked PGD to video inputs, where only the
video frame is allowed to be perturbed.
Each video is assumed to have shape (NFHWC).
"""

def __init__(self, estimator, **kwargs):
super().__init__(estimator=estimator, **kwargs)

def generate(self, x, y=None, patch_ratio=None, **kwargs):
if patch_ratio is None:
raise ValueError("generate_kwargs did not define 'patch_ratio'")
if x.ndim != 5:
raise ValueError("This attack is designed for videos (5-dim)")
width = x.shape[3]
height = x.shape[2]

t1 = (
2 * (width + height)
+ (4 * (width + height) ** 2 - 16 * (patch_ratio * width * height)) ** 0.5
) / 8
t2 = (
2 * (width + height)
- (4 * (width + height) ** 2 - 16 * (patch_ratio * width * height)) ** 0.5
) / 8
thickness = int(min(t1, t2))

if (width - 2 * thickness) * (height - 2 * thickness) < (
1 - patch_ratio
) * width * height:
raise ValueError("patch_ratio does not match height and width")

mask = np.ones(shape=x.shape[1:], dtype=np.float32)

mask[:, thickness : height - thickness, thickness : width - thickness, :] = 0.0

return super().generate(x, y=y, mask=mask, **kwargs)
44 changes: 44 additions & 0 deletions armory/art_experimental/defences/video_compression_normalized.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from art.defences.preprocessor import VideoCompression
from art.defences.preprocessor import VideoCompressionPyTorch
import numpy as np


Expand Down Expand Up @@ -38,3 +39,46 @@ def __call__(self, x, y=None):
x /= scale
x = x.astype(self.dtype)
return x, y


class VideoCompressionNormalizedPytorch(VideoCompressionPyTorch):
"""
Convert x from [0,1] to [0, 255] and back, if necessary
"""

def __init__(
self,
video_format,
constant_rate_factor=28,
channels_first=False,
apply_fit=False,
apply_predict=True,
verbose=False,
dtype=np.float32,
):
super().__init__(
video_format=video_format,
constant_rate_factor=constant_rate_factor,
channels_first=channels_first,
apply_fit=apply_fit,
apply_predict=apply_predict,
verbose=verbose,
)
self.dtype = dtype

def forward(self, x, y=None):
"""
Apply video compression to sample `x`.
:param x: Sample to compress of shape NCFHW or NFHWC. `x` values are expected to be in the data range [0, 255].
:param y: Labels of the sample `x`. This function does not affect them in any way.
:return: Compressed sample.
"""
scale = 1
if x.min() >= 0 and x.max() <= 1.0:
scale = 255

x = x * scale
x_compressed = self._compression_pytorch_numpy.apply(x)
x_compressed = x_compressed / scale
return x_compressed, y
Binary file added armory/utils/triggers/peace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion scenario_configs/apricot_frcnn.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"name": "ObjectDetectionTask"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/apricot_frcnn_defended.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"name": "ObjectDetectionTask"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/cifar10_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"name": "ImageClassificationTask"
},
"sysconfig": {
"docker_image": "twosixarmory/pytorch:0.13.3",
"docker_image": "twosixarmory/pytorch:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/cifar10_defended_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"name": "ImageClassificationTask"
},
"sysconfig": {
"docker_image": "twosixarmory/pytorch:0.13.3",
"docker_image": "twosixarmory/pytorch:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/dapricot_frcnn_masked_pgd.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"name": "ObjectDetectionTask"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": "colour-science/colour",
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"name": "GTSRB"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_baseline_pytorch.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"name": "GTSRB"
},
"sysconfig": {
"docker_image": "twosixarmory/pytorch:0.13.3",
"docker_image": "twosixarmory/pytorch:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_clbd.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"name": "GTSRB_CLBD"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_clbd_bullethole.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"name": "GTSRB_CLBD"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_clbd_defended.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"name": "GTSRB_CLBD"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"output_dir": null,
Expand Down
2 changes: 1 addition & 1 deletion scenario_configs/gtsrb_scenario_poison.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"name": "GTSRB"
},
"sysconfig": {
"docker_image": "twosixarmory/tf1:0.13.3",
"docker_image": "twosixarmory/tf1:0.13.4",
"external_github_repo": null,
"gpus": "all",
"use_gpu": false
Expand Down
Loading

0 comments on commit 8eb10ac

Please sign in to comment.