def test_evalution_iou_multiclass_with_images(): classes = ['bkg', 'kite', 'person'] mask_shape = (480, 640) gt_bbox_1 = BBox(10, 10, 10, 10, 1) mask_bin_GT_1 = utils.bin_mask_from_bb(mask_shape, gt_bbox_1) pred_bbox_1 = BBox(10, 10, 10, 10, 1) mask_bin_pred_1 = utils.bin_mask_from_bb(mask_shape, pred_bbox_1) gt_bbox_2 = BBox(110, 110, 320, 280, 2) mask_bin_GT_2 = utils.bin_mask_from_bb(mask_shape, gt_bbox_2) pred_bbox_2 = BBox(70, 50, 240, 220, 2) mask_bin_pred_2 = utils.bin_mask_from_bb(mask_shape, pred_bbox_2) mask_multi_GT = np.maximum(mask_bin_GT_1, mask_bin_GT_2 * 2) mask_multi_pred = np.maximum(mask_bin_pred_1, mask_bin_pred_2 * 2) metrics = evaluate_segmentation([mask_multi_GT], [mask_multi_pred], classes) assert metrics.count == 480 * 640 assert metrics.by_class[1].iou == 1.0 assert round(metrics.by_class[2].iou, 3) == .290 assert round(metrics.avg_iou_no_bkg, 3) == .645
def evaluate_detection_example(): # Class names classes = ['kite', 'person'] # Image shape mask_shape = (480, 640) # Creating fake data gt_bbox_1 = BBox(10, 10, 10, 10, 0, 1) pred_bbox_1 = BBox(10, 10, 10, 10, 0, 1) gt_bbox_2 = BBox(110, 110, 320, 280, 1, 1) pred_bbox_2 = BBox(70, 50, 240, 220, 1, 1) # Creating data suplier iterator # It is not necessary here, but it's useful if you want to yield data # from the disk i.e. from a Pytorch DataLoader it_gt_masks = identity_iterator([gt_bbox_1, gt_bbox_2]) it_pred_masks = identity_iterator([pred_bbox_1, pred_bbox_2]) # Calculates and shows metrics metrics = evaluate_detection(it_gt_masks, it_pred_masks, classes) # Shows confusion matrix and returns its Figure and Axes fig, axes = metrics.show_confusion_matrix() # Shows confusion matrix for class `a` metrics.by_class[0].show_confusion_matrix()
def show_detection_example(): # Class names classes = ['kite', 'person', 'car'] # Image shape img_shape = (480, 640) # Bboxes creation gt_bbox_1 = BBox(10, 10, 100, 100, 0) pred_bbox_1 = BBox(10, 10, 100, 100, 0, .8212158464) gt_bbox_2 = BBox(110, 110, 320, 280, 1) pred_bbox_2 = BBox(70, 50, 240, 220, 1, .34844545) gt_bbox_3 = BBox(300, 300, 100, 100, 2) # Convert to results results = [ Result(random_image(img_shape[0], img_shape[1]), [gt_bbox_1, gt_bbox_2, gt_bbox_3], [pred_bbox_1, pred_bbox_2]) ] # GT only result results = [ Result(random_image(img_shape[0], img_shape[1]), [gt_bbox_1, gt_bbox_2, gt_bbox_3]) ] + results # Shows results and returns its Figure and Axes fig, axes = show_detections(results, classes, show_bbox_label=False)
def test_center_point_box(): b0 = BBox(0, 0, 3, 3, 0) assert b0.center_point == (1, 1) b1 = BBox(100, 100, 100, 100, 0) assert b1.center_point == (149, 149) b2 = BBox(500, 500, 100, 100, 0) assert b2.center_point == (549, 549) b3 = BBox(100, 200, 76, 76, 0) assert b3.center_point == (137, 237)
def test_evaluation_detection_iou_metric(): classes = ['kite'] gt_bbox = BBox(110, 110, 320, 280, 0) pred_bbox = BBox(70, 50, 240, 220, 0, .5) metrics = evaluate_detection([[gt_bbox]], [[pred_bbox]], classes) assert round(metrics.avg_iou, 3) == .290 assert round(metrics.by_class[0].iou, 3) == .290
def evaluate_segmentation_example(): # Class names - Background must be at 0 index classes = ['bkg', 'kite', 'person'] # Image shape mask_shape = (480, 640) # Creating fake data # Creates a rectangle of 1s in a 0s array gt_bbox_1 = BBox(10, 10, 10, 10, 1) mask_bin_GT_1 = draw_rectangle(np.zeros(mask_shape, np.int), gt_bbox_1.upper_left_point, gt_bbox_1.bottom_right_point, 1) pred_bbox_1 = BBox(10, 10, 10, 10, 1) mask_bin_pred_1 = draw_rectangle(np.zeros(mask_shape, np.int), pred_bbox_1.upper_left_point, pred_bbox_1.bottom_right_point, 1) # Creates a rectangle of 2s in a 0s array gt_bbox_2 = BBox(110, 110, 320, 280, 2) mask_bin_GT_2 = draw_rectangle(np.zeros(mask_shape, np.int), gt_bbox_2.upper_left_point, gt_bbox_2.bottom_right_point, 2) pred_bbox_2 = BBox(70, 50, 240, 220, 2) mask_bin_pred_2 = draw_rectangle(np.zeros(mask_shape, np.int), pred_bbox_2.upper_left_point, pred_bbox_2.bottom_right_point, 2) # Merging masks mask_GT = np.maximum(mask_bin_GT_1, mask_bin_GT_2) mask_pred = np.maximum(mask_bin_pred_1, mask_bin_pred_2) # Creating data suplier iterator # It is not necessary here, but it's useful if you want to yield data # from the disk i.e. from a Pytorch DataLoader it_gt_masks = identity_iterator(mask_GT) it_pred_masks = identity_iterator(mask_pred) # Calculates and shows metrics metrics = evaluate_segmentation(it_gt_masks, it_pred_masks, classes) # Shows confusion matrix and returns its Figure and Axes fig, axes = metrics.show_confusion_matrix() # Shows confusion matrix for class `a` metrics.by_class[0].show_confusion_matrix()
def test_evalution_iou_with_image(): classes = ['bkg', 'kite'] mask_shape = (480, 640) gt_bbox = BBox(110, 110, 320, 280, 1) mask_bin_GT = utils.bin_mask_from_bb(mask_shape, gt_bbox) pred_bbox = BBox(70, 50, 240, 220, 1) mask_bin_pred = utils.bin_mask_from_bb(mask_shape, pred_bbox) metrics = evaluate_segmentation([mask_bin_GT], [mask_bin_pred], classes) assert metrics.count == 480 * 640 assert round(metrics.by_class[1].iou, 3) == .290 assert round(metrics.avg_iou_no_bkg, 3) == .290 assert round(metrics.avg_iou, 3) == .502
def show_segmentation_example(): # Class names classes = ['bkg', 'kite', 'person', 'car'] # Image/mask shape mask_shape = (480, 640) # Multiclass mask creation gt_bbox_1 = BBox(10, 10, 100, 100, 1) mask_bin_GT_1 = draw_bboxes(mask_shape, [gt_bbox_1]) pred_bbox_1 = BBox(10, 10, 100, 100, 1) mask_bin_pred_1 = draw_bboxes(mask_shape, [pred_bbox_1]) gt_bbox_2 = BBox(110, 110, 320, 280, 2) mask_bin_GT_2 = draw_bboxes(mask_shape, [gt_bbox_2]) pred_bbox_2 = BBox(70, 50, 240, 220, 2) mask_bin_pred_2 = draw_bboxes(mask_shape, [pred_bbox_2]) gt_bbox_3 = BBox(300, 300, 100, 100, 3) mask_bin_GT_3 = draw_bboxes(mask_shape, [gt_bbox_3]) mask_multi_GT = np.maximum(np.maximum(mask_bin_GT_1, mask_bin_GT_2 * 2), mask_bin_GT_3 * 3) mask_multi_pred = np.maximum(mask_bin_pred_1, mask_bin_pred_2 * 2) # Convert to results results = [ Result(random_image(mask_shape[0], mask_shape[1]), mask_multi_GT, mask_multi_pred) ] # GT only result results = [ Result(random_image(mask_shape[0], mask_shape[1]), mask_multi_GT) ] + results # Custom color map cmap = ListedColormap(['#ff0000', '#00ff00', '#0000ff', '#ffffff']) # Shows results and returns its Figure and Axes fig, axes = show_segmentations(results, classes, cmap=cmap)
def test_evaluation_detection_no_pred(): classes = ['kite', 'person'] gt_bbox = BBox(110, 110, 320, 280, 0) metrics = evaluate_detection([[gt_bbox]], [[]], classes) assert round(metrics.avg_iou, 3) == 0 assert round(metrics.by_class[0].iou, 3) == 0 assert metrics._by_class[0].FN == 1
def test_evaluation_detection_no_gt(): classes = ['kite', 'person'] pred_bbox = BBox(70, 50, 240, 220, 0) metrics = evaluate_detection([[]], [[pred_bbox]], classes) assert round(metrics.avg_iou, 3) == 0 assert round(metrics.by_class[0].iou, 3) == 0 assert metrics._by_class[0].FP == 1
def test_calculate_pairwise_bbox_ious(): gt_bboxes = [ BBox(39, 63, 203 - 39 + 1, 112 - 63 + 1, 0), BBox(49, 75, 203 - 49 + 1, 125 - 75 + 1, 0), BBox(31, 69, 201 - 31 + 1, 125 - 69 + 1, 0), BBox(50, 72, 197 - 50 + 1, 121 - 72 + 1, 0), BBox(35, 51, 196 - 35 + 1, 110 - 51 + 1, 0), BBox(35, 51, 196, 110, 0), BBox(0, 0, 5, 5, 0), ] pred_bboxes = [ BBox(54, 66, 198 - 54 + 1, 114 - 66 + 1, 0), BBox(42, 78, 186 - 42 + 1, 126 - 78 + 1, 0), BBox(18, 63, 235 - 18 + 1, 135 - 63 + 1, 0), BBox(54, 72, 198 - 54 + 1, 120 - 72 + 1, 0), BBox(36, 60, 180 - 36 + 1, 108 - 60 + 1, 0), BBox(35, 51, 196, 110, 0), BBox(6, 6, 5, 5, 0), ] ious = calculate_pairwise_bbox_ious(gt_bboxes, pred_bboxes) assert round(ious[0, 0], 3) == .798 assert round(ious[1, 1], 3) == .79 assert round(ious[2, 2], 3) == .612 assert round(ious[3, 3], 3) == .947 assert round(ious[4, 4], 3) == .731 assert ious[5, 5] == 1 assert ious[6, 6] == 0