Пример #1
0
    def test_mnist(self):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = utils.to_categorical(y_train, 10)

        # To convert to desirable type
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train[:200], y_train[:200])
        output_shape = mnist_test.output_shape
        mnist_test.test(x_test[:200])
        mnist_test.evaluate(x_train[:100], y_train[:100])

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train[:200], y_train[:200])
        prediction = mnist_test.test(x_test[:200])

        mnist_test.save('mnist_test')
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test[:200])
        mnist_reloaded.jacobian_old(x_test[:2])

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
Пример #2
0
    def test_mnist(self):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = utils.to_categorical(y_train, 10)

        # To convert to desirable type
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train[:200], y_train[:200])
        output_shape = mnist_test.output_shape
        mnist_test.test(x_test[:200])
        mnist_test.evaluate(x_train[:100], y_train[:100])

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train[200:400], y_train[200:400].astype(bool))
        prediction = mnist_test.test(x_test[200:400])
        print("12345: ", mnist_test.input_std)
        mnist_test.save('mnist_test')
        print("12345: ", mnist_test.input_std)
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test[200:400])
        mnist_reloaded.jacobian_old(x_test[:2])
        eval_result = mnist_reloaded.evaluate(x_test[200:400],
                                              y_train[200:400].astype(bool))

        # Cifar10_CNN without dropout is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # test verbose metrics
        mnist_reloaded.metrics = ['accuracy']
        mnist_reloaded.compile()
        print("12345: ", mnist_test.input_std)
        mnist_test.save('mnist_test_accuracy')
        mnist_reloaded_again = load_folder("mnist_test_accuracy")
        eval_result_again = mnist_reloaded_again.evaluate(
            x_test[200:400], y_train[200:400])
        # assert saving again wont affect the model
        self.assertAlmostEqual(eval_result_again['loss'],
                               eval_result['loss'],
                               places=3)
Пример #3
0
    def test_color_images(self):
        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train_color, y_train)
        pred = mnist_test.test(x_test_color)
        test_num = y_test.shape[0]
        assert (np.sum(np.argmax(pred, axis=1) == y_test)) / test_num > 0.9  # assert accuracy

        # create model instance for binary classification
        mnist_test = Galaxy10CNN()
        mnist_test.max_epochs = 1
        mnist_test.mc_num = 3

        mnist_test.train(x_train[:200], y_train[:200])
        prediction = mnist_test.test(x_test[:200])

        mnist_test.save('cifar10_test')
        mnist_reloaded = load_folder("cifar10_test")
        prediction_loaded = mnist_reloaded.test(x_test[:200])
        mnist_reloaded.jacobian(x_test[:2], mean_output=True, mc_num=2)
        # mnist_reloaded.hessian_diag(x_test[:10], mean_output=True, mc_num=2)

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
Пример #4
0
    def test_color_images(self):
        # test colored 8bit images
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        x_train = np.random.randint(0, 255, size=(1000, 28, 28, 3))
        x_test = np.random.randint(0, 255, size=(100, 28, 28, 3))
        y_train = y_train[:1000]
        y_train = np_utils.to_categorical(y_train, 10)
        # To convert to desirable type

        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train, y_train[:1000])
        mnist_test.test(x_test[:1000])

        # create model instance for binary classification
        mnist_test = Galaxy10CNN()
        mnist_test.max_epochs = 1

        mnist_test.train(x_train[:1000], y_train[:1000])
        prediction = mnist_test.test(x_test[:1000])

        mnist_test.save('cifar10_test')
        mnist_reloaded = load_folder("cifar10_test")
        prediction_loaded = mnist_reloaded.test(x_test[:1000])
        mnist_reloaded.jacobian(x_test[:10], mean_output=True, mc_num=2)

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
Пример #5
0
    def test_mnist(self):
        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train, y_train)
        output_shape = mnist_test.output_shape
        pred = mnist_test.test(x_test)
        test_num = y_test.shape[0]
        assert (np.sum(np.argmax(pred, axis=1) == y_test)
                ) / test_num > 0.9  # assert accurancy
        mnist_test.evaluate(x_test, utils.to_categorical(y_test, 10))

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 2
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train, y_train.astype(bool))
        prediction = mnist_test.test(x_test)
        assert (np.sum(np.argmax(prediction, axis=1) == y_test)
                ) / test_num > 0.9  # assert accuracy
        mnist_test.save('mnist_test')
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test)
        eval_result = mnist_reloaded.evaluate(x_test,
                                              utils.to_categorical(y_test, 10))

        # Cifar10_CNN without dropout is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # test verbose metrics
        mnist_reloaded.metrics = ['accuracy']
        mnist_reloaded.compile()
        mnist_test.save('mnist_test_accuracy')
        mnist_reloaded_again = load_folder("mnist_test_accuracy")
        # test with astype boolean deliberately
        eval_result_again = mnist_reloaded_again.evaluate(
            x_test,
            utils.to_categorical(y_test, 10).astype(bool))
        # assert saving again wont affect the model
        self.assertAlmostEqual(eval_result_again['loss'],
                               eval_result['loss'],
                               places=3)