Example #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)
Example #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)
Example #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)
Example #4
0
def evaluate(ind=None):

    testdir = "/home/satyaki/Emotion/Soundtrack/Model2_SongWise/Data/Test"
    index = ind if ind is not None else 0
    while True:
        with tf.Graph().as_default() as g:
            predictionfile = open(
                "/home/satyaki/Emotion/Soundtrack/Model2_SongWise/Outputs/output_"
                + str(index) + ".txt", "w")
            sequencefile = open(
                "/home/satyaki/Emotion/Soundtrack/Model2_SongWise/Outputs/output_sequence_"
                + str(index) + ".txt", "w")
            with tf.Session() as sess:
                print("Index %d" % index)
                for _, _, files in os.walk(testdir):
                    cnt = 1
                    prec_song = 0
                    prec_clips = 0
                    tot_clips = 0
                    for file in files:
                        filename = os.path.join(testdir, file)
                        filename_queue = tf.train.string_input_producer(
                            [filename])
                        images, labels = input(sess, filename_queue)
                        # Build a Graph that computes the logits predictions from the
                        # inference model.
                        logits = convnet.inference(images, eval=True)

                        # 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)
                        p_song, p_clips, t_clips = eval_once(
                            sess, saver, logits, labels, file, predictionfile,
                            sequencefile)
                        prec_song += p_song
                        prec_clips += p_clips
                        tot_clips += t_clips
                        print("Count %d" % cnt)
                        cnt += 1
                    prec_song /= len(files)
                    prec_clips /= tot_clips
                    print(
                        ".........................................................."
                    )
                    print("Total Song# %d" % len(files))
                    print("Total Clips# %d" % tot_clips)
                    print("Song level accuracy %.2f Clip level accuracy %.2f" %
                          (prec_song, prec_clips))
                    print(
                        ".........................................................."
                    )
        time.sleep(FLAGS.eval_interval_secs)
        index += 1
Example #5
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()
Example #6
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