| 30 | import copy |
| 31 | |
| 32 | class COCOeval: |
| 33 | # Interface for evaluating detection on the Microsoft COCO dataset. |
| 34 | # |
| 35 | # The usage for CocoEval is as follows: |
| 36 | # cocoGt=..., cocoDt=... # load dataset and results |
| 37 | # E = CocoEval(cocoGt,cocoDt); # initialize CocoEval object |
| 38 | # E.params.recThrs = ...; # set parameters as desired |
| 39 | # E.evaluate(); # run per image evaluation |
| 40 | # E.accumulate(); # accumulate per image results |
| 41 | # E.summarize(); # display summary metrics of results |
| 42 | # For example usage see evalDemo.m and http://mscoco.org/. |
| 43 | # |
| 44 | # The evaluation parameters are as follows (defaults in brackets): |
| 45 | # imgIds - [all] N img ids to use for evaluation |
| 46 | # catIds - [all] K cat ids to use for evaluation |
| 47 | # iouThrs - [.5:.05:.95] T=10 IoU thresholds for evaluation |
| 48 | # recThrs - [0:.01:1] R=101 recall thresholds for evaluation |
| 49 | # areaRng - [...] A=4 object area ranges for evaluation |
| 50 | # maxDets - [1 10 100] M=3 thresholds on max detections per image |
| 51 | # iouType - ['segm'] set iouType to 'segm', 'bbox' or 'keypoints' |
| 52 | # iouType replaced the now DEPRECATED useSegm parameter. |
| 53 | # useCats - [1] if true use category labels for evaluation |
| 54 | # Note: if useCats=0 category labels are ignored as in proposal scoring. |
| 55 | # Note: multiple areaRngs [Ax2] and maxDets [Mx1] can be specified. |
| 56 | # |
| 57 | # evaluate(): evaluates detections on every image and every category and |
| 58 | # concats the results into the "evalImgs" with fields: |
| 59 | # dtIds - [1xD] id for each of the D detections (dt) |
| 60 | # gtIds - [1xG] id for each of the G ground truths (gt) |
| 61 | # dtMatches - [TxD] matching gt id at each IoU or 0 |
| 62 | # gtMatches - [TxG] matching dt id at each IoU or 0 |
| 63 | # dtScores - [1xD] confidence of each dt |
| 64 | # gtIgnore - [1xG] ignore flag for each gt |
| 65 | # dtIgnore - [TxD] ignore flag for each dt at each IoU |
| 66 | # |
| 67 | # accumulate(): accumulates the per-image, per-category evaluation |
| 68 | # results in "evalImgs" into the dictionary "eval" with fields: |
| 69 | # params - parameters used for evaluation |
| 70 | # date - date evaluation was performed |
| 71 | # counts - [T,R,K,A,M] parameter dimensions (see above) |
| 72 | # precision - [TxRxKxAxM] precision for every evaluation setting |
| 73 | # recall - [TxKxAxM] max recall for every evaluation setting |
| 74 | # Note: precision and recall==-1 for settings with no gt objects. |
| 75 | def __init__(self, cocoGt=None, cocoDt=None, iouType='bbox'): |
| 76 | ''' |
| 77 | Initialize CocoEval using coco APIs for gt and dt |
| 78 | :param cocoGt: coco object with ground truth annotations |
| 79 | :param cocoDt: coco object with detection results |
| 80 | :return: None |
| 81 | ''' |
| 82 | if not iouType: |
| 83 | print('iouType not specified. use default iouType segm') |
| 84 | self.cocoGt = cocoGt # ground truth COCO API |
| 85 | self.cocoDt = cocoDt # detections COCO API |
| 86 | self.evalImgs = defaultdict(list) # per-image per-category evaluation results [KxAxI] elements |
| 87 | self.eval = {} # accumulated evaluation results |
| 88 | self._gts = defaultdict(list) # gt for evaluation |
| 89 | self._dts = defaultdict(list) # dt for evaluation |