Skip to content

Commit

Permalink
Formatting fix
Browse files Browse the repository at this point in the history
Signed-off-by: Kieran Fraser <Kieran.Fraser@ibm.com>
  • Loading branch information
kieranfraser committed Jun 14, 2023
1 parent 2881b44 commit c0332e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
28 changes: 16 additions & 12 deletions art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,14 @@ def __getitem__(self, idx):
target = target.to(self.estimator.device)
else:
targets = []

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable target is not used.
for idx in range(target['boxes'].shape[0]):
targets.append({
'boxes': target['boxes'][idx].to(self.estimator.device),
'labels': target['labels'][idx].to(self.estimator.device),
'scores': target['scores'][idx].to(self.estimator.device),
})
for idx in range(target["boxes"].shape[0]):
targets.append(
{
"boxes": target["boxes"][idx].to(self.estimator.device),
"labels": target["labels"][idx].to(self.estimator.device),
"scores": target["scores"][idx].to(self.estimator.device),
}
)
_ = self._train_step(images=images, target=targets, mask=None)
else:
for images, target, mask_i in data_loader:
Expand All @@ -615,12 +617,14 @@ def __getitem__(self, idx):
target = target.to(self.estimator.device)
else:
targets = []
for idx in range(target['boxes'].shape[0]):
targets.append({
'boxes': target['boxes'][idx].to(self.estimator.device),
'labels': target['labels'][idx].to(self.estimator.device),
'scores': target['scores'][idx].to(self.estimator.device),
})
for idx in range(target["boxes"].shape[0]):
targets.append(
{
"boxes": target["boxes"][idx].to(self.estimator.device),
"labels": target["labels"][idx].to(self.estimator.device),
"scores": target["scores"][idx].to(self.estimator.device),
}
)
mask_i = mask_i.to(self.estimator.device)
_ = self._train_step(images=images, target=targets, mask=mask_i)

Expand Down
48 changes: 31 additions & 17 deletions tests/estimators/object_detection/test_pytorch_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,29 +387,41 @@ def test_pgd(art_warning, get_pytorch_yolo):
except ARTTestException as e:
art_warning(e)


@pytest.mark.only_with_platform("pytorch")
def test_patch(art_warning, get_pytorch_yolo):
try:

from art.attacks.evasion import AdversarialPatchPyTorch

rotation_max=0.0
scale_min=0.1
scale_max=0.3
distortion_scale_max=0.0
learning_rate=1.99
max_iter=2
batch_size=16
patch_shape=(3, 5, 5)
patch_type="circle"
optimizer="pgd"
rotation_max = 0.0
scale_min = 0.1
scale_max = 0.3
distortion_scale_max = 0.0
learning_rate = 1.99
max_iter = 2
batch_size = 16
patch_shape = (3, 5, 5)
patch_type = "circle"
optimizer = "pgd"

object_detector, x_test, y_test = get_pytorch_yolo

ap = AdversarialPatchPyTorch(estimator=object_detector, rotation_max=rotation_max,
scale_min=scale_min, scale_max=scale_max, optimizer=optimizer, distortion_scale_max=distortion_scale_max,
learning_rate=learning_rate, max_iter=max_iter, batch_size=batch_size,
patch_shape=patch_shape, patch_type=patch_type, verbose=True, targeted=False)
ap = AdversarialPatchPyTorch(
estimator=object_detector,
rotation_max=rotation_max,
scale_min=scale_min,
scale_max=scale_max,
optimizer=optimizer,
distortion_scale_max=distortion_scale_max,
learning_rate=learning_rate,
max_iter=max_iter,
batch_size=batch_size,
patch_shape=patch_shape,
patch_type=patch_type,
verbose=True,
targeted=False,
)

_, _ = ap.generate(x=x_test, y=y_test)

Expand All @@ -431,7 +443,9 @@ def test_patch(art_warning, get_pytorch_yolo):
3.6515078e-06,
]
)
np.testing.assert_raises(AssertionError, np.testing.assert_array_almost_equal, result[0]["scores"][:10], expected_detection_scores, 6)
np.testing.assert_raises(
AssertionError, np.testing.assert_array_almost_equal, result[0]["scores"][:10], expected_detection_scores, 6
)

except ARTTestException as e:
art_warning(e)
art_warning(e)

0 comments on commit c0332e1

Please sign in to comment.