coco_eval.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import json
  2. import tempfile
  3. import numpy as np
  4. import copy
  5. import time
  6. import torch
  7. import torch._six
  8. from pycocotools.cocoeval import COCOeval
  9. from pycocotools.coco import COCO
  10. import pycocotools.mask as mask_util
  11. from collections import defaultdict
  12. import utils
  13. # ### OUR CODE ###
  14. # let's import our tensorboard module to track metrics
  15. import tensorboard
  16. # ### END OF OUR CODE ###
  17. class CocoEvaluator(object):
  18. def __init__(self, coco_gt, iou_types):
  19. assert isinstance(iou_types, (list, tuple))
  20. coco_gt = copy.deepcopy(coco_gt)
  21. self.coco_gt = coco_gt
  22. self.iou_types = iou_types
  23. self.coco_eval = {}
  24. for iou_type in iou_types:
  25. self.coco_eval[iou_type] = COCOeval(coco_gt, iouType=iou_type)
  26. self.img_ids = []
  27. self.eval_imgs = {k: [] for k in iou_types}
  28. def update(self, predictions):
  29. img_ids = list(np.unique(list(predictions.keys())))
  30. self.img_ids.extend(img_ids)
  31. for iou_type in self.iou_types:
  32. results = self.prepare(predictions, iou_type)
  33. coco_dt = loadRes(self.coco_gt, results) if results else COCO()
  34. coco_eval = self.coco_eval[iou_type]
  35. coco_eval.cocoDt = coco_dt
  36. coco_eval.params.imgIds = list(img_ids)
  37. img_ids, eval_imgs = evaluate(coco_eval)
  38. self.eval_imgs[iou_type].append(eval_imgs)
  39. def synchronize_between_processes(self):
  40. for iou_type in self.iou_types:
  41. self.eval_imgs[iou_type] = np.concatenate(self.eval_imgs[iou_type], 2)
  42. create_common_coco_eval(self.coco_eval[iou_type], self.img_ids, self.eval_imgs[iou_type])
  43. def accumulate(self):
  44. for coco_eval in self.coco_eval.values():
  45. coco_eval.accumulate()
  46. def summarize(self):
  47. for iou_type, coco_eval in self.coco_eval.items():
  48. print("IoU metric: {}".format(iou_type))
  49. coco_eval.summarize()
  50. # ### OUR CODE ###
  51. if iou_type == "bbox":
  52. # let's add hyperparameters and bind them to metric values
  53. tensorboard.logger.add_hparams(
  54. # passing hyperparameters dictionary (in our case argparse values)
  55. tensorboard.args,
  56. # passing COCO metrics
  57. {
  58. "AP/IoU/0.50-0.95/all/100": coco_eval.stats[0],
  59. "AP/IoU/0.50/all/100": coco_eval.stats[1],
  60. "AP/IoU/0.75/all/100": coco_eval.stats[2],
  61. "AP/IoU/0.50-0.95/small/100": coco_eval.stats[3],
  62. "AP/IoU/0.50-0.95/medium/100": coco_eval.stats[4],
  63. "AP/IoU/0.50-0.95/large/100": coco_eval.stats[5],
  64. "AR/IoU/0.50-0.95/all/1": coco_eval.stats[6],
  65. "AR/IoU/0.50-0.95/all/10": coco_eval.stats[7],
  66. "AR/IoU/0.50-0.95/all/100": coco_eval.stats[8],
  67. "AR/IoU/0.50-0.95/small/100": coco_eval.stats[9],
  68. "AR/IoU/0.50-0.95/medium/100": coco_eval.stats[10],
  69. "AR/IoU/0.50-0.95/large/100": coco_eval.stats[11],
  70. },
  71. name=".",
  72. # passing the current iteration (epoch)
  73. global_step=tensorboard.total_epochs,
  74. )
  75. # incrementing the number of epochs
  76. tensorboard.total_epochs += 1
  77. # ### END OF OUR CODE ###
  78. def prepare(self, predictions, iou_type):
  79. if iou_type == "bbox":
  80. return self.prepare_for_coco_detection(predictions)
  81. elif iou_type == "segm":
  82. return self.prepare_for_coco_segmentation(predictions)
  83. elif iou_type == "keypoints":
  84. return self.prepare_for_coco_keypoint(predictions)
  85. else:
  86. raise ValueError("Unknown iou type {}".format(iou_type))
  87. def prepare_for_coco_detection(self, predictions):
  88. coco_results = []
  89. for original_id, prediction in predictions.items():
  90. if len(prediction) == 0:
  91. continue
  92. boxes = prediction["boxes"]
  93. boxes = convert_to_xywh(boxes).tolist()
  94. scores = prediction["scores"].tolist()
  95. labels = prediction["labels"].tolist()
  96. coco_results.extend(
  97. [
  98. {
  99. "image_id": original_id,
  100. "category_id": labels[k],
  101. "bbox": box,
  102. "score": scores[k],
  103. }
  104. for k, box in enumerate(boxes)
  105. ]
  106. )
  107. return coco_results
  108. def prepare_for_coco_segmentation(self, predictions):
  109. coco_results = []
  110. for original_id, prediction in predictions.items():
  111. if len(prediction) == 0:
  112. continue
  113. scores = prediction["scores"]
  114. labels = prediction["labels"]
  115. masks = prediction["masks"]
  116. masks = masks > 0.5
  117. scores = prediction["scores"].tolist()
  118. labels = prediction["labels"].tolist()
  119. rles = [
  120. mask_util.encode(np.array(mask[0, :, :, np.newaxis], dtype=np.uint8, order="F"))[0]
  121. for mask in masks
  122. ]
  123. for rle in rles:
  124. rle["counts"] = rle["counts"].decode("utf-8")
  125. coco_results.extend(
  126. [
  127. {
  128. "image_id": original_id,
  129. "category_id": labels[k],
  130. "segmentation": rle,
  131. "score": scores[k],
  132. }
  133. for k, rle in enumerate(rles)
  134. ]
  135. )
  136. return coco_results
  137. def prepare_for_coco_keypoint(self, predictions):
  138. coco_results = []
  139. for original_id, prediction in predictions.items():
  140. if len(prediction) == 0:
  141. continue
  142. boxes = prediction["boxes"]
  143. boxes = convert_to_xywh(boxes).tolist()
  144. scores = prediction["scores"].tolist()
  145. labels = prediction["labels"].tolist()
  146. keypoints = prediction["keypoints"]
  147. keypoints = keypoints.flatten(start_dim=1).tolist()
  148. coco_results.extend(
  149. [
  150. {
  151. "image_id": original_id,
  152. "category_id": labels[k],
  153. 'keypoints': keypoint,
  154. "score": scores[k],
  155. }
  156. for k, keypoint in enumerate(keypoints)
  157. ]
  158. )
  159. return coco_results
  160. def convert_to_xywh(boxes):
  161. xmin, ymin, xmax, ymax = boxes.unbind(1)
  162. return torch.stack((xmin, ymin, xmax - xmin, ymax - ymin), dim=1)
  163. def merge(img_ids, eval_imgs):
  164. all_img_ids = utils.all_gather(img_ids)
  165. all_eval_imgs = utils.all_gather(eval_imgs)
  166. merged_img_ids = []
  167. for p in all_img_ids:
  168. merged_img_ids.extend(p)
  169. merged_eval_imgs = []
  170. for p in all_eval_imgs:
  171. merged_eval_imgs.append(p)
  172. merged_img_ids = np.array(merged_img_ids)
  173. merged_eval_imgs = np.concatenate(merged_eval_imgs, 2)
  174. # keep only unique (and in sorted order) images
  175. merged_img_ids, idx = np.unique(merged_img_ids, return_index=True)
  176. merged_eval_imgs = merged_eval_imgs[..., idx]
  177. return merged_img_ids, merged_eval_imgs
  178. def create_common_coco_eval(coco_eval, img_ids, eval_imgs):
  179. img_ids, eval_imgs = merge(img_ids, eval_imgs)
  180. img_ids = list(img_ids)
  181. eval_imgs = list(eval_imgs.flatten())
  182. coco_eval.evalImgs = eval_imgs
  183. coco_eval.params.imgIds = img_ids
  184. coco_eval._paramsEval = copy.deepcopy(coco_eval.params)
  185. #################################################################
  186. # From pycocotools, just removed the prints and fixed
  187. # a Python3 bug about unicode not defined
  188. #################################################################
  189. # Ideally, pycocotools wouldn't have hard-coded prints
  190. # so that we could avoid copy-pasting those two functions
  191. def createIndex(self):
  192. # create index
  193. # print('creating index...')
  194. anns, cats, imgs = {}, {}, {}
  195. imgToAnns, catToImgs = defaultdict(list), defaultdict(list)
  196. if 'annotations' in self.dataset:
  197. for ann in self.dataset['annotations']:
  198. imgToAnns[ann['image_id']].append(ann)
  199. anns[ann['id']] = ann
  200. if 'images' in self.dataset:
  201. for img in self.dataset['images']:
  202. imgs[img['id']] = img
  203. if 'categories' in self.dataset:
  204. for cat in self.dataset['categories']:
  205. cats[cat['id']] = cat
  206. if 'annotations' in self.dataset and 'categories' in self.dataset:
  207. for ann in self.dataset['annotations']:
  208. catToImgs[ann['category_id']].append(ann['image_id'])
  209. # print('index created!')
  210. # create class members
  211. self.anns = anns
  212. self.imgToAnns = imgToAnns
  213. self.catToImgs = catToImgs
  214. self.imgs = imgs
  215. self.cats = cats
  216. maskUtils = mask_util
  217. def loadRes(self, resFile):
  218. """
  219. Load result file and return a result api object.
  220. :param resFile (str) : file name of result file
  221. :return: res (obj) : result api object
  222. """
  223. res = COCO()
  224. res.dataset['images'] = [img for img in self.dataset['images']]
  225. # print('Loading and preparing results...')
  226. # tic = time.time()
  227. if isinstance(resFile, torch._six.string_classes):
  228. anns = json.load(open(resFile))
  229. elif type(resFile) == np.ndarray:
  230. anns = self.loadNumpyAnnotations(resFile)
  231. else:
  232. anns = resFile
  233. assert type(anns) == list, 'results in not an array of objects'
  234. annsImgIds = [ann['image_id'] for ann in anns]
  235. assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \
  236. 'Results do not correspond to current coco set'
  237. if 'caption' in anns[0]:
  238. imgIds = set([img['id'] for img in res.dataset['images']]) & set([ann['image_id'] for ann in anns])
  239. res.dataset['images'] = [img for img in res.dataset['images'] if img['id'] in imgIds]
  240. for id, ann in enumerate(anns):
  241. ann['id'] = id + 1
  242. elif 'bbox' in anns[0] and not anns[0]['bbox'] == []:
  243. res.dataset['categories'] = copy.deepcopy(self.dataset['categories'])
  244. for id, ann in enumerate(anns):
  245. bb = ann['bbox']
  246. x1, x2, y1, y2 = [bb[0], bb[0] + bb[2], bb[1], bb[1] + bb[3]]
  247. if 'segmentation' not in ann:
  248. ann['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]]
  249. ann['area'] = bb[2] * bb[3]
  250. ann['id'] = id + 1
  251. ann['iscrowd'] = 0
  252. elif 'segmentation' in anns[0]:
  253. res.dataset['categories'] = copy.deepcopy(self.dataset['categories'])
  254. for id, ann in enumerate(anns):
  255. # now only support compressed RLE format as segmentation results
  256. ann['area'] = maskUtils.area(ann['segmentation'])
  257. if 'bbox' not in ann:
  258. ann['bbox'] = maskUtils.toBbox(ann['segmentation'])
  259. ann['id'] = id + 1
  260. ann['iscrowd'] = 0
  261. elif 'keypoints' in anns[0]:
  262. res.dataset['categories'] = copy.deepcopy(self.dataset['categories'])
  263. for id, ann in enumerate(anns):
  264. s = ann['keypoints']
  265. x = s[0::3]
  266. y = s[1::3]
  267. x1, x2, y1, y2 = np.min(x), np.max(x), np.min(y), np.max(y)
  268. ann['area'] = (x2 - x1) * (y2 - y1)
  269. ann['id'] = id + 1
  270. ann['bbox'] = [x1, y1, x2 - x1, y2 - y1]
  271. # print('DONE (t={:0.2f}s)'.format(time.time()- tic))
  272. res.dataset['annotations'] = anns
  273. createIndex(res)
  274. return res
  275. def evaluate(self):
  276. '''
  277. Run per image evaluation on given images and store results (a list of dict) in self.evalImgs
  278. :return: None
  279. '''
  280. # tic = time.time()
  281. # print('Running per image evaluation...')
  282. p = self.params
  283. # add backward compatibility if useSegm is specified in params
  284. if p.useSegm is not None:
  285. p.iouType = 'segm' if p.useSegm == 1 else 'bbox'
  286. print('useSegm (deprecated) is not None. Running {} evaluation'.format(p.iouType))
  287. # print('Evaluate annotation type *{}*'.format(p.iouType))
  288. p.imgIds = list(np.unique(p.imgIds))
  289. if p.useCats:
  290. p.catIds = list(np.unique(p.catIds))
  291. p.maxDets = sorted(p.maxDets)
  292. self.params = p
  293. self._prepare()
  294. # loop through images, area range, max detection number
  295. catIds = p.catIds if p.useCats else [-1]
  296. if p.iouType == 'segm' or p.iouType == 'bbox':
  297. computeIoU = self.computeIoU
  298. elif p.iouType == 'keypoints':
  299. computeIoU = self.computeOks
  300. self.ious = {
  301. (imgId, catId): computeIoU(imgId, catId)
  302. for imgId in p.imgIds
  303. for catId in catIds}
  304. evaluateImg = self.evaluateImg
  305. maxDet = p.maxDets[-1]
  306. evalImgs = [
  307. evaluateImg(imgId, catId, areaRng, maxDet)
  308. for catId in catIds
  309. for areaRng in p.areaRng
  310. for imgId in p.imgIds
  311. ]
  312. # this is NOT in the pycocotools code, but could be done outside
  313. evalImgs = np.asarray(evalImgs).reshape(len(catIds), len(p.areaRng), len(p.imgIds))
  314. self._paramsEval = copy.deepcopy(self.params)
  315. # toc = time.time()
  316. # print('DONE (t={:0.2f}s).'.format(toc-tic))
  317. return p.imgIds, evalImgs
  318. #################################################################
  319. # end of straight copy from pycocotools, just removing the prints
  320. #################################################################