Пример #1
0
def train():
    """
    Performs training and evaluation of ConvNet model.

    First define your graph using class ConvNet and its methods. Then define
    necessary operations such as trainer (train_step in this case), savers
    and summarizers. Finally, initialize your model within a tf.Session and
    do the training.

    ---------------------------
    How to evaluate your model:
    ---------------------------
    Evaluation on test set should be conducted over full batch, i.e. 10k images,
    while it is alright to do it over minibatch for train set.

    ---------------------------------
    How often to evaluate your model:
    ---------------------------------
    - on training set every print_freq iterations
    - on test set every eval_freq iterations

    ------------------------
    Additional requirements:
    ------------------------
    Also you are supposed to take snapshots of your model state (i.e. graph,
    weights and etc.) every checkpoint_freq iterations. For this, you should
    study TensorFlow's tf.train.Saver class. For more information, please
    checkout:
    [https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html]
    """

    # Set the random seeds for reproducibility. DO NOT CHANGE.
    tf.set_random_seed(42)
    np.random.seed(42)

    ########################
    # PUT YOUR CODE HERE  #
    ########################

    cifar10 = cifar10_utils.get_cifar10(FLAGS.data_dir)
    x_test, y_test = cifar10.test.images[0:1000], cifar10.test.labels[0:1000]

    #### PARAMETERS
    classes = [
        'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
        'truck'
    ]
    n_classes = len(classes)
    input_data_dim = cifar10.test.images.shape[1]
    #####

    cnn = ConvNet()
    cnn.is_training = tf.placeholder(tf.bool)
    cnn.dropout_rate = 0.5
    x = tf.placeholder(tf.float32,
                       shape=(None, input_data_dim, input_data_dim, 3),
                       name="x")
    y = tf.placeholder(tf.float32, shape=(None, n_classes), name="y")

    with tf.name_scope('train_cnn'):
        infs = cnn.inference(x)
        with tf.name_scope('cross-entropy-loss'):
            loss = cnn.loss(infs, y)
        with tf.name_scope('accuracy'):
            accuracy = cnn.accuracy(infs, y)
        fc2 = cnn.fc2
        merged = tf.merge_all_summaries()
        opt_operation = train_step(loss)

    with tf.Session() as sess:
        saver = tf.train.Saver()

        sess.run(tf.initialize_all_variables())

        test_acc = sess.run(accuracy,
                            feed_dict={
                                x: x_test,
                                y: y_test,
                                cnn.is_training: False
                            })
        print("Initial Test Accuracy = {0:.3f}".format(test_acc))

        train_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/train/",
                                              sess.graph)
        test_writer = tf.train.SummaryWriter(FLAGS.log_dir + "/test/",
                                             sess.graph)

        for iteration in range(FLAGS.max_steps + 1):
            x_batch, y_batch = cifar10.train.next_batch(FLAGS.batch_size)
            _ = sess.run([opt_operation],
                         feed_dict={
                             x: x_batch,
                             y: y_batch,
                             cnn.is_training: False
                         })

            if iteration % FLAGS.print_freq == 0:
                [train_acc, train_loss,
                 summary_train] = sess.run([accuracy, loss, merged],
                                           feed_dict={
                                               x: x_batch,
                                               y: y_batch,
                                               cnn.is_training: False
                                           })
                train_writer.add_summary(summary_train, iteration)
                print(
                    "Iteration {0:d}/{1:d}. Train Loss = {2:.3f}, Train Accuracy = {3:.3f}"
                    .format(iteration, FLAGS.max_steps, train_loss, train_acc))

            if iteration % FLAGS.eval_freq == 0:
                [test_acc, test_loss,
                 summary_test] = sess.run([accuracy, loss, merged],
                                          feed_dict={
                                              x: x_test,
                                              y: y_test,
                                              cnn.is_training: False
                                          })
                test_writer.add_summary(summary_test, iteration)
                print(
                    "Iteration {0:d}/{1:d}. Test Loss = {2:.3f}, Test Accuracy = {3:.3f}"
                    .format(iteration, FLAGS.max_steps, test_loss, test_acc))

            if iteration > 0 and iteration % FLAGS.checkpoint_freq == 0:
                saver.save(sess, FLAGS.checkpoint_dir + '/cnn_model.ckpt')

        train_writer.flush()
        test_writer.flush()
        train_writer.close()
        test_writer.close()

        test_acc = sess.run(accuracy,
                            feed_dict={
                                x: x_test,
                                y: y_test,
                                cnn.is_training: False
                            })
        print("Final Test Accuracy = {0:.3f}".format(test_acc))

        sess.close()
Пример #2
0
def feature_extraction():
    """
    This method restores a TensorFlow checkpoint file (.ckpt) and rebuilds inference
    model with restored parameters. From then on you can basically use that model in
    any way you want, for instance, feature extraction, finetuning or as a submodule
    of a larger architecture. However, this method should extract features from a
    specified layer and store them in data files such as '.h5', '.npy'/'.npz'
    depending on your preference. You will use those files later in the assignment.

    Args:
        [optional]
    Returns:
        None
    """

    ########################
    # PUT YOUR CODE HERE  #
    ########################

    tf.reset_default_graph()
    classes = [
        'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship',
        'truck'
    ]

    tf.set_random_seed(42)
    np.random.seed(42)
    cifar10 = cifar10_utils.get_cifar10(FLAGS.data_dir)
    x_test, y_test = cifar10.test.images, cifar10.test.labels
    y_test = np.argmax(y_test, axis=1)
    input_data_dim = cifar10.test.images.shape[1]
    n_classes = 10
    cnn = ConvNet()
    cnn.dropout_rate = 1.0
    x = tf.placeholder(tf.float32,
                       shape=(None, input_data_dim, input_data_dim, 3),
                       name="x")
    y = tf.placeholder(tf.float32, shape=(None, n_classes), name="y")
    cnn.is_training = tf.placeholder(tf.bool)
    with tf.name_scope('train_cnn'):
        infs = cnn.inference(x)
        flatten = cnn.flatten
        fc1 = cnn.fc1
        fc2 = cnn.fc2

    with tf.Session() as sess:

        saver = tf.train.Saver()

        saver.restore(sess, FLAGS.checkpoint_dir + '/cnn_model.ckpt')

        fc2_features = sess.run([fc2],
                                feed_dict={
                                    x: x_test,
                                    cnn.is_training: False
                                })[0]

        # _plot_tsne("fc2.png", fc2_features, y_test)

        fc1_features = sess.run([fc1],
                                feed_dict={
                                    x: x_test,
                                    cnn.is_training: False
                                })[0]
        # _plot_tsne("fc1.png",  fc1_features, y_test)

        flatten_features = sess.run([flatten],
                                    feed_dict={
                                        x: x_test,
                                        cnn.is_training: False
                                    })[0]
        # _plot_tsne("flatten.png", flatten_features, y_test)

    _train_one_vs_all(fc2_features, y_test, "FC2", classes)
    _train_one_vs_all(fc1_features, y_test, "FC1", classes)
    _train_one_vs_all(flatten_features, y_test, "Flatten", classes)