コード例 #1
0
ファイル: main_cnn.py プロジェクト: toanphung/ml-from-scratch
def main():
    if debug:
        load_dataset_mnist(debug_path + "/libs")
        mndata = MNIST(debug_path + "/libs/data_mnist")
    else:
        load_dataset_mnist("../libs")
        mndata = MNIST('../libs/data_mnist')
    lenet_arch = [{"type": "conv", "filter_size": (5, 5), "filters": 6, "padding": "SAME", "stride": 1, "activation": "sigmoid", "batch_norm": None},
                {"type": "pool", "filter_size": (2, 2), "stride": 2, "mode": "max"},
                {"type": "conv", "filter_size": (5, 5), "filters": 16, "padding": "SAME", "stride": 1, "activation": "sigmoid", "batch_norm": None},
                {"type": "pool", "filter_size": (2, 2), "stride": 2, "mode": "max"},
                "flatten",
                {"type": "fc", "num_neurons": 120, "weight_init": "std", "activation": "tanh"},
                {"type": "fc", "num_neurons": 84, "weight_init": "std", "activation": "tanh"},
                {"type": "fc", "num_neurons": 10, "weight_init": "std", "activation": "softmax"}
                ]
    epochs = 20
    batch_size = 32
    learning_rate = 0.1
    # sgd = SGD(learning_rate)
    optimizer = SGDMomentum(alpha=learning_rate)
    cnn = CNN(epochs=epochs, batch_size=batch_size, optimizer=optimizer, cnn_structure=lenet_arch)
    training_phase = True
    if training_phase:
        images, labels = mndata.load_training()
        images, labels = preprocess_data(images, labels, nn=True)
        cnn.train(images[:100], labels[:100])
コード例 #2
0
def main():
    load_dataset_mnist("../libs")
    mndata = MNIST('../libs/data_mnist', gz=True)
    weight_path = "nn_weights.pkl"
    training_phase = weight_path not in os.listdir(".")
    if training_phase:
        images, labels = mndata.load_training()
        images, labels = preprocess_data(images, labels)
        epochs = 10
        batch_size = 64
        learning_rate = 0.01

        optimizer = Adam(learning_rate)
        loss_func = CrossEntropy()
        archs = [
            InputLayer(),
            FCLayer(num_neurons=100, weight_init="he_normal"),
            ActivationLayer(activation="relu"),
            DropoutLayer(keep_prob=0.8),
            FCLayer(num_neurons=125, weight_init="he_normal"),
            ActivationLayer(activation="relu"),
            DropoutLayer(keep_prob=0.8),
            FCLayer(num_neurons=50, weight_init="he_normal"),
            BatchNormLayer(),
            ActivationLayer(activation="relu"),
            FCLayer(num_neurons=labels.shape[1], weight_init="he_normal"),
            ActivationLayer(activation="softmax"),
        ]
        nn = NeuralNetwork(optimizer=optimizer,
                           layers=archs,
                           loss_func=loss_func)

        trainer = Trainer(nn, batch_size, epochs)
        trainer.train(images, labels)
        trainer.save_model("nn_weights.pkl")
    else:
        import pickle
        images_test, labels_test = mndata.load_testing()
        images_test, labels_test = preprocess_data(images_test,
                                                   labels_test,
                                                   test=True)
        with open(weight_path, "rb") as f:
            nn = pickle.load(f)
        pred = nn.predict(images_test)

        print("Accuracy:", len(pred[labels_test == pred]) / len(pred))
        from sklearn.metrics.classification import confusion_matrix

        print("Confusion matrix: ")
        print(confusion_matrix(labels_test, pred))
コード例 #3
0
def main(use_keras=False):
    load_dataset_mnist("../libs")
    mndata = MNIST('../libs/data_mnist')
    arch = [
        {
            "type": "conv",
            "filter_size": (3, 3),
            "filters": 6,
            "padding": "SAME",
            "stride": 1,
            "activation": "relu",
            "weight_init": "he_normal"
        },
        {
            "type": "pool",
            "filter_size": (2, 2),
            "stride": 2,
            "mode": "max"
        },
        {
            "type": "conv",
            "filter_size": (3, 3),
            "filters": 16,
            "padding": "SAME",
            "stride": 1,
            "activation": "relu",
            "weight_init": "he_normal"
        },
        {
            "type": "pool",
            "filter_size": (2, 2),
            "stride": 2,
            "mode": "max"
        },
        {
            "type": "conv",
            "filter_size": (3, 3),
            "filters": 32,
            "padding": "SAME",
            "stride": 1,
            "activation": "relu",
            "weight_init": "he_normal"
        },
        {
            "type": "pool",
            "filter_size": (2, 2),
            "stride": 2,
            "mode": "max"
        },
        "flatten",
        {
            "type": "fc",
            "num_neurons": 128,
            "weight_init": "he_normal",
            "activation": "relu"
        },  # use "batch_norm": None
        {
            "type": "fc",
            "num_neurons": 64,
            "weight_init": "he_normal",
            "activation": "relu"
        },
        {
            "type": "fc",
            "num_neurons": 10,
            "weight_init": "he_normal",
            "activation": "softmax"
        }
    ]
    epochs = 5
    batch_size = 64
    learning_rate = 0.006
    if use_keras:
        print("Train MNIST dataset by CNN with Keras/Tensorflow.")
        from keras.optimizers import SGD as SGDKeras
        from cnn_keras import CNNKeras
        training_phase = True
        optimizer = SGDKeras(lr=learning_rate)
        cnn = CNNKeras(epochs=epochs,
                       batch_size=batch_size,
                       optimizer=optimizer,
                       cnn_structure=arch)
    else:
        print("Train MNIST dataset by CNN with pure Python: Numpy.")
        optimizer = Adam(alpha=learning_rate)
        cnn = CNN(epochs=epochs,
                  batch_size=batch_size,
                  optimizer=optimizer,
                  cnn_structure=arch)
        weight_path = "cnn_weights.pickle"
        training_phase = weight_path not in os.listdir(".")
    if training_phase:
        images, labels = mndata.load_training()
        images, labels = preprocess_data(images, labels, nn=True)

        if not use_keras:
            cnn.train(images, labels)
            cnn.save(weight_path)
        else:
            cnn.train(images, labels)
            training_phase = False
    if not training_phase:
        import pickle
        images_test, labels_test = mndata.load_testing()
        images_test, labels_test = preprocess_data(images_test,
                                                   labels_test,
                                                   nn=True,
                                                   test=True)
        if not use_keras:
            with open(weight_path, "rb") as f:
                cnn = pickle.load(f)
        pred = cnn.predict(images_test)

        print("Accuracy:", len(pred[labels_test == pred]) / len(pred))
        from sklearn.metrics.classification import confusion_matrix

        print("Confusion matrix: ")
        print(confusion_matrix(labels_test, pred))
コード例 #4
0
def main():
    arch = [
        ConvLayer(filter_size=(3, 3),
                  filters=6,
                  padding="SAME",
                  stride=1,
                  weight_init="he_normal"),
        ActivationLayer(activation="relu"),
        PoolingLayer(filter_size=(2, 2), stride=2, mode="max"),
        ConvLayer(filter_size=(3, 3),
                  filters=16,
                  padding="SAME",
                  stride=1,
                  weight_init="he_normal"),
        ActivationLayer(activation="relu"),
        PoolingLayer(filter_size=(2, 2), stride=2, mode="max"),
        ConvLayer(filter_size=(3, 3),
                  filters=32,
                  padding="SAME",
                  stride=1,
                  weight_init="he_normal"),
        ActivationLayer(activation="relu"),
        PoolingLayer(filter_size=(2, 2), stride=2, mode="max"),
        FlattenLayer(),
        FCLayer(num_neurons=128, weight_init="he_normal"),
        ActivationLayer(activation="relu"),
        FCLayer(num_neurons=64, weight_init="he_normal"),
        ActivationLayer(activation="relu"),
        FCLayer(num_neurons=10, weight_init="he_normal"),
        ActivationLayer(activation="softmax")
    ]

    print("Train MNIST dataset by CNN with pure Python: Numpy.")
    weight_path = "cnn_weights.pkl"
    training_phase = weight_path not in os.listdir(".")
    load_dataset_mnist("../libs")
    mndata = MNIST('../libs/data_mnist', gz=True)

    if training_phase:
        images_train, labels_train = mndata.load_training()
        images_train, labels_train = preprocess_data(images_train,
                                                     labels_train,
                                                     nn=True)

        epochs = 5
        batch_size = 64
        learning_rate = 0.006

        optimizer = Adam(alpha=learning_rate)
        loss_func = CrossEntropy()

        cnn = CNN(optimizer=optimizer, layers=arch, loss_func=loss_func)

        trainer = Trainer(cnn, batch_size, epochs)
        trainer.train(images_train, labels_train)
        trainer.save_model(weight_path)

    else:
        import pickle
        images_test, labels_test = mndata.load_testing()
        images_test, labels_test = preprocess_data(images_test,
                                                   labels_test,
                                                   nn=True,
                                                   test=True)
        with open(weight_path, "rb") as f:
            cnn = pickle.load(f)
        pred = cnn.predict(images_test)

        print("Accuracy:", len(pred[labels_test == pred]) / len(pred))
        from sklearn.metrics.classification import confusion_matrix

        print("Confusion matrix: ")
        print(confusion_matrix(labels_test, pred))
コード例 #5
0
import sys
sys.path.append("..")
import os
from optimizations_algorithms.optimizers import SGD, SGDMomentum, RMSProp, Adam
from neural_network import NeuralNetwork
from libs.utils import load_dataset_mnist, preprocess_data
from libs.mnist_lib import MNIST

load_dataset_mnist("../libs")
mndata = MNIST('../libs/data_mnist')
weight_path = "nn_weights.pickle"
training_phase = weight_path not in os.listdir(".")
if training_phase:
    images, labels = mndata.load_training()
    images, labels = preprocess_data(images, labels)
    epochs = 20
    batch_size = 64
    learning_rate = 0.1

    sgd = SGD(learning_rate)
    archs = [{
        "num_neurons": 100,
        "weight_init": "he",
        "activation": "sigmoid",
        "batch_norm": None
    }, {
        "num_neurons": 125,
        "weight_init": "he",
        "activation": "sigmoid",
        "batch_norm": None
    }, {