Exemplo n.º 1
0
    def _eval(self, x):
        if self.dim >= 2:
            y = 0
            grads = []
            grads = op.grad(x, dim=self.dim, **self.kwargs)
            for g in grads:
                y += np.power(abs(g), 2)
            y = np.sqrt(y)
            return np.sum(y)

        if self.dim == 1:
            dx = op.grad(x, dim=self.dim, **self.kwargs)
            y = np.sum(np.abs(dx), axis=0)
            return np.sum(y)
Exemplo n.º 2
0
    def _eval(self, x):
        if self.dim >= 2:
            y = 0
            grads = []
            grads = op.grad(x, dim=self.dim, **self.kwargs)
            for g in grads:
                y += np.power(abs(g), 2)
            y = np.sqrt(y)
            return np.sum(y)

        if self.dim == 1:
            dx = op.grad(x, dim=self.dim, **self.kwargs)
            y = np.sum(np.abs(dx), axis=0)
            return np.sum(y)
Exemplo n.º 3
0
    def _prox(self, x, T):
        # Time counter
        t_init = time()

        tol = self.tol
        maxit = self.maxit

        # TODO implement test_gamma
        # Initialization
        sol = x

        if self.dim == 1:
            r = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr = deepcopy(r)
        elif self.dim == 2:
            r, s = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss = deepcopy(r), deepcopy(s)
        elif self.dim == 3:
            r, s, k = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss, kk = deepcopy(r), deepcopy(s), deepcopy(k)
        elif self.dim == 4:
            r, s, k, u = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss, kk, uu = deepcopy(r), deepcopy(s), deepcopy(k), deepcopy(u)

        if self.dim >= 1:
            pold = r
        if self.dim >= 2:
            qold = s
        if self.dim >= 3:
            kold = k
        if self.dim >= 4:
            uold = u

        told, prev_obj = 1., 0.

        # Initialization for weights
        if self.dim >= 1:
            try:
                wx = self.kwargs["wx"]
            except (KeyError, TypeError):
                wx = 1.
        if self.dim >= 2:
            try:
                wy = self.kwargs["wy"]
            except (KeyError, TypeError):
                wy = 1.
        if self.dim >= 3:
            try:
                wz = self.kwargs["wz"]
            except (KeyError, TypeError):
                wz = 1.
        if self.dim >= 4:
            try:
                wt = self.kwargs["wt"]
            except (KeyError, TypeError):
                wt = 1.

        if self.dim == 1:
            mt = wx
        elif self.dim == 2:
            mt = np.maximum(wx, wy)
        elif self.dim == 3:
            mt = np.maximum(wx, np.maximum(wy, wz))
        elif self.dim == 4:
            mt = np.maximum(np.maximum(wx, wy), np.maximum(wz, wt))

        if self.verbosity in ['LOW', 'HIGH', 'ALL']:
            print("Proximal TV Operator")

        iter = 0
        while iter <= maxit:
            # Current Solution
            if self.dim == 1:
                sol = x - T * op.div(rr, **self.kwargs)
            elif self.dim == 2:
                sol = x - T * op.div(rr, ss, **self.kwargs)
            elif self.dim == 3:
                sol = x - T * op.div(rr, ss, kk, **self.kwargs)
            elif self.dim == 4:
                sol = x - T * op.div(rr, ss, kk, uu, **self.kwargs)

            #  Objective function value
            obj = 0.5 * np.power(np.linalg.norm(x[:] - sol[:]), 2) + \
                T * np.sum(self._eval(sol), axis=0)
            rel_obj = np.abs(obj - prev_obj) / obj
            prev_obj = obj

            if self.verbosity in ['HIGH', 'ALL']:
                print("Iter: ", iter, " obj = ", obj, " rel_obj = ", rel_obj)

            # Stopping criterion
            if rel_obj < tol:
                crit = "TOL_EPS"
                break

            #  Update divergence vectors and project
            if self.dim == 1:
                dx = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (4 * T * mt**2) * dx
                weights = np.maximum(1, np.abs(r))

            elif self.dim == 2:
                dx, dy = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= (1. / (8. * T * mt**2.)) * dx
                s -= (1. / (8. * T * mt**2.)) * dy
                weights = np.maximum(
                    1,
                    np.sqrt(np.power(np.abs(r), 2) + np.power(np.abs(s), 2)))

            elif self.dim == 3:
                dx, dy, dz = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (12. * T * mt**2) * dx
                s -= 1. / (12. * T * mt**2) * dy
                k -= 1. / (12. * T * mt**2) * dz
                weights = np.maximum(
                    1,
                    np.sqrt(
                        np.power(np.abs(r), 2) + np.power(np.abs(s), 2) +
                        np.power(np.abs(k), 2)))

            elif self.dim == 4:
                dx, dy, dz, dt = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (16 * T * mt**2) * dx
                s -= 1. / (16 * T * mt**2) * dy
                k -= 1. / (16 * T * mt**2) * dz
                u -= 1. / (16 * T * mt**2) * dt
                weights = np.maximum(
                    1,
                    np.sqrt(
                        np.power(np.abs(r), 2) + np.power(np.abs(s), 2) +
                        np.power(np.abs(k), 2) + np.power(np.abs(u), 2)))

            # FISTA update
            t = (1 + np.sqrt(4 * told**2)) / 2.

            if self.dim >= 1:
                p = r / weights
                r = p + (told - 1) / t * (p - pold)
                pold = p
                rr = deepcopy(r)

            if self.dim >= 2:
                q = s / weights
                s = q + (told - 1) / t * (q - qold)
                ss = deepcopy(s)
                qold = q

            if self.dim >= 3:
                o = k / weights
                k = o + (told - 1) / t * (o - kold)
                kk = deepcopy(k)
                kold = o

            if self.dim >= 4:
                m = u / weights
                u = m + (told - 1) / t * (m - uold)
                uu = deepcopy(u)
                uold = m

            told = t
            iter += 1

        try:
            type(crit) == str
        except NameError:
            crit = "MAX_IT"

        t_end = time()
        exec_time = t_end - t_init

        if self.verbosity in ['HIGH', 'ALL']:
            print("Prox_TV: obj = {0}, rel_obj = {1}, {2}, iter = {3}".format(
                obj, rel_obj, crit, iter))
            print("exec_time = ", exec_time)
        return sol
Exemplo n.º 4
0
    def test_grad(self):

        # Test matrices initialization.
        mat1d = np.arange(5) + 1
        mat2d = np.array([[2, 3, 0, 1], [22, 1, 4, 5]])
        mat3d = np.arange(1, 13).reshape(2, 2, 3).transpose((1, 2, 0))
        mat4d = np.arange(1, 25).reshape(2, 2, 2, 3).transpose((2, 3, 1, 0))
        mat5d = np.arange(1, 49).reshape(2, 2, 3, 2, 2)
        mat5d = mat5d.transpose((3, 4, 2, 1, 0))

        # 1D without weights.
        dx = operators.grad(mat1d, dim=1)
        nptest.assert_array_equal(np.array([1, 1, 1, 1, 0]), dx)

        # 2D without weights.
        mat_dx = np.array([[20, -2, 4, 4], [0, 0, 0, 0]])
        mat_dy = np.array([[1, -3, 1, 0], [-21, 3, 1, 0]])
        dx, dy = operators.grad(mat2d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)

        # 2D with weights.
        dx, dy = operators.grad(mat2d, wx=2, wy=0.5, wz=3, wt=2, dim=2)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)

        # 3D without weights.
        dx = operators.grad(mat3d, dim=1)
        mat_dx = np.array([[[3, 3], [3, 3], [3, 3]], [[0, 0], [0, 0], [0, 0]]])
        mat_dy = np.array([[[1, 1], [1, 1], [0, 0]], [[1, 1], [1, 1], [0, 0]]])
        mat_dz = np.array([[[6, 0], [6, 0], [6, 0]], [[6, 0], [6, 0], [6, 0]]])
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat3d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat3d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)

        # 3D with weights.
        dx, dy, dz = operators.grad(mat3d, wx=2, wy=0.5, wz=3, wt=2, dim=3)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)

        # 4D without weights.
        mat_dx = np.array([[[[3, 3], [3, 3]], [[3, 3], [3, 3]], [[3, 3],
                                                                 [3, 3]]],
                           [[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0],
                                                                 [0, 0]]]])
        mat_dy = np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[0, 0],
                                                                 [0, 0]]],
                           [[[1, 1], [1, 1]], [[1, 1], [1, 1]], [[0, 0],
                                                                 [0, 0]]]])
        mat_dz = np.array([[[[6, 6], [0, 0]], [[6, 6], [0, 0]], [[6, 6],
                                                                 [0, 0]]],
                           [[[6, 6], [0, 0]], [[6, 6], [0, 0]], [[6, 6],
                                                                 [0, 0]]]])
        mat_dt = np.array([[[[12, 0], [12, 0]], [[12, 0], [12, 0]],
                            [[12, 0], [12, 0]]],
                           [[[12, 0], [12, 0]], [[12, 0], [12, 0]],
                            [[12, 0], [12, 0]]]])
        dx = operators.grad(mat4d, dim=1)
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat4d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat4d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        dx, dy, dz, dt = operators.grad(mat4d, dim=4)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        nptest.assert_array_equal(mat_dt, dt)

        # 4D with weights.
        dx, dy, dz, dt = operators.grad(mat4d, wx=2, wy=0.5, wz=3, wt=2, dim=4)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        mat_dt_w = mat_dt * 2
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)
        nptest.assert_array_equal(mat_dt_w, dt)

        # 5D without weights.
        mat_dx = np.array([[[[[2, 2], [2, 2]], [[2, 2], [2, 2]],
                             [[2, 2], [2, 2]]],
                            [[[2, 2], [2, 2]], [[2, 2], [2, 2]],
                             [[2, 2], [2, 2]]]],
                           [[[[0, 0], [0, 0]], [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]],
                            [[[0, 0], [0, 0]], [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]]])
        mat_dy = np.array([[[[[1, 1], [1, 1]], [[1, 1], [1, 1]],
                             [[1, 1], [1, 1]]],
                            [[[0, 0], [0, 0]], [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]],
                           [[[[1, 1], [1, 1]], [[1, 1], [1, 1]],
                             [[1, 1], [1, 1]]],
                            [[[0, 0], [0, 0]], [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]]])
        mat_dz = np.array([[[[[4, 4], [4, 4]], [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]],
                            [[[4, 4], [4, 4]], [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]]],
                           [[[[4, 4], [4, 4]], [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]],
                            [[[4, 4], [4, 4]], [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]]]])
        mat_dt = np.array([[[[[12, 12], [0, 0]], [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]],
                            [[[12, 12], [0, 0]], [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]]],
                           [[[[12, 12], [0, 0]], [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]],
                            [[[12, 12], [0, 0]], [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]]]])
        dx = operators.grad(mat5d, dim=1)
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat5d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat5d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        dx, dy, dz, dt = operators.grad(mat5d, dim=4)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        nptest.assert_array_equal(mat_dt, dt)

        # 5D with weights.
        dx, dy, dz, dt = operators.grad(mat5d, wx=2, wy=0.5, wz=3, wt=2, dim=4)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        mat_dt_w = mat_dt * 2
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)
        nptest.assert_array_equal(mat_dt_w, dt)
Exemplo n.º 5
0
    def test_grad(self):

        # Test matrices initialization.
        mat1d = np.arange(5) + 1
        mat2d = np.array([[2, 3, 0, 1], [22, 1, 4, 5]])
        mat3d = np.arange(1, 13).reshape(2, 2, 3).transpose((1, 2, 0))
        mat4d = np.arange(1, 25).reshape(2, 2, 2, 3).transpose((2, 3, 1, 0))
        mat5d = np.arange(1, 49).reshape(2, 2, 3, 2, 2)
        mat5d = mat5d.transpose((3, 4, 2, 1, 0))

        # 1D without weights.
        dx = operators.grad(mat1d, dim=1)
        nptest.assert_array_equal(np.array([1, 1, 1, 1, 0]), dx)

        # 2D without weights.
        mat_dx = np.array([[20, -2, 4, 4], [0, 0, 0, 0]])
        mat_dy = np.array([[1, -3, 1, 0], [-21, 3, 1, 0]])
        dx, dy = operators.grad(mat2d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)

        # 2D with weights.
        dx, dy = operators.grad(mat2d, wx=2, wy=0.5, wz=3, wt=2, dim=2)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)

        # 3D without weights.
        dx = operators.grad(mat3d, dim=1)
        mat_dx = np.array([[[3, 3], [3, 3], [3, 3]],
                           [[0, 0], [0, 0], [0, 0]]])
        mat_dy = np.array([[[1, 1], [1, 1], [0, 0]],
                           [[1, 1], [1, 1], [0, 0]]])
        mat_dz = np.array([[[6, 0], [6, 0], [6, 0]],
                           [[6, 0], [6, 0], [6, 0]]])
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat3d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat3d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)

        # 3D with weights.
        dx, dy, dz = operators.grad(mat3d, wx=2, wy=0.5, wz=3, wt=2, dim=3)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)

        # 4D without weights.
        mat_dx = np.array([[[[3, 3], [3, 3]],
                            [[3, 3], [3, 3]],
                            [[3, 3], [3, 3]]],
                           [[[0, 0], [0, 0]],
                            [[0, 0], [0, 0]],
                            [[0, 0], [0, 0]]]])
        mat_dy = np.array([[[[1, 1], [1, 1]],
                            [[1, 1], [1, 1]],
                            [[0, 0], [0, 0]]],
                           [[[1, 1], [1, 1]],
                            [[1, 1], [1, 1]],
                            [[0, 0], [0, 0]]]])
        mat_dz = np.array([[[[6, 6], [0, 0]],
                            [[6, 6], [0, 0]],
                            [[6, 6], [0, 0]]],
                           [[[6, 6], [0, 0]],
                            [[6, 6], [0, 0]],
                            [[6, 6], [0, 0]]]])
        mat_dt = np.array([[[[12, 0], [12, 0]],
                            [[12, 0], [12, 0]],
                            [[12, 0], [12, 0]]],
                           [[[12, 0], [12, 0]],
                            [[12, 0], [12, 0]],
                            [[12, 0], [12, 0]]]])
        dx = operators.grad(mat4d, dim=1)
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat4d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat4d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        dx, dy, dz, dt = operators.grad(mat4d, dim=4)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        nptest.assert_array_equal(mat_dt, dt)

        # 4D with weights.
        dx, dy, dz, dt = operators.grad(mat4d, wx=2, wy=0.5, wz=3, wt=2, dim=4)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        mat_dt_w = mat_dt * 2
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)
        nptest.assert_array_equal(mat_dt_w, dt)

        # 5D without weights.
        mat_dx = np.array([[[[[2, 2], [2, 2]],
                             [[2, 2], [2, 2]],
                             [[2, 2], [2, 2]]],
                            [[[2, 2], [2, 2]],
                             [[2, 2], [2, 2]],
                             [[2, 2], [2, 2]]]],
                           [[[[0, 0], [0, 0]],
                             [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]],
                            [[[0, 0], [0, 0]],
                             [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]]])
        mat_dy = np.array([[[[[1, 1], [1, 1]],
                             [[1, 1], [1, 1]],
                             [[1, 1], [1, 1]]],
                            [[[0, 0], [0, 0]],
                             [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]],
                           [[[[1, 1], [1, 1]],
                             [[1, 1], [1, 1]],
                             [[1, 1], [1, 1]]],
                            [[[0, 0], [0, 0]],
                             [[0, 0], [0, 0]],
                             [[0, 0], [0, 0]]]]])
        mat_dz = np.array([[[[[4, 4], [4, 4]],
                             [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]],
                            [[[4, 4], [4, 4]],
                             [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]]],
                           [[[[4, 4], [4, 4]],
                             [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]],
                            [[[4, 4], [4, 4]],
                             [[4, 4], [4, 4]],
                             [[0, 0], [0, 0]]]]])
        mat_dt = np.array([[[[[12, 12], [0, 0]],
                             [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]],
                            [[[12, 12], [0, 0]],
                             [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]]],
                           [[[[12, 12], [0, 0]],
                             [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]],
                            [[[12, 12], [0, 0]],
                             [[12, 12], [0, 0]],
                             [[12, 12], [0, 0]]]]])
        dx = operators.grad(mat5d, dim=1)
        nptest.assert_array_equal(mat_dx, dx)
        dx, dy = operators.grad(mat5d, dim=2)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        dx, dy, dz = operators.grad(mat5d, dim=3)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        dx, dy, dz, dt = operators.grad(mat5d, dim=4)
        nptest.assert_array_equal(mat_dx, dx)
        nptest.assert_array_equal(mat_dy, dy)
        nptest.assert_array_equal(mat_dz, dz)
        nptest.assert_array_equal(mat_dt, dt)

        # 5D with weights.
        dx, dy, dz, dt = operators.grad(mat5d, wx=2, wy=0.5, wz=3, wt=2, dim=4)
        mat_dx_w = mat_dx * 2
        mat_dy_w = mat_dy * 0.5
        mat_dz_w = mat_dz * 3
        mat_dt_w = mat_dt * 2
        nptest.assert_array_equal(mat_dx_w, dx)
        nptest.assert_array_equal(mat_dy_w, dy)
        nptest.assert_array_equal(mat_dz_w, dz)
        nptest.assert_array_equal(mat_dt_w, dt)
Exemplo n.º 6
0
    def _prox(self, x, T):
        # Time counter
        t_init = time()

        tol = self.tol
        maxit = self.maxit

        # TODO implement test_gamma
        # Initialization
        sol = x

        if self.dim == 1:
            r = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr = deepcopy(r)
        elif self.dim == 2:
            r, s = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss = deepcopy(r), deepcopy(s)
        elif self.dim == 3:
            r, s, k = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss, kk = deepcopy(r), deepcopy(s), deepcopy(k)
        elif self.dim == 4:
            r, s, k, u = op.grad(x * 0, dim=self.dim, **self.kwargs)
            rr, ss, kk, uu = deepcopy(r), deepcopy(s), deepcopy(k), deepcopy(u)

        if self.dim >= 1:
            pold = r
        if self.dim >= 2:
            qold = s
        if self.dim >= 3:
            kold = k
        if self.dim >= 4:
            uold = u

        told, prev_obj = 1., 0.

        # Initialization for weights
        if self.dim >= 1:
            try:
                wx = self.kwargs["wx"]
            except (KeyError, TypeError):
                wx = 1.
        if self.dim >= 2:
            try:
                wy = self.kwargs["wy"]
            except (KeyError, TypeError):
                wy = 1.
        if self.dim >= 3:
            try:
                wz = self.kwargs["wz"]
            except (KeyError, TypeError):
                wz = 1.
        if self.dim >= 4:
            try:
                wt = self.kwargs["wt"]
            except (KeyError, TypeError):
                wt = 1.

        if self.dim == 1:
            mt = wx
        elif self.dim == 2:
            mt = np.maximum(wx, wy)
        elif self.dim == 3:
            mt = np.maximum(wx, np.maximum(wy, wz))
        elif self.dim == 4:
            mt = np.maximum(np.maximum(wx, wy), np.maximum(wz, wt))

        if self.verbosity in ['LOW', 'HIGH', 'ALL']:
            print("Proximal TV Operator")

        iter = 0
        while iter <= maxit:
            # Current Solution
            if self.dim == 1:
                sol = x - T * op.div(rr, **self.kwargs)
            elif self.dim == 2:
                sol = x - T * op.div(rr, ss, **self.kwargs)
            elif self.dim == 3:
                sol = x - T * op.div(rr, ss, kk, **self.kwargs)
            elif self.dim == 4:
                sol = x - T * op.div(rr, ss, kk, uu, **self.kwargs)

            #  Objective function value
            obj = 0.5 * np.power(np.linalg.norm(x[:] - sol[:]), 2) + \
                T * np.sum(self._eval(sol), axis=0)
            rel_obj = np.abs(obj - prev_obj) / obj
            prev_obj = obj

            if self.verbosity in ['HIGH', 'ALL']:
                print("Iter: ", iter, " obj = ", obj, " rel_obj = ", rel_obj)

            # Stopping criterion
            if rel_obj < tol:
                crit = "TOL_EPS"
                break

            #  Update divergence vectors and project
            if self.dim == 1:
                dx = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (4 * T * mt**2) * dx
                weights = np.maximum(1, np.abs(r))

            elif self.dim == 2:
                dx, dy = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= (1. / (8. * T * mt**2.)) * dx
                s -= (1. / (8. * T * mt**2.)) * dy
                weights = np.maximum(1, np.sqrt(np.power(np.abs(r), 2) +
                                                np.power(np.abs(s), 2)))

            elif self.dim == 3:
                dx, dy, dz = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (12. * T * mt**2) * dx
                s -= 1. / (12. * T * mt**2) * dy
                k -= 1. / (12. * T * mt**2) * dz
                weights = np.maximum(1, np.sqrt(np.power(np.abs(r), 2) +
                                                np.power(np.abs(s), 2) +
                                                np.power(np.abs(k), 2)))

            elif self.dim == 4:
                dx, dy, dz, dt = op.grad(sol, dim=self.dim, **self.kwargs)
                r -= 1. / (16 * T * mt**2) * dx
                s -= 1. / (16 * T * mt**2) * dy
                k -= 1. / (16 * T * mt**2) * dz
                u -= 1. / (16 * T * mt**2) * dt
                weights = np.maximum(1, np.sqrt(np.power(np.abs(r), 2) +
                                                np.power(np.abs(s), 2) +
                                                np.power(np.abs(k), 2) +
                                                np.power(np.abs(u), 2)))

            # FISTA update
            t = (1 + np.sqrt(4 * told**2)) / 2.

            if self.dim >= 1:
                p = r / weights
                r = p + (told - 1) / t * (p - pold)
                pold = p
                rr = deepcopy(r)

            if self.dim >= 2:
                q = s / weights
                s = q + (told - 1) / t * (q - qold)
                ss = deepcopy(s)
                qold = q

            if self.dim >= 3:
                o = k / weights
                k = o + (told - 1) / t * (o - kold)
                kk = deepcopy(k)
                kold = o

            if self.dim >= 4:
                m = u / weights
                u = m + (told - 1) / t * (m - uold)
                uu = deepcopy(u)
                uold = m

            told = t
            iter += 1

        try:
            type(crit) == str
        except NameError:
            crit = "MAX_IT"

        t_end = time()
        exec_time = t_end - t_init

        if self.verbosity in ['HIGH', 'ALL']:
            print("Prox_TV: obj = {0}, rel_obj = {1}, {2}, iter = {3}".format(
                obj, rel_obj, crit, iter))
            print("exec_time = ", exec_time)
        return sol
Exemplo n.º 7
0
 def test_adjoint(self):
     """Test that 1D div op is the adjoint of grad."""
     dx = operators.grad(np.eye(10), dim=1)
     dxt = operators.div(np.eye(10))
     nptest.assert_equal(dx.T, -dxt)  # we use that definition