Skip to content

Commit bed6945

Browse files
authored
Merge branch 'dev_1.15.0' into signedqiu
2 parents 012b940 + 9120a44 commit bed6945

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

art/attacks/evasion/sign_opt.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,14 @@ def _fine_grained_binary_search_local(
306306
lbd = initial_lbd
307307
# For targeted: we want to expand(x1.01) boundary away from targeted dataset
308308
# For untargeted, we want to slim(x0.99) the boundary toward the original dataset
309-
if (not self._is_label(x_0 + lbd * theta, target) and self.targeted) or (
310-
self._is_label(x_0 + lbd * theta, y_0) and not self.targeted
309+
if (self.targeted and not self._is_label(x_0 + lbd * theta, target)) or (
310+
not self.targeted and self._is_label(x_0 + lbd * theta, y_0)
311311
):
312312
lbd_lo = lbd
313313
lbd_hi = lbd * 1.01
314314
nquery += 1
315-
while (not self._is_label(x_0 + lbd_hi * theta, target) and self.targeted) or (
316-
self._is_label(x_0 + lbd_hi * theta, y_0) and not self.targeted
315+
while (self.targeted and not self._is_label(x_0 + lbd_hi * theta, target)) or (
316+
not self.targeted and self._is_label(x_0 + lbd_hi * theta, y_0)
317317
):
318318
lbd_hi = lbd_hi * 1.01
319319
nquery += 1
@@ -323,17 +323,17 @@ def _fine_grained_binary_search_local(
323323
lbd_hi = lbd
324324
lbd_lo = lbd * 0.99
325325
nquery += 1
326-
while (self._is_label(x_0 + lbd_lo * theta, target) and self.targeted) or (
327-
not self._is_label(x_0 + lbd_lo * theta, y_0) and not self.targeted
326+
while (self.targeted and self._is_label(x_0 + lbd_lo * theta, target)) or (
327+
not self.targeted and not self._is_label(x_0 + lbd_lo * theta, y_0)
328328
):
329329
lbd_lo = lbd_lo * 0.99
330330
nquery += 1
331331

332332
while (lbd_hi - lbd_lo) > tol:
333333
lbd_mid = (lbd_lo + lbd_hi) / 2.0
334334
nquery += 1
335-
if (self._is_label(x_0 + lbd_mid * theta, target) and self.targeted) or (
336-
not self._is_label(x_0 + lbd_mid * theta, y_0) and not self.targeted
335+
if (self.targeted and self._is_label(x_0 + lbd_mid * theta, target)) or (
336+
not self.targeted and not self._is_label(x_0 + lbd_mid * theta, y_0)
337337
):
338338
lbd_hi = lbd_mid
339339
else:

art/attacks/poisoning/perturbations/image_perturbations.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def insert_image(
127127
original_dtype = x.dtype
128128
data = np.copy(x)
129129
if channels_first:
130-
data = data.transpose([1, 2, 0])
130+
data = np.transpose(data, (1, 2, 0))
131131

132132
height, width, num_channels = data.shape
133133

@@ -136,37 +136,36 @@ def insert_image(
136136
backdoored_img = Image.new("RGBA", (width, height), 0) # height and width are swapped for PIL
137137

138138
if no_color:
139-
backdoored_input = Image.fromarray((data * 255).astype("uint8").squeeze(axis=2), mode=mode)
139+
backdoored_input = Image.fromarray((data * 255).astype(np.uint8).squeeze(axis=2), mode=mode)
140140
else:
141-
backdoored_input = Image.fromarray((data * 255).astype("uint8"), mode=mode)
141+
backdoored_input = Image.fromarray((data * 255).astype(np.uint8), mode=mode)
142142

143143
orig_img.paste(backdoored_input)
144144

145145
trigger = Image.open(backdoor_path).convert("RGBA")
146-
if size:
147-
trigger = trigger.resize(size)
146+
if size is not None:
147+
trigger = trigger.resize((size[1], size[0])) # height and width are swapped for PIL
148148

149149
backdoor_width, backdoor_height = trigger.size # height and width are swapped for PIL
150150

151151
if backdoor_width > width or backdoor_height > height:
152152
raise ValueError("Backdoor does not fit inside original image")
153153

154154
if random:
155-
x_shift = np.random.randint(width - backdoor_width)
156-
y_shift = np.random.randint(height - backdoor_height)
155+
x_shift = np.random.randint(width - backdoor_width + 1)
156+
y_shift = np.random.randint(height - backdoor_height + 1)
157157

158158
backdoored_img.paste(trigger, (x_shift, y_shift), mask=trigger)
159159
composite = Image.alpha_composite(orig_img, backdoored_img)
160160
backdoored_img = Image.blend(orig_img, composite, blend)
161-
162161
backdoored_img = backdoored_img.convert(mode)
163162

164-
res = np.array(backdoored_img) / 255.0
163+
res = np.asarray(backdoored_img) / 255.0
165164

166165
if no_color:
167166
res = np.expand_dims(res, 2)
168167

169168
if channels_first:
170-
res = res.transpose([2, 0, 1])
169+
res = np.transpose(res, (2, 0, 1))
171170

172171
return res.astype(original_dtype)

0 commit comments

Comments
 (0)