You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.8 KiB

6 years ago
  1. # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. r"""Runs evaluation using OpenImages groundtruth and predictions.
  16. Example usage:
  17. python models/research/object_detection/metrics/oid_od_challenge_evaluation.py \
  18. --input_annotations_boxes=/path/to/input/annotations-human-bbox.csv \
  19. --input_annotations_labels=/path/to/input/annotations-label.csv \
  20. --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \
  21. --input_predictions=/path/to/input/predictions.csv \
  22. --output_metrics=/path/to/output/metric.csv \
  23. CSVs with bounding box annotations and image label (including the image URLs)
  24. can be downloaded from the Open Images Challenge website:
  25. https://storage.googleapis.com/openimages/web/challenge.html
  26. The format of the input csv and the metrics itself are described on the
  27. challenge website.
  28. """
  29. from __future__ import absolute_import
  30. from __future__ import division
  31. from __future__ import print_function
  32. import argparse
  33. import pandas as pd
  34. from google.protobuf import text_format
  35. from object_detection.metrics import io_utils
  36. from object_detection.metrics import oid_od_challenge_evaluation_utils as utils
  37. from object_detection.protos import string_int_label_map_pb2
  38. from object_detection.utils import object_detection_evaluation
  39. def _load_labelmap(labelmap_path):
  40. """Loads labelmap from the labelmap path.
  41. Args:
  42. labelmap_path: Path to the labelmap.
  43. Returns:
  44. A dictionary mapping class name to class numerical id
  45. A list with dictionaries, one dictionary per category.
  46. """
  47. label_map = string_int_label_map_pb2.StringIntLabelMap()
  48. with open(labelmap_path, 'r') as fid:
  49. label_map_string = fid.read()
  50. text_format.Merge(label_map_string, label_map)
  51. labelmap_dict = {}
  52. categories = []
  53. for item in label_map.item:
  54. labelmap_dict[item.name] = item.id
  55. categories.append({'id': item.id, 'name': item.name})
  56. return labelmap_dict, categories
  57. def main(parsed_args):
  58. all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes)
  59. all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels)
  60. all_label_annotations.rename(
  61. columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True)
  62. all_annotations = pd.concat([all_box_annotations, all_label_annotations])
  63. class_label_map, categories = _load_labelmap(parsed_args.input_class_labelmap)
  64. challenge_evaluator = (
  65. object_detection_evaluation.OpenImagesDetectionChallengeEvaluator(
  66. categories))
  67. for _, groundtruth in enumerate(all_annotations.groupby('ImageID')):
  68. image_id, image_groundtruth = groundtruth
  69. groundtruth_dictionary = utils.build_groundtruth_boxes_dictionary(
  70. image_groundtruth, class_label_map)
  71. challenge_evaluator.add_single_ground_truth_image_info(
  72. image_id, groundtruth_dictionary)
  73. all_predictions = pd.read_csv(parsed_args.input_predictions)
  74. for _, prediction_data in enumerate(all_predictions.groupby('ImageID')):
  75. image_id, image_predictions = prediction_data
  76. prediction_dictionary = utils.build_predictions_dictionary(
  77. image_predictions, class_label_map)
  78. challenge_evaluator.add_single_detected_image_info(image_id,
  79. prediction_dictionary)
  80. metrics = challenge_evaluator.evaluate()
  81. with open(parsed_args.output_metrics, 'w') as fid:
  82. io_utils.write_csv(fid, metrics)
  83. if __name__ == '__main__':
  84. parser = argparse.ArgumentParser(
  85. description='Evaluate Open Images Object Detection Challenge predictions.'
  86. )
  87. parser.add_argument(
  88. '--input_annotations_boxes',
  89. required=True,
  90. help='File with groundtruth boxes annotations.')
  91. parser.add_argument(
  92. '--input_annotations_labels',
  93. required=True,
  94. help='File with groundtruth labels annotations')
  95. parser.add_argument(
  96. '--input_predictions',
  97. required=True,
  98. help="""File with detection predictions; NOTE: no postprocessing is
  99. applied in the evaluation script.""")
  100. parser.add_argument(
  101. '--input_class_labelmap',
  102. required=True,
  103. help='Open Images Challenge labelmap.')
  104. parser.add_argument(
  105. '--output_metrics', required=True, help='Output file with csv metrics')
  106. args = parser.parse_args()
  107. main(args)