Exemple #1
0
def evaluate():
    """Eval FER2013 for a number of steps."""
    with tf.Graph().as_default() as graph:
        # Get images and labels for FER2013.

        images, labels = fer2013.inputs(eval_data=FLAGS.eval_data,
                                        input_file=TEST_INPUT_FILE)
        keep_prob = 1

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

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

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            fer2013.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.summary.merge_all()

        summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, graph)

        while True:
            eval_once(saver, summary_writer, top_k_op, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
def evaluate():
    """Eval FER2013 for a number of steps."""
    with tf.Graph().as_default():
        # Get images and labels for FER2013.
        eval_data = FLAGS.eval_data == 'test'
        print(eval_data)
        print("evaluating model...")
        images, labels = fer2013.inputs(eval_data=eval_data)

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

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

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            fer2013.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)

        while True:
            eval_once(saver, summary_writer, top_k_op, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
def evaluate():
  """Eval FER2013 for a number of steps."""
  with tf.Graph().as_default():
    # Get images and labels for FER2013.
    eval_data = FLAGS.eval_data == 'test'
    print(eval_data)
    print("evaluating model...")
    images, labels = fer2013.inputs(eval_data=eval_data)

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

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

    # Restore the moving average version of the learned variables for eval.
    variable_averages = tf.train.ExponentialMovingAverage(
        fer2013.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)

    while True:
      eval_once(saver, summary_writer, top_k_op, summary_op)
      if FLAGS.run_once:
        break
      time.sleep(FLAGS.eval_interval_secs)
Exemple #4
0
def tower_loss(scope):
    """Calculate the total loss on a single tower running the model.
    
    Args:
      scope: unique prefix string identifying the  tower, e.g. 'tower_0'
    
    Returns:
       Tensor of shape [] containing the total loss for a batch of data
    """
    # Get images and labels
    images, labels = fer2013.distorted_inputs(TRAIN_INPUT_FILE)
    keep_prob = 0.5

    # Build inference Graph.
    logits = fer2013.inference(images, keep_prob, FLAGS.train_batch_size)

    # Build the portion of the Graph calculating the losses. Note that we will
    # assemble the total_loss using a custom function below.
    _ = fer2013.loss(logits, labels)

    acc = fer2013.accuracy(logits, labels)

    # Assemble all of the losses for the current tower only.
    losses = tf.get_collection('losses', scope)

    # Calculate the total loss for the current tower.
    total_loss = tf.add_n(losses, name='total_loss')

    # Compute the moving average of all individual losses and the total loss.
    loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
    loss_averages_op = loss_averages.apply(losses + [total_loss])

    # Attach a scalar summary to all individual losses and the total loss; do the
    # same for the averaged version of the losses.
    for l in losses + [total_loss]:
        # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
        # session. This helps the clarity of presentation on tensorboard.
        loss_name = re.sub('%s_[0-9]*/' % fer2013.TOWER_NAME, '', l.op.name)
        # Name each loss as '(raw)' and name the moving average version of the loss
        # as the original loss name.
        tf.summary.scalar(loss_name + ' (raw)', l)
        tf.summary.scalar(loss_name, loss_averages.average(l))

    with tf.control_dependencies([loss_averages_op]):
        total_loss = tf.identity(total_loss)
    return total_loss
def tower_loss(scope):
  """Calculate the total loss on a single tower running the CIFAR model.

  Args:
    scope: unique prefix string identifying the CIFAR tower, e.g. 'tower_0'

  Returns:
     Tensor of shape [] containing the total loss for a batch of data
  """
  # Get images and labels for CIFAR-10.
  images, labels = fer2013.distorted_inputs()

  # Build inference Graph.
  logits = fer2013.inference(images)

  # Build the portion of the Graph calculating the losses. Note that we will
  # assemble the total_loss using a custom function below.
  _ = fer2013.loss(logits, labels)

  # Assemble all of the losses for the current tower only.
  losses = tf.get_collection('losses', scope)

  # Calculate the total loss for the current tower.
  total_loss = tf.add_n(losses, name='total_loss')

  # Compute the moving average of all individual losses and the total loss.
  loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
  loss_averages_op = loss_averages.apply(losses + [total_loss])

  # Attach a scalar summary to all individual losses and the total loss; do the
  # same for the averaged version of the losses.
  for l in losses + [total_loss]:
    # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
    # session. This helps the clarity of presentation on tensorboard.
    loss_name = re.sub('%s_[0-9]*/' % fer2013.TOWER_NAME, '', l.op.name)
    # Name each loss as '(raw)' and name the moving average version of the loss
    # as the original loss name.
    tf.scalar_summary(loss_name +' (raw)', l)
    tf.scalar_summary(loss_name, loss_averages.average(l))

  with tf.control_dependencies([loss_averages_op]):
    total_loss = tf.identity(total_loss)
  return total_loss
Exemple #6
0
def train():
    """Train FER-2013 for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.contrib.framework.get_or_create_global_step()
    
        # Get images and labels for FER2013.
        # Force input pipeline to CPU:0 to avoid operations sometimes ending up on
        # GPU and resulting in a slow down.
        with tf.device('/cpu:0'):  
            images, labels = fer2013.distorted_inputs(TRAIN_INPUT_FILE)
        
        keep_prob = 0.7
    
        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = fer2013.inference(images, keep_prob, FLAGS.train_batch_size)


        # Visualize conv1 kernels
        with tf.variable_scope('conv1'):
            tf.get_variable_scope().reuse_variables()
            weights = tf.get_variable('weights')
            grid = put_kernels_on_grid(weights)
            tf.summary.image('conv1/kernels', grid, max_outputs=1)

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

        # Calculate loss.
        loss = fer2013.loss(logits, labels)

        acc = fer2013.accuracy(logits, labels)
    
        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = fer2013.train(loss, global_step)
    
        # Create a saver.
        saver = tf.train.Saver(tf.global_variables())
    
        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()
    
        # Build an initialization operation to run below.
        init = tf.global_variables_initializer()
    
        # Start running operations on the Graph.
        sess = tf.Session(config=tf.ConfigProto(
            log_device_placement=FLAGS.log_device_placement))
        sess.run(init)

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

        summary_writer = tf.summary.FileWriter(FLAGS.train_dir,
                                                sess.graph)

        epoch_size = int(fer2013.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.train_batch_size)
        epoch_count = 0

        total_sample_count = epoch_size * FLAGS.train_batch_size

        init_local = tf.local_variables_initializer()
        sess.run(init_local)
    
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss])
            duration = time.time() - start_time

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
            
            if step % 10 == 0:
                accu = sess.run([acc])
                print('Acc: ', accu)

                num_examples_per_step = FLAGS.train_batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)
                
                format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                              'sec/batch)')
                print (format_str % (datetime.now(), step, loss_value,
                                     examples_per_sec, sec_per_batch))
            
            if step % 100 == 0:
                summary_str = sess.run(summary_op)
                summary_writer.add_summary(summary_str, step)

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

            if step % epoch_size == 0:
                epoch_count += 1
                print('epoch: ' + str(epoch_count))

        print('Training finished')
        true_count = 0
        st = 0
        while st < epoch_size:
            predictions = sess.run([top_k_op])
            true_count += np.sum(predictions)
            st += 1

        accuracy = true_count / total_sample_count
        print("Accuracy: ", accuracy, ' right predicted: ', true_count)
def train():
  """Train FER2013 for a number of steps."""
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)

    # Get images and labels for FER2013.
    images, labels = fer2013.distorted_inputs()

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

    # Calculate loss.
    loss = fer2013.loss(logits, labels)

    # Build a Graph that trains the model with one batch of examples and
    # updates the model parameters.
    train_op = fer2013.train(loss, global_step)

    # Create a saver.
    saver = tf.train.Saver(tf.all_variables())

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

    # Build an initialization operation to run below.
    init = tf.initialize_all_variables()

    # Start running operations on the Graph.
    sess = tf.Session(config=tf.ConfigProto(
        log_device_placement=FLAGS.log_device_placement))
    sess.run(init)

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

    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
                                            graph_def=sess.graph_def)

    for step in xrange(FLAGS.max_steps):
      start_time = time.time()
      _, loss_value = sess.run([train_op, loss])
      duration = time.time() - start_time

      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

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

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

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

      # Save the model checkpoint periodically.
      if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)
Exemple #8
0
def predict():
    """Eval FER2013 for a number of steps."""
    with tf.Graph().as_default():

        images, _ = fer2013.inputs(eval_data=FLAGS.eval_data,
                                   input_file=input_image_csv)
        keep_prob = 1

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = fer2013.inference(
            images, keep_prob,
            -1)  # batch size -1: accepts dynamic batch sizes

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            fer2013.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # ---------------------- Version 1 with top k predictions --------------------
        _, top_k_pred = tf.nn.top_k(logits, k=3)

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
            if ckpt and ckpt.model_checkpoint_path:
                # Restores from checkpoint
                saver.restore(sess, ckpt.model_checkpoint_path)
                print("checkpoint file found")
            else:
                print('No checkpoint file found')
                return

            # Start input enqueue threads.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            top_indices = sess.run([top_k_pred])
            print("Predicted as TOP 3: ", top_indices[0][0],
                  " for your input image.")
            print('Predicted as 1.: ', top_indices[0][0][0], ' -> ',
                  emotion_dict[top_indices[0][0][0]])
            print('Predicted as 2.: ', top_indices[0][0][1], ' -> ',
                  emotion_dict[top_indices[0][0][1]])
            print('Predicted as 3.: ', top_indices[0][0][2], ' -> ',
                  emotion_dict[top_indices[0][0][2]])

            coord.request_stop()
            coord.join(threads)

        # -------------------- end Version 1 -----------------------

        # Version 2 with argmax
        # Restore the moving average version of the learned variables for eval.
        """
        prediction = make_prediction(saver=saver, logits=logits)
        print('Predicted emotion: ', prediction[0], ' -> ', emotion_dict[prediction[0]])
        """

        # ------------------ end Version 2 --------------------

        img = cv2.imread(local_directory + input_image_png, 0)
        cv2.imshow("Emotion Image", img)
        print(
            "--------------- Press ENTER to return to Webcam ---------------")

        key = cv2.waitKey(0)
        if key == 13:
            cv2.destroyAllWindows()
Exemple #9
0
def train():
  """Train FER2013 for a number of steps."""
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)

    # Get images and labels for FER2013.
    images, labels = fer2013.distorted_inputs()

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

    # Calculate loss.
    loss = fer2013.loss(logits, labels)

    # Build a Graph that trains the model with one batch of examples and
    # updates the model parameters.
    train_op = fer2013.train(loss, global_step)

    # Create a saver.
    saver = tf.train.Saver(tf.all_variables())

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

    # Build an initialization operation to run below.
    init = tf.initialize_all_variables()

    # Start running operations on the Graph.
    sess = tf.Session(config=tf.ConfigProto(
        log_device_placement=FLAGS.log_device_placement))
    sess.run(init)

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

    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
                                            graph_def=sess.graph_def)

    for step in xrange(FLAGS.max_steps):
      start_time = time.time()
      _, loss_value = sess.run([train_op, loss])
      duration = time.time() - start_time

      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

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

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

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

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