Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
    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)