coco_eval.py 2.9 KB

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