Skip to content

Commit 99f053e

Browse files
author
Beat Buesser
committed
Add additional tests for DPatch
Signed-off-by: Beat Buesser <[email protected]>
1 parent 7b10233 commit 99f053e

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

art/attacks/evasion/dpatch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def generate(
112112
raise ValueError("The target_label has to be a 1-dimensional array.")
113113
self.target_label = target_label.tolist()
114114
else:
115-
if not len(target_label == x.shape[0]) or not isinstance(target_label, list):
115+
if not len(target_label) == x.shape[0] or not isinstance(target_label, list):
116116
raise ValueError("The target_label as list of integers needs to of length number of images in `x`.")
117117
self.target_label = target_label
118118

@@ -134,7 +134,7 @@ def generate(
134134

135135
target_dict = dict()
136136
target_dict["boxes"] = np.asarray([[i_x_1, i_y_1, i_x_2, i_y_2]])
137-
target_dict["labels"] = np.asarray([target_label[i_image],])
137+
target_dict["labels"] = np.asarray([self.target_label[i_image],])
138138
target_dict["scores"] = np.asarray([1.0,])
139139

140140
patch_target.append(target_dict)
@@ -150,7 +150,7 @@ def generate(
150150
x=patched_images[i_batch_start:i_batch_end], y=patch_target[i_batch_start:i_batch_end],
151151
)
152152

153-
for i_image in range(self.batch_size):
153+
for i_image in range(patched_images.shape[0]):
154154

155155
i_x_1 = transforms[i_batch_start + i_image]["i_x_1"]
156156
i_x_2 = transforms[i_batch_start + i_image]["i_x_2"]

tests/attacks/evasion/test_dpatch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,39 @@ def test_augment_images_with_patch(random_location, image_format, fix_get_mnist_
130130
np.testing.assert_array_equal(patched_images[1, 2, :, 0], patched_images_column)
131131

132132

133+
def test_exceptions(get_default_mnist_subset, image_dl_estimator):
134+
class ObjectDetector(BaseEstimator, LossGradientsMixin, ObjectDetectorMixin):
135+
136+
clip_values = (0, 1)
137+
channels_first = False
138+
139+
def fit(self):
140+
pass
141+
142+
def loss_gradient(self, x, y, **kwargs):
143+
pass
144+
145+
def predict(self, x, **kwargs):
146+
pass
147+
148+
estimator = ObjectDetector()
149+
150+
(x_train_mnist, y_train_mnist), (_, _) = get_default_mnist_subset
151+
152+
attack = DPatch(estimator=estimator, patch_shape=(4, 4, 1), learning_rate=5.0, max_iter=5, batch_size=16,)
153+
154+
with pytest.raises(ValueError, match="The DPatch attack does not use target labels."):
155+
attack.generate(x=x_train_mnist, y=y_train_mnist)
156+
157+
with pytest.raises(
158+
ValueError, match="The target_label as list of integers needs to of length number of images in" " `x`."
159+
):
160+
attack.generate(x=x_train_mnist, y=None, target_label=[1, 2, 3])
161+
162+
with pytest.raises(ValueError, match="The target_label has to be a 1-dimensional array."):
163+
attack.generate(x=x_train_mnist, y=None, target_label=np.asarray([[1, 2, 3], [4, 5, 6]]))
164+
165+
133166
def test_classifier_type_check_fail():
134167
backend_test_classifier_type_check_fail(DPatch, [BaseEstimator, LossGradientsMixin, ObjectDetectorMixin])
135168

0 commit comments

Comments
 (0)