Пример #1
0
def run_without_soft_lock(n_atoms=25,
                          atom_support=(12, 12),
                          reg=.01,
                          tol=5e-2,
                          n_workers=100,
                          random_state=60):
    rng = np.random.RandomState(random_state)

    X = get_mandril()
    D_init = init_dictionary(X, n_atoms, atom_support, random_state=rng)
    lmbd_max = get_lambda_max(X, D_init).max()
    reg_ = reg * lmbd_max

    z_hat, *_ = dicod(X,
                      D_init,
                      reg_,
                      max_iter=1000000,
                      n_workers=n_workers,
                      tol=tol,
                      strategy='greedy',
                      verbose=1,
                      soft_lock='none',
                      z_positive=False,
                      timing=False)
    pobj = compute_objective(X, z_hat, D_init, reg_)
    z_hat = np.clip(z_hat, -1e3, 1e3)
    print("[DICOD] final cost : {}".format(pobj))

    X_hat = reconstruct(z_hat, D_init)
    X_hat = np.clip(X_hat, 0, 1)
    return X_hat, pobj
Пример #2
0
def run_dicodile_hubble(size, reg, L):
    X = get_hubble(size=size)

    D_init = init_dictionary(X, n_atoms, (L, L), random_state=random_state)

    dicod_kwargs = dict(soft_lock='border')
    pobj, times, D_hat, z_hat = dicodile(X,
                                         D_init,
                                         reg=reg,
                                         z_positive=True,
                                         n_iter=100,
                                         n_workers=400,
                                         eps=1e-5,
                                         tol=1e-3,
                                         verbose=2,
                                         dicod_kwargs=dicod_kwargs)

    # Save the atoms
    prefix = (f"K{n_atoms}_L{L}_reg{reg}"
              f"_seed{random_state}_dicodile_{size}_")
    prefix = prefix.replace(" ", "")
    np.save(f"hubble/{prefix}D_hat.npy", D_hat)
    z_hat[z_hat < 1e-2] = 0
    z_hat_save = [sparse.csr_matrix(z) for z in z_hat]
    np.save(f"hubble/{prefix}z_hat.npy", z_hat_save)

    plot_atom_and_coefs(D_hat, z_hat, prefix)
Пример #3
0
def run_one_grid(n_atoms, atom_support, reg, n_workers, grid, tol, soft_lock,
                 dicod_args, random_state):

    tag = f"[{soft_lock} - {reg:.0e} - {random_state[0]}]"
    random_state = random_state[1]

    # Generate a problem
    print(
        colorify(79 * "=" + f"\n{tag} Start with {n_workers} workers\n" +
                 79 * "="))
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    if grid:
        w_world = 'auto'
    else:
        w_world = n_workers

    z_hat, *_, run_statistics = dicod(X,
                                      D,
                                      reg=reg_,
                                      n_seg='auto',
                                      strategy='greedy',
                                      w_world=w_world,
                                      n_workers=n_workers,
                                      timing=False,
                                      tol=tol,
                                      soft_lock=soft_lock,
                                      **dicod_args)

    runtime = run_statistics['runtime']
    sparsity = len(z_hat.nonzero()[0]) / z_hat.size

    print(
        colorify("=" * 79 + f"\n{tag} End for {n_workers} workers "
                 f"in {runtime:.1e}\n" + "=" * 79,
                 color=GREEN))

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_workers=n_workers,
                      grid=grid,
                      tol=tol,
                      soft_lock=soft_lock,
                      random_state=random_state,
                      dicod_args=dicod_args,
                      sparsity=sparsity,
                      **run_statistics)
Пример #4
0
def run_one_grid(n_atoms, atom_support, reg, n_jobs, grid, tol, random_state,
                 verbose):
    # Generate a problem
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    if grid:
        w_world = 'auto'
    else:
        w_world = n_jobs

    dicod_kwargs = dict(z_positive=False,
                        soft_lock='corner',
                        timeout=None,
                        max_iter=int(1e8))
    z_hat, *_, pobj, cost = dicod(X,
                                  D,
                                  reg=reg_,
                                  n_seg='auto',
                                  strategy='greedy',
                                  w_world=w_world,
                                  n_jobs=n_jobs,
                                  timing=True,
                                  tol=tol,
                                  verbose=verbose,
                                  **dicod_kwargs)

    sparsity = len(z_hat.nonzero()[0]) / z_hat.size

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_jobs=n_jobs,
                      grid=grid,
                      tol=tol,
                      random_state=random_state,
                      sparsity=sparsity,
                      pobj=pobj)
Пример #5
0
def run_one_scaling_2d(n_atoms, atom_support, reg, n_workers, strategy, tol,
                       dicod_args, random_state):
    tag = f"[{strategy} - {reg:.0e} - {random_state[0]}]"
    random_state = random_state[1]

    # Generate a problem
    print(
        colorify(79 * "=" + f"\n{tag} Start with {n_workers} workers\n" +
                 79 * "="))
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    z_hat, *_, run_statistics = dicod(X,
                                      D,
                                      reg=reg_,
                                      strategy=strategy,
                                      n_workers=n_workers,
                                      tol=tol,
                                      **dicod_args)

    runtime = run_statistics['runtime']
    sparsity = len(z_hat.nonzero()[0]) / z_hat.size
    print(
        colorify('=' * 79 + f"\n{tag} End with {n_workers} workers for reg="
                 f"{reg:.0e} in {runtime:.1e}\n" + "=" * 79,
                 color=GREEN))

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_workers=n_workers,
                      strategy=strategy,
                      tol=tol,
                      dicod_args=dicod_args,
                      random_state=random_state,
                      sparsity=sparsity,
                      **run_statistics)
Пример #6
0
def get_D_init(X,
               n_atoms,
               atom_support,
               strategy='patch',
               window=True,
               noise_level=0.1,
               random_state=None):
    """Compute an initial dictionary

    Parameters
    ----------
    X : ndarray, shape (n_channels, *signal_support)
        signal to be encoded.
    n_atoms: int and tuple
        Determine the shape of the dictionary.
    atom_support: tuple (int, int)
        support of the atoms
    strategy: str in {'patch', 'random'} (default: 'patch')
        Strategy to compute initial dictionary:
           - 'random': draw iid coefficients iid in [0, 1]
           - 'patch': draw patches from X uniformly without replacement.
    window: boolean (default: True)
        Whether or not the algorithm will use windowed dictionary.
    noise_level: float (default: .1)
        If larger than 0, add gaussian noise to the initial dictionary. This
        helps escaping sub-optimal state where one atom is used only in one
        place with strategy='patch'.
    random_state : int, RandomState instance or None (default)
        Determines random number generation for centroid initialization and
        random reassignment. Use an int to make the randomness deterministic.

    Returns
    -------
    D_init : ndarray, shape (n_atoms, n_channels, *atom_support)
        initial dictionary
    """
    rng = check_random_state(random_state)

    n_channels = X.shape[0]
    if strategy == 'random':
        D_init = rng.rand(n_atoms, n_channels, *atom_support)
    elif strategy == 'patch':
        D_init = init_dictionary(X,
                                 n_atoms=n_atoms,
                                 atom_support=atom_support,
                                 random_state=rng)
    else:
        raise NotImplementedError('strategy should be one of {patch, random}')

    # normalize the atoms
    D_init = prox_d(D_init)

    # Add a small noise to extracted patches. does not have a large influence
    # on the random init.
    if noise_level > 0:
        noise_level_ = noise_level * D_init.std(axis=(-1, -2), keepdims=True)
        noise = noise_level_ * rng.randn(*D_init.shape)
        D_init = prox_d(D_init + noise)

    # If the algorithm is windowed, correctly initiate the dictionary
    if window:
        atom_support = D_init.shape[-2:]
        tw = tukey_window(atom_support)[None, None]
        D_init *= tw

    return D_init
Пример #7
0
def run_one(method, n_atoms, atom_support, reg, z_positive, n_jobs, n_iter,
            tol, eps, random_state):

    X = get_hubble()[:, 512:1024, 512:1024]
    D_init = init_dictionary(X,
                             n_atoms,
                             atom_support,
                             random_state=random_state)

    if method == 'wohlberg':
        ################################################################
        #            Run parallel consensus ADMM
        #
        lmbd_max = get_lambda_max(X, D_init).max()
        print("Lambda max = {}".format(lmbd_max))
        reg_ = reg * lmbd_max

        D_init_ = np.transpose(D_init, axes=(3, 2, 1, 0))
        X_ = np.transpose(X[None], axes=(3, 2, 1, 0))

        options = {
            'Verbose': True,
            'StatusHeader': False,
            'MaxMainIter': n_iter,
            'CCMOD': {
                'rho': 1.0,
                'ZeroMean': False
            },
            'CBPDN': {
                'rho': 50.0 * reg_ + 0.5,
                'NonNegCoef': z_positive
            },
            'DictSize': D_init_.shape,
        }
        opt = ConvBPDNDictLearn_Consensus.Options(options)
        cdl = ConvBPDNDictLearn_Consensus(D_init_,
                                          X_,
                                          lmbda=reg_,
                                          nproc=n_jobs,
                                          opt=opt,
                                          dimK=1,
                                          dimN=2)

        _, pobj = cdl.solve()
        print(pobj)

        itstat = cdl.getitstat()
        times = itstat.Time

    elif method == "dicodile":
        pobj, times, D_hat, z_hat = dicodile(X,
                                             D_init,
                                             reg=reg,
                                             z_positive=z_positive,
                                             n_iter=n_iter,
                                             eps=eps,
                                             n_jobs=n_jobs,
                                             verbose=2,
                                             tol=tol)
        pobj = pobj[::2]
        times = np.cumsum(times)[::2]

    else:
        raise NotImplementedError()

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_jobs=n_jobs,
                      random_state=random_state,
                      method=method,
                      z_positive=z_positive,
                      times=times,
                      pobj=pobj)