Beispiel #1
0
    def _test_fit_generator(self, custom_activation=False):
        from art.classifiers.keras import generator_fit
        from art.data_generators import KerasDataGenerator

        labels = np.argmax(self.mnist[1][1], axis=1)
        classifier = KerasClassifier((0, 1),
                                     self.model_mnist,
                                     use_logits=False,
                                     custom_activation=custom_activation)
        acc = np.sum(
            np.argmax(classifier.predict(self.mnist[1][0]), axis=1) ==
            labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (acc * 100))

        gen = generator_fit(self.mnist[0][0],
                            self.mnist[0][1],
                            batch_size=BATCH_SIZE)
        data_gen = KerasDataGenerator(generator=gen,
                                      size=NB_TRAIN,
                                      batch_size=BATCH_SIZE)
        classifier.fit_generator(generator=data_gen, nb_epochs=2)
        acc2 = np.sum(
            np.argmax(classifier.predict(self.mnist[1][0]), axis=1) ==
            labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (acc2 * 100))

        self.assertTrue(acc2 >= .8 * acc)
def test_fit_generator(get_default_mnist_subset, default_batch_size, get_image_classifier_list):
    (x_train_mnist, y_train_mnist), (x_test_mnist, y_test_mnist) = get_default_mnist_subset

    gen = generator_fit(x_train_mnist, y_train_mnist, batch_size=default_batch_size)
    data_gen = KerasDataGenerator(iterator=gen, size=x_train_mnist.shape[0], batch_size=default_batch_size)

    classifier, _ = get_image_classifier_list(one_classifier=True)

    expected_values = {"pre_fit_accuracy": ExpectedValue(0.32, 0.06), "post_fit_accuracy": ExpectedValue(0.36, 0.06)}

    backend_test_fit_generator(expected_values, classifier, data_gen, get_default_mnist_subset, nb_epochs=3)
Beispiel #3
0
    def test_fit_generator(self):
        labels = np.argmax(self.y_test_mnist, axis=1)
        classifier = get_image_classifier_kr_tf()
        acc = np.sum(np.argmax(classifier.predict(self.x_test_mnist), axis=1) == labels) / self.n_test
        logger.info("Accuracy: %.2f%%", (acc * 100))

        gen = generator_fit(self.x_train_mnist, self.y_train_mnist, batch_size=self.batch_size)
        data_gen = KerasDataGenerator(iterator=gen, size=self.n_train, batch_size=self.batch_size)
        classifier.fit_generator(generator=data_gen, nb_epochs=2)
        acc2 = np.sum(np.argmax(classifier.predict(self.x_test_mnist), axis=1) == labels) / self.n_test
        logger.info("Accuracy: %.2f%%", (acc2 * 100))

        self.assertEqual(acc, 0.32)
        self.assertAlmostEqual(acc2, 0.70, delta=0.15)
Beispiel #4
0
    def test_fit_generator(self):
        classifier = get_classifier_kr()
        labels = np.argmax(self.y_test, axis=1)
        accuracy = np.sum(np.argmax(classifier.predict(self.x_test), axis=1) == labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (accuracy * 100))

        gen = generator_fit(self.x_train, self.y_train, batch_size=BATCH_SIZE)
        data_gen = KerasDataGenerator(generator=gen, size=NB_TRAIN, batch_size=BATCH_SIZE)
        classifier.fit_generator(generator=data_gen, nb_epochs=3)
        accuracy_2 = np.sum(np.argmax(classifier.predict(self.x_test), axis=1) == labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (accuracy_2 * 100))

        self.assertEqual(accuracy, 0.32)
        self.assertAlmostEqual(accuracy_2, 0.36, delta=0.02)
Beispiel #5
0
    def test_fit_generator(self):
        from art.classifiers.keras import generator_fit
        from art.data_generators import KerasDataGenerator

        labels = np.argmax(self.mnist[1][1], axis=1)
        acc = np.sum(
            np.argmax(self.classifier.predict(self.mnist[1][0]), axis=1) ==
            labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (acc * 100))

        gen = generator_fit(self.mnist[0][0], self.mnist[0][1], batch_size=100)
        data_gen = KerasDataGenerator(generator=gen,
                                      size=NB_TRAIN,
                                      batch_size=100)
        self.classifier.fit_generator(generator=data_gen, nb_epochs=2)
        acc2 = np.sum(
            np.argmax(self.classifier.predict(self.mnist[1][0]), axis=1) ==
            labels) / NB_TEST
        logger.info('Accuracy: %.2f%%', (acc2 * 100))

        self.assertTrue(acc2 >= .8 * acc)