Exemplo n.º 1
0
def display_differences(image,
                        gt_box,
                        gt_class_id,
                        gt_mask,
                        pred_box,
                        pred_class_id,
                        pred_score,
                        pred_mask,
                        class_names,
                        title="",
                        ax=None,
                        show_mask=True,
                        show_box=True,
                        iou_threshold=0.5,
                        score_threshold=0.5):
    """Display ground truth and prediction instances on the same image."""
    # Match predictions to ground truth
    gt_match, pred_match, overlaps = utils.compute_matches(
        gt_box,
        gt_class_id,
        gt_mask,
        pred_box,
        pred_class_id,
        pred_score,
        pred_mask,
        iou_threshold=iou_threshold,
        score_threshold=score_threshold)
    # Ground truth = green. Predictions = red
    colors = [(0, 1, 0, .8)] * len(gt_match)\
           + [(1, 0, 0, 1)] * len(pred_match)
    # Concatenate GT and predictions
    class_ids = np.concatenate([gt_class_id, pred_class_id])
    scores = np.concatenate([np.zeros([len(gt_match)]), pred_score])
    boxes = np.concatenate([gt_box, pred_box])
    masks = np.concatenate([gt_mask, pred_mask], axis=-1)
    # Captions per instance show score/IoU
    captions = ["" for m in gt_match] + [
        "{:.2f} / {:.2f}".format(pred_score[i],
                                 (overlaps[i, int(pred_match[i])] if
                                  pred_match[i] > -1 else overlaps[i].max()))
        for i in range(len(pred_match))
    ]
    # Set title if not provided
    title = title or "Ground Truth and Detections\n GT=green, pred=red, captions: score/IoU"
    # Display
    display_instances(image,
                      boxes,
                      masks,
                      class_ids,
                      class_names,
                      scores,
                      ax=ax,
                      show_bbox=show_box,
                      show_mask=show_mask,
                      colors=colors,
                      captions=captions,
                      title=title)
Exemplo n.º 2
0
def compute_batch_metrics(model,
                          dataset,
                          inference_config,
                          image_ids,
                          verbose=False):

    all_results = dict()
    for image_id in image_ids:
        print("Processing image id: %s" % image_id)
        all_results[image_id] = dict()
        image, image_meta, gt_class_ids, gt_boxes, gt_masks =\
            modellib.load_image_gt(dataset, inference_config,
                                   image_id=image_id, use_mini_mask=False)
        all_results[image_id]['gt_class_ids'] = gt_class_ids
        all_results[image_id]['gt_boxes'] = gt_boxes
        all_results[image_id]['gt_masks'] = gt_masks

        # Run object detection
        r = model.detect([image], verbose=False)[0]
        class_result = get_class_result(gt_class=gt_class_ids,
                                        pred_class=r['class_ids'])
        all_results[image_id]['class_result'] = class_result
        all_results[image_id]['pred_class_ids'] = r['class_ids']
        all_results[image_id]['pred_boxes'] = r['rois']
        all_results[image_id]['pred_scores'] = r['scores']
        all_results[image_id]['pred_masks'] = r['masks']

        print("Ground Truth Classes: %s" % gt_class_ids)
        print("Predicted Classes: %s" % r['class_ids'])
        print("Predicted Scores: %s" % r['scores'])
        print("Class Result: %s" % class_result)

        if gt_masks.size > 0 and r['masks'].size > 0:
            gt_match, pred_match, overlaps = modelutils.compute_matches(
                gt_boxes=gt_boxes,
                gt_class_ids=gt_class_ids,
                gt_masks=gt_masks,
                pred_boxes=r['rois'],
                pred_class_ids=r['class_ids'],
                pred_scores=r['scores'],
                pred_masks=r['masks'],
                iou_threshold=0.5,
                score_threshold=0.0)
            all_results[image_id]['gt_match'] = gt_match
            all_results[image_id]['pred_match'] = pred_match
            all_results[image_id]['overlaps'] = overlaps
        elif gt_masks.size == 0 and r['masks'].size > 0:
            all_results[image_id]['overlaps'] = np.zeros(
                shape=(r['class_ids'].shape[0], 1))
        else:
            all_results[image_id]['overlaps'] = np.array([])

        print("Overlap: \n%s" % all_results[image_id]['overlaps'])
        print("\n")

    return all_results