def evaluate(create_input_dict_fn, create_model_fn, eval_config, categories, checkpoint_dir, eval_dir, graph_hook_fn=None, evaluator_list=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. 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 = {} result_dict, result_losses_dict = sess.run( [tensor_dict, losses_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, result_losses_dict if graph_hook_fn: graph_hook_fn() variables_to_restore = tf.global_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) 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, eval_export_path=eval_config.export_path) return metrics
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_dicts = _extract_prediction_tensors( model=model, create_input_dict_fn=create_input_dict_fn, ignore_groundtruth=eval_config.ignore_groundtruth) def _process_batch(tensor_dicts, sess, batch_index, counters, losses_dict=None): """Evaluates tensors in tensor_dicts, visualizing the first K examples. This function calls sess.run on tensor_dicts, evaluating the original_image tensor only on the first K examples and visualizing detections overlaid on this original_image. Args: tensor_dicts: 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. Necessary only for matching function signiture in third_party eval_util.py. 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. Necessary only for matching function signiture in third_party eval_util.py. """ if batch_index % 10 == 0: logging.info('Running eval ops batch %d', batch_index) if not losses_dict: losses_dict = {} try: result_dicts, result_losses_dict = sess.run([tensor_dicts, losses_dict]) counters['success'] += 1 except tf.errors.InvalidArgumentError: logging.info('Skipping image') counters['skipped'] += 1 return {} num_images = len(tensor_dicts) for i in range(num_images): result_dict = result_dicts[i] global_step = tf.train.global_step(sess, tf.train.get_global_step()) tag = 'image-%d' % (batch_index * num_images + i) if batch_index < eval_config.num_visualizations / num_images: 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) if num_images > 1: return result_dicts, result_losses_dict else: return result_dicts[0], 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() for key in variables_to_restore.keys(): if 'moving_mean' in key: variables_to_restore[key.replace( 'moving_mean', 'moving_mean/ExponentialMovingAverage')] = ( variables_to_restore[key]) del variables_to_restore[key] if 'moving_variance' in key: variables_to_restore[key.replace( 'moving_variance', 'moving_variance/ExponentialMovingAverage')] = ( variables_to_restore[key]) del variables_to_restore[key] 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_dicts, 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
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 ''))
def evaluate(create_input_dict_fn, create_model_fn, eval_config, categories, checkpoint_dir, eval_dir, graph_hook_fn=None, evaluator_list=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. 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() #hotspot = 0 #not_hotspot = 0 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 = {} ''' testing ''' # # tensor_dict = [n.name for n in tf.get_default_graph().as_graph_def().node] # test_layer = tf.get_default_graph().get_tensor_by_name('SecondStageFeatureExtractor/InceptionV2/Mixed_5b/Branch_1/Conv2d_0a_1x1/Relu:0') # test_layer_dict = {"test_layer":test_layer} # feature_dict, result_dict, result_losses_dict = sess.run([test_layer, tensor_dict, losses_dict]) # # feature_dict = sess.run([test_layer, tensor_dict]) # import numpy as np # import scipy.misc # feature_tensor = feature_dict[0] # origin_image = result_dict['original_image'] # feature_map = np.sum(feature_tensor,axis=(2)) # # feature_map /= feature_map.max() # # feature_map = 1 - feature_map # # rgb_feature_map = np.zeros((256, 256, 3)) # # rgb_feature_map[..., 0] = feature_map * 30 # # rgb_feature_map[..., 1] = feature_map * 144 # # rgb_feature_map[..., 2] = feature_map * 255 # # rgb_feature_map[..., 0] = np.where(feature_map==0, 30, 255) # # rgb_feature_map[..., 1] = np.where(feature_map==0, 144, 255) # # rgb_feature_map[..., 2] = np.where(feature_map==0, 255, 255) # origin_image = origin_image[0] # # origin_image[..., 0] = np.where(origin_image[..., 0]==0, 30, 255) # # origin_image[..., 1] = np.where(origin_image[..., 1]==0, 144, 255) # # origin_image[..., 2] = np.where(origin_image[..., 2]==0, 255, 255) # scipy.misc.imsave('feature_map_{}.jpg'.format(counters['success']), feature_map) # # scipy.misc.imsave('origin_image_{}.jpg'.format(counters['success']), origin_image) # # from IPython import embed;embed() '''''' result_dict, result_losses_dict = sess.run( [tensor_dict, losses_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( eval_config.num_visualizations, 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, num_examples=eval_config.num_examples, keep_image_id_for_visualization_export=eval_config. keep_image_id_for_visualization_export) return result_dict, result_losses_dict if graph_hook_fn: graph_hook_fn() variables_to_restore = tf.global_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) 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) print("metrics:", metrics) return metrics