def show(uid, x, y, y_c, y_m, save=False): threshold = config['param'].getfloat('threshold') threshold_edge = config['param'].getfloat('threshold_edge') threshold_mark = config['param'].getfloat('threshold_mark') segmentation = config['post'].getboolean('segmentation') remove_objects = config['post'].getboolean('remove_objects') min_object_size = config['post'].getint('min_object_size') remove_fiber = config['post'].getboolean('filter_fiber') view_color_equalize = config['valid'].getboolean('view_color_equalize') fig, (ax1, ax2) = plt.subplots(2, 3, sharey=True, figsize=(10, 8)) fig.suptitle(uid, y=1) ax1[1].set_title('Final Pred, P > {}'.format(threshold)) ax1[2].set_title('Overlay, P > {}'.format(threshold)) y_bw = y > threshold if view_color_equalize: x = clahe(x) ax1[0].set_title('Image') ax1[0].imshow(x, aspect='auto') markers = np.zeros_like(x) if segmentation: y, markers = partition_instances(y, y_m, y_c) if remove_objects: y = remove_small_objects(y, min_size=min_object_size) if remove_fiber: y = filter_fiber(y) y, cmap = _make_overlay(y) ax1[1].imshow(y, cmap=cmap, aspect='auto') # alpha ax1[2].imshow(x, aspect='auto') ax1[2].imshow(y, cmap=cmap, alpha=0.3, aspect='auto') ax2[0].set_title('Semantic Pred, P > {}'.format(threshold)) ax2[0].imshow(y_bw, cmap='gray', aspect='auto') _, count = label(markers, return_num=True) ax2[1].set_title('Markers, #={}'.format(count)) ax2[1].imshow(markers, cmap='gray', aspect='auto') if y_c is not None: ax2[2].set_title('Contour Pred, P > {}'.format(threshold_edge)) y_c = y_c > threshold_edge ax2[2].imshow(y_c, cmap='gray', aspect='auto') plt.tight_layout() if save: dir = predict_save_folder() fp = os.path.join(dir, uid + '.png') plt.savefig(fp) else: show_figure()
def get_iou(y, y_c, y_m, gt): segmentation = config['post'].getboolean('segmentation') remove_objects = config['post'].getboolean('remove_objects') min_object_size = config['post'].getint('min_object_size') remove_fiber = config['post'].getboolean('filter_fiber') only_contour = config['contour'].getboolean('exclusive') if segmentation: y, markers = partition_instances(y, y_m, y_c) if remove_objects: y = remove_small_objects(y, min_size=min_object_size) if remove_fiber: y = filter_fiber(y) if only_contour: iou = iou_metric(y, label(gt > 0)) else: iou = iou_metric(y, gt) return iou
def save_mask(uid, y, y_c, y_m): threshold = config['param'].getfloat('threshold') segmentation = config['post'].getboolean('segmentation') remove_objects = config['post'].getboolean('remove_objects') min_object_size = config['post'].getint('min_object_size') remove_fiber = config['post'].getboolean('filter_fiber') if segmentation: y, _ = partition_instances(y, y_m, y_c) if remove_objects: y = remove_small_objects(y, min_size=min_object_size) if remove_fiber: y = filter_fiber(y) idxs = np.unique(y) # sorted, 1st is background (e.g. 0) dir = os.path.join(predict_save_folder(), uid, 'masks') if not os.path.exists(dir): os.makedirs(dir) for idx in idxs[1:]: mask = (y == idx).astype(np.uint8) mask *= 255 img = Image.fromarray(mask, mode='L') img.save(os.path.join(dir, str(uuid.uuid4()) + '.png'), 'PNG')
def show_groundtruth(uid, x, y, y_c, y_m, gt, gt_s, gt_c, gt_m, save=False): threshold = config['param'].getfloat('threshold') threshold_edge = config['param'].getfloat('threshold_edge') threshold_mark = config['param'].getfloat('threshold_mark') segmentation = config['post'].getboolean('segmentation') remove_objects = config['post'].getboolean('remove_objects') remove_fiber = config['post'].getboolean('filter_fiber') min_object_size = config['post'].getint('min_object_size') only_contour = config['contour'].getboolean('exclusive') view_color_equalize = config['valid'].getboolean('view_color_equalize') print_table = config['valid'].getboolean('print_table') fig, (ax1, ax2, ax3) = plt.subplots(3, 4, sharey=True, figsize=(12, 8)) fig.suptitle(uid, y=1) y_s = y # to show pure semantic predict later if view_color_equalize: x = clahe(x) ax1[0].set_title('Image') ax1[0].imshow(x, aspect='auto') if segmentation : y, markers = partition_instances(y, y_m, y_c) if remove_objects: y = remove_small_objects(y, min_size=min_object_size) if remove_fiber: y = filter_fiber(y) _, count = label(y, return_num=True) ax1[1].set_title('Final Pred, #={}'.format(count)) ax1[1].imshow(y, cmap='gray', aspect='auto') # overlay contour to semantic ground truth (another visualized view for instance ground truth, eg. gt) _, count = label(gt, return_num=True) ax1[2].set_title('Instance Lbls, #={}'.format(count)) ax1[2].imshow(gt_s, cmap='gray', aspect='auto') gt_c2, cmap = _make_overlay(gt_c) ax1[2].imshow(gt_c2, cmap=cmap, alpha=0.7, aspect='auto') if only_contour: # can not tell from instances in this case iou = iou_metric(y, label(gt > 0), print_table) else: iou = iou_metric(y, gt, print_table) ax1[3].set_title('Overlay, IoU={:.3f}'.format(iou)) ax1[3].imshow(gt_s, cmap='gray', aspect='auto') y, cmap = _make_overlay(y) ax1[3].imshow(y, cmap=cmap, alpha=0.3, aspect='auto') y_s = y_s > threshold _, count = label(y_s, return_num=True) ax2[0].set_title('Semantic Predict, #={}'.format(count)) ax2[0].imshow(y_s, cmap='gray', aspect='auto') _, count = label(gt_s, return_num=True) ax2[1].set_title('Semantic Lbls, #={}'.format(count)) ax2[1].imshow(gt_s, cmap='gray', aspect='auto') if y_c is not None: y_c = y_c > threshold_edge _, count = label(y_c, return_num=True) ax2[2].set_title('Contour Predict, #={}'.format(count)) ax2[2].imshow(y_c, cmap='gray', aspect='auto') _, count = label(gt_c, return_num=True) ax2[3].set_title('Contour Lbls, #={}'.format(count)) ax2[3].imshow(gt_c, cmap='gray', aspect='auto') _, count = label(markers, return_num=True) ax3[0].set_title('Final Markers, #={}'.format(count)) ax3[0].imshow(markers, cmap='gray', aspect='auto') if y_m is not None: y_m = y_m > threshold_mark _, count = label(y_m, return_num=True) ax3[1].set_title('Marker Predict, #={}'.format(count)) ax3[1].imshow(y_m, cmap='gray', aspect='auto') _, count = label(gt_m, return_num=True) ax3[2].set_title('Marker Lbls, #={}'.format(count)) ax3[2].imshow(gt_m, cmap='gray', aspect='auto') plt.tight_layout() if save: dir = predict_save_folder() fp = os.path.join(dir, uid + '.png') plt.savefig(fp) else: show_figure()