Skip to content

Commit

Permalink
line length
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Eykholt <kheykholt@gmail.com>
  • Loading branch information
keykholt committed Dec 1, 2021
1 parent 02839ac commit f6a402c
Show file tree
Hide file tree
Showing 95 changed files with 1,474 additions and 349 deletions.
20 changes: 16 additions & 4 deletions art/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"std": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s", "datefmt": "%Y-%m-%d %H:%M",}
"std": {
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
"datefmt": "%Y-%m-%d %H:%M",
}
},
"handlers": {
"default": {"class": "logging.NullHandler",},
"test": {"class": "logging.StreamHandler", "formatter": "std", "level": logging.INFO,},
"default": {
"class": "logging.NullHandler",
},
"test": {
"class": "logging.StreamHandler",
"formatter": "std",
"level": logging.INFO,
},
},
"loggers": {
"art": {"handlers": ["default"]},
"tests": {"handlers": ["test"], "level": "INFO", "propagate": True},
},
"loggers": {"art": {"handlers": ["default"]}, "tests": {"handlers": ["test"], "level": "INFO", "propagate": True},},
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger(__name__)
4 changes: 3 additions & 1 deletion art/attacks/attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class Attack(abc.ABC):
_estimator_requirements: Optional[Union[Tuple[Any, ...], Tuple[()]]] = None

def __init__(
self, estimator, tensor_board: Union[str, bool] = False,
self,
estimator,
tensor_board: Union[str, bool] = False,
):
"""
:param estimator: An estimator.
Expand Down
35 changes: 27 additions & 8 deletions art/attacks/evasion/adversarial_patch/adversarial_patch_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,27 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T
i_batch_end = (i_batch + 1) * self.batch_size

gradients = self.estimator.loss_gradient(
patched_images[i_batch_start:i_batch_end], y_target[i_batch_start:i_batch_end],
patched_images[i_batch_start:i_batch_end],
y_target[i_batch_start:i_batch_end],
)

for i_image in range(gradients.shape[0]):
patch_gradients_i = self._reverse_transformation(
gradients[i_image, :, :, :], patch_mask_transformed[i_image, :, :, :], transforms[i_image],
gradients[i_image, :, :, :],
patch_mask_transformed[i_image, :, :, :],
transforms[i_image],
)
if self.nb_dims == 4:
patch_gradients_i = np.mean(patch_gradients_i, axis=0)
patch_gradients += patch_gradients_i

# patch_gradients = patch_gradients / (num_batches * self.batch_size)
self.patch -= patch_gradients * self.learning_rate
self.patch = np.clip(self.patch, a_min=self.estimator.clip_values[0], a_max=self.estimator.clip_values[1],)
self.patch = np.clip(
self.patch,
a_min=self.estimator.clip_values[0],
a_max=self.estimator.clip_values[1],
)

return self.patch, self._get_circular_patch_mask()

Expand Down Expand Up @@ -329,9 +336,11 @@ def _augment_images_with_random_patch(self, images, patch, mask=None, scale=None
else:
mask_2d = mask

(patch_transformed, patch_mask_transformed, transformation,) = self._random_transformation(
patch, scale, mask_2d
)
(
patch_transformed,
patch_mask_transformed,
transformation,
) = self._random_transformation(patch, scale, mask_2d)

inverted_patch_mask_transformed = 1 - patch_mask_transformed

Expand Down Expand Up @@ -486,8 +495,18 @@ def _random_transformation(self, patch, scale, mask_2d):
transformation["pad_h_before"] = pad_h_before
transformation["pad_w_before"] = pad_w_before

patch = np.pad(patch, pad_width=pad_width, mode="constant", constant_values=(0, 0),)
patch_mask = np.pad(patch_mask, pad_width=pad_width, mode="constant", constant_values=(0, 0),)
patch = np.pad(
patch,
pad_width=pad_width,
mode="constant",
constant_values=(0, 0),
)
patch_mask = np.pad(
patch_mask,
pad_width=pad_width,
mode="constant",
constant_values=(0, 0),
)

# shift
if mask_2d is None:
Expand Down
30 changes: 23 additions & 7 deletions art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def _predictions(self, images: "torch.Tensor", mask: Optional["torch.Tensor"]) -

patched_input = self._random_overlay(images, self._patch, mask=mask)
patched_input = torch.clamp(
patched_input, min=self.estimator.clip_values[0], max=self.estimator.clip_values[1],
patched_input,
min=self.estimator.clip_values[0],
max=self.estimator.clip_values[1],
)

predictions = self.estimator._predict_framework(patched_input) # pylint: disable=W0212
Expand Down Expand Up @@ -255,7 +257,9 @@ def _random_overlay(
smallest_image_edge = np.minimum(self.image_shape[self.i_h], self.image_shape[self.i_w])

image_mask = torchvision.transforms.functional.resize(
img=image_mask, size=(smallest_image_edge, smallest_image_edge), interpolation=2,
img=image_mask,
size=(smallest_image_edge, smallest_image_edge),
interpolation=2,
)

pad_h_before = int((self.image_shape[self.i_h] - image_mask.shape[self.i_h_patch + 1]) / 2)
Expand All @@ -281,7 +285,9 @@ def _random_overlay(
padded_patch = torch.stack([patch] * nb_samples)

padded_patch = torchvision.transforms.functional.resize(
img=padded_patch, size=(smallest_image_edge, smallest_image_edge), interpolation=2,
img=padded_patch,
size=(smallest_image_edge, smallest_image_edge),
interpolation=2,
)

padded_patch = torchvision.transforms.functional.pad(
Expand Down Expand Up @@ -449,13 +455,19 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T
if mask is None:
dataset = torch.utils.data.TensorDataset(x_tensor, y_tensor)
data_loader = torch.utils.data.DataLoader(
dataset=dataset, batch_size=self.batch_size, shuffle=shuffle, drop_last=False,
dataset=dataset,
batch_size=self.batch_size,
shuffle=shuffle,
drop_last=False,
)
else:
mask_tensor = torch.Tensor(mask)
dataset = torch.utils.data.TensorDataset(x_tensor, y_tensor, mask_tensor)
data_loader = torch.utils.data.DataLoader(
dataset=dataset, batch_size=self.batch_size, shuffle=shuffle, drop_last=False,
dataset=dataset,
batch_size=self.batch_size,
shuffle=shuffle,
drop_last=False,
)

for i_iter in trange(self.max_iter, desc="Adversarial Patch PyTorch", disable=not self.verbose):
Expand All @@ -474,7 +486,9 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T

if self.summary_writer is not None: # pragma: no cover
self.summary_writer.add_image(
"patch", self._patch, global_step=i_iter,
"patch",
self._patch,
global_step=i_iter,
)

if hasattr(self.estimator, "compute_losses"):
Expand All @@ -485,7 +499,9 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T

for key, value in losses.items():
self.summary_writer.add_scalar(
"loss/{}".format(key), np.mean(value.detach().cpu().numpy()), global_step=i_iter,
"loss/{}".format(key),
np.mean(value.detach().cpu().numpy()),
global_step=i_iter,
)

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def _predictions(self, images: "tf.Tensor", mask: Optional["tf.Tensor"]) -> "tf.
patched_input = self._random_overlay(images, self._patch, mask=mask)

patched_input = tf.clip_by_value(
patched_input, clip_value_min=self.estimator.clip_values[0], clip_value_max=self.estimator.clip_values[1],
patched_input,
clip_value_min=self.estimator.clip_values[0],
clip_value_max=self.estimator.clip_values[1],
)

predictions = self.estimator._predict_framework(patched_input) # pylint: disable=W0212
Expand Down Expand Up @@ -335,7 +337,10 @@ def _random_overlay(

# Rotation
rotation_matrix = np.array(
[[math.cos(-phi_rotate), -math.sin(-phi_rotate)], [math.sin(-phi_rotate), math.cos(-phi_rotate)],]
[
[math.cos(-phi_rotate), -math.sin(-phi_rotate)],
[math.sin(-phi_rotate), math.cos(-phi_rotate)],
]
)

# Scale
Expand All @@ -355,11 +360,27 @@ def _random_overlay(
transform_vectors.append([a_0, a_1, x_origin_delta, b_0, b_1, y_origin_delta, 0, 0])
translation_vectors.append([1, 0, -x_shift, 0, 1, -y_shift, 0, 0])

image_mask = tfa.image.transform(image_mask, transform_vectors, "BILINEAR",)
padded_patch = tfa.image.transform(padded_patch, transform_vectors, "BILINEAR",)
image_mask = tfa.image.transform(
image_mask,
transform_vectors,
"BILINEAR",
)
padded_patch = tfa.image.transform(
padded_patch,
transform_vectors,
"BILINEAR",
)

image_mask = tfa.image.transform(image_mask, translation_vectors, "BILINEAR",)
padded_patch = tfa.image.transform(padded_patch, translation_vectors, "BILINEAR",)
image_mask = tfa.image.transform(
image_mask,
translation_vectors,
"BILINEAR",
)
padded_patch = tfa.image.transform(
padded_patch,
translation_vectors,
"BILINEAR",
)

if self.nb_dims == 4:
image_mask = tf.stack([image_mask] * images.shape[1], axis=1)
Expand Down Expand Up @@ -439,7 +460,9 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T

if self.summary_writer is not None: # pragma: no cover
self.summary_writer.add_image(
"patch", self._patch.numpy().transpose((2, 0, 1)), global_step=i_iter,
"patch",
self._patch.numpy().transpose((2, 0, 1)),
global_step=i_iter,
)

if hasattr(self.estimator, "compute_losses"):
Expand All @@ -448,7 +471,9 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> T

for key, value in losses.items():
self.summary_writer.add_scalar(
"loss/{}".format(key), np.mean(value), global_step=i_iter,
"loss/{}".format(key),
np.mean(value),
global_step=i_iter,
)

return (
Expand Down
19 changes: 16 additions & 3 deletions art/attacks/evasion/auto_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
attack.set_params(targeted=False)

x_adv, sample_is_robust = self._run_attack(
x=x_adv, y=y, sample_is_robust=sample_is_robust, attack=attack, **kwargs,
x=x_adv,
y=y,
sample_is_robust=sample_is_robust,
attack=attack,
**kwargs,
)

# Targeted attacks
Expand All @@ -205,13 +209,22 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
target = check_and_transform_label_format(targeted_labels[:, i], self.estimator.nb_classes)

x_adv, sample_is_robust = self._run_attack(
x=x_adv, y=target, sample_is_robust=sample_is_robust, attack=attack, **kwargs,
x=x_adv,
y=target,
sample_is_robust=sample_is_robust,
attack=attack,
**kwargs,
)

return x_adv

def _run_attack(
self, x: np.ndarray, y: np.ndarray, sample_is_robust: np.ndarray, attack: EvasionAttack, **kwargs,
self,
x: np.ndarray,
y: np.ndarray,
sample_is_robust: np.ndarray,
attack: EvasionAttack,
**kwargs,
) -> Tuple[np.ndarray, np.ndarray]:
"""
Run attack.
Expand Down
43 changes: 36 additions & 7 deletions art/attacks/evasion/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
return x_adv

def _perturb(
self, x: np.ndarray, y: int, y_p: int, init_pred: int, adv_init: np.ndarray, clip_min: float, clip_max: float,
self,
x: np.ndarray,
y: int,
y_p: int,
init_pred: int,
adv_init: np.ndarray,
clip_min: float,
clip_max: float,
) -> np.ndarray:
"""
Internal attack function for one example.
Expand All @@ -212,7 +219,14 @@ def _perturb(

# If an initial adversarial example found, then go with boundary attack
x_adv = self._attack(
initial_sample[0], x, y_p, initial_sample[1], self.delta, self.epsilon, clip_min, clip_max,
initial_sample[0],
x,
y_p,
initial_sample[1],
self.delta,
self.epsilon,
clip_min,
clip_max,
)

return x_adv
Expand Down Expand Up @@ -258,7 +272,10 @@ def _attack(
potential_adv = np.clip(potential_adv, clip_min, clip_max)
potential_advs.append(potential_adv)

preds = np.argmax(self.estimator.predict(np.array(potential_advs), batch_size=self.batch_size), axis=1,)
preds = np.argmax(
self.estimator.predict(np.array(potential_advs), batch_size=self.batch_size),
axis=1,
)

if self.targeted:
satisfied = preds == target
Expand All @@ -285,7 +302,10 @@ def _attack(
perturb *= self.curr_epsilon
potential_advs = x_advs + perturb
potential_advs = np.clip(potential_advs, clip_min, clip_max)
preds = np.argmax(self.estimator.predict(potential_advs, batch_size=self.batch_size), axis=1,)
preds = np.argmax(
self.estimator.predict(potential_advs, batch_size=self.batch_size),
axis=1,
)

if self.targeted:
satisfied = preds == target
Expand Down Expand Up @@ -343,7 +363,14 @@ def _orthogonal_perturb(self, delta: float, current_sample: np.ndarray, original
return perturb

def _init_sample(
self, x: np.ndarray, y: int, y_p: int, init_pred: int, adv_init: np.ndarray, clip_min: float, clip_max: float,
self,
x: np.ndarray,
y: int,
y_p: int,
init_pred: int,
adv_init: np.ndarray,
clip_min: float,
clip_max: float,
) -> Optional[Tuple[np.ndarray, int]]:
"""
Find initial adversarial example for the attack.
Expand Down Expand Up @@ -373,7 +400,8 @@ def _init_sample(
for _ in range(self.init_size):
random_img = nprd.uniform(clip_min, clip_max, size=x.shape).astype(x.dtype)
random_class = np.argmax(
self.estimator.predict(np.array([random_img]), batch_size=self.batch_size), axis=1,
self.estimator.predict(np.array([random_img]), batch_size=self.batch_size),
axis=1,
)[0]

if random_class == y:
Expand All @@ -393,7 +421,8 @@ def _init_sample(
for _ in range(self.init_size):
random_img = nprd.uniform(clip_min, clip_max, size=x.shape).astype(x.dtype)
random_class = np.argmax(
self.estimator.predict(np.array([random_img]), batch_size=self.batch_size), axis=1,
self.estimator.predict(np.array([random_img]), batch_size=self.batch_size),
axis=1,
)[0]

if random_class != y_p:
Expand Down
Loading

0 comments on commit f6a402c

Please sign in to comment.