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_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_targeted_images(art_warning, fix_get_mnist_subset, image_dl_estimator_for_attack):
    try:
        classifier = image_dl_estimator_for_attack(FastGradientMethod)
        attack = FastGradientMethod(classifier, eps=1.0, targeted=True)
        attack_params = {"minimal": True, "eps_step": 0.01, "eps": 1.0}
        attack.set_params(**attack_params)

        backend_targeted_images(attack, fix_get_mnist_subset)
    except ARTTestException as e:
        art_warning(e)
def test_targeted_images(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 classifier in classifier_list:
        attack = FastGradientMethod(classifier, eps=1.0, targeted=True)
        attack_params = {"minimal": True, "eps_step": 0.01, "eps": 1.0}
        attack.set_params(**attack_params)

        backend_targeted_images(attack, fix_get_mnist_subset)