Ejemplo n.º 1
0
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_classifier_defended_images(art_warning, fix_get_mnist_subset, image_dl_estimator_for_attack):
    try:
        classifier = image_dl_estimator_for_attack(FastGradientMethod, defended=True)
        attack = FastGradientMethod(classifier, eps=1.0, batch_size=128)
        backend_test_defended_images(attack, fix_get_mnist_subset)
    except ARTTestException as e:
        art_warning(e)
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)
def test_classifier_defended_images(fix_get_mnist_subset,
                                    image_dl_estimator_for_attack):

    classifier_list = image_dl_estimator_for_attack(FastGradientMethod,
                                                    defended=True)
    # TODO this if statement must be removed once we have a classifier for both image and tabular data
    if classifier_list is None:
        logging.warning(
            "Couldn't perform  this test because no classifier is defined")
        return

    for classifier in classifier_list:
        attack = FastGradientMethod(classifier, eps=1, batch_size=128)
        backend_test_defended_images(attack, fix_get_mnist_subset)