def test_fgsm_defences(fix_get_mnist_subset, image_dl_estimator, is_tf_version_2): if is_tf_version_2: clip_values = (0, 1) smooth_3x3 = SpatialSmoothingTensorFlowV2(window_size=3, channels_first=False) smooth_5x5 = SpatialSmoothingTensorFlowV2(window_size=5, channels_first=False) smooth_7x7 = SpatialSmoothingTensorFlowV2(window_size=7, channels_first=False) classifier_, _ = image_dl_estimator(one_classifier=True) loss_object = tf.keras.losses.CategoricalCrossentropy(from_logits=True) classifier = TensorFlowV2Classifier( clip_values=clip_values, model=classifier_.model, preprocessing_defences=[smooth_3x3, smooth_5x5, smooth_7x7], loss_object=loss_object, input_shape=(28, 28, 1), nb_classes=10, ) assert len(classifier.preprocessing_defences) == 3 attack = FastGradientMethod(classifier, eps=1, batch_size=128) backend_test_defended_images(attack, fix_get_mnist_subset)
def test_spatial_smoothing_video_data(art_warning, video_batch, channels_first): try: test_input, test_output = video_batch if channels_first: exc_msg = "Only channels last input data is supported" with pytest.raises(ValueError, match=exc_msg): _ = SpatialSmoothingTensorFlowV2(channels_first=channels_first, window_size=2) else: spatial_smoothing = SpatialSmoothingTensorFlowV2(channels_first=channels_first, window_size=2) assert_array_equal(spatial_smoothing(test_input)[0], test_output) except ARTTestException as e: art_warning(e)
def test_defences_chaining(art_warning, get_default_mnist_subset, image_dl_estimator): try: smooth_3x3 = SpatialSmoothingTensorFlowV2(window_size=3, channels_first=False) smooth_5x5 = SpatialSmoothingTensorFlowV2(window_size=5, channels_first=False) smooth_7x7 = SpatialSmoothingTensorFlowV2(window_size=7, channels_first=False) preprocessing_defences = [smooth_3x3, smooth_5x5, smooth_7x7] 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_spatial_smoothing_video_data(self, video_batch, channels_first, is_tf_version_2): if is_tf_version_2: test_input, test_output = video_batch if channels_first: exc_msg = "Only channels last input data is supported" with pytest.raises(ValueError, match=exc_msg): _ = SpatialSmoothingTensorFlowV2( channels_first=channels_first, window_size=2) else: spatial_smoothing = SpatialSmoothingTensorFlowV2( channels_first=channels_first, window_size=2) assert_array_equal( spatial_smoothing(test_input)[0], test_output)
def test_relation_clip_values_error(art_warning): try: exc_msg = "Invalid 'clip_values': min >= max." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(clip_values=(1, 0)) except ARTTestException as e: art_warning(e)
def test_triple_clip_values_error(art_warning): try: exc_msg = "'clip_values' should be a tuple of 2 floats or arrays containing the allowed data range." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(clip_values=(0, 1, 2)) except ARTTestException as e: art_warning(e)
def test_window_size_error(art_warning): try: exc_msg = "Sliding window size must be a positive integer." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(window_size=0) except ARTTestException as e: art_warning(e)
def test_defences_chaining(get_default_mnist_subset, image_dl_estimator, is_tf_version_2): if is_tf_version_2: smooth_3x3 = SpatialSmoothingTensorFlowV2(window_size=3, channels_first=False) smooth_5x5 = SpatialSmoothingTensorFlowV2(window_size=5, channels_first=False) smooth_7x7 = SpatialSmoothingTensorFlowV2(window_size=7, channels_first=False) preprocessing_defences = [smooth_3x3, smooth_5x5, smooth_7x7] 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_spatial_smoothing_median_filter_call(self, is_tf_version_2): if is_tf_version_2: test_input = np.array([[[[1], [2]], [[3], [4]]]]) test_output = np.array([[[[1], [2]], [[3], [3]]]]) spatial_smoothing = SpatialSmoothingTensorFlowV2( channels_first=False, window_size=2) assert_array_equal(spatial_smoothing(test_input)[0], test_output)
def test_spatial_smoothing_median_filter_call_expected_behavior(art_warning): try: test_input = np.array([[[[1], [2]], [[3], [4]]]]) test_output = np.array([[[[2], [2]], [[2], [2]]]]) spatial_smoothing = SpatialSmoothingTensorFlowV2(channels_first=False, window_size=2) assert_array_equal(spatial_smoothing(test_input)[0], test_output) except ARTTestException as e: art_warning(e)
def test_non_spatial_data_error(self, tabular_batch, is_tf_version_2): if is_tf_version_2: test_input = tabular_batch spatial_smoothing = SpatialSmoothingTensorFlowV2( channels_first=False) exc_msg = "Unrecognized input dimension. Spatial smoothing can only be applied to image" with pytest.raises(ValueError, match=exc_msg): spatial_smoothing(test_input)
def test_non_spatial_data_error(art_warning, tabular_batch): try: test_input = tabular_batch spatial_smoothing = SpatialSmoothingTensorFlowV2(channels_first=False) exc_msg = "Unrecognized input dimension. Spatial smoothing can only be applied to image" with pytest.raises(ValueError, match=exc_msg): spatial_smoothing(test_input) except ARTTestException as e: art_warning(e)
def test_spatial_smoothing_estimate_gradient(art_warning): try: test_input = np.array([[[[1], [2]], [[3], [4]]]]) test_output = np.array([[[[2], [2]], [[2], [2]]]]) spatial_smoothing = SpatialSmoothingTensorFlowV2(channels_first=False, window_size=2) test_gradients = spatial_smoothing.estimate_gradient( x=test_input, grad=np.ones_like(test_output)) assert test_gradients.shape == test_input.shape 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_defences_tensorflow_and_nontensorflow(get_default_mnist_subset, image_dl_estimator, device_type, is_tf_version_2): if is_tf_version_2: 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)
def test_window_size_error(self, is_tf_version_2): if is_tf_version_2: exc_msg = "Sliding window size must be a positive integer." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(window_size=0)
def test_relation_clip_values_error(self, is_tf_version_2): if is_tf_version_2: exc_msg = "Invalid 'clip_values': min >= max." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(clip_values=(1, 0))
def test_triple_clip_values_error(self, is_tf_version_2): if is_tf_version_2: exc_msg = "'clip_values' should be a tuple of 2 floats or arrays containing the allowed data range." with pytest.raises(ValueError, match=exc_msg): SpatialSmoothingTensorFlowV2(clip_values=(0, 1, 2))