Пример #1
0
 def test_02(self):
     rho = 1e-1
     opt = ccmod.ConvCnstrMOD_CG.Options({
         'Verbose': False,
         'MaxMainIter': 500,
         'LinSolveCheck': True,
         'ZeroMean': True,
         'RelStopTol': 1e-5,
         'rho': rho,
         'AutoRho': {
             'Enabled': False
         },
         'CG': {
             'StopTol': 1e-5
         }
     })
     Xr = self.X.reshape(self.X.shape[0:2] + (
         1,
         1,
     ) + self.X.shape[2:])
     Sr = self.S.reshape(self.S.shape + (1, ))
     c = ccmod.ConvCnstrMOD_CG(Xr, Sr, self.D0.shape, opt)
     c.solve()
     D1 = cr.bcrop(c.Y, self.D0.shape).squeeze()
     assert rrs(self.D0, D1) < 1e-4
     assert np.array(c.getitstat().XSlvRelRes).max() < 1e-3
Пример #2
0
    def test_18(self):
        N = 64
        M = 4
        Nd = 8
        D0 = cr.normalise(cr.zeromean(np.random.randn(Nd, Nd, M), (Nd, Nd, M),
                                      dimN=2),
                          dimN=2)
        X = np.zeros((N, N, M))
        xr = np.random.randn(N, N, M)
        xp = np.abs(xr) > 3
        X[xp] = np.random.randn(X[xp].size)
        S = np.sum(ifftn(
            fftn(D0, (N, N), (0, 1)) * fftn(X, None, (0, 1)), None,
            (0, 1)).real,
                   axis=2)
        L = 50.0
        opt = ccmod.ConvCnstrMOD.Options({
            'Verbose': False,
            'MaxMainIter': 3000,
            'ZeroMean': True,
            'RelStopTol': 0.,
            'L': L,
            'Monotone': True
        })
        Xr = X.reshape(X.shape[0:2] + (
            1,
            1,
        ) + X.shape[2:])
        Sr = S.reshape(S.shape + (1, ))
        c = ccmod.ConvCnstrMOD(Xr, Sr, D0.shape, opt)
        c.solve()
        D1 = cr.bcrop(c.X, D0.shape).squeeze()

        assert rrs(D0, D1) < 1e-4
        assert np.array(c.getitstat().Rsdl)[-1] < 1e-5
Пример #3
0
    def test_13(self):
        N = 64
        M = 4
        Nd = 8
        D0 = cr.normalise(cr.zeromean(
            np.random.randn(Nd, Nd, M), (Nd, Nd, M), dimN=2), dimN=2)
        X = np.zeros((N, N, M))
        xr = np.random.randn(N, N, M)
        xp = np.abs(xr) > 3
        X[xp] = np.random.randn(X[xp].size)
        S = np.sum(sl.ifftn(sl.fftn(D0, (N, N), (0, 1)) *
                            sl.fftn(X, None, (0, 1)), None, (0, 1)).real,
                   axis=2)
        L = 0.5
        opt = ccmod.ConvCnstrMOD.Options(
            {'Verbose': False, 'MaxMainIter': 3000, 'ZeroMean': True,
             'RelStopTol': 0., 'L': L, 'BackTrack': {'Enabled': True}})
        Xr = X.reshape(X.shape[0:2] + (1, 1,) + X.shape[2:])
        Sr = S.reshape(S.shape + (1,))
        c = ccmod.ConvCnstrMOD(Xr, Sr, D0.shape, opt)
        c.solve()
        D1 = cr.bcrop(c.X, D0.shape).squeeze()

        assert sl.rrs(D0, D1) < 1e-4
        assert np.array(c.getitstat().Rsdl)[-1] < 1e-5
Пример #4
0
 def test_13(self):
     dsz = ((8, 8, 16), (12, 12, 32))
     u = np.zeros((24, 24, 48))
     u[0:8, 0:8, 0:16] = 1.0
     u[0:12, 0:12, 16:] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == (12, 12, 48)
Пример #5
0
 def test_13(self):
     dsz = ((8, 8, 16), (12, 12, 32))
     u = np.zeros((24, 24, 48))
     u[0:8, 0:8, 0:16] = 1.0
     u[0:12, 0:12, 16:] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == (12, 12, 48)
Пример #6
0
def dictionary_learning_by_L1(X, S, dsz, G1, H1, G0, H0, parameter_rho_dic,
                              iteration, thr, cri):
    Xf = con.convert_to_Xf(X, S, cri)

    bar_D = tqdm(total=iteration, desc='D', leave=False)
    for i in range(iteration):

        D, G0, H0, XD = dictionary_learning_by_L1_Dstep(
            Xf, S, G1, H1, G0, H0, parameter_rho_dic, i, cri, dsz)

        Dr = np.asarray(D.reshape(cri.shpD), dtype=S.dtype)
        H1r = np.asarray(H1.reshape(cri.shpD), dtype=S.dtype)
        Pcn = cr.getPcn(dsz, cri.Nv, cri.dimN, cri.dimCd)
        G1r = Pcn(Dr + H1r)
        G1 = cr.bcrop(G1r, dsz, cri.dimN).squeeze()

        H1 = H1 + D - G1

        Est = sp.norm_l1(XD - S)
        if (i == 0):
            pre_Est = 1.1 * Est

        if ((pre_Est - Est) / pre_Est <= thr):
            bar_D.update(iteration - i)
            break

        pre_Est = Est

        bar_D.update(1)
    bar_D.close()
    return D, G0, G1, H0, H1
Пример #7
0
 def test_02(self):
     N = 32
     M = 4
     Nd = 5
     D0 = cr.normalise(cr.zeromean(
         np.random.randn(Nd, Nd, M), (Nd, Nd, M), dimN=2), dimN=2)
     X = np.zeros((N, N, M))
     xr = np.random.randn(N, N, M)
     xp = np.abs(xr) > 3
     X[xp] = np.random.randn(X[xp].size)
     S = np.sum(sl.ifftn(sl.fftn(D0, (N, N), (0,1)) *
                sl.fftn(X, None, (0,1)), None, (0,1)).real, axis=2)
     rho = 1e-1
     opt = ccmod.ConvCnstrMOD_CG.Options({'Verbose': False,
                 'MaxMainIter': 500, 'LinSolveCheck': True,
                 'ZeroMean': True, 'RelStopTol': 1e-5, 'rho': rho,
                 'AutoRho': {'Enabled': False},
                 'CG': {'StopTol': 1e-5}})
     Xr = X.reshape(X.shape[0:2] + (1,1,) + X.shape[2:])
     Sr = S.reshape(S.shape + (1,))
     c = ccmod.ConvCnstrMOD_CG(Xr, Sr, D0.shape, opt)
     c.solve()
     D1 = cr.bcrop(c.Y, D0.shape).squeeze()
     assert(sl.rrs(D0, D1) < 1e-4)
     assert(np.array(c.getitstat().XSlvRelRes).max() < 1e-3)
Пример #8
0
 def test_02(self):
     N = 32
     M = 4
     Nd = 5
     D0 = cr.normalise(cr.zeromean(
         np.random.randn(Nd, Nd, M), (Nd, Nd, M), dimN=2), dimN=2)
     X = np.zeros((N, N, M))
     xr = np.random.randn(N, N, M)
     xp = np.abs(xr) > 3
     X[xp] = np.random.randn(X[xp].size)
     S = np.sum(sl.ifftn(sl.fftn(D0, (N, N), (0, 1)) *
                sl.fftn(X, None, (0, 1)), None, (0, 1)).real, axis=2)
     rho = 1e-1
     opt = ccmod.ConvCnstrMOD_CG.Options({'Verbose': False,
                 'MaxMainIter': 500, 'LinSolveCheck': True,
                 'ZeroMean': True, 'RelStopTol': 1e-5, 'rho': rho,
                 'AutoRho': {'Enabled': False},
                 'CG': {'StopTol': 1e-5}})
     Xr = X.reshape(X.shape[0:2] + (1, 1,) + X.shape[2:])
     Sr = S.reshape(S.shape + (1,))
     c = ccmod.ConvCnstrMOD_CG(Xr, Sr, D0.shape, opt)
     c.solve()
     D1 = cr.bcrop(c.Y, D0.shape).squeeze()
     assert sl.rrs(D0, D1) < 1e-4
     assert np.array(c.getitstat().XSlvRelRes).max() < 1e-3
Пример #9
0
 def test_14(self):
     dsz = (((5, 5, 2, 8), (7, 7, 1, 8)), ((9, 9, 2, 16), (10, 10, 1, 16)))
     u = np.zeros((16, 16, 3, 24))
     u[0:5, 0:5, 0:2, 0:8] = 1.0
     u[0:7, 0:7, 2, 0:8] = 1.0
     u[0:9, 0:9, 0:2, 8:] = 1.0
     u[0:10, 0:10, 2, 8:] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == (10, 10, 3, 24)
Пример #10
0
    def getdict(self, crop=True):
        """Get final dictionary. If ``crop`` is ``True``, apply
        :func:`.cnvrep.bcrop` to returned array.
        """

        D = self.Y
        if crop:
            D = cr.bcrop(D, self.cri.dsz, self.cri.dimN)
        return D
Пример #11
0
    def getdict(self, crop=True):
        """Get final dictionary. If ``crop`` is ``True``, apply
        :func:`.cnvrep.bcrop` to returned array.
        """

        D = self.Y
        if crop:
            D = cr.bcrop(D, self.cri.dsz, self.cri.dimN)
        return D
Пример #12
0
 def test_14(self):
     dsz = (((5, 5, 2, 8), (7, 7, 1, 8)),
            ((9, 9, 2, 16), (10, 10, 1, 16)))
     u = np.zeros((16, 16, 3, 24))
     u[0:5, 0:5, 0:2, 0:8] = 1.0
     u[0:7, 0:7, 2, 0:8] = 1.0
     u[0:9, 0:9, 0:2, 8:] = 1.0
     u[0:10, 0:10, 2, 8:] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == (10, 10, 3, 24)
Пример #13
0
def getdict(dsz):
    if type(dsz[0]) is int:
        return np.random.standard_normal(dsz)
    else:
        num_filt = 0
        tmp = [0 for i in range(len(dsz[0]))]
        for i in dsz:
            i = list(i)
            num_filt = num_filt + i[len(i) - 1]
            if i[0] > tmp[0]:
                tmp = i
        tmp[len(tmp) - 1] = num_filt
        return cnvrep.bcrop(np.random.standard_normal(tuple(tmp)), dsz)
Пример #14
0
def dictionary_learning_by_L2(X, S, dsz, G, H, parameter_rho_dic, iteration,
                              thr, cri):
    Xf = con.convert_to_Xf(X, S, cri)

    bar_D = tqdm(total=iteration, desc='D', leave=False)
    for i in range(iteration):

        Gf = con.convert_to_Df(G, S, cri)
        Hf = con.convert_to_Df(H, S, cri)
        GH = Gf - Hf
        Sf = con.convert_to_Sf(S, cri)
        b = parameter_rho_dic * GH + sl.inner(np.conj(Xf), Sf, cri.axisK)
        Df = sl.solvemdbi_ism(Xf, parameter_rho_dic, b, cri.axisM, cri.axisK)
        D = con.convert_to_D(Df, dsz, cri)

        XfDf = np.sum(Xf * Df, axis=cri.axisM)
        XD = con.convert_to_S(XfDf, cri)

        Dr = np.asarray(D.reshape(cri.shpD), dtype=S.dtype)
        Hr = np.asarray(H.reshape(cri.shpD), dtype=S.dtype)
        Pcn = cr.getPcn(dsz, cri.Nv, cri.dimN, cri.dimCd)
        Gr = Pcn(Dr + Hr)
        G = cr.bcrop(Gr, dsz, cri.dimN).squeeze()

        H = H + D - G

        Est = sp.norm_2l2(XD - S)
        if (i == 0):
            pre_Est = 1.1 * Est

        if ((pre_Est - Est) / pre_Est <= thr):
            bar_D.update(iteration - i)
            break

        pre_Est = Est

        bar_D.update(1)
    bar_D.close()
    return D, G, H
Пример #15
0
 def test_02(self):
     N = 32
     M = 4
     Nd = 5
     D0 = cr.normalise(cr.zeromean(
         np.random.randn(Nd, Nd, M), (Nd, Nd, M), dimN=2), dimN=2)
     X = np.zeros((N, N, M))
     xr = np.random.randn(N, N, M)
     xp = np.abs(xr) > 3
     X[xp] = np.random.randn(X[xp].size)
     S = np.sum(sl.ifftn(sl.fftn(D0, (N, N), (0,1)) *
                sl.fftn(X, None, (0,1)), None, (0,1)).real, axis=2)
     L = 1e1
     opt = ccmod.ConvCnstrMOD.Options({'Verbose': False,
                 'MaxMainIter': 3000,
                 'ZeroMean': True, 'RelStopTol': 1e-6, 'L': L,
                 'BackTrack': {'Enabled': True}})
     Xr = X.reshape(X.shape[0:2] + (1,1,) + X.shape[2:])
     Sr = S.reshape(S.shape + (1,))
     c = ccmod.ConvCnstrMOD(Xr, Sr, D0.shape, opt)
     c.solve()
     D1 = cr.bcrop(c.X, D0.shape).squeeze()
     assert(sl.rrs(D0, D1) < 1e-4)
     assert(np.array(c.getitstat().Rsdl)[-1] < 1e-5)
Пример #16
0
def convert_to_D(Df, dsz, cri):
    D = sl.irfftn(Df, cri.Nv, cri.axisN)
    D = cr.bcrop(D, dsz, cri.dimN).squeeze()
    return D
Пример #17
0
 def test_12(self):
     dsz = (8, 8, 32)
     u = np.zeros((16, 16, 32))
     u[0:8, 0:8, 0:16] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == dsz
Пример #18
0
 def test_12(self):
     dsz = (8, 8, 32)
     u = np.zeros((16, 16, 32))
     u[0:8, 0:8, 0:16] = 1.0
     v = cnvrep.bcrop(u, dsz)
     assert v.shape == dsz