def test_6_keras(self):
        """
        Second test with the KerasClassifier.
        :return:
        """
        krc = get_image_classifier_kr(from_logits=True)

        attack_ap = AdversarialPatch(
            krc, rotation_max=0.5, scale_min=0.4, scale_max=0.41, learning_rate=5.0, batch_size=10, max_iter=5
        )

        target = np.zeros(self.x_train_mnist.shape[0])
        patch_adv, _ = attack_ap.generate(self.x_train_mnist, target)

        self.assertAlmostEqual(patch_adv[8, 8, 0], 0.67151666, delta=0.05)
        self.assertAlmostEqual(patch_adv[14, 14, 0], 0.6292826, delta=0.05)
        self.assertAlmostEqual(float(np.sum(patch_adv)), 424.31439208984375, delta=1.0)

        # insert_transformed_patch
        x_out = attack_ap.insert_transformed_patch(
            self.x_train_mnist[0], np.ones((14, 14, 1)), np.asarray([[2, 13], [2, 18], [12, 22], [8, 13]])
        )
        x_out_expexted = np.array(
            [
                0.0,
                0.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                0.84313726,
                0.0,
                0.0,
                0.0,
                0.0,
                0.1764706,
                0.7294118,
                0.99215686,
                0.99215686,
                0.5882353,
                0.10588235,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
            dtype=np.float32,
        )
        np.testing.assert_almost_equal(x_out[15, :, 0], x_out_expexted, decimal=3)
Esempio n. 2
0
    def test_3_tensorflow_v2_framework(self):
        """
        First test with the TensorFlowClassifier.
        :return:
        """
        tfc, _ = get_image_classifier_tf(from_logits=True)

        attack_ap = AdversarialPatch(
            tfc,
            rotation_max=0.5,
            scale_min=0.4,
            scale_max=0.41,
            learning_rate=5.0,
            batch_size=10,
            max_iter=10,
            patch_shape=(28, 28, 1),
            verbose=False,
        )

        target = np.zeros(self.x_train_mnist.shape[0])
        patch_adv, _ = attack_ap.generate(self.x_train_mnist, target, shuffle=False)

        self.assertAlmostEqual(patch_adv[8, 8, 0], 1.0, delta=0.05)
        self.assertAlmostEqual(patch_adv[14, 14, 0], 0.0, delta=0.05)
        self.assertAlmostEqual(float(np.sum(patch_adv)), 377.415771484375, delta=1.0)

        # insert_transformed_patch
        x_out = attack_ap.insert_transformed_patch(
            self.x_train_mnist[0], np.ones((14, 14, 1)), np.asarray([[2, 13], [2, 18], [12, 22], [8, 13]])
        )
        x_out_expexted = np.array(
            [
                0.0,
                0.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                0.84313726,
                0.0,
                0.0,
                0.0,
                0.0,
                0.1764706,
                0.7294118,
                0.99215686,
                0.99215686,
                0.5882353,
                0.10588235,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
            dtype=np.float32,
        )
        np.testing.assert_almost_equal(x_out[15, :, 0], x_out_expexted, decimal=3)

        mask = np.ones((1, 28, 28)).astype(bool)
        attack_ap.apply_patch(x=self.x_train_mnist, scale=0.1, mask=mask)
        attack_ap.reset_patch(initial_patch_value=None)
        attack_ap.reset_patch(initial_patch_value=1.0)
        attack_ap.reset_patch(initial_patch_value=patch_adv)