Esempio n. 1
0
def ap_for_image(cfg, checkpoint, image_dir, anno_dir, out_dir):
    model = init_detector(cfg, checkpoint, device='cuda:0')
    images = os.listdir(image_dir)
    anno_files = os.listdir(anno_dir)
    output_name = 'precision.json'
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    output_name = os.path.join(out_dir, output_name)
    for image, anno in zip(images, anno_files):
        assert image.split('.')[0] == anno.split(
            '.')[0], "image and annotation mismatch!"
        image_path = os.path.join(image_dir, image)
        json_name = os.path.join(anno_dir, anno)
        results = inference_detector(model, image_path)
        annotations = json_to_annotation(json_name)
        mean_ap, _ = eval_map(results[0], annotations)
        with open(output_name, 'w') as f:
            json.dump([annotations['image_id'], mean_ap], f)
Esempio n. 2
0
 def bbox_map_eval(self, det_result, annotation):
     # use only bbox det result
     if isinstance(det_result, tuple):
         bbox_det_result = [det_result[0]]
     else:
         bbox_det_result = [det_result]
     # mAP
     iou_thrs = np.linspace(.5,
                            0.95,
                            int(np.round((0.95 - .5) / .05)) + 1,
                            endpoint=True)
     mean_aps = []
     for thr in iou_thrs:
         mean_ap, _ = eval_map(bbox_det_result, [annotation],
                               iou_thr=thr,
                               logger='silent')
         mean_aps.append(mean_ap)
     return sum(mean_aps) / len(mean_aps)
Esempio n. 3
0
def bbox_map_eval(det_result, annotation):
    """Evaluate mAP of single image det result.

    Args:
        det_result (list[list]): [[cls1_det, cls2_det, ...], ...].
            The outer list indicates images, and the inner list indicates
            per-class detected bboxes.
        annotation (dict): Ground truth annotations where keys of
             annotations are:

            - bboxes: numpy array of shape (n, 4)
            - labels: numpy array of shape (n, )
            - bboxes_ignore (optional): numpy array of shape (k, 4)
            - labels_ignore (optional): numpy array of shape (k, )

    Returns:
        float: mAP
    """

    # use only bbox det result
    if isinstance(det_result, tuple):
        bbox_det_result = [det_result[0]]
    else:
        bbox_det_result = [det_result]
    # mAP
    iou_thrs = np.linspace(.5,
                           0.95,
                           int(np.round((0.95 - .5) / .05)) + 1,
                           endpoint=True)
    mean_aps = []
    for thr in iou_thrs:
        mean_ap, _ = eval_map(bbox_det_result, [annotation],
                              iou_thr=thr,
                              logger='silent')
        mean_aps.append(mean_ap)
    return sum(mean_aps) / len(mean_aps)