How to use the art.utils_test.get_classifier_pt function in art

To help you get started, we’ve selected a few art examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IBM / adversarial-robustness-toolbox / tests / attacks / test_projected_gradient_descent.py View on Github external
scores = cls.classifier_k._model.evaluate(x_test, y_test)
        logger.info('[Keras, MNIST] Accuracy on test set: %.2f%%', scores[1] * 100)

        # Create basic CNN on MNIST using TensorFlow
        cls.classifier_tf, sess = get_classifier_tf()

        scores = get_labels_np_array(cls.classifier_tf.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logger.info('[TF, MNIST] Accuracy on training set: %.2f%%', acc * 100)

        scores = get_labels_np_array(cls.classifier_tf.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logger.info('[TF, MNIST] Accuracy on test set: %.2f%%', acc * 100)

        # Create basic PyTorch model
        cls.classifier_py = get_classifier_pt()
        x_train, x_test = np.swapaxes(x_train, 1, 3), np.swapaxes(x_test, 1, 3)
        x_train, x_test = x_train.astype(np.float32), x_test.astype(np.float32)

        scores = get_labels_np_array(cls.classifier_py.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logger.info('[PyTorch, MNIST] Accuracy on training set: %.2f%%', acc * 100)

        scores = get_labels_np_array(cls.classifier_py.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logger.info('[PyTorch, MNIST] Accuracy on test set: %.2f%%', acc * 100)
github IBM / adversarial-robustness-toolbox / tests / classifiers / test_pytorch.py View on Github external
def test_input_shape(self):
        classifier = get_classifier_pt()
        self.assertEqual(classifier.input_shape, (1, 28, 28))
github IBM / adversarial-robustness-toolbox / tests / classifiers / test_pytorch.py View on Github external
def test_fit_predict(self):
        classifier = get_classifier_pt()
        predictions = classifier.predict(self.x_test)
        accuracy = np.sum(np.argmax(predictions, axis=1) == np.argmax(self.y_test, axis=1)) / NB_TEST
        logger.info('Accuracy after fitting: %.2f%%', (accuracy * 100))
        self.assertEqual(accuracy, 0.3)
github IBM / adversarial-robustness-toolbox / tests / attacks / test_virtual_adversarial.py View on Github external
scores = cls.classifier_k._model.evaluate(x_test, y_test)
        logging.info('[Keras, MNIST] Accuracy on test set: %.2f%%', (scores[1] * 100))

        # Create basic CNN on MNIST using TensorFlow
        cls.classifier_tf, sess = get_classifier_tf()

        scores = get_labels_np_array(cls.classifier_tf.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logging.info('[TF, MNIST] Accuracy on training set: %.2f%%', (acc * 100))

        scores = get_labels_np_array(cls.classifier_tf.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logging.info('[TF, MNIST] Accuracy on test set: %.2f%%', (acc * 100))

        # Create basic PyTorch model
        cls.classifier_py = get_classifier_pt()
        x_train, x_test = np.swapaxes(x_train, 1, 3), np.swapaxes(x_test, 1, 3)

        scores = get_labels_np_array(cls.classifier_py.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logging.info('[PyTorch, MNIST] Accuracy on training set: %.2f%%', (acc * 100))

        scores = get_labels_np_array(cls.classifier_py.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logging.info('[PyTorch, MNIST] Accuracy on test set: %.2f%%', (acc * 100))
github IBM / adversarial-robustness-toolbox / tests / attacks / test_adversarial_patch.py View on Github external
def test_ptclassifier(self):
        """
        Third test with the PyTorchClassifier.
        :return:
        """
        ptc = get_classifier_pt()

        x_train = np.swapaxes(self.x_train, 1, 3).astype(np.float32)

        attack_ap = AdversarialPatch(ptc, rotation_max=22.5, scale_min=0.1, scale_max=1.0, learning_rate=5.0,
                                     batch_size=10, max_iter=500)

        patch_adv, _ = attack_ap.generate(x_train)

        self.assertAlmostEqual(patch_adv[0, 8, 8], -3.143605902784875, delta=0.1)
        self.assertAlmostEqual(patch_adv[0, 14, 14], 19.790434152473054, delta=0.1)
        self.assertAlmostEqual(float(np.sum(patch_adv)), 394.905, delta=0.1)
github IBM / adversarial-robustness-toolbox / tests / attacks / test_spatial_transformation.py View on Github external
def test_ptclassifier(self):
        """
        Third test with the PyTorchClassifier.
        :return:
        """
        # Build PyTorchClassifier
        ptc = get_classifier_pt()

        x_train = np.swapaxes(self.x_train, 1, 3).astype(np.float32)
        x_test = np.swapaxes(self.x_test, 1, 3).astype(np.float32)

        # Attack
        attack_st = SpatialTransformation(ptc, max_translation=10.0, num_translations=3, max_rotation=30.0,
                                          num_rotations=3)
        x_train_adv = attack_st.generate(x_train)

        print('abs(x_train_adv[0, 0, 13, :]', abs(x_train[0, 0, 13, :]))
        print('abs(x_train_adv[0, 0, 13, :]', abs(x_train_adv[0, 0, 13, :]))

        self.assertAlmostEqual(x_train_adv[0, 0, 13, 7], 0.287, delta=0.01)
        self.assertAlmostEqual(attack_st.fooling_rate, 0.82, delta=0.01)

        self.assertEqual(attack_st.attack_trans_x, 0)
github IBM / adversarial-robustness-toolbox / tests / classifiers / test_pytorch.py View on Github external
def test_loss_gradient(self):
        classifier = get_classifier_pt()
        gradients = classifier.loss_gradient(self.x_test, self.y_test)

        self.assertEqual(gradients.shape, (NB_TEST, 1, 28, 28))

        expected_gradients_1 = np.asarray([1.15053124e-04, 1.09845030e-04, 1.15488816e-04, 3.37422716e-05,
                                           1.84526041e-04, 1.60381125e-04, 3.05866299e-04, 1.81207652e-04,
                                           6.27528992e-04, -8.84818073e-05, 4.38439834e-04, 9.20038365e-05,
                                           -4.22611454e-04, -8.74137331e-05, 1.77365995e-03, 6.17997837e-04,
                                           -1.99291739e-04, 1.21479861e-04, -8.62729270e-04, -2.84300098e-04,
                                           5.79916057e-04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
                                           0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00])
        np.testing.assert_array_almost_equal(gradients[0, 0, :, 14], expected_gradients_1, decimal=4)

        expected_gradients_2 = np.asarray([-6.5131334e-04, 3.0298581e-04, 1.0413901e-03, 8.3430990e-04,
                                           1.2461779e-04, 1.0898571e-03, 1.1422117e-03, 0.0000000e+00,
                                           0.0000000e+00, -6.2288583e-04, -1.2216593e-04, 9.0777961e-04,
github IBM / adversarial-robustness-toolbox / tests / attacks / test_virtual_adversarial.py View on Github external
scores = cls.classifier_k._model.evaluate(x_test, y_test)
        logging.info('[Keras, MNIST] Accuracy on test set: %.2f%%', (scores[1] * 100))

        # Create basic CNN on MNIST using TensorFlow
        cls.classifier_tf, sess = get_classifier_tf()

        scores = get_labels_np_array(cls.classifier_tf.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logging.info('[TF, MNIST] Accuracy on training set: %.2f%%', (acc * 100))

        scores = get_labels_np_array(cls.classifier_tf.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logging.info('[TF, MNIST] Accuracy on test set: %.2f%%', (acc * 100))

        # Create basic PyTorch model
        cls.classifier_py = get_classifier_pt()
        x_train, x_test = np.swapaxes(x_train, 1, 3), np.swapaxes(x_test, 1, 3)
        x_train, x_test = x_train.astype(np.float32), x_test.astype(np.float32)

        scores = get_labels_np_array(cls.classifier_py.predict(x_train))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logging.info('[PyTorch, MNIST] Accuracy on training set: %.2f%%', (acc * 100))

        scores = get_labels_np_array(cls.classifier_py.predict(x_test))
        acc = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logging.info('[PyTorch, MNIST] Accuracy on test set: %.2f%%', (acc * 100))
github IBM / adversarial-robustness-toolbox / tests / attacks / test_fast_gradient.py View on Github external
scores = cls.classifier_k._model.evaluate(x_test, y_test)
        logger.info('[Keras, MNIST] Accuracy on test set: %.2f%%', (scores[1] * 100))

        # Create basic CNN on MNIST using TensorFlow
        cls.classifier_tf, sess = get_classifier_tf()

        scores = get_labels_np_array(cls.classifier_tf.predict(x_train))
        accuracy = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logger.info('[TF, MNIST] Accuracy on training set: %.2f%%', (accuracy * 100))

        scores = get_labels_np_array(cls.classifier_tf.predict(x_test))
        accuracy = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logger.info('[TF, MNIST] Accuracy on test set: %.2f%%', (accuracy * 100))

        # Create basic PyTorch model
        cls.classifier_py = get_classifier_pt()
        x_train, x_test = np.swapaxes(x_train, 1, 3), np.swapaxes(x_test, 1, 3)

        scores = get_labels_np_array(cls.classifier_py.predict(x_train.astype(np.float32)))
        accuracy = np.sum(np.argmax(scores, axis=1) == np.argmax(y_train, axis=1)) / y_train.shape[0]
        logger.info('[PyTorch, MNIST] Accuracy on training set: %.2f%%', (accuracy * 100))

        scores = get_labels_np_array(cls.classifier_py.predict(x_test.astype(np.float32)))
        accuracy = np.sum(np.argmax(scores, axis=1) == np.argmax(y_test, axis=1)) / y_test.shape[0]
        logger.info('[PyTorch, MNIST] Accuracy on test set: %.2f%%', (accuracy * 100))
github IBM / adversarial-robustness-toolbox / tests / classifiers / test_pytorch.py View on Github external
def test_nb_classes(self):
        classifier = get_classifier_pt()
        self.assertEqual(classifier.nb_classes(), 10)