Exemplo n.º 1
0
def main():
    x_train, y_train = load_mnist_data('training')
    x_test, y_test = load_mnist_data('testing')

    fid = open('accuracy_stage.txt', 'w')
    for stage in range(-1, 3):
        model = lenet300_layerwise('TRAIN', stage, math.log(0.1))

        with tf.Session() as sess:
            saver = tf.train.Saver(model.all_weights)
            writer = tf.summary.FileWriter('graph_stage', sess.graph)
            sess.run(tf.global_variables_initializer())
            if stage >= 0:
                saver.restore(sess, 'model/model_stage' + str(stage))

            train_model(model, sess, writer, x_train, y_train, 100)
            saver.save(sess, 'model/model_stage' + str(stage + 1))

        tf.reset_default_graph()

        model = lenet300_layerwise('TEST', stage)
        with tf.Session() as sess:
            saver = tf.train.Saver(model.all_weights)
            saver.restore(sess, 'model/model_stage' + str(stage + 1))
            count1, count2, count3 = model.pruned_structure(sess, 0.005)
            print('Prunced structure: {0}-{1}-{2}.'.format(
                count1, count2, count3))
            train_acc = test_model(model, sess, x_train, y_train)
            test_acc = test_model(model, sess, x_test, y_test)
            print('Train accuracy = {0}.'.format(train_acc))
            print('Test accuracy = {0}.'.format(test_acc))
            fid.write('{0}\t{1}\t{2}\t{3}\t{4}\n'.format(
                count1, count2, count3, train_acc, test_acc))

        tf.reset_default_graph()
Exemplo n.º 2
0
def main():
    x_train, y_train = load_mnist_data('training')
    x_test, y_test = load_mnist_data('testing')
    label_train = np.argmax(y_train, 1)
    label_test = np.argmax(y_test, 1)

    model = SparseVae(784, 50, [200, 200], [200, 200], tf.nn.tanh, 0.0001)

    with tf.Session() as sess:
        saver = tf.train.Saver()
        writer = tf.summary.FileWriter('graph', sess.graph)
        sess.run(tf.global_variables_initializer())

#        train_model(model, sess, writer, x_train, 20, 100, 0.001)
#        if not os.path.exists('model'):
#            os.mkdir('model')
#        saver.save(sess, 'model/model')
        saver.restore(sess, 'model/model')

        mu_z, sd_z = test_model(model, sess, x_train)
        mu_z_label = []
        sd_z_label = []
        for digit in range(10):
            idx = [i for i, x in enumerate(label_train) if x == digit]
            for i in range(50):
                mu_z_label.append(mu_z[idx[i], :])
                sd_z_label.append(sd_z[idx[i], :])
        plt.imshow(mu_z_label)
        plt.show()
Exemplo n.º 3
0
def main():
    x_train, y_train = load_mnist_data('training')
    x_test, y_test = load_mnist_data('testing')

    fid = open('result.txt', 'a')
    for i in range(2, 3):
        if i == 0:
            alpha = 0.1
        elif i == 2:
            alpha = 1.0
        else:
            alpha = 10.0
        model = lenet300('TRAIN', alpha)

        with tf.Session() as sess:
            saver = tf.train.Saver()
            writer = tf.summary.FileWriter('graph_' + str(i), sess.graph)
            sess.run(tf.global_variables_initializer())

            train_model(model, sess, writer, x_train, y_train, 200)
            saver.save(sess, 'model/model_' + str(i))

        tf.reset_default_graph()
        model = lenet300('TEST')

        with tf.Session() as sess:
            saver = tf.train.Saver()
            saver.restore(sess, 'model/model_' + str(i))
            count1, count2, count3 = model.pruned_structure(sess, 0.005)
            print('Prunced structure: {0}-{1}-{2}.'.format(
                count1, count2, count3))
            train_acc = test_model(model, sess, x_train, y_train)
            test_acc = test_model(model, sess, x_test, y_test)
            print('Train accuracy = {0}.'.format(train_acc))
            print('Test accuracy = {0}.'.format(test_acc))
            fid.write('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n'.format(
                alpha, count1, count2, count3, train_acc, test_acc))

        tf.reset_default_graph()
    fid.close()
Exemplo n.º 4
0
from chainer import functions as F

parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--data_dir', type=str, default="dataset")
parser.add_argument('--dataset', type=str, default="svhn")
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--output_file', type=str, default="flying.mp4")
parser.add_argument('--output_dir', type=str, default="flying")

args = parser.parse_args()

if args.dataset == 'mnist':
    im_size = (28, 28)
    data_dir = args.data_dir
    mnist = dataset.load_mnist_data(data_dir)
    all_x = np.array(mnist['data'], dtype=np.float32) / 255
    all_y_tmp = np.array(mnist['target'], dtype=np.float32)
    all_y = np.zeros((all_x.shape[0], (np.max(all_y_tmp) + 1.0)),
                     dtype=np.float32)
    for i in range(all_y_tmp.shape[0]):
        all_y[i][all_y_tmp[i]] = 1.

    train_x = all_x[:50000]
    train_y = all_y[:50000]
    valid_x = all_x[50000:60000]
    valid_y = all_y[50000:60000]
    test_x = all_x[60000:]
    test_y = all_y[60000:]

    size = 28
from chainer import functions as F

parser = argparse.ArgumentParser()
parser.add_argument('--model',          type=str,   required=True)
parser.add_argument('--data_dir',       type=str,   default="dataset")
parser.add_argument('--dataset',        type=str,   default="svhn")
parser.add_argument('--gpu',            type=int,   default=-1)
parser.add_argument('--output_file',    type=str,   default="flying.mp4")
parser.add_argument('--output_dir',       type=str,   default="flying")

args = parser.parse_args()

if args.dataset == 'mnist':
    im_size = (28, 28)
    data_dir = args.data_dir
    mnist = dataset.load_mnist_data(data_dir)
    all_x = np.array(mnist['data'], dtype=np.float32) /255
    all_y_tmp = np.array(mnist['target'], dtype=np.float32)
    all_y = np.zeros((all_x.shape[0], (np.max(all_y_tmp) + 1.0)), dtype=np.float32)
    for i in range(all_y_tmp.shape[0]):
        all_y[i][all_y_tmp[i]] = 1.

    train_x = all_x[:50000]
    train_y = all_y[:50000]
    valid_x = all_x[50000:60000]
    valid_y = all_y[50000:60000]
    test_x  = all_x[60000:]
    test_y  = all_y[60000:]

    size    = 28
    n_x     = size*size
Exemplo n.º 6
0
                        'Number of epochs before apply validation testing')
tf.flags.DEFINE_integer('save_after_n', 2,
                        'Number of epochs before saving network')
tf.flags.DEFINE_string('logdir', './logs', 'Logs folder')
tf.flags.DEFINE_string('model_description', 'garment_classifier', 'Model name')
FLAGS = tf.flags.FLAGS

# Prepare output directories
results_folder = create_results_folder(
    os.path.join('Results', FLAGS.model_description))
model_folder = create_folder(os.path.join('Models', FLAGS.model_description))
delete_old_logs(FLAGS.logdir)

# Create tf dataset
with tf.name_scope('DataPipe'):
    datasets = load_mnist_data(FLAGS.batch_size)
    iterator = tf.data.Iterator.from_structure(datasets['train'].output_types,
                                               datasets['train'].output_shapes)

    # Create the initialisation operations for training and test
    train_init_op = iterator.make_initializer(datasets['train'])
    test_init_op = iterator.make_initializer(datasets['test'])

    # Create iterator
    input_batch, labels = iterator.get_next()
    input_batch = tf.reshape(input_batch, shape=[-1, 28, 28, 1])

    # This placeholder is necessary to feed new data after training
    input_data = tf.placeholder_with_default(input_batch, [None, 28, 28, 1],
                                             name='input_data')
Exemplo n.º 7
0
def main():
    """
    Explore digit classification confidence for all rotations. Produces charts
    for us to look at.
    """
    x_train, y_train, x_test, y_test = load_mnist_data()

    if MODEL == 'LENET':
        y, model_var_dict, x, y_, cross_entropy = model_lenet()
        ckpt_fname = 'lenet'
    elif MODEL == 'BEGINNER':
        x, y_, W, b, y, cross_entropy, model_var_dict = model_simple()
        ckpt_fname = 'beginner.ckpt'
    if MODEL == 'SMALL_FNN':
        y, model_var_dict, x, y_, cross_entropy = model_small_fnn()
        ckpt_fname = 'small_fnn.ckpt'

    sess = tf.InteractiveSession()
    sess.run(tf.initialize_all_variables())
    saver = tf.train.Saver(model_var_dict)
    saver.restore(sess, ckpt_fname)
    confidence_matches = [0 for e in range(10)]
    incorrect_confidence_diffs = []
    correct_confidence_diffs = []

    for i in range(NUM_EXAMPLES):
        plts = []
        for j in range(1, 13):
            plts.append(plt.subplot(13, 1, j))

        entropy_vals = []
        confidence_vals = [[] for e in range(10)]
        delta_angle = 360 / NUM_ROTATIONS
        for j in range(NUM_ROTATIONS):
            angle = j * delta_angle
            x_case = x_test[i].reshape((28, 28))
            x_case = rotate(x_case, angle, reshape=False)
            y_case = y_test[i]  # rename for later conciseness

            prediction, entropy = sess.run([y, cross_entropy],
                                           feed_dict={
                                               x: x_case.reshape(1, 784),
                                               y_: [y_case],
                                           })
            prediction = prediction[0]
            entropy_vals.append(entropy)
            for digit in range(10):
                confidence_vals[digit].append(prediction[digit])

        plts[0].cla()
        plts[0].set_title('Original')
        plts[0].imshow(x_test[i].reshape((28, 28)),
                       cmap='gray',
                       interpolation='none')
        plts[0].axis('off')

        corr_pred = np.argmax(y_case)
        print '[Example {}] Correct Prediction: {}'.format(i, corr_pred)
        # max confidence over rotation for each digit
        digit_confidences = [
            np.max(confidence_vals[digit]) for digit in range(10)
        ]
        # note [::-1] reverses order of np array
        digit_confidence_order = np.argsort(digit_confidences)[::-1]
        correct_rank = np.where(digit_confidence_order == corr_pred)
        correct_rank = correct_rank[0][0]
        confidence_matches[correct_rank] += 1
        confidence_diff = abs(digit_confidences[corr_pred] -
                              np.max(digit_confidences))
        if correct_rank != 0:  # only do if it was incorrect
            incorrect_confidence_diffs.append(confidence_diff)
        else:
            # if it was correct, how much did it beat the others by?
            corr_conf_diff = (digit_confidences[digit_confidence_order[0]] -
                              digit_confidences[digit_confidence_order[1]])
            correct_confidence_diffs.append(corr_conf_diff)

        print('[Example {}] Confidence matches so far: {}'.format(
            i, confidence_matches[0]))

        for digit in range(10):
            plts[digit + 1].cla()
            confidence_vals[digit] = np.roll(confidence_vals[digit],
                                             NUM_ROTATIONS / 2)
            x_axis = range(-NUM_ROTATIONS / 2, NUM_ROTATIONS / 2)
            plts[digit + 1].fill_between(x_axis, 0, confidence_vals[digit])
            print '[Example {}] {}: {} {}'.format(
                i, digit, np.max(confidence_vals[digit]),
                np.argmax(confidence_vals[digit]))
            plts[digit + 1].axis('off')
        plts[11].cla()
        entropy_vals = np.roll(entropy_vals, NUM_ROTATIONS / 2)
        entropy_x_axis = range(-NUM_ROTATIONS / 2, NUM_ROTATIONS / 2)
        plts[11].fill_between(entropy_x_axis, 0, entropy_vals, facecolor='red')
        plts[11].set_yticks([])
        plt.savefig('{}/fig{}.png'.format(SAVEFIG_DIR, i))
        print 'Saved figure {} ({} total)'.format(i, NUM_EXAMPLES)

    print 'Model used: {}'.format(MODEL)
    print 'Confidence matches: {}'.format(confidence_matches)
    confidence_matches_pdf = (np.array(confidence_matches).astype(float) /
                              NUM_EXAMPLES)
    confidence_matches_cdf = np.cumsum(confidence_matches_pdf)
    plt.clf()

    plt1 = plt.subplot(2, 1, 1)
    plt2 = plt.subplot(2, 2, 3)
    plt3 = plt.subplot(2, 2, 4)

    plt1.plot(range(1, 11), confidence_matches_pdf)
    plt1.plot(range(1, 11), confidence_matches_cdf, 'r--')
    plt1.set_title('% cases with correct class with top confidence')
    plt1.set_xlabel('Confidence ranking')
    # When correct class doesn't have highest confidence
    plt2.plot(range(1,
                    len(incorrect_confidence_diffs) + 1),
              incorrect_confidence_diffs, '*')
    # Confidence difference for correct and best classes
    plt2.set_title('Confidence delta (incorrect)')
    plt2.set_xlabel('Case #')
    plt2.set_yscale('log')

    plt3.plot(range(1,
                    len(correct_confidence_diffs) + 1),
              correct_confidence_diffs, '*')
    # Confidence differences between correct class and second best, when corr
    # class was highest conf
    plt3.set_title('Confidence delta (correct)')
    plt3.set_xlabel('examples')
    plt3.set_yscale('log')
    plt.tight_layout()
    plt.savefig('{}/confidence-matches{}.png'.format(SAVEFIG_DIR,
                                                     NUM_EXAMPLES))