Esempio n. 1
0
def evaluate(create_input_dict_fn,
             create_model_fn,
             eval_config,
             categories,
             checkpoint_dir,
             eval_dir,
             graph_hook_fn=None,
             evaluator_list=None,
             intra_op=0,
             inter_op=0):
    """Evaluation function for detection models.

    Args:
      create_input_dict_fn: a function to create a tensor input dictionary.
      create_model_fn: a function that creates a DetectionModel.
      eval_config: a eval_pb2.EvalConfig protobuf.
      categories: a list of category dictionaries. Each dict in the list should
                  have an integer 'id' field and string 'name' field.
      checkpoint_dir: directory to load the checkpoints to evaluate from.
      eval_dir: directory to write evaluation metrics summary to.
      graph_hook_fn: Optional function that is called after the training graph is
        completely built. This is helpful to perform additional changes to the
        training graph such as optimizing batchnorm. The function should modify
        the default graph.
      evaluator_list: Optional list of instances of DetectionEvaluator. If not
        given, this list of metrics is created according to the eval_config.

    Returns:
      metrics: A dictionary containing metric names and values from the latest
        run.
    """

    model = create_model_fn()

    if eval_config.ignore_groundtruth and not eval_config.export_path:
        logging.fatal('If ignore_groundtruth=True then an export_path is '
                      'required. Aborting!!!')

    tensor_dict, losses_dict = _extract_predictions_and_losses(
        model=model,
        create_input_dict_fn=create_input_dict_fn,
        ignore_groundtruth=eval_config.ignore_groundtruth)

    def _process_batch(tensor_dict,
                       sess,
                       batch_index,
                       counters,
                       losses_dict=None):
        """Evaluates tensors in tensor_dict, losses_dict and visualizes examples.

        This function calls sess.run on tensor_dict, evaluating the original_image
        tensor only on the first K examples and visualizing detections overlaid
        on this original_image.

        Args:
          tensor_dict: a dictionary of tensors
          sess: tensorflow session
          batch_index: the index of the batch amongst all batches in the run.
          counters: a dictionary holding 'success' and 'skipped' fields which can
            be updated to keep track of number of successful and failed runs,
            respectively.  If these fields are not updated, then the success/skipped
            counter values shown at the end of evaluation will be incorrect.
          losses_dict: Optional dictonary of scalar loss tensors.

        Returns:
          result_dict: a dictionary of numpy arrays
          result_losses_dict: a dictionary of scalar losses. This is empty if input
            losses_dict is None.
        """
        try:
            if not losses_dict:
                losses_dict = {}
            trace = False
            if batch_index == 0 and trace:
                run_options = tf.RunOptions(
                    trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()
            else:
                run_options = None
                run_metadata = None
            start_time = time.time()
            result_dict, result_losses_dict = sess.run(
                [tensor_dict, losses_dict],
                options=run_options,
                run_metadata=run_metadata)
            if (batch_index % 100 == 0):
                logging.info('Step %d: %.3f sec', batch_index,
                             time.time() - start_time)
            if batch_index == 0 and trace:
                trace = timeline.Timeline(step_stats=run_metadata.step_stats)
                dir = 'logs'
                if not os.path.exists(dir):
                    os.makedirs(dir)
                with open(
                        dir + '/rfcn-timeline-' +
                        time.strftime("%Y%m%d-%H%M%S") + '.json', 'w') as file:
                    file.write(
                        trace.generate_chrome_trace_format(show_memory=False))
            counters['success'] += 1
        except tf.errors.InvalidArgumentError:
            logging.info('Skipping image')
            counters['skipped'] += 1
            return {}, {}
        global_step = tf.train.global_step(sess, tf.train.get_global_step())
        if batch_index < eval_config.num_visualizations:
            tag = 'image-{}'.format(batch_index)
            eval_util.visualize_detection_results(
                result_dict,
                tag,
                global_step,
                categories=categories,
                summary_dir=eval_dir,
                export_dir=eval_config.visualization_export_dir,
                show_groundtruth=eval_config.visualize_groundtruth_boxes,
                groundtruth_box_visualization_color=eval_config.
                groundtruth_box_visualization_color,
                min_score_thresh=eval_config.min_score_threshold,
                max_num_predictions=eval_config.max_num_boxes_to_visualize,
                skip_scores=eval_config.skip_scores,
                skip_labels=eval_config.skip_labels,
                keep_image_id_for_visualization_export=eval_config.
                keep_image_id_for_visualization_export)
        return result_dict, result_losses_dict

    variables_to_restore = tf.global_variables()
    global_step = tf.train.get_or_create_global_step()
    variables_to_restore.append(global_step)

    if graph_hook_fn:
        graph_hook_fn()

    if eval_config.use_moving_averages:
        variable_averages = tf.train.ExponentialMovingAverage(0.0)
        variables_to_restore = variable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)

    def _restore_latest_checkpoint(sess):
        latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
        saver.restore(sess, latest_checkpoint)

    if not evaluator_list:
        evaluator_list = get_evaluators(eval_config, categories)

    metrics = eval_util.repeated_checkpoint_run(
        tensor_dict=tensor_dict,
        summary_dir=eval_dir,
        evaluators=evaluator_list,
        batch_processor=_process_batch,
        checkpoint_dirs=[checkpoint_dir],
        variables_to_restore=None,
        restore_fn=_restore_latest_checkpoint,
        num_batches=eval_config.num_examples,
        eval_interval_secs=eval_config.eval_interval_secs,
        max_number_of_evaluations=(
            1 if eval_config.ignore_groundtruth else
            eval_config.max_evals if eval_config.max_evals else None),
        master=eval_config.eval_master,
        save_graph=eval_config.save_graph,
        save_graph_dir=(eval_dir if eval_config.save_graph else ''),
        losses_dict=losses_dict)

    return metrics
Esempio n. 2
0
def evaluate(create_input_dict_fn, create_model_fn, eval_config, categories,
             checkpoint_dir, eval_dir):
    """Evaluation function for detection models.

  Args:
    create_input_dict_fn: a function to create a tensor input dictionary.
    create_model_fn: a function that creates a DetectionModel.
    eval_config: a eval_pb2.EvalConfig protobuf.
    categories: a list of category dictionaries. Each dict in the list should
                have an integer 'id' field and string 'name' field.
    checkpoint_dir: directory to load the checkpoints to evaluate from.
    eval_dir: directory to write evaluation metrics summary to.
  """

    model = create_model_fn()

    if eval_config.ignore_groundtruth and not eval_config.export_path:
        logging.fatal('If ignore_groundtruth=True then an export_path is '
                      'required. Aborting!!!')

    tensor_dict = _extract_prediction_tensors(
        model=model,
        create_input_dict_fn=create_input_dict_fn,
        ignore_groundtruth=eval_config.ignore_groundtruth)

    def _process_batch(tensor_dict, sess, batch_index, counters, update_op):
        """Evaluates tensors in tensor_dict, visualizing the first K examples.

    This function calls sess.run on tensor_dict, evaluating the original_image
    tensor only on the first K examples and visualizing detections overlaid
    on this original_image.

    Args:
      tensor_dict: a dictionary of tensors
      sess: tensorflow session
      batch_index: the index of the batch amongst all batches in the run.
      counters: a dictionary holding 'success' and 'skipped' fields which can
        be updated to keep track of number of successful and failed runs,
        respectively.  If these fields are not updated, then the success/skipped
        counter values shown at the end of evaluation will be incorrect.
      update_op: An update op that has to be run along with output tensors. For
        example this could be an op to compute statistics for slim metrics.

    Returns:
      result_dict: a dictionary of numpy arrays
    """
        if batch_index >= eval_config.num_visualizations:
            if 'original_image' in tensor_dict:
                tensor_dict = {
                    k: v
                    for (k, v) in tensor_dict.items() if k != 'original_image'
                }
        try:
            (result_dict, _) = sess.run([tensor_dict, update_op])
            counters['success'] += 1
        except tf.errors.InvalidArgumentError:
            logging.info('Skipping image')
            counters['skipped'] += 1
            return {}
        global_step = tf.train.global_step(sess, slim.get_global_step())
        if batch_index < eval_config.num_visualizations:
            tag = 'image-{}'.format(batch_index)
            eval_util.visualize_detection_results(
                result_dict,
                tag,
                global_step,
                categories=categories,
                summary_dir=eval_dir,
                export_dir=eval_config.visualization_export_dir,
                show_groundtruth=eval_config.visualization_export_dir)
        return result_dict

    def _process_aggregated_results(result_lists):
        eval_metric_fn_key = eval_config.metrics_set
        if eval_metric_fn_key not in EVAL_METRICS_FN_DICT:
            raise ValueError('Metric not found: {}'.format(eval_metric_fn_key))
        return EVAL_METRICS_FN_DICT[eval_metric_fn_key](result_lists,
                                                        categories=categories)

    variables_to_restore = tf.global_variables()
    global_step = slim.get_or_create_global_step()
    variables_to_restore.append(global_step)
    if eval_config.use_moving_averages:
        variable_averages = tf.train.ExponentialMovingAverage(0.0)
        variables_to_restore = variable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)

    def _restore_latest_checkpoint(sess):
        latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
        saver.restore(sess, latest_checkpoint)

    eval_util.repeated_checkpoint_run(
        tensor_dict=tensor_dict,
        update_op=tf.no_op(),
        summary_dir=eval_dir,
        aggregated_result_processor=_process_aggregated_results,
        batch_processor=_process_batch,
        checkpoint_dirs=[checkpoint_dir],
        variables_to_restore=None,
        restore_fn=_restore_latest_checkpoint,
        num_batches=eval_config.num_examples,
        eval_interval_secs=eval_config.eval_interval_secs,
        max_number_of_evaluations=(
            1 if eval_config.ignore_groundtruth else
            eval_config.max_evals if eval_config.max_evals else None),
        master=eval_config.eval_master,
        save_graph=eval_config.save_graph,
        save_graph_dir=(eval_dir if eval_config.save_graph else ''))
Esempio n. 3
0
def evaluate(create_input_dict_fn, create_model_fn, eval_config, input_config, categories,
             checkpoint_dir, eval_dir, run_mode='latest', is_save_detection_results=False, detection_results_name=''):
    """Evaluation function for detection models.

    Args:
      create_input_dict_fn: a function to create a tensor input dictionary.
      create_model_fn: a function that creates a DetectionModel.
      eval_config: a eval_pb2.EvalConfig protobuf.
      input_config: a input_reader.InputReader protobuf.
      categories: a list of category dictionaries. Each dict in the list should
                  have an integer 'id' field and string 'name' field.
      checkpoint_dir: directory to load the checkpoints to evaluate from.
      eval_dir: directory to write evaluation metrics summary to.
      run_mode: when run_mode is latest, it run infinite and the latest checkpoint is evaluated.
                when run_mode is all, all checkpoints are evaluated and finish evaluation)
      is_save_detection_results: whether or not to save detection results.
      detection_results_name: a filename to a detection_results pickle file.
    """

    model = create_model_fn()

    if eval_config.ignore_groundtruth and not eval_config.export_path:
        logging.fatal('If ignore_groundtruth=True then an export_path is required. Aborting!!!')

    preprocess_input_options = [preprocessor_input_builder.build(step)
                                for step in input_config.preprocess_input_options]

    tensor_dict = _extract_prediction_tensors(model=model,
                                              create_input_dict_fn=create_input_dict_fn,
                                              ignore_groundtruth=eval_config.ignore_groundtruth,
                                              preprocess_input_options=preprocess_input_options)

    def _process_batch(tensor_dict, sess, batch_index, counters, update_op):
        """Evaluates tensors in tensor_dict, visualizing the first K examples.

        This function calls sess.run on tensor_dict, evaluating the original_image
        tensor only on the first K examples and visualizing detections overlaid
        on this original_image.

        Args:
          tensor_dict: a dictionary of tensors
          sess: tensorflow session
          batch_index: the index of the batch amongst all batches in the run.
          counters: a dictionary holding 'success' and 'skipped' fields which can
            be updated to keep track of number of successful and failed runs,
            respectively.  If these fields are not updated, then the success/skipped
            counter values shown at the end of evaluation will be incorrect.
          update_op: An update op that has to be run along with output tensors. For
            example this could be an op to compute statistics for slim metrics.

        Returns:
          result_dict: a dictionary of numpy arrays
        """
        if batch_index >= eval_config.num_visualizations and not is_save_detection_results:
            if 'original_image' in tensor_dict:
                tensor_dict = {k: v for (k, v) in tensor_dict.items()
                               if k != 'original_image'}
        try:
            (result_dict, _) = sess.run([tensor_dict, update_op])
            counters['success'] += 1
        except tf.errors.InvalidArgumentError:
            logging.info('Skipping image')
            counters['skipped'] += 1
            return {}
        global_step = tf.train.global_step(sess, slim.get_global_step())
        if batch_index < eval_config.num_visualizations:
            tag = 'image-{}'.format(batch_index)
            eval_util.visualize_detection_results(
                result_dict, tag, global_step, categories=categories,
                summary_dir=eval_dir,
                export_dir=eval_config.visualization_export_dir)
        return result_dict

    def _process_aggregated_results(result_lists):
        eval_metric_fn_key = eval_config.metrics_set
        if eval_metric_fn_key not in EVAL_METRICS_FN_DICT:
            raise ValueError('Metric not found: {}'.format(eval_metric_fn_key))
        return EVAL_METRICS_FN_DICT[eval_metric_fn_key](result_lists,
                                                        categories=categories,
                                                        box_type='rbox' if model.is_rbbox else 'box',
                                                        is_save_detection_results=is_save_detection_results,
                                                        detection_results_name=detection_results_name)

    variables_to_restore = tf.global_variables()
    global_step = slim.get_or_create_global_step()
    variables_to_restore.append(global_step)
    if eval_config.use_moving_averages:
        variable_averages = tf.train.ExponentialMovingAverage(0.0)
        variables_to_restore = variable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)

    def _restore_latest_checkpoint(sess):
        latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
        saver.restore(sess, latest_checkpoint)

    class _Restorer(object):
        def __init__(self, checkpoint_dir, run_mode):
            ckpt_list = glob(checkpoint_dir + '/*.index')
            ckpt_list = [ckpt[:-6] for ckpt in ckpt_list]
            sort_nicely(ckpt_list)

            if run_mode == 'latest_once':
                self.ckpt_list = ckpt_list[-1:]
            else:  # run_mode == 'all':
                self.ckpt_list = ckpt_list[1:]

            self.n_ckpt = len(self.ckpt_list)
            self.cnt = 0

        def __call__(self, sess):
            saver.restore(sess, self.ckpt_list[self.cnt])
            self.cnt += 1

        def all_checked(self):
            if self.cnt == self.n_ckpt:
                return True
            else:
                return False

    if run_mode == 'all' or run_mode == 'latest_once':
        eval_util.all_checkpoint_run(
            tensor_dict=tensor_dict,
            update_op=tf.no_op(),
            summary_dir=eval_dir,
            aggregated_result_processor=_process_aggregated_results,
            batch_processor=_process_batch,
            checkpoint_dirs=[checkpoint_dir],
            variables_to_restore=None,
            restorer=_Restorer(checkpoint_dir, run_mode),
            num_batches=eval_config.num_examples,
            master=eval_config.eval_master,
            save_graph=eval_config.save_graph,
            save_graph_dir=(eval_dir if eval_config.save_graph else ''))
    else:  # run_mode == 'latest'
        eval_util.repeated_checkpoint_run(
            tensor_dict=tensor_dict,
            update_op=tf.no_op(),
            summary_dir=eval_dir,
            aggregated_result_processor=_process_aggregated_results,
            batch_processor=_process_batch,
            checkpoint_dirs=[checkpoint_dir],
            variables_to_restore=None,
            restore_fn=_restore_latest_checkpoint,
            num_batches=eval_config.num_examples,
            eval_interval_secs=eval_config.eval_interval_secs,
            max_number_of_evaluations=(
                1 if eval_config.ignore_groundtruth else
                eval_config.max_evals if eval_config.max_evals else
                None),
            master=eval_config.eval_master,
            save_graph=eval_config.save_graph,
            save_graph_dir=(eval_dir if eval_config.save_graph else ''))
Esempio n. 4
0
def evaluate(create_input_dict_fn, create_model_fn, eval_config, categories,
             checkpoint_dir, eval_dir, graph_hook_fn=None):
  """Evaluation function for detection models.

  Args:
    create_input_dict_fn: a function to create a tensor input dictionary.
    create_model_fn: a function that creates a DetectionModel.
    eval_config: a eval_pb2.EvalConfig protobuf.
    categories: a list of category dictionaries. Each dict in the list should
                have an integer 'id' field and string 'name' field.
    checkpoint_dir: directory to load the checkpoints to evaluate from.
    eval_dir: directory to write evaluation metrics summary to.
    graph_hook_fn: Optional function that is called after the training graph is
      completely built. This is helpful to perform additional changes to the
      training graph such as optimizing batchnorm. The function should modify
      the default graph.

  Returns:
    metrics: A dictionary containing metric names and values from the latest
      run.
  """

  model = create_model_fn()

  if eval_config.ignore_groundtruth and not eval_config.export_path:
    logging.fatal('If ignore_groundtruth=True then an export_path is '
                  'required. Aborting!!!')

  tensor_dict = _extract_prediction_tensors(
      model=model,
      create_input_dict_fn=create_input_dict_fn,
      ignore_groundtruth=eval_config.ignore_groundtruth)

  def _process_batch(tensor_dict, sess, batch_index, counters):
    """Evaluates tensors in tensor_dict, visualizing the first K examples.

    This function calls sess.run on tensor_dict, evaluating the original_image
    tensor only on the first K examples and visualizing detections overlaid
    on this original_image.

    Args:
      tensor_dict: a dictionary of tensors
      sess: tensorflow session
      batch_index: the index of the batch amongst all batches in the run.
      counters: a dictionary holding 'success' and 'skipped' fields which can
        be updated to keep track of number of successful and failed runs,
        respectively.  If these fields are not updated, then the success/skipped
        counter values shown at the end of evaluation will be incorrect.

    Returns:
      result_dict: a dictionary of numpy arrays
    """
    try:
      result_dict = sess.run(tensor_dict)
      counters['success'] += 1
    except tf.errors.InvalidArgumentError:
      logging.info('Skipping image')
      counters['skipped'] += 1
      return {}
    global_step = tf.train.global_step(sess, tf.train.get_global_step())
    if batch_index < eval_config.num_visualizations:
      tag = 'image-{}'.format(batch_index)
      eval_util.visualize_detection_results(
          result_dict,
          tag,
          global_step,
          categories=categories,
          summary_dir=eval_dir,
          export_dir=eval_config.visualization_export_dir,
          show_groundtruth=eval_config.visualize_groundtruth_boxes,
          groundtruth_box_visualization_color=eval_config.
          groundtruth_box_visualization_color,
          min_score_thresh=eval_config.min_score_threshold,
          max_num_predictions=eval_config.max_num_boxes_to_visualize,
          skip_scores=eval_config.skip_scores,
          skip_labels=eval_config.skip_labels,
          keep_image_id_for_visualization_export=eval_config.
          keep_image_id_for_visualization_export)
    return result_dict

  variables_to_restore = tf.global_variables()
  global_step = tf.train.get_or_create_global_step()
  variables_to_restore.append(global_step)

  if graph_hook_fn: graph_hook_fn()

  if eval_config.use_moving_averages:
    variable_averages = tf.train.ExponentialMovingAverage(0.0)
    variables_to_restore = variable_averages.variables_to_restore()
  saver = tf.train.Saver(variables_to_restore)

  def _restore_latest_checkpoint(sess):
    latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
    saver.restore(sess, latest_checkpoint)

  metrics = eval_util.repeated_checkpoint_run(
      tensor_dict=tensor_dict,
      summary_dir=eval_dir,
      evaluators=get_evaluators(eval_config, categories),
      batch_processor=_process_batch,
      checkpoint_dirs=[checkpoint_dir],
      variables_to_restore=None,
      restore_fn=_restore_latest_checkpoint,
      num_batches=eval_config.num_examples,
      eval_interval_secs=eval_config.eval_interval_secs,
      max_number_of_evaluations=(1 if eval_config.ignore_groundtruth else
                                 eval_config.max_evals
                                 if eval_config.max_evals else None),
      master=eval_config.eval_master,
      save_graph=eval_config.save_graph,
      save_graph_dir=(eval_dir if eval_config.save_graph else ''))

  return metrics
Esempio n. 5
0
def evaluate(create_input_dict_fn, create_model_fn, eval_config,
             checkpoint_dir, eval_dir,
             repeat_evaluation=True):
  model = create_model_fn()
  data_preprocessing_steps = [
      preprocessor_builder.build(step)
      for step in eval_config.data_preprocessing_steps]

  tensor_dict = _extract_prediction_tensors(
      model=model,
      create_input_dict_fn=create_input_dict_fn,
      data_preprocessing_steps=data_preprocessing_steps,
      ignore_groundtruth=eval_config.ignore_groundtruth,
      evaluate_with_lexicon=eval_config.eval_with_lexicon)

  summary_writer = tf.summary.FileWriter(eval_dir)

  def _process_batch(tensor_dict, sess, batch_index, counters, update_op):
    if batch_index >= eval_config.num_visualizations:
      if 'original_image' in tensor_dict:
        tensor_dict = {k: v for (k, v) in tensor_dict.items()
                       if k != 'original_image'}
    try:
      (result_dict, _, glyphs) = sess.run([tensor_dict, update_op, tf.get_collection('glyph')])
      counters['success'] += 1
    except tf.errors.InvalidArgumentError:
      logging.info('Skipping image')
      counters['skipped'] += 1
      return {}
    global_step = tf.train.global_step(sess, tf.train.get_global_step())
    if batch_index < eval_config.num_visualizations:
      eval_util.visualize_recognition_results(
          result_dict,
          'Recognition_{}'.format(batch_index),
          global_step,
          summary_dir=eval_dir,
          export_dir=os.path.join(eval_dir, 'vis'),
          summary_writer=summary_writer,
          only_visualize_incorrect=eval_config.only_visualize_incorrect)

    return result_dict

  def _process_aggregated_results(result_lists):
    eval_metric_fn_key = eval_config.metrics_set
    if eval_metric_fn_key not in EVAL_METRICS_FN_DICT:
      raise ValueError('Metric not found: {}'.format(eval_metric_fn_key))
    return EVAL_METRICS_FN_DICT[eval_metric_fn_key](result_lists)

  variables_to_restore = tf.global_variables()
  #variables_to_restore = tf.trainable_variables()
  global_step = tf.train.get_or_create_global_step()
  variables_to_restore.append(global_step)
  if eval_config.use_moving_averages:
    variable_averages = tf.train.ExponentialMovingAverage(0.0)
    variables_to_restore = variable_averages.variables_to_restore()
  saver = tf.train.Saver(variables_to_restore)
  def _restore_latest_checkpoint(sess):
    latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
    saver.restore(sess, latest_checkpoint)

  eval_util.repeated_checkpoint_run(
      tensor_dict=tensor_dict,
      update_op=tf.no_op(),
      summary_dir=eval_dir,
      aggregated_result_processor=_process_aggregated_results,
      batch_processor=_process_batch,
      checkpoint_dirs=[checkpoint_dir],
      variables_to_restore=None,
      restore_fn=_restore_latest_checkpoint,
      num_batches=eval_config.num_examples,
      eval_interval_secs=eval_config.eval_interval_secs,
      max_number_of_evaluations=(
          1 if eval_config.ignore_groundtruth else
          eval_config.max_evals if eval_config.max_evals else
          None if repeat_evaluation else 1),
      master=eval_config.eval_master,
      save_graph=eval_config.save_graph,
      save_graph_dir=(eval_dir if eval_config.save_graph else ''))

  summary_writer.close()