Skip to content

Commit 442bada

Browse files
committed
Fixing compatibility issue between PyTorch YOLO and AdversarialPatchPyTorch, updating notebook
Signed-off-by: Kieran Fraser <[email protected]>
1 parent 49dab71 commit 442bada

File tree

3 files changed

+1608
-119
lines changed

3 files changed

+1608
-119
lines changed

art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ def __getitem__(self, idx):
573573
img = torch.from_numpy(self.x[idx])
574574

575575
target = {}
576-
target["boxes"] = torch.from_numpy(y[idx]["boxes"])
577-
target["labels"] = torch.from_numpy(y[idx]["labels"])
578-
target["scores"] = torch.from_numpy(y[idx]["scores"])
576+
target["boxes"] = torch.from_numpy(self.y[idx]["boxes"])
577+
target["labels"] = torch.from_numpy(self.y[idx]["labels"])
578+
target["scores"] = torch.from_numpy(self.y[idx]["scores"])
579579
mask_i = torch.from_numpy(self.mask[idx])
580580

581581
return img, target, mask_i
@@ -600,19 +600,21 @@ def __getitem__(self, idx):
600600
if isinstance(target, torch.Tensor):
601601
target = target.to(self.estimator.device)
602602
else:
603-
target["boxes"] = target["boxes"].to(self.estimator.device)
604-
target["labels"] = target["labels"].to(self.estimator.device)
605-
target["scores"] = target["scores"].to(self.estimator.device)
603+
target["boxes"] = target["boxes"][0].to(self.estimator.device)
604+
target["labels"] = target["labels"][0].to(self.estimator.device)
605+
target["scores"] = target["scores"][0].to(self.estimator.device)
606+
target = [target]
606607
_ = self._train_step(images=images, target=target, mask=None)
607608
else:
608609
for images, target, mask_i in data_loader:
609610
images = images.to(self.estimator.device)
610611
if isinstance(target, torch.Tensor):
611612
target = target.to(self.estimator.device)
612613
else:
613-
target["boxes"] = target["boxes"].to(self.estimator.device)
614-
target["labels"] = target["labels"].to(self.estimator.device)
615-
target["scores"] = target["scores"].to(self.estimator.device)
614+
target["boxes"] = target["boxes"][0].to(self.estimator.device)
615+
target["labels"] = target["labels"][0].to(self.estimator.device)
616+
target["scores"] = target["scores"][0].to(self.estimator.device)
617+
target = [target]
616618
mask_i = mask_i.to(self.estimator.device)
617619
_ = self._train_step(images=images, target=target, mask=mask_i)
618620

art/estimators/object_detection/pytorch_yolo.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ def _preprocess_and_convert_inputs(
321321

322322
# Set gradients
323323
if not no_grad:
324-
x_tensor.requires_grad = True
324+
if x_tensor.is_leaf:
325+
x_tensor.requires_grad = True
326+
else:
327+
x_tensor.retain_grad()
325328

326329
# Apply framework-specific preprocessing
327330
x_preprocessed, y_preprocessed = self._apply_preprocessing(x=x_tensor, y=y_tensor, fit=fit, no_grad=no_grad)
@@ -375,6 +378,8 @@ def _get_losses(
375378
:return: Loss gradients of the same shape as `x`.
376379
"""
377380
self._model.train()
381+
self.set_batchnorm(train=False)
382+
self.set_dropout(train=False)
378383

379384
# Apply preprocessing and convert to tensors
380385
x_preprocessed, y_preprocessed = self._preprocess_and_convert_inputs(x=x, y=y, fit=False, no_grad=False)

notebooks/adversarial_patch/attack_adversarial_patch_pytorch_yolo.ipynb

Lines changed: 1591 additions & 109 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)