Exemplo n.º 1
0
 def run_eval_debug(self, results, save_dir):
     self.save_results(results, save_dir)
     save_path = "cache/fail/"
     coco_dets = self.coco.loadRes('{}/results.json'.format(save_dir))
     coco_eval = COCOeval(self.coco, coco_dets, "bbox")
     p = coco_eval.params
     p.imgIds = list(np.unique(p.imgIds))
     p.maxDets = sorted(p.maxDets)
     coco_eval.params = p
     coco_eval._prepare()
     catIds = [-1]
     computeIoU = coco_eval.computeIoU
     coco_eval.ious = {(imgId, catId): coco_eval.computeIoU(imgId, catId) \
                 for imgId in p.imgIds
                 for catId in catIds}
     maxDet = p.maxDets[-1]
     for imgId in p.imgIds:
         img_path = "/home/user/home/user/Xinyuan/work/CenterNet-1/data/coco/val2017/"
         fullImgId = str(imgId).zfill(11)
         img_path = os.path.join(img_path, fullImgId + '.jpg')
         img = cv2.imread(img_path)
         for catId in catIds:
             # for i, areaRng in enumerate(p.areaRng):
             areaRng = p.areaRng[0]
             print("areaRng: " + areaRng)
             result = coco_eval.getImageFailCase(imgId, catId, areaRng,
                                                 maxDet)
             gtIds = result['gtIds']
             dtIds = result['dtIds']
             gtFailIds = gtIds[result['gtfail'][0]]
             dtFailIds = dtIds[result['dtfail'][0]]
             gts = [
                 _ for cId in p.catIds for _ in coco_eval._gts[imgId, cId]
             ]
             dts = [
                 _ for cId in p.catIds for _ in coco_eval._dts[imgId, cId]
             ]
             gts = [g['bbox'] for g in gts]
             dts = [d['bbox'] for d in dts]
             pdb.set_trace()
             c = [255, 0, 0]
             for dt in dts:
                 bbox = np.array(dt, dtype=np.int32)
                 cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
                               c, 2)
             c = [0, 255, 0]
             for gt in gts:
                 bbox = np.array(gt, dtype=np.int32)
                 cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
                               c, 2)
     # saveImage
     save_path = os.path.join(save_path, fullImgId + "_" + str(i))
     cv2.imwrite(save_path, img)
Exemplo n.º 2
0
coco_dets = coco.loadRes('{}/results.json'.format(save_dir))
coco_eval = COCOeval(coco, coco_dets, "bbox")
print(dir(coco_eval))
save_path = "src/cache/fail/"
coco_dets = coco.loadRes('{}/results.json'.format(save_dir))
coco_eval = COCOeval(coco, coco_dets, "bbox")
p = coco_eval.params
p.imgIds = list(np.unique(p.imgIds))
p.maxDets = sorted(p.maxDets)
coco_eval.params = p
coco_eval._prepare()
cat_dict = coco.cats
# print(cat_dict)
catIds = coco_eval.params.catIds
computeIoU = coco_eval.computeIoU
coco_eval.ious = {(imgId, catId): coco_eval.computeIoU(imgId, catId) \
            for imgId in p.imgIds
            for catId in catIds}
maxDet = p.maxDets[-1]
for imgId in p.imgIds:
    img_path = "/home/user/home/user/Xinyuan/work/CenterNet-1/data/coco/val2017/"
    fullImgId = str(imgId).zfill(12)
    img_path = os.path.join(img_path, fullImgId + '.jpg')
    print(img_path)
    img = cv2.imread(img_path)
    for catId in catIds:
        cat = int(catId)
        txt = cat_dict[cat]['name']
        font = cv2.FONT_HERSHEY_SIMPLEX
        cat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]
        areaRng = p.areaRng[0]
Exemplo n.º 3
0
class MapEvaluator:
    def __init__(self, gt_json, cat_ids):
        # Eval options
        self.iou_threshs = [.5]
        self.max_dets = [1, 10, 100]

        self.cat_ids = cat_ids

        self.coco_gt = COCO(gt_json)

    def _init_coco_eval(self, dt_json_data):
        coco_dt = self.coco_gt.loadRes(dt_json_data)

        self.coco_eval = COCOeval(self.coco_gt, coco_dt, 'bbox')
        self.coco_eval.params.iouThrs = np.array(self.iou_threshs)
        self.coco_eval.params.maxDets = self.max_dets
        self.coco_eval.params.catIds = self.cat_ids

    def do_eval(self, dt_json_data):
        self._init_coco_eval(dt_json_data)

        self.coco_eval.evaluate()
        self.coco_eval.accumulate()

    def do_eval_and_print(self, dt_json_data):
        self._init_coco_eval(dt_json_data)

        self.coco_eval.evaluate()
        self.coco_eval.accumulate()
        self.coco_eval.summarize()

    def get_avg_precision_recall(self, t=0, a=0, m=-1):
        p = self.coco_eval.eval['precision'][t, :, :, a, m]
        r = self.coco_eval.eval['recall'][t, :, a, m]

        # Removing -1 entries for classes that have no GT objects.
        p = p[:, np.where(r > -1)[0]]
        r = r[r > -1]
        assert (np.all(p >= 0.) and np.all(r >= 0.))

        ap = p.mean(axis=1)
        ar = r.mean()

        return ap, ar

    def evaluateImg(self, imgId, catId, aRng=(-np.inf, np.inf), maxDet=100):
        p = self.coco_eval.params
        # add backward compatibility if useSegm is specified in params
        p.iouType = 'bbox'
        p.imgIds = list(np.unique(p.imgIds))
        if p.useCats:
            p.catIds = list(np.unique(p.catIds))

        p.maxDets = sorted(p.maxDets)
        self.coco_eval.params = p

        self.coco_eval._prepare()

        self.coco_eval.ious = {
            (imgId, catId): self.coco_eval.computeIoU(imgId, catId)
        }
        img_eval = self.coco_eval.evaluateImg(imgId, catId, aRng, maxDet)
        inds = img_eval['dtIds']
        matches = img_eval['dtMatches']

        return inds, matches