コード例 #1
0
def test_fgsm_defences(fix_get_mnist_subset, image_dl_estimator, device_type):

    clip_values = (0, 1)
    smooth_3x3 = SpatialSmoothingPyTorch(window_size=3,
                                         channels_first=True,
                                         device_type=device_type)
    smooth_5x5 = SpatialSmoothingPyTorch(window_size=5,
                                         channels_first=True,
                                         device_type=device_type)
    smooth_7x7 = SpatialSmoothingPyTorch(window_size=7,
                                         channels_first=True,
                                         device_type=device_type)
    classifier_, _ = image_dl_estimator(one_classifier=True)

    criterion = nn.CrossEntropyLoss()
    classifier = PyTorchClassifier(
        clip_values=clip_values,
        model=classifier_.model,
        preprocessing_defences=[smooth_3x3, smooth_5x5, smooth_7x7],
        loss=criterion,
        input_shape=(1, 28, 28),
        nb_classes=10,
        device_type=device_type,
    )
    assert len(classifier.preprocessing_defences) == 3

    attack = FastGradientMethod(classifier, eps=1, batch_size=128)
    backend_test_defended_images(attack, fix_get_mnist_subset)
コード例 #2
0
def test_spatial_smoothing_estimate_gradient(art_warning):
    try:
        test_input = np.array([[[[1], [2]], [[3], [4]]]]).astype(float)
        test_output = np.array([[[[2], [2]], [[2], [2]]]]).astype(float)
        spatial_smoothing = SpatialSmoothingPyTorch(channels_first=False,
                                                    window_size=2)

        test_gradients = spatial_smoothing.estimate_gradient(
            x=test_input, grad=np.ones_like(test_output).astype(float))

        assert test_gradients.shape == test_input.shape
    except ARTTestException as e:
        art_warning(e)
コード例 #3
0
def test_defences_chaining(art_warning, get_default_mnist_subset, image_dl_estimator, device_type):
    try:
        smooth_3x3 = SpatialSmoothingPyTorch(window_size=3, channels_first=True, device_type=device_type)
        smooth_5x5 = SpatialSmoothingPyTorch(window_size=5, channels_first=True, device_type=device_type)
        smooth_7x7 = SpatialSmoothingPyTorch(window_size=7, channels_first=True, device_type=device_type)
        preprocessing_defences = [smooth_3x3, smooth_5x5, smooth_7x7]
        _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_image_data(self, image_batch, channels_first,
                                          window_size):
        test_input, test_output = image_batch
        spatial_smoothing = SpatialSmoothingPyTorch(
            channels_first=channels_first, window_size=window_size)

        assert_array_equal(spatial_smoothing(test_input)[0], test_output)
コード例 #5
0
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):
            SpatialSmoothingPyTorch(clip_values=(0, 1, 2))
    except ARTTestException as e:
        art_warning(e)
    def test_non_spatial_data_error(self, tabular_batch):
        test_input = tabular_batch
        spatial_smoothing = SpatialSmoothingPyTorch(channels_first=True)

        exc_msg = "Unrecognized input dimension. Spatial smoothing can only be applied to image and video data."
        with pytest.raises(ValueError, match=exc_msg):
            spatial_smoothing(test_input)
コード例 #7
0
def test_relation_clip_values_error(art_warning):
    try:
        exc_msg = "Invalid 'clip_values': min >= max."
        with pytest.raises(ValueError, match=exc_msg):
            SpatialSmoothingPyTorch(clip_values=(1, 0))
    except ARTTestException as e:
        art_warning(e)
    def test_spatial_smoothing_median_filter_call_expected_behavior(self):
        test_input = np.array([[[[1, 2], [3, 4]]]])
        test_output = np.array([[[[2, 2], [2, 2]]]])
        spatial_smoothing = SpatialSmoothingPyTorch(channels_first=True,
                                                    window_size=2)

        assert_array_equal(spatial_smoothing(test_input)[0], test_output)
コード例 #9
0
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):
            SpatialSmoothingPyTorch(window_size=0)
    except ARTTestException as e:
        art_warning(e)
コード例 #10
0
def test_spatial_smoothing_image_data(art_warning, image_batch, channels_first,
                                      window_size):
    try:
        test_input, test_output = image_batch
        spatial_smoothing = SpatialSmoothingPyTorch(
            channels_first=channels_first, window_size=window_size)

        assert_array_equal(spatial_smoothing(test_input)[0], test_output)
    except ARTTestException as e:
        art_warning(e)
コード例 #11
0
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 = SpatialSmoothingPyTorch(channels_first=True,
                                                    window_size=2)

        assert_array_equal(spatial_smoothing(test_input)[0], test_output)
    except ARTTestException as e:
        art_warning(e)
コード例 #12
0
def test_defence_pytorch(get_default_mnist_subset, image_dl_estimator,
                         device_type):
    smooth_3x3 = SpatialSmoothingPyTorch(window_size=3,
                                         channels_first=True,
                                         device_type=device_type)
    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)
コード例 #13
0
def test_defences_pytorch_and_nonpytorch(art_warning, get_default_mnist_subset, image_dl_estimator, device_type):
    try:
        smooth_3x3_nonpth = SpatialSmoothing(window_size=3, channels_first=True)
        smooth_3x3_pth = SpatialSmoothingPyTorch(window_size=3, channels_first=True, device_type=device_type)
        preprocessing_defences = [smooth_3x3_nonpth, smooth_3x3_pth]
        _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_window_size_error(self):
     exc_msg = "Sliding window size must be a positive integer."
     with pytest.raises(ValueError, match=exc_msg):
         SpatialSmoothingPyTorch(window_size=0)
 def test_triple_clip_values_error(self):
     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):
         SpatialSmoothingPyTorch(clip_values=(0, 1, 2))
    def test_spatial_smoothing_video_data(self, video_batch, channels_first):
        test_input, test_output = video_batch
        spatial_smoothing = SpatialSmoothingPyTorch(
            channels_first=channels_first, window_size=2)

        assert_array_equal(spatial_smoothing(test_input)[0], test_output)
 def test_relation_clip_values_error(self):
     exc_msg = "Invalid 'clip_values': min >= max."
     with pytest.raises(ValueError, match=exc_msg):
         SpatialSmoothingPyTorch(clip_values=(1, 0))