Exemplo n.º 1
0
def test_bbox_search(img):
    prob = edt_prob(img)
    dist = star_dist(img, n_rays=32, mode="cpp")
    coord = dist_to_coord(dist)
    nms_a = non_maximum_suppression(
        coord, prob, prob_thresh=0.4, verbose=False, max_bbox_search=False)
    nms_b = non_maximum_suppression(
        coord, prob, prob_thresh=0.4, verbose=False, max_bbox_search=True)
    check_similar(nms_a, nms_b)
Exemplo n.º 2
0
def test_old_new(shape, n_rays, grid, radius=10, noise=.1, nms_thresh=.3):
    np.random.seed(42)
    from stardist.geometry.geom2d import _polygons_to_label_old, _dist_to_coord_old
    from stardist.nms import _non_maximum_suppression_old

    prob, dist = create_random_data(shape, n_rays = n_rays, radius=10, noise=.1)
    prob = prob[::grid[0],::grid[1]]
    dist = dist[::grid[0],::grid[1]]
    coord = _dist_to_coord_old(dist, grid = grid)
    inds1 = _non_maximum_suppression_old(coord, prob, prob_thresh=0.9,
                                         grid = grid,
                                         nms_thresh=nms_thresh,
                                         verbose=True)
    points1 = inds1*np.array(grid)
    sort_ind = np.argsort(prob[tuple(inds1.T)])[::-1]
    points1 = points1[sort_ind]

    points2, probi2, disti2 = non_maximum_suppression(dist, prob,
                                                      grid = grid, prob_thresh=0.9,
                                                      nms_thresh=nms_thresh,
                                                      verbose=True)

    img1 = _polygons_to_label_old(coord, prob, inds1, shape=shape)
    img2 = polygons_to_label(disti2, points2, shape=shape)

    assert len(points1) == len(points2)
    assert np.allclose(points1, points2)
    assert np.allclose(img1>0, img2>0)
    
    return points1, img1, points2, img2
Exemplo n.º 3
0
def create_random_suppressed(shape=(356, 299), radius=10, noise=.1, n_rays=32, nms_thresh=.4):
    prob, dist = create_random_data(shape, radius, noise, n_rays)
    coord = dist_to_coord(dist)
    nms = non_maximum_suppression(coord, prob, prob_thresh=0.9,
                                  nms_thresh=nms_thresh,
                                  verbose=True)
    return nms
Exemplo n.º 4
0
def test_acc(img, grid):
    prob = edt_prob(img)[::grid[0],::grid[1]]
    dist = star_dist(img, n_rays=32, mode="cpp")[::grid[0],::grid[1]]
    points, probi, disti = non_maximum_suppression(dist, prob, grid = grid, prob_thresh=0.4)
    img2 = polygons_to_label(disti, points, shape=img.shape)
    m = matching(img, img2)
    acc = m.accuracy
    print("accuracy {acc:.2f}".format(acc=acc))
    assert acc > 0.9
Exemplo n.º 5
0
def create_random_suppressed(shape=(356, 299), grid = (1,1), radius=10, noise=.1, n_rays=32, nms_thresh=.1):
    prob, dist = create_random_data(shape, radius, noise, n_rays)
    prob = prob[::grid[0],::grid[1]]
    dist = dist[::grid[0],::grid[1]]
    points, probi, disti = non_maximum_suppression(dist, prob, prob_thresh=0.9,
                                  nms_thresh=nms_thresh,
                                  verbose=True)
    img = polygons_to_label(disti, points, prob=probi, shape=shape)
    return img
Exemplo n.º 6
0
def test_acc(img):
    prob = edt_prob(img)
    dist = star_dist(img, n_rays=32, mode="cpp")
    coord = dist_to_coord(dist)
    points = non_maximum_suppression(coord, prob, prob_thresh=0.4)
    img2 = polygons_to_label(coord, prob, points, shape=img.shape)
    m = matching(img, img2)
    acc = m.accuracy
    print("accuracy {acc:.2f}".format(acc=acc))
    assert acc > 0.9
Exemplo n.º 7
0
    def _f2(n):
        _prob = np.tile(prob, (n,n))
        _dist = np.tile(dist, (n,n,1))

        t = time()
        points2, probi2, disti2 = non_maximum_suppression(_dist, _prob,
                                                          prob_thresh=prob_thresh,
                                                          nms_thresh=.2,
                                                          use_kdtree = True,
                                                      verbose=True)
        t = time()-t
        return np.count_nonzero(_prob>prob_thresh), t
Exemplo n.º 8
0
def _star_convex_polynoms(dapi_path,
                          membrane_dic,
                          model_path,
                          full_output=False):
    """Nuclei segmentation using the stardist algorithm.

    Parameters
    ----------
    dapi_path : string
      path to the DAPI image
    membrane_dic: dic obtained with extract_membranes.
      Dictionnary containing relevant informations about the membranes.
    model_path : string
      path to the stardist model

    Returns
    -------

    clockwise_centers : np.ndarray
      An array of cell centers, sort in clockwise order.
    """
    images = sorted(glob(dapi_path))
    images = list(map(imread, images))
    img = normalize(images[0], 1, 99.8)
    model_sc = StarDist2D(None,
                          name="stardist_shape_completion",
                          basedir=model_path)
    prob, dist = model_sc.predict(img)
    coord = dist_to_coord(dist)
    points = non_maximum_suppression(coord, prob, prob_thresh=0.4)
    points = np.flip(points, 1)

    rho, phi = _card_coords(points, membrane_dic["center_inside"])
    cleaned = _quick_del_art(points, rho, membrane_dic["rIn"])
    clockwise_centers = _quick_clockwise(cleaned, phi, rho,
                                         membrane_dic["rIn"])

    clockwise_centers = np.subtract(np.float32(clockwise_centers),
                                    np.array(membrane_dic["img_shape"]) / 2.0)
    if full_output:
        return clockwise_centers, (prob, dist, points)

    return clockwise_centers
Exemplo n.º 9
0
def test_speed(nms_thresh = 0.3, grid = (1,1)):
    np.random.seed(42)
    from stardist.geometry.geom2d import _polygons_to_label_old, _dist_to_coord_old
    from stardist.nms import _non_maximum_suppression_old
    from time import time

    shape = (128,128)
    prob, dist = create_random_data(shape, n_rays = 32, radius=10, noise=.1)
    prob = np.tile(prob, (8,8))
    dist = np.tile(dist, (8,8,1))
    prob = prob[::grid[0],::grid[1]]
    dist = dist[::grid[0],::grid[1]]

    t1 = time()
    coord = _dist_to_coord_old(dist, grid = grid)
    points1 = _non_maximum_suppression_old(coord, prob, prob_thresh=0.9,
                                           grid = grid,
                                           nms_thresh=nms_thresh,
                                           verbose=True)
    t1 = time()-t1

    points1 = points1*np.array(grid)
    sort_ind = np.argsort(prob[tuple(points1.T)])[::-1]
    points1 = points1[sort_ind]

    t2 = time()

    points2, probi2, disti2 = non_maximum_suppression(dist, prob,
                                                      grid = grid, prob_thresh=0.9,
                                                      nms_thresh=nms_thresh,
                                                      use_kdtree = True,
                                                      verbose=True)
    t2 = time()-t2

    print("\n\n")
    print(f"old         : {t1:.2f}s")
    print(f"new (kdtree): {t2:.2f}s")

    return points1, points2
Exemplo n.º 10
0
    plt.show()
        
def predictions(model_dist,i):
    img = normalize(X[i],1,99.8,axis=axis_norm)
    input = torch.tensor(img)
    input = input.unsqueeze(0).unsqueeze(0)#unsqueeze 2 times
    dist,prob = model_dist(input)
    dist_numpy= dist.detach().cpu().numpy().squeeze()
    prob_numpy= prob.detach().cpu().numpy().squeeze()
    return dist_numpy,prob_numpy
    
model_dist = UNet(1,N_RAYS)
model_dist.load_state_dict(torch.load(MODEL_WEIGHTS_PATH))
print('Distance weights loaded')

apscore_nms = []
prob_thres = 0.4
for idx,img_target in enumerate(zip(X,Y)):
    print(idx)
    image,target = img_target
    dists,probs=predictions(model_dist,idx)
    dists = np.transpose(dists,(1,2,0))
    coord = dist_to_coord(dists)
    points = non_maximum_suppression(coord,probs,prob_thresh=prob_thres)
    star_label = polygons_to_label(coord,probs,points)
    apscore_nms.append(metric.calculateAPScore(star_label,target,IOU_tau=0.5))
    #plot(target,star_label) 
print('Total images',idx+1)
ap_nms = sum(apscore_nms)/(len(apscore_nms))
print('AP NMS',ap_nms)
   
Exemplo n.º 11
0
import numpy as np
from stardist import non_maximum_suppression

n=128

prob = np.random.uniform(0,1,(n,n))
coord = np.random.uniform(0,10,(n,n,2,32))

inds = non_maximum_suppression(coord, prob, nms_thresh =.3)
inds = non_maximum_suppression(coord, prob, nms_thresh =.5)
inds = non_maximum_suppression(coord, prob, nms_thresh =.7)