Exemplo n.º 1
0
def map_energy(S):
    """
    negated log-posterior
    """
    ppm = S.ppm.copy()
    ppm[S.mask] = binarize_ppm(ppm[S.mask])
    return S.free_energy(ppm=ppm)
Exemplo n.º 2
0
def run_vem(data, mask, laplace_init=False):

    mu, sigma = moment_matching(data[mask])

    S = Segmentation(data, mask=mask, mu=mu, sigma=sigma, beta=0)
    S.run(niters=PRE_NITERS)
    S = Segmentation(data, mask=mask, mu=S.mu, sigma=S.sigma,
                     ngb_size=NGB_SIZE, beta=BETA)

    e, f, v, vf = [], [], [], []

    # Initialize ppm with RW optionally
    if laplace_init:
        S = run_rw(S)
        S.ppm[S.mask] = binarize_ppm(S.ppm[S.mask])

    for s in range(len(NITERS)):

        # Run VEM for a few iterations
        S, es, fs, vs, vfs = _run_vem(S, niters=NITERS[s])

        e += es
        f += fs
        v += vs
        vf += vfs

    return S, e, f, v, vf
Exemplo n.º 3
0
def post_vem(S, update_params=True):
    S.ppm[S.mask] = binarize_ppm(S.ppm[S.mask])
    e, f = [], []

    for it in range(NITERS):
        t0 = time()
        if update_params:
            S.vm_step()
        S.ve_step()
        dt = time() - t0 
        print('VEM iteration time: %f' % dt)
        f += [S.free_energy()]
        ppm = S.ppm.copy()
        ppm[S.mask] = binarize_ppm(ppm[S.mask])
        e += [S.free_energy(ppm=ppm)]
    return S, e, f
Exemplo n.º 4
0
def init_laplace(img, mask, mu, sigma):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                 ngb_size=NGB_SIZE, beta=BETA)
    prior, gamma = config_random_walker(S.ext_field(), BETA)
    q = random_walker(mask, prior, gamma)
    if BINARIZE:
        S.ppm[mask] = binarize_ppm(q)
    else:
        S.ppm[mask] = q
    return S
Exemplo n.º 5
0
def _run_vem(S, niters):

    e, f, v, vf = [], [], [], []

    for it in range(niters):

        print('VE-step...')
        S.ve_step()

        print('Compute energies...')
        f += [S.free_energy()]
        vf += [np.sum(S.ppm[S.mask], 0)]
        ppm = S.ppm.copy()
        ppm[S.mask] = binarize_ppm(ppm[S.mask])
        e += [S.free_energy(ppm=ppm)]
        v += [np.sum(ppm[S.mask], 0)]

    print('VM-step...')
    S.vm_step()

    return S, e, f, v, vf