Exemplo n.º 1
0
from network import Network
from layers import Relu, Linear, Conv2D, AvgPool2D, Reshape
from utils import LOG_INFO
from loss import EuclideanLoss, SoftmaxCrossEntropyLoss
from solve_net import train_net, test_net
from load_data import load_mnist_4d
from plot import show
from solve_net import show4category
train_data, test_data, train_label, test_label = load_mnist_4d('data')

# Your model defintion here
# You should explore different model architecture
model = Network()
model.add(Conv2D('conv1', 1, 4, 3, 1, 0.01))
model.add(Relu('relu1'))
model.add(AvgPool2D('pool1', 2, 0))  # output shape: N x 4 x 14 x 14
model.add(Conv2D('conv2', 4, 8, 3, 1, 0.01))
model.add(Relu('relu2'))
model.add(AvgPool2D('pool2', 2, 0))  # output shape: N x 8 x 7 x 7
model.add(Reshape('flatten', (-1, 392)))
model.add(Linear('fc3', 392, 10, 0.01))

loss = SoftmaxCrossEntropyLoss(name='loss')

# Training configuration
# You should adjust these hyperparameters
# NOTE: one iteration means model forward-backwards one batch of samples.
#       one epoch means model has gone through all the training samples.
#       'disp_freq' denotes number of iterations in one epoch to display information.

config = {
Exemplo n.º 2
0
    loss /= times
    acc /= times
    return acc, loss


def inference(model, sess, X):
    return sess.run([model.pred], {model.x_: X, model.keep_prob: 1.0})[0]


with tf.Session() as sess:
    if not os.path.exists(FLAGS.train_dir):
        os.mkdir(FLAGS.train_dir)
    if not os.path.exists(FLAGS.img_dir):
        os.mkdir(FLAGS.img_dir)
    if FLAGS.is_train:
        X_train, X_test, y_train, y_test = load_mnist_4d(FLAGS.data_dir)
        X_val, y_val = X_train[50000:], y_train[50000:]
        X_train, y_train = X_train[:50000], y_train[:50000]
        cnn_model = Model(is_train=True)
        if tf.train.get_checkpoint_state(FLAGS.train_dir):
            cnn_model.saver.restore(
                sess, tf.train.latest_checkpoint(FLAGS.train_dir))
        else:
            tf.global_variables_initializer().run()

        pre_losses = [1e18] * 3
        best_val_acc = 0.0

        loss_plot_list = []
        acc_plot_list = []
Exemplo n.º 3
0
# =============================================================================
# This is Vincent's code to visualize.
# Right now this approach does not work, network needs to save weights
# I also don't like the way it ad hoc generates output
# =============================================================================

import numpy as np
from load_data import load_mnist_4d
from utils import vis_square

_, test_data, _, test_label = load_mnist_4d('data')

data = np.array([test_data[test_label == x][0] for x in np.arange(10)])

W = np.load('./weights-save/conv1-W-99.npy')
b = np.load('./weights-save/conv1-b-99.npy')

output = conv2d_forward(data, W, b, W.shape[2], W.shape[2] // 2).transpose(
    (1, 0, 2, 3)).clip(0)
output = output.reshape(output.shape[0] * output.shape[1], output.shape[2],
                        output.shape[3])

vis_square(W.squeeze(), n=1, figsize=5)

vis_square(output, n=10, figsize=10)