コード例 #1
0
def evaluate():
    os.environ['CUDA_VISIBLE_DEVICES'] = str(FLAGS.gpuid)
    with tf.Graph().as_default() as g:
        images, labels = data_input.inputs(data_dir=FLAGS.data_dir,
                                           batch_size=FLAGS.eval_batch_size,
                                           num_tags=num_tags)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = model.inference(images,
                                 is_training=False,
                                 num_classes=num_tags)

        # Calculate predictions.
        prob_op = tf.sigmoid(logits)

        # Restore the moving average version of the learned variables for eval.
        saver = tf.train.Saver(tf.global_variables())

        summary_op = tf.summary.merge_all()

        summary_writer = tf.summary.FileWriter(eval_dir, g)

        while True:
            eval_once(saver, summary_writer, prob_op, labels, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
コード例 #2
0
def inputs(eval_data, sessid):
	if not FLAGS.data_dir:
		raise ValueError('Please supply a data_dir')
	images, labels, i_paths = data_input.inputs(eval_data=eval_data,
										  batch_size=FLAGS.batch_size, sessid = sessid)
	labels = tf.cast(tf.divide(labels,255),tf.int32)
	if FLAGS.use_fp16:
		images = tf.cast(images, tf.float16)
		labels = tf.cast(labels, tf.float16)
	return images, labels, i_paths
コード例 #3
0
def inputs(eval_data):
  """Construct input for data evaluation using the Reader ops.

  Args:
    eval_data: bool, indicating if one should use the train or eval data set.

  Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.

  Raises:
    ValueError: If no data_dir
  """
  if not FLAGS.data_dir:
    raise ValueError('Please supply a data_dir')
  #data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
  return data_input.inputs(eval_data=eval_data, data_dir=FLAGS.data_dir, batch_size=FLAGS.batch_size)
コード例 #4
0
def inputs(eval_data, sessid):
    """Construct input for BBBC006 evaluation using the Reader ops.
	Args:
		eval_data: bool, indicating if one should use the train or eval data set.
	Returns:
		images: Images. 4D tensor of [batch_size, IMAGE_WIDTH, IMAGE_HEIGHT, 1] size.
		labels: Labels. 4D tensor of [batch_size, IMAGE_WIDTH, IMAGE_HEIGHT, 2] size.

	Raises:
		ValueError: If no data_dir
	"""
    if not FLAGS.data_dir:
        raise ValueError('Please supply a data_dir')
    images, labels, i_paths = data_input.inputs(eval_data=eval_data,
                                                batch_size=FLAGS.batch_size,
                                                sessid=sessid)
    labels = tf.cast(tf.divide(labels, 255), tf.int32)
    if FLAGS.use_fp16:
        images = tf.cast(images, tf.float16)
        labels = tf.cast(labels, tf.float16)
    return images, labels, i_paths
コード例 #5
0
def run_training():
    """Train model for a number of steps."""
    # Get the sets of images and labels for training, validation, and
    # test on MNIST.

    # Tell TensorFlow that the model will be built into the default Graph.
    train_dir = os.path.join(FLAGS.model_dir, FLAGS.name)

    with open('kitti.json', 'r') as f:
        hypes = json.load(f)

    with tf.Graph().as_default():

        global_step = tf.Variable(0, trainable=False)

        with tf.name_scope('Input'):
            q = data_input.create_queues(hypes)
            image_batch, label_batch = data_input.inputs(
                q, 'train', hypes['solver']['batch_size'])

            # Generate placeholders for the images and labels.
            keep_prob = utils.placeholder_inputs()

        # Build a Graph that computes predictions from the inference model.
        logits = model.inference(image_batch, keep_prob)

        # Add to the Graph the Ops for loss calculation.
        loss = model.loss(logits, label_batch)

        # Add to the Graph the Ops that calculate and apply gradients.
        train_op = model.training(loss,
                                  global_step=global_step,
                                  learning_rate=FLAGS.learning_rate)

        # Add the Op to compare the logits to the labels during evaluation.
        eval_correct = model.evaluation(logits, label_batch)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver()

        # Create a session for running Ops on the Graph.
        sess = tf.Session()

        # Run the Op to initialize the variables.
        init = tf.initialize_all_variables()
        sess.run(init)

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        data_input.start_enqueuing_threads(hypes, q, sess)

        # Instantiate a SummaryWriter to output summaries and the Graph.
        summary_writer = tf.train.SummaryWriter(train_dir,
                                                graph_def=sess.graph_def)

        # And then after everything is built, start the training loop.
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()

            # Fill a feed dictionary with the actual set of images and labels
            # for this particular training step.
            feed_dict = utils.fill_feed_dict(keep_prob, train=True)

            # Run one step of the model.  The return values are the activations
            # from the `train_op` (which is discarded) and the `loss` Op.  To
            # inspect the values of your Ops or variables, you may include them
            # in the list passed to sess.run() and the value tensors will be
            # returned in the tuple from the call.
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

            # Write the summaries and print an overview fairly often.
            if step % 100 == 0:
                # Print status to stdout.
                duration = time.time() - start_time
                examples_per_sec = hypes['solver']['batch_size'] / duration
                sec_per_batch = float(duration)
                print(
                    'Step %d: loss = %.2f ( %.3f sec (per Batch); %.1f examples/sec;)'
                    % (step, loss_value, sec_per_batch, examples_per_sec))
                # Update the events file.
                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, step)

            # Save a checkpoint and evaluate the model periodically.
            if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
                # Evaluate against the training set.

            if (step + 1) % 10000 == 0 or (step + 1) == FLAGS.max_steps:
                print('Training Data Eval:')
                utils.do_eval(sess, eval_correct, keep_prob,
                              data_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN)
コード例 #6
0
def evaluate_last():
    """Loads the model and runs evaluation
  """

    with tf.Graph().as_default():
        # Get images and labels for CIFAR-10.
        model_dir = os.path.join(FLAGS.model_dir, FLAGS.name)

        eval_data = FLAGS.eval_data == 'test'
        images, labels = data_input.inputs(eval_data=eval_data,
                                           data_dir=FLAGS.data_dir,
                                           batch_size=FLAGS.batch_size)

        #images, labels =  data_input.distorted_inputs(eval_data=eval_data, data_dir=FLAGS.data_dir,
        #                                       batch_size=FLAGS.batch_size)

        # Generate placeholders for the images and labels.
        keep_prob = utils.placeholder_inputs(FLAGS.batch_size)

        # Build a Graph that computes predictions from the inference model.
        logits = model.inference(images, keep_prob)

        # Add to the Graph the Ops for loss calculation.
        loss = model.loss(logits, labels)

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

        # Add the Op to compare the logits to the labels during evaluation.
        eval_correct = model.evaluation(logits, labels)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver()

        # Create a session for running Ops on the Graph.
        sess = tf.Session()

        # Restore the moving average version of the learned variables for eval.
        # variable_averages = tf.train.ExponentialMovingAverage(
        #     cifar10.MOVING_AVERAGE_DECAY)
        # variables_to_restore = variable_averages.variables_to_restore()
        # saver = tf.train.Saver(variables_to_restore)
        # Build the summary operation based on the TF collection of Summaries.
        # summary_op = tf.merge_all_summaries()

        #graph_def = tf.get_default_graph().as_graph_def()
        #summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir,
        #	                                     graph_def=graph_def)

        # Run the Op to initialize the variables.
        init = tf.initialize_all_variables()
        sess.run(init)

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        print(model_dir)

        ckpt = tf.train.get_checkpoint_state(model_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            print("No checkpoints found! ")
            exit(1)

        print("Doing Evaluation with lots of data")
        utils.do_eval(sess=sess,
                      eval_correct=eval_correct,
                      keep_prob=keep_prob,
                      num_examples=data_input.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL)
コード例 #7
0
def run_training():
  """Train model for a number of steps."""
  # Get the sets of images and labels for training, validation, and
  # test on MNIST.

  # Tell TensorFlow that the model will be built into the default Graph.
  train_dir = os.path.join(FLAGS.model_dir,FLAGS.name)

  with open('kitti.json', 'r') as f:
    hypes = json.load(f)

  with tf.Graph().as_default():

    global_step = tf.Variable(0, trainable=False)


    with tf.name_scope('Input'):
      q = data_input.create_queues(hypes)
      image_batch, label_batch = data_input.inputs(q, 'train', hypes['solver']['batch_size'])

      # Generate placeholders for the images and labels.
      keep_prob = utils.placeholder_inputs()

    # Build a Graph that computes predictions from the inference model.
    logits = model.inference(image_batch, keep_prob)

    # Add to the Graph the Ops for loss calculation.
    loss = model.loss(logits, label_batch)

    # Add to the Graph the Ops that calculate and apply gradients.
    train_op = model.training(loss, global_step=global_step, learning_rate=FLAGS.learning_rate)

    # Add the Op to compare the logits to the labels during evaluation.
    eval_correct = model.evaluation(logits, label_batch)

    # Build the summary operation based on the TF collection of Summaries.
    summary_op = tf.merge_all_summaries()

    # Create a saver for writing training checkpoints.
    saver = tf.train.Saver()

    # Create a session for running Ops on the Graph.
    sess = tf.Session()

    # Run the Op to initialize the variables.
    init = tf.initialize_all_variables()
    sess.run(init)

    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)

    data_input.start_enqueuing_threads(hypes, q, sess)

    # Instantiate a SummaryWriter to output summaries and the Graph.
    summary_writer = tf.train.SummaryWriter(train_dir,
                                            graph_def=sess.graph_def)

    # And then after everything is built, start the training loop.
    for step in xrange(FLAGS.max_steps):
      start_time = time.time()

      # Fill a feed dictionary with the actual set of images and labels
      # for this particular training step.
      feed_dict = utils.fill_feed_dict(keep_prob,
                                       train = True)

      # Run one step of the model.  The return values are the activations
      # from the `train_op` (which is discarded) and the `loss` Op.  To
      # inspect the values of your Ops or variables, you may include them
      # in the list passed to sess.run() and the value tensors will be
      # returned in the tuple from the call.
      _, loss_value = sess.run([train_op, loss],
                               feed_dict=feed_dict)

      # Write the summaries and print an overview fairly often.
      if step % 100 == 0:
        # Print status to stdout.
        duration = time.time() - start_time
        examples_per_sec = hypes['solver']['batch_size'] / duration
        sec_per_batch = float(duration)
        print('Step %d: loss = %.2f ( %.3f sec (per Batch); %.1f examples/sec;)' % (step, loss_value,
                                     sec_per_batch, examples_per_sec))
        # Update the events file.
        summary_str = sess.run(summary_op, feed_dict=feed_dict)
        summary_writer.add_summary(summary_str, step)

      # Save a checkpoint and evaluate the model periodically.
      if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        checkpoint_path = os.path.join(train_dir, 'model.ckpt')
        saver.save(sess, checkpoint_path , global_step=step)
        # Evaluate against the training set.

      if (step + 1) % 10000 == 0 or (step + 1) == FLAGS.max_steps:  
        print('Training Data Eval:')
        utils.do_eval(sess,
                      eval_correct,
                      keep_prob,
                      data_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN)
コード例 #8
0
def evaluate_last():
  """Loads the model and runs evaluation
  """

  with tf.Graph().as_default():
    # Get images and labels for CIFAR-10.
    model_dir = os.path.join(FLAGS.model_dir,FLAGS.name)

    eval_data = FLAGS.eval_data == 'test'
    images, labels =  data_input.inputs(eval_data=eval_data, data_dir=FLAGS.data_dir,
                                           batch_size=FLAGS.batch_size)

    #images, labels =  data_input.distorted_inputs(eval_data=eval_data, data_dir=FLAGS.data_dir,
    #                                       batch_size=FLAGS.batch_size)

    # Generate placeholders for the images and labels.
    keep_prob = utils.placeholder_inputs(FLAGS.batch_size)

    # Build a Graph that computes predictions from the inference model.
    logits = model.inference(images, keep_prob)

    # Add to the Graph the Ops for loss calculation.
    loss = model.loss(logits, labels)
  
    # Calculate predictions.
    top_k_op = tf.nn.in_top_k(logits, labels, 1)

    # Add the Op to compare the logits to the labels during evaluation.
    eval_correct = model.evaluation(logits, labels)

    # Build the summary operation based on the TF collection of Summaries.
    summary_op = tf.merge_all_summaries()

    # Create a saver for writing training checkpoints.
    saver = tf.train.Saver()

    # Create a session for running Ops on the Graph.
    sess = tf.Session()
  
    # Restore the moving average version of the learned variables for eval.
    # variable_averages = tf.train.ExponentialMovingAverage(
    #     cifar10.MOVING_AVERAGE_DECAY)
    # variables_to_restore = variable_averages.variables_to_restore()
    # saver = tf.train.Saver(variables_to_restore)
    # Build the summary operation based on the TF collection of Summaries.
    # summary_op = tf.merge_all_summaries()

    #graph_def = tf.get_default_graph().as_graph_def()
    #summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir,
    #	                                     graph_def=graph_def)

    # Run the Op to initialize the variables.
    init = tf.initialize_all_variables()
    sess.run(init)

    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)

    print(model_dir)

    ckpt = tf.train.get_checkpoint_state(model_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
    else:
      print("No checkpoints found! ")
      exit(1)

    print("Doing Evaluation with lots of data")  
    utils.do_eval(sess=sess,
                  eval_correct=eval_correct,
                  keep_prob=keep_prob, 
                  num_examples=data_input.NUM_EXAMPLES_PER_EPOCH_FOR_EVAL)