coco_eval.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (c) 2024 PaddlePaddle 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. import argparse
  15. import os
  16. import sys
  17. from pycocotools.coco import COCO
  18. from pycocotools.cocoeval import COCOeval
  19. def parse_args():
  20. """Parse input arguments"""
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument("--prediction_json_path", type=str, default="./bbox.json")
  23. parser.add_argument("--gt_json_path", type=str, default="./instance_val.json")
  24. args = parser.parse_args()
  25. return args
  26. def json_eval_results(args):
  27. """
  28. cocoapi eval with already exists bbox.json
  29. """
  30. prediction_json_path = args.prediction_json_path
  31. gt_json_path = args.gt_json_path
  32. assert os.path.exists(
  33. prediction_json_path
  34. ), "The json directory:{} does not exist".format(prediction_json_path)
  35. cocoapi_eval(prediction_json_path, "bbox", anno_file=gt_json_path)
  36. def cocoapi_eval(
  37. jsonfile,
  38. style,
  39. coco_gt=None,
  40. anno_file=None,
  41. max_dets=(100, 300, 1000),
  42. sigmas=None,
  43. use_area=True,
  44. ):
  45. """
  46. Args:
  47. jsonfile (str): Evaluation json file, eg: bbox.json
  48. style (str): COCOeval style, can be `bbox`
  49. coco_gt (str): Whether to load COCOAPI through anno_file,
  50. eg: coco_gt = COCO(anno_file)
  51. anno_file (str): COCO annotations file.
  52. max_dets (tuple): COCO evaluation maxDets.
  53. sigmas (nparray): keypoint labelling sigmas.
  54. use_area (bool): If gt annotations (eg. CrowdPose, AIC)
  55. do not have 'area', please set use_area=False.
  56. """
  57. assert coco_gt is not None or anno_file is not None
  58. if coco_gt is None:
  59. coco_gt = COCO(anno_file)
  60. coco_dt = coco_gt.loadRes(jsonfile)
  61. if style == "proposal":
  62. coco_eval = COCOeval(coco_gt, coco_dt, "bbox")
  63. coco_eval.params.useCats = 0
  64. coco_eval.params.maxDets = list(max_dets)
  65. elif style == "keypoints_crowd":
  66. coco_eval = COCOeval(coco_gt, coco_dt, style, sigmas, use_area)
  67. else:
  68. coco_eval = COCOeval(coco_gt, coco_dt, style)
  69. coco_eval.evaluate()
  70. coco_eval.accumulate()
  71. coco_eval.summarize()
  72. # flush coco evaluation result
  73. sys.stdout.flush()
  74. return coco_eval.stats
  75. if __name__ == "__main__":
  76. args = parse_args()
  77. json_eval_results(args)