Exemplo n.º 1
0
def evaluate(experiment_name, step=''):
    logging.info('*' * 50)
    logging.info('RUNNING EVALUATION FOR MODEL: %s', experiment_name)
    if step == '':
        interesting_checkpoint = tf.train.latest_checkpoint(
            os.path.join(CURRENT_DIR, '..', 'checkpoint', experiment_name))
    else:
        interesting_checkpoint = os.path.join(CURRENT_DIR, '..', 'checkpoint',
                                              experiment_name,
                                              'step-{}'.format(step))
    dataset_manager = DatasetManager()
    dataset_manager.boot()

    with tf.Graph().as_default() as gr:
        logging.info('-- Restoring graph for model: %s',
                     interesting_checkpoint)
        saver = tf.train.import_meta_graph(
            '{}.meta'.format(interesting_checkpoint))
        logging.info('-- Restored graph for model named: %s',
                     interesting_checkpoint)

        with tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True)).as_default() as sess:
            saver.restore(sess=sess, save_path=interesting_checkpoint)
            logging.info('-- Restored variables for model named: %s',
                         interesting_checkpoint)
            list_predictions = []
            list_labels = []
            for docs, labels in dataset_manager.get_test_by_batch(
                    batch_size=FLAGS.BATCH_SIZE):
                tf_input = gr.get_tensor_by_name('input/tf_input:0')
                tf_predictions = gr.get_tensor_by_name('prediction:0')

                prediction = sess.run(tf_predictions,
                                      feed_dict={tf_input: docs})
                list_predictions.extend(prediction)
                list_labels.extend(labels)
                logging.debug('-- Prediction length: %s/%s',
                              len(list_predictions),
                              dataset_manager.test_y.shape[0])
            logging.info('-- Report for model: %s', experiment_name)
            logging.info(
                classification_report(y_true=list_labels,
                                      y_pred=list_predictions,
                                      digits=4))
            logging.info(
                confusion_matrix(y_true=list_labels, y_pred=list_predictions))