Beispiel #1
0
def test_pointwise_norm_gradient_real(exponent):
    # The operator is not differentiable for exponent 'inf'
    if exponent == float('inf'):
        fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2))
        vfspace = ProductSpace(fspace, 1)
        pwnorm = PointwiseNorm(vfspace, exponent)
        point = vfspace.one()
        with pytest.raises(NotImplementedError):
            pwnorm.derivative(point)
        return

    # 1d
    fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2))
    vfspace = ProductSpace(fspace, 1)
    pwnorm = PointwiseNorm(vfspace, exponent)

    point = noise_element(vfspace)
    direction = noise_element(vfspace)

    # Computing expected result
    tmp = pwnorm(point).ufuncs.power(1 - exponent)
    v_field = vfspace.element()
    for i in range(len(v_field)):
        v_field[i] = tmp * point[i] * np.abs(point[i])**(exponent - 2)
    pwinner = odl.PointwiseInner(vfspace, v_field)
    expected_result = pwinner(direction)

    func_pwnorm = pwnorm.derivative(point)

    assert all_almost_equal(func_pwnorm(direction), expected_result)

    # 3d
    fspace = odl.uniform_discr([0, 0], [1, 1], (2, 2))
    vfspace = ProductSpace(fspace, 3)
    pwnorm = PointwiseNorm(vfspace, exponent)

    point = noise_element(vfspace)
    direction = noise_element(vfspace)

    # Computing expected result
    tmp = pwnorm(point).ufuncs.power(1 - exponent)
    v_field = vfspace.element()
    for i in range(len(v_field)):
        v_field[i] = tmp * point[i] * np.abs(point[i])**(exponent - 2)
    pwinner = odl.PointwiseInner(vfspace, v_field)
    expected_result = pwinner(direction)

    func_pwnorm = pwnorm.derivative(point)
    assert all_almost_equal(func_pwnorm(direction), expected_result)
Beispiel #2
0
 def inf_action(self, lie_alg_element):
     assert lie_alg_element in self.lie_group.associated_algebra
     pts = self.domain.points()
     deformed_pts = lie_alg_element.arr.dot(pts.T) - pts.T
     deformed_pts = self.domain.tangent_bundle.element(deformed_pts)
     pointwise_inner = odl.PointwiseInner(self.gradient.range, deformed_pts)
     return pointwise_inner * self.gradient
 def _call(self, x, out):
     xi = vfield
     Id = IdentityOperator(domain)
     xiT = odl.PointwiseInner(domain, xi)
     xixiT = odl.BroadcastOperator(*[x * xiT for x in xi])
     gamma = 1
     P = (Id - gamma * xixiT)
     out.assign(P(x))
def gradient(space,
             sinfo=None,
             mode=None,
             gamma=1,
             eta=1e-2,
             show_sinfo=False,
             prefix=None):

    grad = odl.Gradient(space, method='forward', pad_mode='symmetric')

    if sinfo is not None:
        if mode == 'direction':
            norm = odl.PointwiseNorm(grad.range)
            grad_sinfo = grad(sinfo)
            ngrad_sinfo = norm(grad_sinfo)

            for i in range(len(grad_sinfo)):
                grad_sinfo[i] /= ngrad_sinfo.ufuncs.max()

            ngrad_sinfo = norm(grad_sinfo)
            ngrad_sinfo_eta = np.sqrt(ngrad_sinfo**2 + eta**2)

            xi = grad.range.element([g / ngrad_sinfo_eta
                                     for g in grad_sinfo])  # UGLY

            Id = odl.operator.IdentityOperator(grad.range)
            xiT = odl.PointwiseInner(grad.range, xi)
            xixiT = odl.BroadcastOperator(*[x * xiT for x in xi])

            grad = (Id - gamma * xixiT) * grad

            if show_sinfo:
                misc.save_image(ngrad_sinfo, prefix + '_sinfo_norm')
                misc.save_vfield(xi.asarray(), filename=prefix + '_sinfo_xi')
                misc.save_vfield_cmap(filename=prefix + '_sinfo_xi_cmap')

        elif mode == 'location':
            norm = odl.PointwiseNorm(grad.range)
            ngrad_sinfo = norm(grad(sinfo))
            ngrad_sinfo /= ngrad_sinfo.ufuncs.max()

            w = eta / np.sqrt(ngrad_sinfo**2 + eta**2)
            grad = odl.DiagonalOperator(odl.MultiplyOperator(w), 2) * grad

            if show_sinfo:
                misc.save_image(ngrad_sinfo, prefix + '_sinfo_norm')
                misc.save_image(w, prefix + '_w')

        else:
            grad = None

    return grad
Beispiel #5
0
        sideinfo_grad = gradient(sideinfo)

        gradient_space = gradient.range
        norm = odl.PointwiseNorm(gradient_space, 2)

        norm_sideinfo_grad = norm(sideinfo_grad)

        max_norm = np.max(norm_sideinfo_grad)
        eta_scaled = eta * max(max_norm, 1e-4)
        norm_eta_sideinfo_grad = np.sqrt(norm_sideinfo_grad**2 + eta_scaled**2)

        xi = gradient_space.element(
            [g / norm_eta_sideinfo_grad for g in sideinfo_grad])  # UGLY

        I = odl.operator.IdentityOperator(gradient_space)
        xiT = odl.PointwiseInner(gradient_space, xi)
        xixiT = odl.BroadcastOperator(*[x * xiT for x in xi])
        D = (I - xixiT) * gradient

        norm_D = misc.norm(D, '{}/norm_D.npy'.format(folder_param))

        c = float(norm_K) / float(norm_D)
        D *= c
        norm_D *= c
        L1 = (alpha / c) * odl.solvers.GroupL1Norm(D.range)
        L164 = (alpha / c) * odl.solvers.GroupL1Norm(D.range.astype('float64'))
        g = odl.solvers.IndicatorBox(X, lower=0)

        obj_fun = KL * K + L1 * D + g  # objective functional

        fldr = '{}/pics'.format(folder_param)
    def __init__(self,
                 domain,
                 alpha=1.,
                 sinfo=None,
                 NonNeg=False,
                 prox_options={},
                 gamma=1,
                 eta=1e-2):

        if isinstance(domain, odl.ProductSpace):
            grad_basic = odl.Gradient(domain[0],
                                      method='forward',
                                      pad_mode='symmetric')

            pd = [
                odl.discr.diff_ops.PartialDerivative(domain[0],
                                                     i,
                                                     method='forward',
                                                     pad_mode='symmetric')
                for i in range(2)
            ]
            cp = [
                odl.operator.ComponentProjection(domain, i) for i in range(2)
            ]

            if sinfo is None:
                self.grad = odl.BroadcastOperator(
                    *[pd[i] * cp[j] for i in range(2) for j in range(2)])

            else:
                vfield = gamma * generate_vfield_from_sinfo(
                    sinfo, grad_basic, eta)
                inner = odl.PointwiseInner(domain, vfield) * grad_basic
                self.grad = odl.BroadcastOperator(*[
                    pd[i] * cp[j] - vfield[i] * inner * cp[j] for i in range(2)
                    for j in range(2)
                ])

            self.grad.norm = self.grad.norm(estimate=True)

        else:
            grad_basic = odl.Gradient(domain,
                                      method='forward',
                                      pad_mode='symmetric')

            if sinfo is None:
                self.grad = grad_basic
            else:
                vfield = gamma * generate_vfield_from_sinfo(
                    sinfo, grad_basic, eta)
                P = project_on_fixed_vfield(grad_basic.range, vfield)
                self.grad = P * grad_basic

            grad_norm = 2 * np.sqrt(sum(1 / grad_basic.domain.cell_sides**2))
            self.grad.norm = grad_norm

        self.tv = total_variation(domain, grad=self.grad)

        if NonNeg is True:
            self.nn = IndicatorBox(domain, 0, np.inf)
        else:
            self.nn = ZeroFunctional(domain)

        if 'name' not in prox_options:
            prox_options['name'] = 'FGP'
        if 'warmstart' not in prox_options:
            prox_options['warmstart'] = True
        if 'niter' not in prox_options:
            prox_options['niter'] = 5
        if 'p' not in prox_options:
            prox_options['p'] = None
        if 'tol' not in prox_options:
            prox_options['tol'] = None

        prox_options['grad'] = self.grad
        prox_options['proj_P'] = self.tv.left.convex_conj.proximal(0)
        prox_options['proj_C'] = self.nn.proximal(1)

        self.prox_options = prox_options
        self.alpha = alpha

        super().__init__(space=domain, linear=False, grad_lipschitz=0)