1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import os import math import numpy as np from PIL import Image from skimage.morphology import binary_dilation, disk
def db_eval_iou(annotation, segmentation): """ Compute region similarity as the Jaccard Index.
Arguments: annotation (ndarray): binary annotation map. segmentation (ndarray): binary segmentation map.
Return: jaccard (float): region similarity """ annotation = annotation.astype(np.bool_) segmentation = segmentation.astype(np.bool_)
if np.isclose(np.sum(annotation), 0) and np.isclose(np.sum(segmentation), 0): return 1 else: return np.sum((annotation & segmentation)) / np.sum((annotation | segmentation), dtype=np.float32)
def db_eval_boundary(foreground_mask, gt_mask, bound_th=0.008): """ Compute mean, recall and decay from per-frame evaluation. Calculates precision/recall for boundaries between foreground_mask and gt_mask using morphological operators to speed it up.
Arguments: foreground_mask (ndarray): binary segmentation image. gt_mask (ndarray): binary annotated image.
Returns: F (float): boundaries F-measure P (float): boundaries precision R (float): boundaries recall """ assert np.atleast_3d(foreground_mask).shape[2] == 1
bound_pix = bound_th if bound_th >= 1 else \ np.ceil(bound_th * np.linalg.norm(foreground_mask.shape))
fg_boundary = seg2bmap(foreground_mask) gt_boundary = seg2bmap(gt_mask)
fg_dil = binary_dilation(fg_boundary, disk(bound_pix)) gt_dil = binary_dilation(gt_boundary, disk(bound_pix))
gt_match = gt_boundary * fg_dil fg_match = fg_boundary * gt_dil
n_fg = np.sum(fg_boundary) n_gt = np.sum(gt_boundary)
if n_fg == 0 and n_gt > 0: precision = 1 recall = 0 elif n_fg > 0 and n_gt == 0: precision = 0 recall = 1 elif n_fg == 0 and n_gt == 0: precision = 1 recall = 1 else: precision = np.sum(fg_match) / float(n_fg) recall = np.sum(gt_match) / float(n_gt)
if precision + recall == 0: F_score = 0 else: F_score = 2 * precision * recall / (precision + recall)
return F_score
def seg2bmap(seg, width=None, height=None): """ From a segmentation, compute a binary boundary map with 1 pixel wide boundaries. The boundary pixels are offset by 1/2 pixel towards the origin from the actual segment boundary.
Arguments: seg : Segments labeled from 1..k. width : Width of desired bmap <= seg.shape[1] height : Height of desired bmap <= seg.shape[0]
Returns: bmap (ndarray): Binary boundary map. """ seg = seg.astype(np.bool_) seg[seg > 0] = 1
assert np.atleast_3d(seg).shape[2] == 1
width = seg.shape[1] if width is None else width height = seg.shape[0] if height is None else height
h,w = seg.shape[:2]
ar1 = float(width) / float(height) ar2 = float(w) / float(h)
assert not (width > w | height > h | abs(ar1 - ar2) > 0.01),\ 'Can''t convert %dx%d seg to %dx%d bmap.'%(w, h, width, height)
e = np.zeros_like(seg) s = np.zeros_like(seg) se = np.zeros_like(seg)
e[:, :-1] = seg[:, 1:] s[:-1, :] = seg[1:, :] se[:-1, :-1] = seg[1:, 1:]
b = seg^e | seg^s | seg^se b[-1, :] = seg[-1, :]^e[-1, :] b[:, -1] = seg[:, -1]^s[:, -1] b[-1, -1] = 0
if w == width and h == height: bmap = b else: bmap = np.zeros((height, width)) for x in range(w): for y in range(h): if b[y, x]: j = 1 + math.floor((y - 1) + height / h) i = 1 + math.floor((x - 1) + width / h) bmap[j, i] = 1
return bmap
database_path = '../data/mask_database/' test_path = '../data/mask_test/' database_img_name_list = os.listdir(database_path) test_img_name_list = os.listdir(test_path)
for test_img_name in test_img_name_list: test_img = Image.open(test_path + test_img_name) h, w = test_img.size test_img = np.asarray(test_img)
best_iou, best_F, best_iou_dbname, best_F_dbname = 0.0, 0.0, None, None for database_img_name in database_img_name_list: database_img = Image.open(database_path + database_img_name).resize((h, w)) database_img = np.asarray(database_img)
iou = db_eval_iou(test_img, database_img) F_score = db_eval_boundary(test_img, database_img)
if iou > best_iou: best_iou = iou best_iou_dbname = database_img_name if F_score > best_F: best_F = F_score best_F_dbname = database_img_name
print(f'[{test_img_name}] best iou: {best_iou:.4f} ({best_iou_dbname}), best F: {best_F:.4f} ({best_F_dbname})')
|