def test_preprocessing_defences(self): from art.estimators.object_detection.pytorch_faster_rcnn import PyTorchFasterRCNN from art.defences.preprocessor.spatial_smoothing import SpatialSmoothing pre_def = SpatialSmoothing() frcnn = PyTorchFasterRCNN( clip_values=(0, 1), attack_losses=("loss_classifier", "loss_box_reg", "loss_objectness", "loss_rpn_box_reg"), preprocessing_defences=pre_def, ) # Create labels result = frcnn.predict(x=self.x_test) y = [ { "boxes": result[0]["boxes"], "labels": result[0]["labels"], "scores": np.ones_like(result[0]["labels"]), }, { "boxes": result[1]["boxes"], "labels": result[1]["labels"], "scores": np.ones_like(result[1]["labels"]), }, ] # Compute gradients grads = frcnn.loss_gradient(x=self.x_test, y=y) self.assertTrue(grads.shape == (2, 28, 28, 3))
def test_defence_non_pytorch(get_default_mnist_subset, image_dl_estimator, device_type): smooth_3x3 = SpatialSmoothing(window_size=3, channels_first=True) preprocessing_defences = [smooth_3x3] _test_preprocessing_defences_forward(get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences) _test_preprocessing_defences_backward(get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences)
def test_defence_non_tensorflow(get_default_mnist_subset, image_dl_estimator, is_tf_version_2): if is_tf_version_2: smooth_3x3 = SpatialSmoothing(window_size=3, channels_first=False) preprocessing_defences = [smooth_3x3] device_type = None _test_preprocessing_defences_forward(get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences) _test_preprocessing_defences_backward(get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences)
def test_defence_non_pytorch(art_warning, get_default_mnist_subset, image_dl_estimator, device_type): try: smooth_3x3 = SpatialSmoothing(window_size=3, channels_first=True) preprocessing_defences = [smooth_3x3] _test_preprocessing_defences_forward( get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences ) _test_preprocessing_defences_backward( get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences ) except ARTTestException as e: art_warning(e)
def test_defences_tensorflow_and_nontensorflow(art_warning, get_default_mnist_subset, image_dl_estimator, device_type): try: smooth_3x3_nonpth = SpatialSmoothing(window_size=3, channels_first=False) smooth_3x3_pth = SpatialSmoothingTensorFlowV2(window_size=3, channels_first=False) preprocessing_defences = [smooth_3x3_nonpth, smooth_3x3_pth] device_type = None _test_preprocessing_defences_forward( get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences ) _test_preprocessing_defences_backward( get_default_mnist_subset, image_dl_estimator, device_type, preprocessing_defences ) except ARTTestException as e: art_warning(e)
def test_preprocessing_defences(art_warning, get_mnist_dataset): try: from art.estimators.object_detection.tensorflow_faster_rcnn import TensorFlowFasterRCNN from art.defences.preprocessor.spatial_smoothing import SpatialSmoothing images = tf.placeholder(tf.float32, shape=[1, 28, 28, 3]) pre_def = SpatialSmoothing() with pytest.raises(ValueError): _ = TensorFlowFasterRCNN( images=images, clip_values=(0, 1), attack_losses=("loss_classifier", "loss_box_reg", "loss_objectness", "loss_rpn_box_reg"), preprocessing_defences=pre_def, ) except ARTTestException as e: art_warning(e)
def test_compute_loss(self): from art.estimators.object_detection.pytorch_faster_rcnn import PyTorchFasterRCNN from art.defences.preprocessor.spatial_smoothing import SpatialSmoothing pre_def = SpatialSmoothing() frcnn = PyTorchFasterRCNN( clip_values=(0, 1), attack_losses=("loss_classifier", "loss_box_reg", "loss_objectness", "loss_rpn_box_reg"), preprocessing_defences=pre_def, ) # Create labels result = frcnn.predict( np.repeat(self.x_test_mnist[:2].astype(np.float32), repeats=3, axis=3)) y = [ { "boxes": result[0]["boxes"], "labels": result[0]["labels"], "scores": np.ones_like(result[0]["labels"]), }, { "boxes": result[1]["boxes"], "labels": result[1]["labels"], "scores": np.ones_like(result[1]["labels"]), }, ] # Compute loss loss = frcnn.compute_loss(x=self.x_test, y=y) self.assertAlmostEqual(float(loss), 0.6324392, delta=0.01)