def test_predict3D(model3d): model = model3d img = real_image3d()[0] img = normalize(img, 1, 99.8) img = repeat(img, 2) ref_labels, ref_polys = model.predict_instances(img) res_labels, res_polys = model.predict_instances_big(img, axes='ZYX', block_size=(55, 105, 105), min_overlap=(13, 25, 25), context=(17, 30, 30)) m = matching(ref_labels, res_labels) assert (1.0, 1.0) == (m.accuracy, m.mean_true_score) # sort them first lexicographic ref_inds = np.lexsort(ref_polys["points"].T) res_inds = np.lexsort(res_polys["points"].T) assert np.allclose(ref_polys["dist"][ref_inds], res_polys["dist"][res_inds], atol=1e-2) assert np.allclose(ref_polys["points"][ref_inds], res_polys["points"][res_inds], atol=1e-2) assert np.allclose(ref_polys["prob"][ref_inds], res_polys["prob"][res_inds], atol=1e-2) return ref_polys, res_polys
def test_mesh_export(model3d): model = model3d img, mask = real_image3d() x = normalize(img, 1, 99.8) labels, polys = model.predict_instances(x, nms_thresh=.5, overlap_label=-3) s = export_to_obj_file3D(polys, "mesh.obj", scale=(.2, .1, .1)) return s
def test_load_and_predict_with_overlap(model3d): model = model3d img, mask = real_image3d() x = normalize(img, 1, 99.8) prob, dist = model.predict(x, n_tiles=(1, 2, 2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x, nms_thresh=.5, overlap_label=-3) assert np.min(labels) == -3 return model, labels
def test_cover3D(block_size, context, grid): lbl = real_image3d()[1] lbl = lbl.astype(np.int32) max_sizes = tuple(calculate_extents(lbl, func=np.max)) min_overlap = tuple(1 + v for v in max_sizes) lbl = repeat(lbl, (2, 4, 4)) assert max_sizes == tuple(calculate_extents(lbl, func=np.max)) reassemble(lbl, 'ZYX', block_size, min_overlap, context, grid)
def test_stardistdata(): from stardist.models import StarDistData3D from stardist import Rays_GoldenSpiral img, mask = real_image3d() s = StarDistData3D([img, img], [mask, mask], batch_size=1, patch_size=(30, 40, 50), rays=Rays_GoldenSpiral(64)) (img, mask), (prob, dist) = s[0] return (img, mask), (prob, dist), s
def test_load_and_predict_with_overlap(): model_path = path_model3d() model = StarDist3D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image3d() x = normalize(img,1,99.8) prob, dist = model.predict(x, n_tiles=(1,2,2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x, nms_thresh = .5, overlap_label = -3) assert np.min(labels) == -3 return model, labels
def test_load_and_predict(model3d): model = model3d img, mask = real_image3d() x = normalize(img, 1, 99.8) prob, dist = model.predict(x, n_tiles=(1, 2, 2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x) assert labels.shape == img.shape[:3] stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (0, 30, 21) return model, labels
def test_load_and_predict(): model_path = path_model3d() model = StarDist3D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image3d() x = normalize(img,1,99.8) prob, dist = model.predict(x, n_tiles=(1,2,2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x) assert labels.shape == img.shape[:3] stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (0, 30, 21) return model, labels
def test_predict_dense_sparse(): model_path = path_model3d() model = StarDist3D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image3d() x = normalize(img, 1, 99.8) labels1, res1 = model.predict_instances(x, n_tiles=(1, 2, 2), sparse=False) labels2, res2 = model.predict_instances(x, n_tiles=(1, 2, 2), sparse=True) assert np.allclose(labels1, labels2) assert all( np.allclose(res1[k], res2[k]) for k in set(res1.keys()).union(set(res2.keys()))) return labels2, labels2
def _check_single_val(n_classes, classes=1): img, y_gt = real_image3d() labels_gt = set(np.unique(y_gt[y_gt > 0])) p, cls_dict = mask_to_categorical(y_gt, n_classes=n_classes, classes=classes, return_cls_dict=True) assert p.shape == y_gt.shape + (n_classes + 1, ) assert tuple(cls_dict.keys()) == (classes, ) and set( cls_dict[classes]) == labels_gt assert set(np.where(np.count_nonzero(p, axis=(0, 1, 2)))[0]) == set( {0, classes}) return p, cls_dict
def test_polyhedron_order_3D(model3d): model = model3d img = real_image3d()[0] img = normalize(img, 1, 99.8) labels, polys = model.predict_instances(img, nms_thresh=0) for i,(dist,point) in enumerate(zip(polys['dist'],polys['points']), start=1): # polygon representing object with id i p = Polyhedron(dist, point, polys['rays'], shape_max=labels.shape) # mask of object with id i in label image (not occluded since nms_thresh=0) mask_i = labels[p.slice] == i # assert np.all(p.mask == mask_i) # few pixels are sometimes different, why? frac_same = np.count_nonzero(p.mask == mask_i) / p.mask.size assert frac_same > 0.99
def test_stardistdata(grid): np.random.seed(42) from stardist.models import StarDistData3D from stardist import Rays_GoldenSpiral img, mask = real_image3d() s = StarDistData3D([img, img], [mask, mask], batch_size=1, grid=grid, patch_size=(30, 40, 50), rays=Rays_GoldenSpiral(64), length=1) (img, ), (prob, dist) = s[0] return (img, ), (prob, dist), s
def test_optimize_thresholds(model3d): model = model3d img, mask = real_image3d() x = normalize(img, 1, 99.8) def _opt(model): return model.optimize_thresholds([x], [mask], nms_threshs=[.3, .5], iou_threshs=[.3, .5], optimize_kwargs=dict(tol=1e-1), save_to_json=False) t1 = _opt(model) # enforce implicit tiling model.config.train_batch_size = 1 model.config.train_patch_size = tuple(s - 1 for s in x.shape) t2 = _opt(model) assert all(np.allclose(t1[k], t2[k]) for k in t1.keys()) return model
def test_edt_prob(anisotropy): try: import edt from stardist.utils import _edt_prob_edt, _edt_prob_scipy masks = (np.tile(real_image3d()[1], (2, 2, 2)), np.zeros( (70, 81, 92)), np.ones((70, 51, 112))) dtypes = (np.uint16, np.int32) slices = (slice(None), ) * 3, (slice(1, -1), ) * 3 for mask, dtype, sl in product(masks, dtypes, slices): mask = mask.astype(dtype)[sl] print(f"\nEDT {dtype.__name__} {mask.shape} slice {sl} ") with Timer("scipy "): ed1 = _edt_prob_scipy(mask, anisotropy=anisotropy) with Timer("edt: "): ed2 = _edt_prob_edt(mask, anisotropy=anisotropy) assert np.percentile(np.abs(ed1 - ed2), 99.9) < 1e-3 except ImportError: print("Install edt to run test")
import numpy as np import pytest from stardist import star_dist3D, Rays_GoldenSpiral, relabel_image_stardist3D from utils import random_image, real_image3d, check_similar, circle_image @pytest.mark.parametrize('img', (real_image3d()[1], random_image( (33, 44, 55)))) @pytest.mark.parametrize('n_rays', (4, 16, 32)) @pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4))) def test_types(img, n_rays, grid): mode = "cpp" rays = Rays_GoldenSpiral(n_rays) gt = star_dist3D(img, rays=rays, grid=grid, mode=mode) for dtype in (np.int8, np.int16, np.int32, np.uint8, np.uint16, np.uint32): x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode) print( "test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}" .format(mode=mode, img=img, dtype=dtype)) check_similar(gt, x) @pytest.mark.gpu @pytest.mark.parametrize('img', (real_image3d()[1], random_image( (33, 44, 55)))) @pytest.mark.parametrize('n_rays', (4, 16, 32)) @pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4))) def test_types_gpu(img, n_rays, grid): mode = "opencl" rays = Rays_GoldenSpiral(n_rays) gt = star_dist3D(img, rays=rays, grid=grid, mode=mode)
import numpy as np import pytest from stardist import star_dist3D, Rays_GoldenSpiral, relabel_image_stardist3D from utils import random_image, real_image3d, check_similar, circle_image, Timer from time import time @pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55)))) @pytest.mark.parametrize('n_rays', (4, 16, 32)) @pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4))) def test_types(img, n_rays, grid): mode = "cpp" rays = Rays_GoldenSpiral(n_rays) gt = star_dist3D(img, rays=rays, grid=grid, mode=mode) for dtype in (np.int8, np.int16, np.int32, np.uint8, np.uint16, np.uint32): x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode) print("test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}".format( mode=mode, img=img, dtype=dtype)) check_similar(gt, x) @pytest.mark.gpu @pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55)))) @pytest.mark.parametrize('n_rays', (4, 16, 32)) @pytest.mark.parametrize('grid', ((1, 1, 1), (1, 2, 4))) def test_types_gpu(img, n_rays, grid): mode = "opencl" rays = Rays_GoldenSpiral(n_rays) gt = star_dist3D(img, rays=rays, grid=grid, mode=mode) for dtype in (np.int8, np.int16, np.int32, np.uint8, np.uint16, np.uint32):
def _test_model_multiclass(n_classes=1, classes="auto", n_channel=None, basedir=None, epochs=20, batch_size=1): from skimage.measure import regionprops img, mask = real_image3d() img = normalize(img, 1, 99.8) if n_channel is not None: img = np.repeat(img[..., np.newaxis], n_channel, axis=-1) else: n_channel = 1 X, Y = [img, img, img], [mask, mask, mask] conf = Config3D( n_rays=32, grid=(2, 1, 2), n_channel_in=n_channel, n_classes=n_classes, use_gpu=False, train_epochs=1, train_steps_per_epoch=10, train_batch_size=batch_size, train_loss_weights=(1., .2) if n_classes is None else (1, .2, 1.), train_patch_size=(24, 32, 32), ) # efine some classes according to the areas if n_classes is not None and n_classes > 1 and classes == "auto": regs = regionprops(mask) areas = tuple(r.area for r in regs) inds = np.argsort(areas) ss = tuple( slice(n * len(regs) // n_classes, (n + 1) * len(regs) // n_classes) for n in range(n_classes)) classes = {} for i, s in enumerate(ss): for j in inds[s]: classes[regs[j].label] = i + 1 classes = (classes, ) * len(X) model = StarDist3D(conf, name=None if basedir is None else "stardist", basedir=str(basedir)) val_classes = {k: 1 for k in set(mask[mask > 0])} s = model.train(X, Y, classes=classes, epochs=epochs, validation_data=(X[:1], Y[:1]) if n_classes is None else (X[:1], Y[:1], (val_classes, ))) labels, res = model.predict_instances(img) # return model, X,Y, classes, labels, res img = np.tile(img, (4, 2, 2) if img.ndim == 3 else (4, 2, 2, 1)) kwargs = dict(prob_thresh=.5) labels1, res1 = model.predict_instances(img, **kwargs) labels2, res2 = model.predict_instances(img, sparse=True, **kwargs) labels3, res3 = model.predict_instances_big( img, axes="ZYX" if img.ndim == 3 else "ZYXC", block_size=96, min_overlap=8, context=8, **kwargs) assert np.allclose(labels1, labels2) assert all([ np.allclose(res1[k], res2[k]) for k in set(res1.keys()).union(set(res2.keys())) if isinstance(res1[k], np.ndarray) ]) return model, img, res1, res2, res3