Exemple #1
0
def _log_all_wrong_predictions(sess, prediction, labels_pl, images_pl, dropout_pl, dataset):
    # Calculate predictions of trained network
    y_true = []
    y_pred = []
    invalid_images = []
    for i in range(len(dataset.image_list)):
        feed_dict = utils.create_feed_data(sess, images_pl, labels_pl, dropout_pl, dataset, 1.0)
        y_true.extend(feed_dict[labels_pl])
        y_pred.extend(sess.run([prediction], feed_dict=feed_dict)[0])

        if(y_true[i] != y_pred[i]):
            invalid_images.append(dataset.image_list[i])

        sys.stdout.write("  Calculating predictions ... %d%%\r" % (i * 100 / len(dataset.image_list)))
        sys.stdout.flush()
    sys.stdout.write("                                                  \r")
    sys.stdout.flush()

    # Append experience to file
    experience_file = FLAGS.generation_experience_file.format(FLAGS.generation)
    with open(experience_file, 'a') as file_handler:
        file_handler.write("\n".join(invalid_images))
        file_handler.write("\n")

    return
def calc_metrics(sess, prediction, labels_pl, images_pl, dropout_pl, dataset, should_calc_confusion_matrix=False):
    # Calculate predictions of trained network
    y_true = []
    y_pred = []
    steps_per_epoch = dataset.size // dataset.batch_size
    for pred_step in range(steps_per_epoch):
        feed_dict = utils.create_feed_data(sess, images_pl, labels_pl, dropout_pl, dataset, 1.0)
        y_true.extend(feed_dict[labels_pl])
        y_pred.extend(sess.run([prediction], feed_dict=feed_dict)[0])
        sys.stdout.write("  Calculating predictions ... %d%%\r" % (pred_step * 100 / steps_per_epoch))
        sys.stdout.flush()
    sys.stdout.write("                                                 \r")
    sys.stdout.flush()

    # Rows ~ True Labels, Cols ~ Predicted labels_pl
    # http://scikit-learn.org/stable/modules/model_evaluation.html#confusion-matrix
    confusion_matrix = metrics.confusion_matrix(y_true, y_pred)
    print("## Confusion Matrix")
    print(confusion_matrix)

    # Accuracy   
    print("\n## Summary")
    val = metrics.accuracy_score(y_true, y_pred)
    print("Accuracy: %0.04f" % (val))

    # Precision recall and fone score
    metric_results = metrics.precision_recall_fscore_support(y_true, y_pred)
    precision_per_class = metric_results[0]
    recall_per_class = metric_results[1]
    fone_per_class = metric_results[2]

    # Per class summary
    print("Precision per class: %s" % ["%.2f" % v for v in precision_per_class])
    print("Recall per class: %s" % ["%.2f" % v for v in recall_per_class])
    print("F1-Score per class: %s" % ["%.2f" % v for v in fone_per_class])

    # Precision   
    val = np.mean(precision_per_class)
    print("Precision: %0.04f" % (val))

    # Precision   
    val = np.mean(recall_per_class)
    print("Recall: %0.04f" % (val))

    # F1 Score    
    val = np.mean(fone_per_class)
    print("F1-Score: %0.04f" % (val))
    print("\n")

    return
def main(argv=None):
    try:
        with tf.Graph().as_default():

            # Load all images from disk
            data_set = data_input.read_image_batches_without_labels_from_file_list(images_to_predict, FLAGS)

            # Inference model
            images_placeholder, labels_placeholder = utils.create_placeholder_inputs(data_set.batch_size, FLAGS.image_height, FLAGS.image_width)
            logits = model.inference(images_placeholder, data_set.batch_size, FLAGS.num_classes)

            # Max. value is our prediction
            prediction=tf.argmax(logits,1)

            # Add the variable initializer Op.
            init = tf.initialize_all_variables()
            saver = tf.train.Saver()

            # Create a session for running Ops on the Graph.
            with tf.Session() as sess:
                sess.run(init)
                
                try:
                    # Start the queue runners.
                    coord = tf.train.Coordinator()
                    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

                    # Restore variables from disk.
                    print("Restore session from checkpoint {0}\n".format(FLAGS.checkpoint))
                    saver.restore(sess, FLAGS.checkpoint)

                    # Predict
                    feed_dict = utils.create_feed_data(sess, images_placeholder, labels_placeholder, data_set)
                    predictions, classes = sess.run([prediction, logits], feed_dict=feed_dict)
                    
                    # Print results
                    for i in range(0, data_set.size):
                        print("{0} top-1-class: {1}".format(images_to_predict[i], predictions[i]))
                        print("Out = {0}\n".format(", ".join("{:0.2f}".format(i) for i in classes[i])))

                finally:
                    print("\nWaiting for all threads...")
                    coord.request_stop()
                    coord.join(threads)

    except:
        traceback.print_exc()

    finally:
        print("\nDone.\n")
def do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_set):
    # Run evaluation against all images
    true_count = 0  # Counts the number of correct predictions.
    steps_per_epoch = data_set.size // data_set.batch_size
    num_examples = steps_per_epoch * data_set.batch_size
    
    for step in xrange(steps_per_epoch):
        feed_dict = utils.create_feed_data(sess, images_placeholder, labels_placeholder, data_set)
        true_count += sess.run(eval_correct, feed_dict=feed_dict)

        sys.stdout.write("  Calculating accuracy...%d%%\r" % (step * 100 / steps_per_epoch))
        sys.stdout.flush()


    return num_examples, true_count
Exemple #5
0
def do_eval(sess, eval_correct, images_placeholder, labels_placeholder,
            data_set):
    # Run evaluation against all images
    true_count = 0  # Counts the number of correct predictions.
    steps_per_epoch = data_set.size // data_set.batch_size
    num_examples = steps_per_epoch * data_set.batch_size

    for step in xrange(steps_per_epoch):
        feed_dict = utils.create_feed_data(sess, images_placeholder,
                                           labels_placeholder, data_set)
        true_count += sess.run(eval_correct, feed_dict=feed_dict)

        sys.stdout.write("  Calculating accuracy...%d%%\r" %
                         (step * 100 / steps_per_epoch))
        sys.stdout.flush()

    return num_examples, true_count
def main(argv=None):
    try:
        
        # Create log dir if not exists
        if tf.gfile.Exists(FLAGS.log_dir):
            tf.gfile.DeleteRecursively(FLAGS.log_dir)
        tf.gfile.MakeDirs(FLAGS.log_dir)

        # Tell TensorFlow that the model will be built into the default Graph.
        with tf.Graph().as_default():
            data_sets = data_input.read_evaluation_and_train_image_batches(FLAGS)
            train_data_set = data_sets.train
            evaluation_data_set = data_sets.evaluation
            
            images_placeholder, labels_placeholder = utils.create_placeholder_inputs(FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width)
                    
            # Build a Graph that computes predictions from the inference model.
            # We use the same weight's etc. for the training and evaluation
            logits = model.inference(images_placeholder, train_data_set.batch_size, train_data_set.num_classes)
                            
            # Accuracy
            correct = tf.nn.in_top_k(logits, labels_placeholder, 1)
            eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32))

            # Add to the Graph the Ops for loss calculation.
            train_loss = _create_train_loss(logits, labels_placeholder)

            # Add to the Graph the Ops that calculate and apply gradients.
            global_step = tf.Variable(0, trainable=False)

            if(FLAGS.optimizer == 0):
                print("Using gradient descent optimizer.")
                train_op = _create_gradient_descent_train_op(train_loss, global_step, train_data_set.size)
            else:
                print("Using adam optimizer.")
                train_op = _create_adam_train_op(train_loss, global_step)

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

            # Add tensorboard summaries
            tf.image_summary('image_train', train_data_set.images, max_images = 5)
            tf.image_summary('image_evaluation', evaluation_data_set.images, max_images = 5)

            # Build the summary operation based on the TF collection of Summaries.
            summary_op = tf.merge_all_summaries()
            
            # Add the variable initializer Op.
            init = tf.initialize_all_variables()
            
            # Create a session for running Ops on the Graph.
            sess = tf.Session()

            # And then after everything is built:
            # Run the Op to initialize the variables.
            sess.run(init)

            # Start the queue runners.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            # Instantiate a SummaryWriter to output summaries and the Graph.
            summary_writer = tf.train.SummaryWriter(FLAGS.log_dir, sess.graph)
            
            #
            # Training loop
            #
            try:
                for step in xrange(FLAGS.max_steps):
                    if coord.should_stop():
                        break
                        
                    start_time = time.time()
                    
                    train_feed = utils.create_feed_data(sess, images_placeholder, labels_placeholder, train_data_set)
                    _, loss_value = sess.run([train_op, train_loss], feed_dict=train_feed)
                    assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

                    duration = time.time() - start_time

                    # Print step loss etc. to console
                    if step % 10 == 0:
                        steps_per_epoch = train_data_set.size // train_data_set.batch_size
                        epoch = int(step / steps_per_epoch)
                        num_examples_per_step = train_data_set.batch_size
                        examples_per_sec = num_examples_per_step / duration
                        sec_per_batch = float(duration)

                        print ('%s: step %d, epoch %d | loss = %.6f | %.1f examples/sec; %.3f '
                                    'sec/batch' % (datetime.now(), step, epoch, loss_value,
                                    examples_per_sec, sec_per_batch))
                    
                    # Write summary to tensorboard
                    if step % 100 == 0:
                        summary_str = sess.run([summary_op], feed_dict=train_feed)
                        summary_writer.add_summary(summary_str[0], step)
                        
                    # Evaluation 
                    if step % 1000 == 0:

                        if step == 0 and not FLAGS.initial_accuracy:
                            continue     

                        num_examples, true_count = evaluation.do_eval(sess, eval_correct, images_placeholder, 
                                labels_placeholder, evaluation_data_set)
                        precision = true_count / num_examples
                        print('  Evaluation data | Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' %
                                (num_examples, true_count, precision)) 

                        summary = tf.Summary(value=[tf.Summary.Value(tag="accuracy_evaluation", simple_value=precision)])
                        summary_writer.add_summary(summary, step) 
                        
                        num_examples, true_count = evaluation.do_eval(sess, eval_correct, images_placeholder, labels_placeholder, train_data_set)
                        precision = true_count / num_examples
                        print('  Training data | Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' %
                                (num_examples, true_count, precision)) 
                        summary = tf.Summary(value=[tf.Summary.Value(tag="accuracy_train", simple_value=precision)])
                        summary_writer.add_summary(summary, step) 

                    # Save model checkpoint
                    if step % 2000 == 0 or (step + 1) == FLAGS.max_steps:
                        checkpoint_path = os.path.join(FLAGS.log_dir, 'model.ckpt')
                        saver.save(sess, checkpoint_path, global_step=global_step)
            
            
            except tf.errors.OutOfRangeError:
                checkpoint_path = os.path.join(FLAGS.log_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=global_step)
                print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
            
            finally:
                # Finished
                print("\nWaiting for all threads...")
                coord.request_stop()
                coord.join(threads)
                print("Closing session...\n")
                sess.close()

    except:
        traceback.print_exc()

    finally:
        print("\nDone.\n")
Exemple #7
0
def main(argv=None):
    try:

        # Create log dir if not exists
        if tf.gfile.Exists(FLAGS.log_dir):
            tf.gfile.DeleteRecursively(FLAGS.log_dir)
        tf.gfile.MakeDirs(FLAGS.log_dir)

        # Tell TensorFlow that the model will be built into the default Graph.
        with tf.Graph().as_default():
            data_sets = data_input.read_evaluation_and_train_image_batches(
                FLAGS)
            train_data_set = data_sets.train
            evaluation_data_set = data_sets.evaluation

            images_placeholder, labels_placeholder = utils.create_placeholder_inputs(
                FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width)

            # Build a Graph that computes predictions from the inference model.
            # We use the same weight's etc. for the training and evaluation
            logits = model.inference(images_placeholder,
                                     train_data_set.batch_size,
                                     train_data_set.num_classes)

            # Accuracy
            correct = tf.nn.in_top_k(logits, labels_placeholder, 1)
            eval_correct = tf.reduce_sum(tf.cast(correct, tf.int32))

            # Add to the Graph the Ops for loss calculation.
            train_loss = _create_train_loss(logits, labels_placeholder)

            # Add to the Graph the Ops that calculate and apply gradients.
            global_step = tf.Variable(0, trainable=False)

            if (FLAGS.optimizer == 0):
                print("Using gradient descent optimizer.")
                train_op = _create_gradient_descent_train_op(
                    train_loss, global_step, train_data_set.size)
            else:
                print("Using adam optimizer.")
                train_op = _create_adam_train_op(train_loss, global_step)

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

            # Add tensorboard summaries
            tf.image_summary('image_train',
                             train_data_set.images,
                             max_images=5)
            tf.image_summary('image_evaluation',
                             evaluation_data_set.images,
                             max_images=5)

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

            # Add the variable initializer Op.
            init = tf.initialize_all_variables()

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

            # And then after everything is built:
            # Run the Op to initialize the variables.
            sess.run(init)

            # Start the queue runners.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            # Instantiate a SummaryWriter to output summaries and the Graph.
            summary_writer = tf.train.SummaryWriter(FLAGS.log_dir, sess.graph)

            #
            # Training loop
            #
            try:
                for step in xrange(FLAGS.max_steps):
                    if coord.should_stop():
                        break

                    start_time = time.time()

                    train_feed = utils.create_feed_data(
                        sess, images_placeholder, labels_placeholder,
                        train_data_set)
                    _, loss_value = sess.run([train_op, train_loss],
                                             feed_dict=train_feed)
                    assert not np.isnan(
                        loss_value), 'Model diverged with loss = NaN'

                    duration = time.time() - start_time

                    # Print step loss etc. to console
                    if step % 10 == 0:
                        steps_per_epoch = train_data_set.size // train_data_set.batch_size
                        epoch = int(step / steps_per_epoch)
                        num_examples_per_step = train_data_set.batch_size
                        examples_per_sec = num_examples_per_step / duration
                        sec_per_batch = float(duration)

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

                    # Write summary to tensorboard
                    if step % 100 == 0:
                        summary_str = sess.run([summary_op],
                                               feed_dict=train_feed)
                        summary_writer.add_summary(summary_str[0], step)

                    # Evaluation
                    if step % 1000 == 0:

                        if step == 0 and not FLAGS.initial_accuracy:
                            continue

                        num_examples, true_count = evaluation.do_eval(
                            sess, eval_correct, images_placeholder,
                            labels_placeholder, evaluation_data_set)
                        precision = true_count / num_examples
                        print(
                            '  Evaluation data | Num examples: %d  Num correct: %d  Precision @ 1: %0.04f'
                            % (num_examples, true_count, precision))

                        summary = tf.Summary(value=[
                            tf.Summary.Value(tag="accuracy_evaluation",
                                             simple_value=precision)
                        ])
                        summary_writer.add_summary(summary, step)

                        num_examples, true_count = evaluation.do_eval(
                            sess, eval_correct, images_placeholder,
                            labels_placeholder, train_data_set)
                        precision = true_count / num_examples
                        print(
                            '  Training data | Num examples: %d  Num correct: %d  Precision @ 1: %0.04f'
                            % (num_examples, true_count, precision))
                        summary = tf.Summary(value=[
                            tf.Summary.Value(tag="accuracy_train",
                                             simple_value=precision)
                        ])
                        summary_writer.add_summary(summary, step)

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

            except tf.errors.OutOfRangeError:
                checkpoint_path = os.path.join(FLAGS.log_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=global_step)
                print('Done training for %d epochs, %d steps.' %
                      (FLAGS.num_epochs, step))

            finally:
                # Finished
                print("\nWaiting for all threads...")
                coord.request_stop()
                coord.join(threads)
                print("Closing session...\n")
                sess.close()

    except:
        traceback.print_exc()

    finally:
        print("\nDone.\n")
Exemple #8
0
def main(argv=None):
    try:
        with tf.Graph().as_default():

            # Load all images from disk
            data_set = data_input.read_image_batches_without_labels_from_file_list(
                images_to_predict, FLAGS)

            # Inference model
            images_placeholder, labels_placeholder = utils.create_placeholder_inputs(
                data_set.batch_size, FLAGS.image_height, FLAGS.image_width)
            logits = model.inference(images_placeholder, data_set.batch_size,
                                     FLAGS.num_classes)

            # Max. value is our prediction
            prediction = tf.argmax(logits, 1)

            # Add the variable initializer Op.
            init = tf.initialize_all_variables()
            saver = tf.train.Saver()

            # Create a session for running Ops on the Graph.
            with tf.Session() as sess:
                sess.run(init)

                try:
                    # Start the queue runners.
                    coord = tf.train.Coordinator()
                    threads = tf.train.start_queue_runners(sess=sess,
                                                           coord=coord)

                    # Restore variables from disk.
                    print("Restore session from checkpoint {0}\n".format(
                        FLAGS.checkpoint))
                    saver.restore(sess, FLAGS.checkpoint)

                    # Predict
                    feed_dict = utils.create_feed_data(sess,
                                                       images_placeholder,
                                                       labels_placeholder,
                                                       data_set)
                    predictions, classes = sess.run([prediction, logits],
                                                    feed_dict=feed_dict)

                    # Print results
                    for i in range(0, data_set.size):
                        print("{0} top-1-class: {1}".format(
                            images_to_predict[i], predictions[i]))
                        print("Out = {0}\n".format(", ".join(
                            "{:0.2f}".format(i) for i in classes[i])))

                finally:
                    print("\nWaiting for all threads...")
                    coord.request_stop()
                    coord.join(threads)

    except:
        traceback.print_exc()

    finally:
        print("\nDone.\n")
Exemple #9
0
def _train_model():
    try:
        # Tell TensorFlow that the model will be built into the default Graph.
        with tf.Graph().as_default():
            # If a generation already exists, learn from their experience about the data
            if (FLAGS.generation > 0):
                fathers_experience = FLAGS.generation - 1
                experience_file = FLAGS.generation_experience_file.format(
                    fathers_experience)
                with open(experience_file, 'r') as file_handler:
                    invalid_images = [
                        line.rstrip('\n') for line in file_handler
                    ]
            else:
                invalid_images = []

            # Read social network images
            data_sets = data_input.read_validation_and_train_image_batches(
                FLAGS, FLAGS.train_dir, invalid_images)
            train_dataset = data_sets.train
            validation_dataset = data_sets.validation

            # Log images
            f = open(FLAGS.log_dir + "train_images.txt", "w")
            f.write("\n".join(train_dataset.image_list))
            f.close()

            f = open(FLAGS.log_dir + "validation_images.txt", "w")
            f.write("\n".join(validation_dataset.image_list))
            f.close()

            images_pl, labels_pl, dropout_pl = utils.create_placeholder_inputs(
                FLAGS.image_height, FLAGS.image_width, FLAGS.image_depth)

            # Build a Graph that computes predictions from the inference model.
            # We use the same weight's etc. for the training and validation
            logits = model.inference(images_pl, train_dataset.num_classes,
                                     FLAGS.image_depth, dropout_pl)

            # Add graph and placeholder to meta file
            tf.add_to_collection('logits', logits)

            # Prediction for accuracy, precision, recall and f1-score
            prediction = tf.argmax(logits, 1)

            # Add to the Graph the Ops for loss calculation.
            train_loss = _create_train_loss(logits,
                                            labels_pl)  # train_dataset.labels)

            # Add to the Graph the Ops that calculate and apply gradients.
            global_step = tf.Variable(0, trainable=False)

            # Initialize optimizer
            train_op = _create_adam_train_op(train_loss, global_step)

            # Create a saver for writing training checkpoints.
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=10)

            # Add tensorboard summaries
            tf.summary.image('image_train',
                             train_dataset.images,
                             max_outputs=5)
            tf.summary.image('image_validation',
                             validation_dataset.images,
                             max_outputs=5)

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

            # Add the variable initializer Op.
            init = tf.global_variables_initializer()

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

            # And then after everything is built:
            # Run the Op to initialize the variables.
            sess.run(init)

            # Start the queue runners.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            # Instantiate a SummaryWriter to output summaries and the Graph.
            summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph)

            # Ensure, that no nodes are added to our computation graph.
            # Otherwise the learning will slow down dramatically
            tf.get_default_graph().finalize()

            log_file = open(os.path.join(FLAGS.log_dir, 'console.log'), "w")

            #
            # Training loop
            #
            try:
                for step in range(FLAGS.max_steps):
                    if coord.should_stop():
                        break

                    start_time = time.time()

                    train_feed = utils.create_feed_data(
                        sess, images_pl, labels_pl, dropout_pl, train_dataset,
                        FLAGS.dropout_keep_prob)
                    _, loss_value = sess.run([train_op, train_loss],
                                             feed_dict=train_feed)
                    #_, loss_value = sess.run([train_op, train_loss])
                    assert not np.isnan(
                        loss_value), 'Model diverged with loss = NaN'

                    duration = time.time() - start_time

                    # Print step loss etc. to console
                    if (step % 10 == 0) or (step >= FLAGS.max_steps - 1):
                        steps_per_epoch = train_dataset.size // train_dataset.batch_size
                        epoch = int(step / steps_per_epoch) + 1
                        num_examples_per_step = train_dataset.batch_size
                        examples_per_sec = num_examples_per_step / duration
                        sec_per_batch = float(duration)

                        _log_line(
                            log_file,
                            '%s: step %d, epoch %d | loss = %.6f (%.1f examples/sec; %.3f '
                            'sec/batch)' %
                            (datetime.now(), step, epoch, loss_value,
                             examples_per_sec, sec_per_batch))

                    # Write summary to tensorboard
                    if (step % 100 == 0) or (step >= FLAGS.max_steps - 1):
                        log_file.flush()
                        summary_str = sess.run([summary_op],
                                               feed_dict=train_feed)
                        summary_writer.add_summary(summary_str[0], step)

                    # Evaluation
                    if (step % 4000 == 0) or (step >= FLAGS.max_steps - 1):
                        validate(log_file, sess, "Validation", images_pl,
                                 labels_pl, dropout_pl, validation_dataset,
                                 summary_writer, step, prediction)
                        validate(log_file, sess, "Training", images_pl,
                                 labels_pl, dropout_pl, train_dataset,
                                 summary_writer, step, prediction)

                    # Save model checkpoint
                    if (step % 4000 == 0) or (step >= FLAGS.max_steps - 1):
                        checkpoint_path = os.path.join(FLAGS.log_dir,
                                                       'model.ckpt')
                        saver.save(sess,
                                   checkpoint_path,
                                   global_step=global_step)

            except tf.errors.OutOfRangeError:
                checkpoint_path = os.path.join(FLAGS.log_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=global_step)
                print('Done training for %d epochs, %d steps.' %
                      (FLAGS.num_epochs, step))

            finally:
                # Finished
                log_file.close()
                print("\nWaiting for all threads...")
                coord.request_stop()
                coord.join(threads)
                print("Closing session...\n")
                sess.close()

    except:
        traceback.print_exc()

    finally:
        print("\nDone.\n")
Exemple #10
0
def validate(log_file, sess, dataset_name, images_pl, labels_pl, dropout_pl,
             dataset, summary_writer, train_step, prediction):
    """
    Measure different metrics, print a report and write the values to tensorboard
    """

    # Create test dataset
    y_true = []
    y_pred = []
    dropout_keep_propability = 1.0
    steps_per_epoch = dataset.size // dataset.batch_size
    for pred_step in range(steps_per_epoch):
        feed_dict = utils.create_feed_data(sess, images_pl, labels_pl,
                                           dropout_pl, dataset,
                                           dropout_keep_propability)
        y_true.extend(feed_dict[labels_pl])
        y_pred.extend(sess.run([prediction], feed_dict=feed_dict)[0])
        sys.stdout.write("  Calculating predictions for %s...%d%%\r" %
                         (dataset_name, pred_step * 100 / steps_per_epoch))
        sys.stdout.flush()
    sys.stdout.write("                                                 \r")
    sys.stdout.flush()

    # Rows ~ True Labels, Cols ~ Predicted labels_pl
    # http://scikit-learn.org/stable/modules/model_evaluation.html#confusion-matrix
    confusion_matrix = metrics.confusion_matrix(y_true, y_pred)
    _log_line(log_file, "\n# %s\n" % dataset_name)
    _log_line(log_file, "## Confusion Matrix")
    _log_line(log_file, confusion_matrix)

    # Accuracy
    _log_line(log_file, "\n## Summary")
    name = "Accuracy"
    val = metrics.accuracy_score(y_true, y_pred)
    log_metric(log_file, name, val, dataset_name, train_step, summary_writer)

    # Precision recall and fone score
    metric_results = metrics.precision_recall_fscore_support(y_true, y_pred)
    precision_per_class = metric_results[0]
    recall_per_class = metric_results[1]
    fone_per_class = metric_results[2]

    # Per class summary
    _log_line(
        log_file,
        "Precision per class: %s" % ["%.2f" % v for v in precision_per_class])
    _log_line(log_file,
              "Recall per class: %s" % ["%.2f" % v for v in recall_per_class])
    _log_line(log_file,
              "F1-Score per class: %s" % ["%.2f" % v for v in fone_per_class])

    # Precision
    name = "Precision"
    val = np.mean(precision_per_class)
    log_metric(log_file, name, val, dataset_name, train_step, summary_writer)

    # Precision
    name = "Recall"
    val = np.mean(recall_per_class)
    log_metric(log_file, name, val, dataset_name, train_step, summary_writer)

    # F1 Score
    name = "F1-Score"
    val = np.mean(fone_per_class)
    log_metric(log_file, name, val, dataset_name, train_step, summary_writer)
    _log_line(log_file, "\n")