예제 #1
0
def train():
    global_step = tf.Variable(0, trainable=False)
    dataset = coco_input.get_dataset()
    labels, images = dataset.train_input()

    network = model.Network(is_train=True)
    logits = network.inference(images)

    for var in tf.trainable_variables():
        tf.histogram_summary(var.op.name, var)

    entropy, loss = model.get_loss(labels, logits)

    lr, opt = get_opt(loss, global_step)
    summary_op = tf.merge_all_summaries()

    #gpu_options = tf.GPUOptions(allow_growth=True)
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        init = tf.initialize_all_variables()
        sess.run(init)
        if FLAGS.dir_pretrain is not None:
            saver = tf.train.Saver(model.get_pretrain_variables())
            restore_model(saver, sess)

        summary_writer = tf.train.SummaryWriter("log", sess.graph)

        tf.train.start_queue_runners(sess=sess)
        saver = tf.train.Saver(model.get_restore_variables())

        for num_iter in range(1, FLAGS.max_steps + 1):
            start_time = time.time()
            value_entropy, value_loss, value_lr, _ = sess.run(
                [entropy, loss, lr, opt])
            duration = time.time() - start_time
            assert not np.isnan(value_loss), 'Model diverged with loss = NaN'

            if num_iter % 10 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                print(
                    "step = {} entropy = {:.2f} loss = {:.2f} ({:.1f} examples/sec; {:.1f} sec/batch)"
                    .format(num_iter, value_entropy, value_loss,
                            examples_per_sec, sec_per_batch))

            if num_iter % 100 == 0:
                summary_str = sess.run(summary_op)
                summary_writer.add_summary(summary_str, num_iter)

            if num_iter % 1000 == 0:
                print "lr = {:.2f}".format(value_lr)
                checkpoint_path = os.path.join(FLAGS.dir_parameter,
                                               'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=num_iter)
예제 #2
0
    def inference(self, x):
        """
        forward network
        """
        layers = []
        feats = []

        D = x.get_shape()[3]
        output_ch = 32
        dataset = coco_input.get_dataset()
        num_classes = dataset.num_classes
        feat = x

        # 28x28
        for idx_layer in range(5):
            name = "layer{}".format(idx_layer)

            if idx_layer == 0:
                layer = Layer(name,
                              output_ch,
                              retain_ratio=0.8,
                              is_train=self.is_train)
            else:
                layer = Layer(name, output_ch, is_train=self.is_train)

            feat = layer.inference(feat)
            output_ch = int(output_ch * 2)

            feats.append(feat)
            layers.append(layer)

        self.conv_outputs = feats
        self.conv_layers = layers

        # Global Average Pooling
        with tf.variable_scope("GAP"):
            N, H, W, C = feat.get_shape()

            # avg pool
            feat = tf.nn.avg_pool(feat, [1, H, W, 1],
                                  strides=[1, 1, 1, 1],
                                  padding="VALID")

            # [batch_size, 1, 1, C] -> [batch_size, C]
            feat = tf.reshape(feat, [int(N), int(C)])

            # fc layer
            W = _var("W", [C, num_classes],
                     initializer=tf.truncated_normal_initializer(stddev=0.01))
            b = _var("b", [num_classes], initializer=tf.constant_initializer())

            logits = tf.matmul(feat, W) + b

        return logits
예제 #3
0
def eval_once(summary_writer, top_k_op, entropy):
    saver = tf.train.Saver(model.get_restore_variables())
    # Build an initialization operation to run below.
    init = tf.initialize_all_variables()

    # Start running operations on the Graph.
    gpu_options = tf.GPUOptions(allow_growth=True)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            allow_soft_placement=True))
    sess.run(init)
    global_step = restore_model(saver, sess)

    if global_step is None:
        return

    # Start the queue runners.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    dataset = coco_input.get_dataset()
    true_count = 0  # Counts the number of correct predictions.
    total_sample_count = dataset.get_validation_size()

    num_iter = total_sample_count / FLAGS.batch_size
    entropies = []
    use_sample_count = 0

    #for i in range(num_iter):
    # limit for performance (samples are chosen randomly)
    for i in range(min(num_iter, 1000)):
        predictions, value_entropy = sess.run([top_k_op, entropy])
        true_count += np.sum(predictions)
        use_sample_count += FLAGS.batch_size
        entropies.append(value_entropy)

    # Compute precision @ 1.
    precision = true_count / float(use_sample_count)
    mean_entropy = float(np.mean(entropies))

    print("use data {} / {}".format(use_sample_count, total_sample_count))
    print('step %d precision @ 1 = %.2f entropy = %.2f' %
          (int(global_step), precision, mean_entropy))

    summary = tf.Summary()
    summary.value.add(tag='Precision @ 1', simple_value=precision)
    summary.value.add(tag='entropy', simple_value=mean_entropy)
    summary_writer.add_summary(summary, global_step)

    coord.request_stop()
    coord.join(threads, stop_grace_period_secs=10)
예제 #4
0
def evaluate():
    with tf.Graph().as_default() as g, tf.device("/gpu:0"):
        dataset = coco_input.get_dataset()
        labels, images = dataset.validate_input()

        network = model.Network(is_train=False)
        logits = network.inference(images)
        entropy, _ = model.get_loss(labels, logits)

        top_k_op = tf.nn.in_top_k(logits, labels, 1)

        summary_writer = tf.train.SummaryWriter(FLAGS.dir_log_val, g)

        while True:
            eval_once(summary_writer, top_k_op, entropy)
            time.sleep(FLAGS.eval_interval_secs)
예제 #5
0
def get_loss(labels, logits):
    dataset = coco_input.get_dataset()
    tf.histogram_summary("logits", logits)

    vector_labels = tf.one_hot(labels, dataset.num_classes, dtype=tf.float32)
    tf.histogram_summary("labels", labels)

    entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
        logits, vector_labels),
                             name="entropy")
    decays = tf.add_n(tf.get_collection('losses'), name="weight_loss")
    total_loss = tf.add(entropy, decays, "total_loss")

    tf.scalar_summary("entropy", entropy)
    tf.scalar_summary("total_loss", total_loss)

    return entropy, total_loss