Beispiel #1
0
    def descent(self, tolerance=1e-4, max_iter=150):

        self.operator.get_spec_rad()
        cost0 = 0.0

        for i in range(max_iter):

            grad = self.operator.grad_step(self.algorithm.data_rec, self.data)
            self.algorithm.check_threshold(i, grad)
            self.algorithm.update(grad, self.operator.inv_spec_rad)

            l2norm0 = norm(self.data - pca_convolve(self.algorithm.data_rec_prev, self.operator.psf_pcs, self.operator.psf_coef), 2)
            l2norm1 = norm(self.data - pca_convolve(self.algorithm.data_rec, self.operator.psf_pcs, self.operator.psf_coef), 2)

            if l2norm1 > l2norm0:
                self.algorithm.speed_switch(False)

            nuc_norm = nuclear_norm(map2matrix(self.algorithm.data_rec,
                                    self.layout))
            l2norm = norm(self.data - pca_convolve(self.algorithm.data_rec,
                          self.operator.psf_pcs, self.operator.psf_coef), 2)
            cost = (0.5 * l2norm ** 2 + self.algorithm.thresh * nuc_norm)

            print ' - i:', i, cost, l2norm, nuc_norm

            if np.abs(cost - cost0) < tolerance:
                print ' - Gradient Descent converged after %d iterations!' % \
                      (i + 1)
                print ' - Final cost, l2norm, nuc_norm:', cost, l2norm, nuc_norm
                break

            elif i == max_iter - 1:
                print ' - Gradient Descent did not converge after %d iterations!' \
                      % max_iter
                print ' - Final cost, l2norm, nuc_norm:', cost, l2norm, nuc_norm

            cost0 = cost

        return self.algorithm.data_rec
Beispiel #2
0
def condat(image, psf_pcs, psf_coef, image_rec=None, layout=None,
           inv_spec_rad=None, filters=None, wavelet_levels=None,
           wavelet_opt=None, weights=None, thresh_factor=3, n_reweights=0,
           tolerance=1e-4, max_iter=150):

    print ' - Wavelet Levels:', wavelet_levels
    print ' - Threshold Factor:', thresh_factor
    print ' - Reweights:', n_reweights

    if isinstance(filters, type(None)):
            filters = get_mr_filters(image.shape, wavelet_levels, wavelet_opt)

    filters_rot = rotate_filters(filters)

    x_old = np.ones(image.shape)
    y_old = np.ones([filters.shape[0]] + list(image.shape))

    transpose_test(convolve_mr_filters, deconvolve_mr_filters, x_old.shape,
                   (filters,), y_old.shape, (filters_rot,))

    if isinstance(weights, type(None)):
        error_weights = get_noise_est(psf_pcs, psf_coef, filters)

    if isinstance(inv_spec_rad, type(None)):
        beta = power_method(psf_pcs, psf_coef)
    else:
        beta = 1.0 / inv_spec_rad

    l1norm_filters = sum([norm(filter, 1) for filter in filters])
    tau = 1.0 / (beta + l1norm_filters)
    # tau = (l1norm_filters**2 + inv_spec_rad**2/2)**(-1)
    sigma = tau
    # sigma=1
    rho_n = 0.5

    print ''
    print ' SPEC_RAD:', beta
    print ' SIGMA:', sigma
    print ' TAU:', tau
    print ' TAU/SIGMA TEST:', (1 / tau - sigma * l1norm_filters ** 2 >= beta / 2)
    print ' RHO_n:', rho_n
    print ''
    print ' i COST          L2_NORM       L1_NORM'

    # Set initial weights
    weights = thresh_factor * np.copy(error_weights)

    for j in range(n_reweights + 1):
        for i in range(max_iter):

            grad = grad_step(x_old, image, psf_coef, psf_pcs)
            # weights = sigma * get_sigma_map(grad)

            x_prox = x_old - tau * grad - tau * deconvolve_mr_filters(y_old, filters_rot)
            x_temp = keep_positive(x_prox)

            y_prox = y_old + sigma * convolve_mr_filters(2 * x_temp - x_old, filters)
            y_temp = y_prox - sigma * weighted_threshold(y_prox / sigma, 1 / sigma, weights)

            x_new, y_new = rho_n * np.array([x_temp, y_temp]) + (1 - rho_n) * np.array([x_old, y_old])

            np.copyto(x_old, x_new)
            np.copyto(y_old, y_new)

            l2norm = norm(image - pca_convolve(x_new, psf_pcs, psf_coef), 2)
            l1norm = sum([norm(a, 1) for a in np.multiply(weights, convolve_mr_filters(x_new, filters))])
            cost = 0.5 * l2norm ** 2 + sigma * l1norm

            print '', i, cost, l2norm, l1norm

        # Reweight
        x_wave = convolve_mr_filters(x_new, filters)
        weights *= (1.0 / (1.0 + np.abs(x_wave) / (thresh_factor * error_weights)))
        print ''

    return x_new
Beispiel #3
0
def gradient_descent(image, psf_pcs, psf_coef, image_rec=None, layout=None,
                     inv_spec_rad=None, thresh_factor=3, tolerance=1e-4,
                     max_iter=150):

    operator = PixelVariantPSF(psf_pcs, psf_coef)
    operator.get_spec_rad()
    algorithm = LowRankMatrix(image.shape, layout)

    if isinstance(image_rec, type(None)):

        # Set inital guess for gradient descent.
        image_rec = np.ones(image.shape)

        # Get the inverse spectral radius
        if isinstance(inv_spec_rad, type(None)):
            inv_spec_rad = operator.inv_spec_rad

        rec_flag = False

    else:

        # Get SVD of the initial image reconstruction.
        image_matrix = map2matrix(image_rec, layout)
        u, s, v = np.linalg.svd(image_matrix)
        s = np.diag(s)
        s.resize(image_matrix.shape)
        a = np.dot(s, v)
        a0 = np.copy(a)

        # Get the inverse spectral radius
        if isinstance(inv_spec_rad, type(None)):
            inv_spec_rad = 1. / power_method_UA([u, a], psf_pcs, psf_coef,
                                                [image.shape, layout])

        rec_flag = True

    # Set inital values for testing convergence.
    image_rec_xn = np.copy(image_rec)
    image_rec_zn = np.copy(image_rec_xn)
    cost0 = 0.0
    t0 = 1.0
    nuc_norm = 0.0
    use_speed_up = True

    print '   * 1/rho =', inv_spec_rad

    # Perfom gradient descent to reconstruct image.
    for i in range(max_iter):

        if rec_flag:
            image_matrix = map2matrix(-1. * operator.grad_step(image_rec_zn,
                                      image), layout)
            a1 = a + inv_spec_rad * np.dot(u.T, image_matrix)
            a1 = np.dot(u.T, keep_positive(np.dot(u, a1)))
            a, t0 = speed_up(a1, a0, t0)
            image_rec_zn = matrix2map(np.dot(u, a), image.shape)
            a0 = np.copy(a1)

        else:
            # Calculate the gradient for this step.
            grad = operator.grad_step(image_rec_zn, image)

            algorithm.check_threshold(i, grad)
            algorithm.update(grad, inv_spec_rad)

            l2norm0 = norm(image - pca_convolve(algorithm.data_rec_prev, psf_pcs, psf_coef), 2)
            l2norm1 = norm(image - pca_convolve(algorithm.data_rec, psf_pcs, psf_coef), 2)

            if l2norm1 > l2norm0:
                use_speed_up = False
            if use_speed_up:
                image_rec_zn, t0 = speed_up(algorithm.data_rec, algorithm.data_rec_prev, t0)
            else:
                image_rec_zn = np.copy(algorithm.data_rec)

        u3, s3, v3 = np.linalg.svd(map2matrix(image_rec_zn, layout))
        nuc_norm = np.sum(s3)
        l2norm = norm(image - pca_convolve(image_rec_zn, psf_pcs, psf_coef), 2)
        cost = (0.5 * l2norm ** 2 + algorithm.thresh * nuc_norm)
        # print ' - i:', i, cost, thresh, l2norm

        if np.abs(cost - cost0) < tolerance:
            print ' - Gradient Descent converged after %d iterations!' % \
                  (i + 1)
            print ' - Final cost, thresh, l2norm:', cost, algorithm.thresh, l2norm
            break

        elif i == max_iter - 1:
            print ' - Gradient Descent did not converge after %d iterations!' \
                  % max_iter
            print ' - Final cost, thresh, l2norm:', cost, algorithm.thresh, l2norm

        cost0 = cost

    image_rec = image_rec_zn

    return image_rec
Beispiel #4
0
def gradient_descent(image, psf_pcs, psf_coef, image_rec=None, layout=None,
                     inv_spec_rad=None, thresh_factor=3, tolerance=1e-4,
                     max_iter=150):

    operator = PixelVariantPSF(psf_pcs, psf_coef)

    if isinstance(image_rec, type(None)):

        # Set inital guess for gradient descent.
        image_rec = np.ones(image.shape)

        # Get the inverse spectral radius
        if isinstance(inv_spec_rad, type(None)):
            inv_spec_rad = 1. / power_method(psf_pcs, psf_coef)

        rec_flag = False

    else:

        # Get SVD of the initial image reconstruction.
        image_matrix = map2matrix(image_rec, layout)
        u, s, v = np.linalg.svd(image_matrix)
        s = np.diag(s)
        s.resize(image_matrix.shape)
        a = np.dot(s, v)
        a0 = np.copy(a)

        # Get the inverse spectral radius
        if isinstance(inv_spec_rad, type(None)):
            inv_spec_rad = 1. / power_method_UA([u, a], psf_pcs, psf_coef,
                                                [image.shape, layout])

        rec_flag = True

    # Set inital values for testing convergence.
    image_rec_xn = np.copy(image_rec)
    image_rec_zn = np.copy(image_rec_xn)
    cost0 = 0.0
    t0 = 1.0
    nuc_norm = 0.0
    thresh = 0.0
    thresh0 = 100.0
    update_thresh = True
    use_speed_up = True

    print '   * 1/rho =', inv_spec_rad

    # Perfom gradient descent to reconstruct image.
    for i in range(max_iter):

        if rec_flag:
            image_matrix = map2matrix(-1. * grad_step(image_rec_zn, image,
                                      psf_coef, psf_pcs), layout)
            a1 = a + inv_spec_rad * np.dot(u.T, image_matrix)
            a1 = np.dot(u.T, keep_positive(np.dot(u, a1)))
            a, t0 = speed_up(a1, a0, t0)
            image_rec_zn = matrix2map(np.dot(u, a), image.shape)
            a0 = np.copy(a1)

        else:
            # Calculate the gradient for this step.
            # grad = grad_step(image_rec_zn, image, psf_coef, psf_pcs)
            grad = operator.grad_step(image_rec_zn, image)

            uu, ss, vv = np.linalg.svd(map2matrix(grad, layout))
            if update_thresh:
                thresh = thresh_factor * np.median(ss)
                if np.abs(thresh - thresh0) < tolerance:
                    update_thresh = False
                    print ' - Threshold converged!'
                else:
                    thresh0 = thresh
            if update_thresh and i == 50:
                update_thresh = False
                print ' - Threshold stabalised after 50 iterations.'

            image_rec_yn = image_rec_zn - inv_spec_rad * grad

            image_matrix = map2matrix(image_rec_yn, layout)
            image_matrix, nuc_normxx = threshold(image_matrix, thresh,
                                                 threshold_type='soft',
                                                 return_nuc_norm=True)
            image_rec_xn_1 = matrix2map(image_matrix, image.shape)

            l2norm0 = norm(image - pca_convolve(image_rec_xn, psf_pcs, psf_coef), 2)
            l2norm1 = norm(image - pca_convolve(image_rec_xn_1, psf_pcs, psf_coef), 2)

            if l2norm1 > l2norm0:
                use_speed_up = False
            if use_speed_up:
                image_rec_zn, t0 = speed_up(image_rec_xn_1, image_rec_xn, t0)
            else:
                image_rec_zn = np.copy(image_rec_xn_1)

            image_rec_xn = np.copy(image_rec_xn_1)

        u3, s3, v3 = np.linalg.svd(map2matrix(image_rec_zn, layout))
        nuc_norm = np.sum(s3)
        l2norm = norm(image - pca_convolve(image_rec_zn, psf_pcs, psf_coef), 2)
        cost = (0.5 * l2norm ** 2 + thresh * nuc_norm)
        # print ' - i:', i, cost, thresh, l2norm

        if np.abs(cost - cost0) < tolerance:
            print ' - Gradient Descent converged after %d iterations!' % \
                  (i + 1)
            print ' - Final cost, thresh, l2norm:', cost, thresh, l2norm
            break

        elif i == max_iter - 1:
            print ' - Gradient Descent did not converge after %d iterations!' \
                  % max_iter
            print ' - Final cost, thresh, l2norm:', cost, thresh, l2norm

        cost0 = cost

    image_rec = image_rec_zn

    return image_rec