Пример #1
0
def create_aop_denoiser(Omega, noisy, nu, alpha):
    Omega_sym = T.matrix('Omega')
    noisy_shared = theano.shared(noisy.astype(np.float32))
    denoised = theano.shared(np.copy(noisy.astype(np.float32)))
    denoised_sym = T.matrix("denoised")
    Omega_normal = normalizing_AOP(Omega_sym, noisy.shape[0])
    cost = (((noisy_shared - denoised_sym) ** 2).sum() / denoised_sym.shape[1]) + \
           alpha * normalized_l2norm_cost(logsquared_cost(T.dot(Omega_normal, denoised_sym), nu, axis=0), denoised_sym.shape[1])
    grad = theano.grad(cost, denoised_sym)
    cg_denoising = CG(denoised,
                      cost,
                      grad,
                      denoised_sym,
                      k=noisy.shape[0],
                      n=noisy.shape[1],
                      t_init=1,
                      rho=0.9,
                      max_iter_line_search=125,
                      other_givens={Omega_sym: Omega})
    return cg_denoising, denoised
Пример #2
0
    def __init__(
            self,
            n,  # input dimension
            k,  # subspace rank
            p,  # p for pseudo-norm
            mu,  # Smoothing
            t,  # Step size
            phi,  # This is the foreground weighting factor.
            y_iters=5,
            U_init=None,
            step_size_func=lambda iter: 1.0 / iter,
            median_filter_radius=3):
        self.n = n
        self.k = k
        self.p = p
        self.mu = mu
        self.y_iters = y_iters

        self.phi = theano.shared(np.float32(phi))
        # Step size
        self.t = theano.shared(np.float32(t))
        self.image_shape = None
        self.step_size_func = step_size_func
        self.median_filter_radius = median_filter_radius

        if U_init == None:
            # Initialize with random subspace
            U = npr.randn(n, k).astype(np.float32)
            U, _ = npl.qr(U)
        else:
            U = U_init

        # subspace basis
        self.U_sym = T.fmatrix("U")
        self.U_shared = theano.shared(U.astype(np.float32))

        # foreground weights
        self.W_shared = theano.shared(np.ones((n, 1), dtype=np.float32))

        # subspace coordinates of current data point
        self.y_sym = T.fmatrix("y")
        self.y_shared = theano.shared(npr.randn(k, 1).astype(np.float32))

        # current image
        self.image_sym = T.fmatrix("image")
        self.image_shared = theano.shared(np.zeros((n, 1), dtype=np.float32))
        self.init_y_func = T.dot(self.U_shared.T, self.image_shared)
        self.init_y = theano.function(
            [], [], updates={self.y_shared: self.init_y_func})

        # pROST cost function
        self.cost = (self.W_shared *
                     #reconstruction error || I - Uy||_(p,mu)
                     ((((self.image_sym - T.dot(self.U_sym, self.y_sym))) ** 2 + self.mu) ** (self.p / 2))) \
                     .sum()
        self.reconstruction_func = T.dot(self.U_sym, self.y_sym)
        self.error_image_func = T.abs_(self.image_sym -
                                       T.dot(self.U_sym, self.y_sym))

        self.segmentation_func = self.error_image_func > self.t

        self.grad_y = theano.grad(self.cost, self.y_sym)
        self.grad_U = theano.grad(self.cost, self.U_sym)
        self.optimize_y = CG(
            self.y_shared,
            self.cost,
            self.grad_y,
            self.y_sym,
            self.k,
            1,
            t_init=0.1,
            rho=0.5,
            max_iter_line_search=200,  # this is a bit excessive
            other_givens={
                self.U_sym: self.U_shared,
                self.image_sym: self.image_shared
            })
        self.optimize_U = GrassmanGD(self.U_shared,
                                     self.y_shared,
                                     self.cost,
                                     self.grad_U,
                                     self.U_sym,
                                     rho=0.6,
                                     max_iter_line_search=5,
                                     other_givens={
                                         self.image_sym: self.image_shared,
                                         self.y_sym: self.y_shared
                                     },
                                     step_size_func=self.step_size_func)

        self.recerrsegweight = theano.function(
            [], [
                self.reconstruction_func, self.error_image_func,
                self.segmentation_func
            ],
            givens={
                self.U_sym: self.U_shared,
                self.y_sym: self.y_shared,
                self.image_sym: self.image_shared
            })

        self.error_image = None
        self.reconstruction = None
        self.segmentation = None