@@ -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+
133166def test_classifier_type_check_fail ():
134167 backend_test_classifier_type_check_fail (DPatch , [BaseEstimator , LossGradientsMixin , ObjectDetectorMixin ])
135168
0 commit comments