def test_minimal_perturbations_images(fix_get_mnist_subset,
                                      get_image_classifier_list_for_attack):
    classifier_list = get_image_classifier_list_for_attack(FastGradientMethod)
    # 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.0, batch_size=11)
        attack_params = {"minimal": True, "eps_step": 0.1, "eps": 5.0}
        attack.set_params(**attack_params)

        expected_values = {
            "x_test_mean":
            ExpectedValue(0.03896513, 0.01),
            "x_test_min":
            ExpectedValue(-0.30000000, 0.00001),
            "x_test_max":
            ExpectedValue(0.30000000, 0.00001),
            "y_test_pred_adv_expected":
            ExpectedValue(np.asarray([4, 2, 4, 7, 0, 4, 7, 2, 0, 7, 0]), 2),
        }
        backend_check_adverse_values(attack, fix_get_mnist_subset,
                                     expected_values)
def test_norm_images(art_warning, norm, fix_get_mnist_subset, image_dl_estimator_for_attack):
    try:
        classifier = image_dl_estimator_for_attack(FastGradientMethod)

        if norm == np.inf:
            expected_values = {
                "x_test_mean": ExpectedValue(0.2346725, 0.002),
                "x_test_min": ExpectedValue(-1.0, 0.00001),
                "x_test_max": ExpectedValue(1.0, 0.00001),
                "y_test_pred_adv_expected": ExpectedValue(np.asarray([4, 4, 4, 7, 7, 4, 7, 2, 2, 3, 0]), 2),
            }

        elif norm == 1:
            expected_values = {
                "x_test_mean": ExpectedValue(0.00051374, 0.002),
                "x_test_min": ExpectedValue(-0.01486498, 0.001),
                "x_test_max": ExpectedValue(0.014761963, 0.001),
                "y_test_pred_adv_expected": ExpectedValue(np.asarray([7, 1, 1, 4, 4, 1, 4, 4, 4, 4, 4]), 4),
            }
        elif norm == 2:
            expected_values = {
                "x_test_mean": ExpectedValue(0.007636416, 0.001),
                "x_test_min": ExpectedValue(-0.211054801, 0.001),
                "x_test_max": ExpectedValue(0.209592223, 0.001),
                "y_test_pred_adv_expected": ExpectedValue(np.asarray([7, 2, 4, 4, 4, 7, 7, 4, 0, 4, 4]), 2),
            }

        attack = FastGradientMethod(classifier, eps=1.0, norm=norm, batch_size=128)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)
    except ARTTestException as e:
        art_warning(e)
Exemple #3
0
def test_one_shot(fix_get_mnist_subset, image_dl_estimator_for_attack):
    classifier_list = image_dl_estimator_for_attack(FastGradientMethod)
    # 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 the one-shot method, frame saliency attack should resort to plain FastGradientMethod
    expected_values = {
        "x_test_mean":
        ExpectedValue(0.2346725, 0.002),
        "x_test_min":
        ExpectedValue(-1.0, 0.00001),
        "x_test_max":
        ExpectedValue(1.0, 0.00001),
        "y_test_pred_adv_expected":
        ExpectedValue(np.asarray([4, 4, 4, 7, 7, 4, 7, 2, 2, 3, 0]), 2),
    }

    for classifier in classifier_list:
        attacker = FastGradientMethod(classifier, eps=1, batch_size=128)
        attack = FrameSaliencyAttack(classifier, attacker, "one_shot")
        backend_check_adverse_values(attack, fix_get_mnist_subset,
                                     expected_values)
def test_minimal_perturbations_images(art_warning, fix_get_mnist_subset, image_dl_estimator_for_attack):
    try:
        classifier = image_dl_estimator_for_attack(FastGradientMethod)

        expected_values = {
            "x_test_mean": ExpectedValue(0.03896513, 0.01),
            "x_test_min": ExpectedValue(-0.30000000, 0.00001),
            "x_test_max": ExpectedValue(0.30000000, 0.00001),
            "y_test_pred_adv_expected": ExpectedValue(np.asarray([4, 2, 4, 7, 0, 4, 7, 2, 0, 7, 0]), 2),
        }

        attack = FastGradientMethod(classifier, eps=1.0, batch_size=11)

        # Test eps of float type
        attack_params = {"minimal": True, "eps_step": 0.1, "eps": 5.0}
        attack.set_params(**attack_params)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)

        # Test eps of array type 1
        (_, _, x_test_mnist, _) = fix_get_mnist_subset
        eps = np.ones(shape=x_test_mnist.shape) * 5.0
        eps_step = np.ones_like(eps) * 0.1

        attack_params = {"minimal": True, "eps_step": eps_step, "eps": eps}
        attack.set_params(**attack_params)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)

        # Test eps of array type 2
        eps = np.ones(shape=x_test_mnist.shape[1:]) * 5.0
        eps_step = np.ones_like(eps) * 0.1

        attack_params = {"minimal": True, "eps_step": eps_step, "eps": eps}
        attack.set_params(**attack_params)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)

        # Test eps of array type 3
        eps = np.ones(shape=x_test_mnist.shape[2:]) * 5.0
        eps_step = np.ones_like(eps) * 0.1

        attack_params = {"minimal": True, "eps_step": eps_step, "eps": eps}
        attack.set_params(**attack_params)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)

        # Test eps of array type 4
        eps = np.ones(shape=x_test_mnist.shape[3:]) * 5.0
        eps_step = np.ones_like(eps) * 0.1

        attack_params = {"minimal": True, "eps_step": eps_step, "eps": eps}
        attack.set_params(**attack_params)

        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)

    except ARTTestException as e:
        art_warning(e)
def test_norm_images(norm, fix_get_mnist_subset,
                     get_image_classifier_list_for_attack):
    classifier_list = get_image_classifier_list_for_attack(FastGradientMethod)
    # 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

    elif norm == np.inf:
        expected_values = {
            "x_test_mean":
            ExpectedValue(0.2346725, 0.002),
            "x_test_min":
            ExpectedValue(-1.0, 0.00001),
            "x_test_max":
            ExpectedValue(1.0, 0.00001),
            "y_test_pred_adv_expected":
            ExpectedValue(np.asarray([4, 4, 4, 7, 7, 4, 7, 2, 2, 3, 0]), 2),
        }

    elif norm == 1:
        expected_values = {
            "x_test_mean":
            ExpectedValue(0.00051374, 0.002),
            "x_test_min":
            ExpectedValue(-0.01486498, 0.001),
            "x_test_max":
            ExpectedValue(0.014761963, 0.001),
            "y_test_pred_adv_expected":
            ExpectedValue(np.asarray([7, 1, 1, 4, 4, 1, 4, 4, 4, 4, 4]), 4),
        }
    elif norm == 2:
        expected_values = {
            "x_test_mean":
            ExpectedValue(0.007636416, 0.001),
            "x_test_min":
            ExpectedValue(-0.211054801, 0.001),
            "x_test_max":
            ExpectedValue(0.209592223, 0.001),
            "y_test_pred_adv_expected":
            ExpectedValue(np.asarray([7, 2, 4, 4, 4, 7, 7, 4, 0, 4, 4]), 2),
        }

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

        backend_check_adverse_values(attack, fix_get_mnist_subset,
                                     expected_values)
Exemple #6
0
def test_one_shot(art_warning, fix_get_mnist_subset, image_dl_estimator_for_attack):
    try:
        classifier = image_dl_estimator_for_attack(FastGradientMethod)

        # for the one-shot method, frame saliency attack should resort to plain FastGradientMethod
        expected_values = {
            "x_test_mean": ExpectedValue(0.2346725, 0.002),
            "x_test_min": ExpectedValue(-1.0, 0.00001),
            "x_test_max": ExpectedValue(1.0, 0.00001),
            "y_test_pred_adv_expected": ExpectedValue(np.asarray([4, 4, 4, 7, 7, 4, 7, 2, 2, 3, 0]), 2),
        }

        attacker = FastGradientMethod(classifier, eps=1.0, batch_size=128)
        attack = FrameSaliencyAttack(classifier, attacker, "one_shot")
        backend_check_adverse_values(attack, fix_get_mnist_subset, expected_values)
    except ARTTestException as e:
        art_warning(e)