Exemple #1
0
def evaluate():

    with tf.Graph().as_default() as g:
        eval_data = FLAGS.eval_data == 'test'
        images, labels = convnet.inputs(eval_data=True)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = convnet.inference(images, eval=True)
        loss = convnet.loss(logits, labels)

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

        #Compute confusion matrix
        conf_mat = tf.contrib.metrics.confusion_matrix(
            tf.arg_max(logits, 1), tf.cast(labels, tf.int64))

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

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

        while True:
            eval_once(saver, summary_writer, logits, labels, top_k_op,
                      conf_mat, loss, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
Exemple #2
0
def evaluate():
    """Eval CIFAR-10 for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels for CIFAR-10.
        eval_data = FLAGS.eval_data == 'test'
        images, labels = convnet.inputs(eval_data=True)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = convnet.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(
            convnet.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()

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

        while True:
            eval_once(saver, summary_writer, top_k_op, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
Exemple #3
0
def evaluate():

    with tf.Graph().as_default() as g:
        eval_data = FLAGS.eval_data == 'test'
        images, labels = convnet.inputs(eval_data=True)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = convnet.inference(images, eval=True)
        loss = convnet.loss(logits, labels)
        # Calculate r-squared measure
        R_y = tf.reduce_sum(tf.square(labels - tf.squeeze(logits)))
        R_e = tf.reduce_sum(
            tf.square(labels - tf.reduce_mean(tf.squeeze(logits))))

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

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

        while True:
            eval_once(saver, summary_writer, logits, labels, loss, R_y, R_e,
                      summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
Exemple #4
0
def train():
  	with tf.Graph().as_default():
    	global_step = tf.Variable(0, trainable=False)

    	# Get images and labels.
    	images, labels = convnet.inputs()

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

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

    	# Build a Graph that trains the model with one batch of examples and
    	# updates the model parameters.
    	train_op = convnet.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, sess.graph)

    	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_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 __name__ == '__main__':
	train()
Exemple #5
0
def train():
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        # Get images and labels.
        images, labels = convnet.inputs(eval_data=False)

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

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

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = convnet.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)

        # Load previously stored model from checkpoint
        ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            saver.restore(sess, ckpt.model_checkpoint_path)
            # Assuming model_checkpoint_path looks something like:
            #   /my-favorite-path/cifar10_train/model.ckpt-0,
            # extract global_step from it.
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            print("Loading from checkpoint.Global step %s" % global_step)
        else:
            print("No checkpoint file found...Creating a new model...")

        stepfile = "/home/soms/EmotionMusic/Model1/stepfile.txt"
        if not os.path.exists(stepfile):
            print("No step file found.")
            step = 0
        else:
            f = open(stepfile, "r")
            step = int(f.readlines()[0])
            print("Step file step %d" % step)

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

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

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

            def signal_handler(signal, frame):
                f = open(stepfile, 'w')
                f.write(str(step))
                print("Step file written to.")
                sys.exit(0)

            signal.signal(signal.SIGINT, signal_handler)

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
            if step % 10 == 0:
                num_examples_per_step = FLAGS.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 % 500 == 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)
            step += 1