Skip to content

Commit 8827ce9

Browse files
committed
Fix bug in sampling patch location from mask
Signed-off-by: Beat Buesser <[email protected]>
1 parent 260e9eb commit 8827ce9

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,23 +381,23 @@ def _random_overlay(
381381
else:
382382
mask_2d = mask[i_sample, :, :]
383383

384-
edge_x_0 = int(im_scale * padded_patch.shape[self.i_w + 1]) // 2
385-
edge_x_1 = int(im_scale * padded_patch.shape[self.i_w + 1]) - edge_x_0
386-
edge_y_0 = int(im_scale * padded_patch.shape[self.i_h + 1]) // 2
387-
edge_y_1 = int(im_scale * padded_patch.shape[self.i_h + 1]) - edge_y_0
388-
389-
mask_2d[0:edge_x_0, :] = False
390-
if edge_x_1 > 0:
391-
mask_2d[-edge_x_1:, :] = False
392-
mask_2d[:, 0:edge_y_0] = False
393-
if edge_y_1 > 0:
394-
mask_2d[:, -edge_y_1:] = False
395-
396-
num_pos = np.argwhere(mask_2d).shape[0]
397-
pos_id = np.random.choice(num_pos, size=1)
398-
pos = np.argwhere(mask_2d)[pos_id[0]]
399-
x_shift = pos[1] - self.image_shape[self.i_w] // 2
384+
edge_h_0 = int(im_scale * padded_patch.shape[self.i_h + 1]) // 2
385+
edge_h_1 = int(im_scale * padded_patch.shape[self.i_h + 1]) - edge_h_0
386+
edge_w_0 = int(im_scale * padded_patch.shape[self.i_w + 1]) // 2
387+
edge_w_1 = int(im_scale * padded_patch.shape[self.i_w + 1]) - edge_w_0
388+
389+
mask_2d[0:edge_h_0, :] = False
390+
if edge_h_1 > 0:
391+
mask_2d[-edge_h_1:, :] = False
392+
mask_2d[:, 0:edge_w_0] = False
393+
if edge_w_1 > 0:
394+
mask_2d[:, -edge_w_1:] = False
395+
396+
num_pos = np.nonzero(mask_2d.int())
397+
pos_id = np.random.choice(num_pos.shape[0], size=1, replace=False)
398+
pos = num_pos[pos_id[0]]
400399
y_shift = pos[0] - self.image_shape[self.i_h] // 2
400+
x_shift = pos[1] - self.image_shape[self.i_w] // 2
401401

402402
phi_rotate = float(np.random.uniform(-self.rotation_max, self.rotation_max))
403403

0 commit comments

Comments
 (0)