def render_label_pred_example(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, y_gt = real_image2d() x = normalize(img, 1, 99.8) y, _ = model.predict_instances(x) im = render_label_pred(y_gt, y, img=x) import matplotlib.pyplot as plt plt.figure(1, figsize=(12, 4)) plt.subplot(1, 4, 1) plt.imshow(x) plt.title("img") plt.subplot(1, 4, 2) plt.imshow(render_label(y_gt, img=x)) plt.title("gt") plt.subplot(1, 4, 3) plt.imshow(render_label(y, img=x)) plt.title("pred") plt.subplot(1, 4, 4) plt.imshow(im) plt.title("tp (green) fp (red) fn(blue)") plt.tight_layout() plt.show() return im
def _model2d(): from utils import path_model2d from stardist.models import StarDist2D model_path = path_model2d() return StarDist2D(None, name=model_path.name, basedir=str(model_path.parent))
def test_load_and_predict_big(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, _ = real_image2d() x = normalize(img, 1, 99.8) x = np.tile(x, (8, 8)) labels, polygons = model.predict_instances(x) return labels
def test_load_and_export_TF(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) assert any(g > 1 for g in model.config.grid) # model.export_TF(single_output=False, upsample_grid=False) # model.export_TF(single_output=False, upsample_grid=True) model.export_TF(single_output=True, upsample_grid=False) model.export_TF(single_output=True, upsample_grid=True)
def test_load_and_predict(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image2d() x = normalize(img,1,99.8) prob, dist = model.predict(x, n_tiles=(2,3)) assert prob.shape == dist.shape[:2] assert model.config.n_rays == dist.shape[-1] labels, polygons = model.predict_instances(x) assert labels.shape == img.shape[:2] assert labels.max() == len(polygons['coord']) assert len(polygons['coord']) == len(polygons['points']) == len(polygons['prob']) stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (1, 48, 17)
def render_label_example(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, y_gt = real_image2d() x = normalize(img, 1, 99.8) y, _ = model.predict_instances(x) # im = render_label(y,img = x, alpha = 0.3, alpha_boundary=1, cmap = (.3,.4,0)) im = render_label(y, img=x, alpha=0.3, alpha_boundary=1) import matplotlib.pyplot as plt plt.figure(1) plt.imshow(im) plt.show() return im
def test_optimize_thresholds(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image2d() x = normalize(img, 1, 99.8) res = model.optimize_thresholds([x], [mask], nms_threshs=[.3, .5], iou_threshs=[.3, .5], optimize_kwargs=dict(tol=1e-1), save_to_json=False) np.testing.assert_almost_equal(res["prob"], 0.454617141955, decimal=3) np.testing.assert_almost_equal(res["nms"], 0.3, decimal=3)