Ejemplo n.º 1
0
def evaluate_orig():
    """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 = itNet.inputs(eval_data=eval_data)

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

        while True:
            eval_once(saver, summary_writer, top_k_op, summary_op)
            if FLAGS.run_once:
                break
            time.sleep(FLAGS.eval_interval_secs)
def one_eval():
    images_test, labels_test = itNet.inputs(eval_data=True)
    num_iter = int(FLAGS.num_examples / FLAGS.batch_size)
    print("Number of iterations = {}".format(num_iter))
    true_count = 0  # Counts the number of correct predictions.
    total_sample_count = num_iter * FLAGS.batch_size
    print("total_sample_count = {}".format(total_sample_count))
    step = 0

    top_k_op = tf.nn.in_top_k(logits_test, labels_test, 1)
    while step < num_iter:
        print("Step is {}".format(step))
        logits_test = itNet.inference_switch(images_test_batch, FLAGS.network_architecture)
        print(logits_test)
        top_k_op = tf.nn.in_top_k(logits_test, labels_test, 1)
        predictions = top_k_op
        batchPredictions = np.asarray(predictions)
        print("Here's the batch predictions")
        print(batchPredictions)
        true_count += np.sum(predictions)
        step += 1
    # Compute precision @ 1.
    precision = true_count / total_sample_count
    print('%s: precision @ 1 = %.5f' % (datetime.now(), precision))
    return precision
Ejemplo n.º 3
0
def evaluate(returnConfusionMatrix=True):
    num_classes = itNet_input.NUM_CLASSES
    #if FLAGS.binarise_label > 0:
    #    num_classes = 2
    #elif FLAGS.binarise_label == -2:
    #    num_classes = 4
    #elif FLAGS.binarise_label == -3:
    #    num_classes = 3
    #print("And again {} classes".format(num_classes))
    """Eval CIFAR-10 for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels for CIFAR-10.
        #print("getting input data {}".format(FLAGS.eval_data))
        eval_data = FLAGS.eval_data == 'test'
        images, labels = itNet.inputs(eval_data=True)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #print("calling inference")
        logits = itNet.inference_switch(images, FLAGS.network_architecture)
        predictions = tf.argmax(logits, 1)
        gen_confusionMatrix = tf.confusion_matrix(labels,
                                                  predictions,
                                                  num_classes=num_classes)

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

        # Restore the moving average version of the learned variables for eval.
        #print("restore moving average version of learned variables")
        variable_averages = tf.train.ExponentialMovingAverage(
            itNet.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.
        #print("Building a summary")
        summary_op = tf.summary.merge_all()

        #print("And a summary writer")
        summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g)

        logfile = os.path.join(FLAGS.mylog_dir_eval, "log_evals.txt")
        log = open(logfile, 'w')
        start = datetime.now()

        runtimes = 0
        while True:
            #print("Calling eval_once")
            precision, confusionMatrix, predLabels = eval_once(
                saver, summary_writer, top_k_op, summary_op,
                gen_confusionMatrix, predictions)
            #precision = eval_once(saver, summary_writer, top_k_op, summary_op)
            if FLAGS.run_once:
                if returnConfusionMatrix:
                    return precision, confusionMatrix
                    #return precision
                else:
                    return precision
                break
            if FLAGS.run_times != 0 and runtimes > FLAGS.run_times:
                break
            if FLAGS.run_times == 0:  #only sleep when it's not a limited run
                time.sleep(FLAGS.eval_interval_secs)
            np.set_printoptions(threshold='nan')
            print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))
            print('{}: confusionMatrix: \n {}'.format(datetime.now(),
                                                      confusionMatrix))
            print(
                'AND predictions (provided you only used one thread!!!): \n ')
            print(np.array2string(predLabels, separator=', '))
            #print('AND predictions: \n')
            #print("{}".format(repr(predLabels)))
            #avg = np.average(predLabels)
            #print("Average is {}".format(avg))
            rightNow = datetime.now()
            difference = rightNow - start
            log.write(
                "*******************************************************\n")
            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
            if ckpt and ckpt.model_checkpoint_path:
                log.write("The checkpoint was: {} ok now".format(
                    ckpt.model_checkpoint_path))
            else:
                log.write("No checkpoint found yet")
            log.write("time: {} seconds \n".format(difference.total_seconds()))
            log.write('precision @ 1 = %.5f \n' % (precision))
            #log.write('confusionMatrix: \n {} \n'.format(confusionMatrix))
            cmString = np.array2string(confusionMatrix, separator='\t')
            cmString = cmString.replace('[', '')
            cmString = cmString.replace(']', '')
            log.write('confusionMatrix: \n {} \n'.format(cmString))

            #log.write("******************************************************* \n")
            log.flush()
            runtimes = runtimes + 1