Exemplo n.º 1
0
def get_convolutional(params):
    from convolutional import Convolutional
    activation = get_activation(params.get('activation', 'linear'))
    batch_normalize = params.get('batch_normalize',
                                 0)  # TODO: add this param processing
    padding = "same" if params.get('pad', 0) else "valid"
    return Convolutional(**params)
Exemplo n.º 2
0
def train(train_loader, validation_loader, criterion, m, n):
    num_epochs = 40
    learning_rate = 1e-4
    log_interval = 1
    best_accuracy = 0.80
    best_epoch = 0

    # model definition
    model = Convolutional(m, n)    
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)

    # Training
    for epoch in range(num_epochs):
        start = time.time()     
        for step, (x, y) in enumerate(train_loader):
            batch_X = Variable(x)   # batch x (image)
            batch_y = Variable(y)   # batch y (target)

            # run batch through model
            y_pred = model(batch_X)
            loss = criterion(y_pred, batch_y)

            # run backprop
            optimizer.zero_grad()           
            loss.backward()                 
            optimizer.step()

        # log validation results and save if the results are good
        if epoch % log_interval == 0:
            print("Epoch {}:\n  ".format(epoch), end='')
            print(round(time.time()-start, 2), "seconds to train\n  ", end='')
            accuracy, _ = test(model, criterion, validation_loader)
            if accuracy > best_accuracy:
                best_accuracy = accuracy
                best_epoch = epoch
                save_model(model, epoch)

        # reduce the learning rate periodically
        if epoch == 5 or epoch == 15 or epoch == 25:
            print("Adjusting learning rate")
            learning_rate *= 0.5
            for param_group in optimizer.param_groups:
                param_group["lr"] = learning_rate

    return best_epoch
    x, y = x[all_indices], y[all_indices]
    x = x.reshape(len(x), 1, 28, 28)
    x = x.astype("float32") / 255
    y = np_utils.to_categorical(y)
    y = y.reshape(len(y), 2, 1)
    return x, y


# load MNIST from server, limit to 100 images per class since we're not training on GPU
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, y_train = preprocess_data(x_train, y_train, 100)
x_test, y_test = preprocess_data(x_test, y_test, 100)

# neural network
network = [
    Convolutional((1, 28, 28), 3, 5),
    Sigmoid(),
    Reshape((5, 26, 26), (5 * 26 * 26, 1)),
    Dense(5 * 26 * 26, 100),
    Sigmoid(),
    Dense(100, 2),
    Sigmoid()
]

epochs = 20
learning_rate = 0.1

# train
for e in range(epochs):
    error = 0
    for x, y in zip(x_train, y_train):
Exemplo n.º 4
0
    for i in range(int(eponum * (trainsize / 100))):  #i <- N/B * 100 or so
        rannumlist = numpy.random.choice(trainsize, 100, replace=False)
        image = (dlist[rannumlist] / 255)  #.T
        #xlist.shape=[784,100]
        ylist = anslist[rannumlist]
        #ylist.shape=[100,]
        #one-hot-vector-ize
        y_onehot = numpy.identity(10)[ylist]
        #y_onehot.shape=[100,10]

        normclass_mid = Batchnorm(gamma_mid, beta_mid, batchmoment,
                                  running_mean_mid, running_var_mid)
        normclass_out = Batchnorm(gamma_out, beta_out, batchmoment,
                                  running_mean_out, running_var_out)

        convclass = Convolutional(w_conv, b_conv, srclass_c)
        xlist = convclass.forward(image)  #xlist.shape = (20*28*28,100)

        (midrslt, outrslt, loss_ave, \
         new_running_mean_mid, new_running_var_mid, new_running_mean_out, new_running_var_out) = \
                threeclass.three_nn(xlist, y_onehot, \
                                    w_one, b_one, w_two, b_two, \
                                    100, itbool, srclass, dropclass, normclass_mid, normclass_out)
        print("(No.",
              "{0:02d}".format(int(i * 100 / trainsize)),
              " epoch) ",
              sep="",
              end="")
        print(loss_ave)

        (en_x, en_w_one, en_w_two, en_b_one, en_b_two, en_gamma_mid, en_beta_mid, en_gamma_out, en_beta_out) = \
Exemplo n.º 5
0
    m = 64
    n = 128

    print("\nGetting data")
    train_loader, validation_loader, test_loader = get_data(training_data, test_data, m, n)
    criterion = nn.CrossEntropyLoss()

    if False:
        best_epoch = train(train_loader, validation_loader, criterion, m, n)
        print("Training complete. Best model was at epoch", best_epoch)

    if test_data:
        print("\nLoading saved model for the test set")
        checkpoint = torch.load("cnn_17.model")
        # checkpoint = torch.load("cnn_{}.model".format(best_epoch))
        model = Convolutional(m,n)
        model.load_state_dict(checkpoint)
        model.eval()
        print("Testing...")
        start = time.time()
        _, y_pred_one_hot = test(model, criterion, test_loader)
        print(round(time.time()-start, 2), "seconds to test\n  ", end='')

        # build label vector
        grass = np.zeros(200)
        ocean = np.ones(200)
        redcarpet = 2 * np.ones(200)
        road = 3 * np.ones(200)
        wheatfield = 4 * np.ones(200)

        y_test = np.hstack((grass, ocean, redcarpet, road, wheatfield))
Exemplo n.º 6
0
def test(model, test_loader):
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            output = model(images)
            _, predicted = torch.max(output.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

        accuracy = 100 * correct / total
        print('accuracy: {} %'.format(accuracy))
        return accuracy


model = Convolutional()

train(model, train_loader)
test(model, test_loader)

ds = DrawingScreen(10, 14)


def handler(scr):
    with torch.no_grad():
        tensor_im = torch.Tensor(scr)
        # print(tensor_im)
        output = model(tensor_im)
        prediction = np.array(output[0]).argmax()
        print('prediction: {}'.format(prediction))
Exemplo n.º 7
0
    summary = tf.Summary()
    summary.value.add(tag='Accuracy_' + name, simple_value=precision)
    summary_writer.add_summary(summary, learning_step)
    print("Accuracy %.3f" % precision)


if __name__ == '__main__':

    # Download mnist
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

    with tf.Graph().as_default():
        # Wiring and different models
        #model = Feed_forward()
        #model = Feed_forward_two_layers()
        model = Convolutional()
        inputs_placeholder, labels_placeholder = model.input_placeholders()
        logits = model.inference(inputs_placeholder)
        loss = model.loss(logits, labels_placeholder)
        training = model.training(loss, 0.0001)
        evaluation = model.evaluation(logits, labels_placeholder)

        # Initialization
        session = tf.InteractiveSession()
        init = tf.global_variables_initializer()
        session.run(init)

        # visualize graph
        writer = tf.summary.FileWriter("visualizations/" + model.get_name())
        writer.add_graph(session.graph)