Exemple #1
0
def test_relabel_consistency(n_rays, eps, plot=False):
    """ test whether an already star-convex label image gets perfectly relabeld"""

    # img = random_image((128, 123))
    lbl1 = circle_image(shape=(32, 32), radius=8, eps=eps)

    lbl1 = relabel_image_stardist(lbl1, n_rays)

    lbl2 = relabel_image_stardist(lbl1, n_rays)

    rel_error = 1 - np.count_nonzero(np.bitwise_and(
        lbl1 > 0, lbl2 > 0)) / np.count_nonzero(lbl1 > 0)
    print(rel_error)
    assert rel_error < 1e-1

    if plot:
        import matplotlib.pyplot as plt
        plt.figure(num=1, figsize=(8, 4))
        plt.subplot(1, 3, 1)
        plt.imshow(lbl1)
        plt.title("GT")
        plt.subplot(1, 3, 2)
        plt.imshow(lbl2)
        plt.title("Reco")
        plt.subplot(1, 3, 3)
        plt.imshow(1 * (lbl1 > 0) + 2 * (lbl2 > 0))
        plt.title("Overlay")
        plt.tight_layout()
        plt.show()

    return lbl1, lbl2
Exemple #2
0
def test_model(tmpdir, n_rays, grid, n_channel, backbone):
    img = circle_image(shape=(64, 80, 96))
    imgs = np.repeat(img[np.newaxis], 3, axis=0)

    if n_channel is not None:
        imgs = np.repeat(imgs[..., np.newaxis], n_channel, axis=-1)
    else:
        n_channel = 1

    X = imgs + .6 * np.random.uniform(0, 1, imgs.shape)
    Y = (imgs if imgs.ndim == 4 else imgs[..., 0]).astype(int)

    conf = Config3D(
        backbone=backbone,
        rays=n_rays,
        grid=grid,
        n_channel_in=n_channel,
        use_gpu=False,
        train_epochs=1,
        train_steps_per_epoch=2,
        train_batch_size=2,
        train_loss_weights=(4, 1),
        train_patch_size=(48, 64, 64),
    )

    model = StarDist3D(conf, name='stardist', basedir=str(tmpdir))
    model.train(X, Y, validation_data=(X[:2], Y[:2]))
    ref = model.predict(X[0])
    res = model.predict(X[0],
                        n_tiles=((1, 2, 3) if X[0].ndim == 3 else
                                 (1, 2, 3, 1)))
Exemple #3
0
def test_model(tmpdir, n_rays, grid, n_channel):
    img = circle_image(shape=(160,160))
    imgs = np.repeat(img[np.newaxis], 3, axis=0)

    if n_channel is not None:
        imgs = np.repeat(imgs[...,np.newaxis], n_channel, axis=-1)
    else:
        n_channel = 1

    X = imgs+.6*np.random.uniform(0,1,imgs.shape)
    Y = (imgs if imgs.ndim==3 else imgs[...,0]).astype(int)

    conf = Config2D (
        n_rays                = n_rays,
        grid                  = grid,
        n_channel_in          = n_channel,
        use_gpu               = False,
        train_epochs          = 1,
        train_steps_per_epoch = 2,
        train_batch_size      = 2,
        train_loss_weights    = (4,1),
        train_patch_size      = (128,128),
    )

    model = StarDist2D(conf, name='stardist', basedir=str(tmpdir))
    model.train(X, Y, validation_data=(X[:2],Y[:2]))
Exemple #4
0
def test_relabel_consistency(n_rays, eps, plot = False):
    """ test whether an already star-convex label image gets perfectly relabeld"""

    rays = Rays_GoldenSpiral(n_rays, anisotropy = 1./np.array(eps))
    # img = random_image((128, 123))
    lbl1 = circle_image(shape=(32,32,32), radius=8, eps = eps)
    
    lbl1 = relabel_image_stardist3D(lbl1, rays)

    lbl2 = relabel_image_stardist3D(lbl1, rays)

    rel_error = 1-np.count_nonzero(np.bitwise_and(lbl1>0, lbl2>0))/np.count_nonzero(lbl1>0)
    print(rel_error)
    assert rel_error<1e-1

    if plot:
        import matplotlib.pyplot as plt
        plt.figure(num=1, figsize=(8,4))
        plt.subplot(2,3,1);plt.imshow(np.max(lbl1,0));plt.title("GT")
        plt.subplot(2,3,2);plt.imshow(np.max(lbl2,0));plt.title("Reco")
        plt.subplot(2,3,3);plt.imshow(np.max(1*(lbl1>0)+2*(lbl2>0),0));plt.title("Overlay")
        plt.subplot(2,3,4);plt.imshow(np.max(lbl1,1));plt.title("GT")
        plt.subplot(2,3,5);plt.imshow(np.max(lbl2,1));plt.title("Reco")
        plt.subplot(2,3,6);plt.imshow(np.max(1*(lbl1>0)+2*(lbl2>0),1));plt.title("Overlay")
        plt.tight_layout()
        plt.show()
        
    return lbl1, lbl2
Exemple #5
0
def test_grid(grid,shape):
    ss_grid = tuple(slice(0, None, g) for g in grid)
    lbl = circle_image(shape=shape, radius=min(shape)//3)

    n_rays = 32
    d1 = star_dist(lbl, n_rays=n_rays)
    d1 = d1[ss_grid]
    d2 = star_dist(lbl, n_rays=n_rays, grid=grid)

    assert np.allclose(d1,d2)
    return lbl, d1, d2
Exemple #6
0
def test_model(tmpdir, n_rays, grid, n_channel, backbone):
    img = circle_image(shape=(64, 80, 96))
    imgs = np.repeat(img[np.newaxis], 3, axis=0)

    if n_channel is not None:
        imgs = np.repeat(imgs[..., np.newaxis], n_channel, axis=-1)
    else:
        n_channel = 1

    X = imgs + .6 * np.random.uniform(0, 1, imgs.shape)
    Y = (imgs if imgs.ndim == 4 else imgs[..., 0]).astype(int)

    conf = Config3D(
        backbone=backbone,
        rays=n_rays,
        grid=grid,
        n_channel_in=n_channel,
        use_gpu=False,
        train_epochs=1,
        train_steps_per_epoch=2,
        train_batch_size=2,
        train_loss_weights=(4, 1),
        train_patch_size=(48, 64, 64),
    )

    model = StarDist3D(conf, name='stardist', basedir=str(tmpdir))
    model.train(X, Y, validation_data=(X[:2], Y[:2]))
    ref = model.predict(X[0])
    res = model.predict(X[0],
                        n_tiles=((1, 2, 3) if X[0].ndim == 3 else
                                 (1, 2, 3, 1)))
    # assert all(np.allclose(u,v) for u,v in zip(ref,res))

    # ask to train only with foreground patches when there are none
    # include a constant label image that must trigger a warning
    conf.train_foreground_only = 1
    conf.train_steps_per_epoch = 1
    _X = X[:2]
    _Y = [np.zeros_like(Y[0]), np.ones_like(Y[1])]
    with pytest.warns(UserWarning):
        StarDist3D(conf, name='stardist',
                   basedir=None).train(_X,
                                       _Y,
                                       validation_data=(X[-1:], Y[-1:]))
Exemple #7
0
def test_model(n_rays, grid):
    img = circle_image()
    imgs = np.repeat(img[np.newaxis], 10, axis=0)

    X = imgs + .6 * np.random.uniform(0, 1, imgs.shape)
    Y = imgs.astype(int)

    conf = Config2D(n_rays=n_rays,
                    grid=grid,
                    use_gpu=False,
                    train_epochs=1,
                    train_steps_per_epoch=10,
                    train_loss_weights=(4, 1),
                    train_patch_size=(128, 128),
                    n_channel_in=1)

    with tempfile.TemporaryDirectory() as tmp:
        model = StarDist2D(conf, name='stardist', basedir=tmp)
        model.train(X, Y, validation_data=(X[:3], Y[:3]))
Exemple #8
0
def test_grid(grid,shape):
    ss_grid = tuple(slice(0, None, g) for g in grid)
    
    lbl = circle_image(shape=shape, radius=min(shape)//3)

    rays = Rays_GoldenSpiral(32)
    
    t1 = time()
    d1 = star_dist3D(lbl, rays = rays)
    d1 = d1[ss_grid]
    t1 = time()-t1
    
    t2 = time()
    d2 = star_dist3D(lbl, rays = rays, grid = grid)
    t2 = time()-t2

    print(f"time 1: {1000*t1:.2f}ms")
    print(f"time 2: {1000*t2:.2f}ms")
    assert np.allclose(d1,d2)
    return lbl, d1, d2
Exemple #9
0
def test_model(tmpdir, n_rays, grid, n_channel, backbone, workers,
               use_sequence):
    img = circle_image(shape=(64, 80, 96))
    imgs = np.repeat(img[np.newaxis], 3, axis=0)

    if n_channel is not None:
        imgs = np.repeat(imgs[..., np.newaxis], n_channel, axis=-1)
    else:
        n_channel = 1

    X = imgs + .6 * np.random.uniform(0, 1, imgs.shape)
    Y = (imgs if imgs.ndim == 4 else imgs[..., 0]).astype(int)

    if use_sequence:
        X, Y = NumpySequence(X), NumpySequence(Y)

    conf = Config3D(
        backbone=backbone,
        rays=n_rays,
        grid=grid,
        n_channel_in=n_channel,
        use_gpu=False,
        train_epochs=1,
        train_steps_per_epoch=1,
        train_batch_size=2,
        train_loss_weights=(4, 1),
        train_patch_size=(48, 64, 32),
        train_sample_cache=not use_sequence,
    )

    model = StarDist3D(conf, name='stardist', basedir=str(tmpdir))
    model.train(X, Y, validation_data=(X[:2], Y[:2]), workers=workers)
    ref = model.predict(X[0])
    res = model.predict(X[0],
                        n_tiles=((1, 2, 3) if X[0].ndim == 3 else
                                 (1, 2, 3, 1)))

    # deactivate as order of labels might not be the same
    # assert all(np.allclose(u,v) for u,v in zip(ref,res))
    return model
Exemple #10
0
    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):
        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_cpu_gpu(img, n_rays, grid):
    rays = Rays_GoldenSpiral(n_rays)
    s_cpp = star_dist3D(img, rays=rays, grid=grid, mode="cpp")
    s_ocl = star_dist3D(img, rays=rays, grid=grid, mode="opencl")
    check_similar(s_cpp, s_ocl)


if __name__ == '__main__':
    from utils import circle_image

    rays = Rays_GoldenSpiral(4)
    lbl = circle_image((64,) * 3)

    a = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="cpp")
    b = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="opencl")
    print(np.amax(np.abs(a - b)))