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
def test_11(self): N = 63 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) X0 = np.zeros((N, N, M)) xr = np.random.randn(N, N, M) xp = np.abs(xr) > 3 X0[xp] = np.random.randn(X0[xp].size) S = np.sum(sl.ifftn( sl.fftn(D, (N, N), (0, 1)) * sl.fftn(X0, None, (0, 1)), None, (0, 1)).real, axis=2) lmbda = 1e-4 rho = 1e-1 opt = cbpdn.ConvBPDN.Options({ 'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-3, 'rho': rho, 'AutoRho': { 'Enabled': False } }) b = cbpdn.ConvBPDN(D, S, lmbda, opt) b.solve() X1 = b.Y.squeeze() assert sl.rrs(X0, X1) < 5e-5 Sr = b.reconstruct().squeeze() assert sl.rrs(S, Sr) < 1e-4
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)
def test_10(self): N = 64 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) X0 = np.zeros((N, N, M)) xr = np.random.randn(N, N, M) xp = np.abs(xr) > 3 X0[xp] = np.random.randn(X0[xp].size) S = np.sum(sl.ifftn( sl.fftn(D, (N, N), (0, 1)) * sl.fftn(X0, None, (0, 1)), None, (0, 1)).real, axis=2) lmbda = 1e-2 L = 1e3 opt = cbpdn.ConvBPDN.Options({ 'Verbose': False, 'MaxMainIter': 2000, 'RelStopTol': 1e-5, 'L': L, 'BackTrack': { 'Enabled': False } }) b = cbpdn.ConvBPDN(D, S, lmbda, opt) b.solve() X1 = b.X.squeeze() assert (sl.rrs(X0, X1) < 5e-4) Sr = b.reconstruct().squeeze() assert (sl.rrs(S, Sr) < 2e-4)
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
def tikhonov_filter(s, lmbda, npd=16): r"""Lowpass filter based on Tikhonov regularization. Lowpass filter image(s) and return low and high frequency components, consisting of the lowpass filtered image and its difference with the input image. The lowpass filter is equivalent to Tikhonov regularization with `lmbda` as the regularization parameter and a discrete gradient as the operator in the regularization term, i.e. the lowpass component is the solution to .. math:: \mathrm{argmin}_\mathbf{x} \; (1/2) \left\|\mathbf{x} - \mathbf{s} \right\|_2^2 + (\lambda / 2) \sum_i \| G_i \mathbf{x} \|_2^2 \;\;, where :math:`\mathbf{s}` is the input image, :math:`\lambda` is the regularization parameter, and :math:`G_i` is an operator that computes the discrete gradient along image axis :math:`i`. Once the lowpass component :math:`\mathbf{x}` has been computed, the highpass component is just :math:`\mathbf{s} - \mathbf{x}`. Parameters ---------- s : array_like Input image or array of images. lmbda : float Regularization parameter controlling lowpass filtering. npd : int, optional (default=16) Number of samples to pad at image boundaries. Returns ------- sl : array_like Lowpass image or array of images. sh : array_like Highpass image or array of images. """ grv = np.array([-1.0, 1.0]).reshape([2, 1]) gcv = np.array([-1.0, 1.0]).reshape([1, 2]) Gr = sla.fftn(grv, (s.shape[0]+2*npd, s.shape[1]+2*npd), (0, 1)) Gc = sla.fftn(gcv, (s.shape[0]+2*npd, s.shape[1]+2*npd), (0, 1)) A = 1.0 + lmbda*np.conj(Gr)*Gr + lmbda*np.conj(Gc)*Gc if s.ndim > 2: A = A[(slice(None),)*2 + (np.newaxis,)*(s.ndim-2)] sp = np.pad(s, ((npd, npd),)*2 + ((0, 0),)*(s.ndim-2), 'symmetric') slp = np.real(sla.ifftn(sla.fftn(sp, axes=(0, 1)) / A, axes=(0, 1))) sl = slp[npd:(slp.shape[0]-npd), npd:(slp.shape[1]-npd)] sh = s - sl return sl.astype(s.dtype), sh.astype(s.dtype)
def tikhonov_filter(s, lmbda, npd=16): r"""Lowpass filter based on Tikhonov regularization. Lowpass filter image(s) and return low and high frequency components, consisting of the lowpass filtered image and its difference with the input image. The lowpass filter is equivalent to Tikhonov regularization with `lmbda` as the regularization parameter and a discrete gradient as the operator in the regularization term, i.e. the lowpass component is the solution to .. math:: \mathrm{argmin}_\mathbf{x} \; (1/2) \left\|\mathbf{x} - \mathbf{s} \right\|_2^2 + (\lambda / 2) \sum_i \| G_i \mathbf{x} \|_2^2 \;\;, where :math:`\mathbf{s}` is the input image, :math:`\lambda` is the regularization parameter, and :math:`G_i` is an operator that computes the discrete gradient along image axis :math:`i`. Once the lowpass component :math:`\mathbf{x}` has been computed, the highpass component is just :math:`\mathbf{s} - \mathbf{x}`. Parameters ---------- s : array_like Input image or array of images. lmbda : float Regularization parameter controlling lowpass filtering. npd : int, optional (default=16) Number of samples to pad at image boundaries. Returns ------- sl : array_like Lowpass image or array of images. sh : array_like Highpass image or array of images. """ grv = np.array([-1.0, 1.0]).reshape([2, 1]) gcv = np.array([-1.0, 1.0]).reshape([1, 2]) Gr = sla.fftn(grv, (s.shape[0] + 2*npd, s.shape[1] + 2*npd), (0, 1)) Gc = sla.fftn(gcv, (s.shape[0] + 2*npd, s.shape[1] + 2*npd), (0, 1)) A = 1.0 + lmbda*np.conj(Gr)*Gr + lmbda*np.conj(Gc)*Gc if s.ndim > 2: A = A[(slice(None),)*2 + (np.newaxis,)*(s.ndim-2)] sp = np.pad(s, ((npd, npd),)*2 + ((0, 0),)*(s.ndim-2), 'symmetric') slp = np.real(sla.ifftn(sla.fftn(sp, axes=(0, 1)) / A, axes=(0, 1))) sl = slp[npd:(slp.shape[0] - npd), npd:(slp.shape[1] - npd)] sh = s - sl return sl.astype(s.dtype), sh.astype(s.dtype)
def reconstruct(self, X=None): """Reconstruct representation.""" Df = self.Df Nz = self.Nvf Nz.append(self.cri.M) # # Stupid Option # Tz = self.getcoef() # Xf = sl.rfftn(Tz,None,self.cri.axisN) #Smart Option Zf = np.dot(tl.tenalg.khatri_rao(self.Kf, reverse=True), self.getweights()) Xf = tl.base.vec_to_tensor(Z, Nz) Sf = np.sum(Df * Xf, axis=self.cri.axisM) return sl.ifftn(Sf, self.cri.Nv, self.cri.axisN)
def tikhonov_filter(s, lmbda, npd=16): """Lowpass filter based on Tikhonov regularization. Lowpass filter image(s) and return low and high frequency components, consisting of the lowpass filtered image and its difference with the input image. The lowpass filter is equivalent to Tikhonov regularization with `lmbda` as the regularization parameter and a discrete gradient as the operator in the regularization term. Parameters ---------- s : array_like Input image or array of images. lmbda : float Regularization parameter controlling lowpass filtering. npd : int, optional (default=16) Number of samples to pad at image boundaries. Returns ------- sl : array_like Lowpass image or array of images. sh : array_like Highpass image or array of images. """ grv = np.array([-1.0, 1.0]).reshape([2, 1]) gcv = np.array([-1.0, 1.0]).reshape([1, 2]) Gr = sla.fftn(grv, (s.shape[0] + 2 * npd, s.shape[1] + 2 * npd), (0, 1)) Gc = sla.fftn(gcv, (s.shape[0] + 2 * npd, s.shape[1] + 2 * npd), (0, 1)) A = 1.0 + lmbda * np.conj(Gr) * Gr + lmbda * np.conj(Gc) * Gc if s.ndim > 2: A = A[(slice(None), ) * 2 + (np.newaxis, ) * (s.ndim - 2)] sp = np.pad(s, ((npd, npd), ) * 2 + ((0, 0), ) * (s.ndim - 2), 'symmetric') slp = np.real(sla.ifftn(sla.fftn(sp, axes=(0, 1)) / A, axes=(0, 1))) sl = slp[npd:(slp.shape[0] - npd), npd:(slp.shape[1] - npd)] sh = s - sl return sl.astype(s.dtype), sh.astype(s.dtype)
def test_11(self): N = 63 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) X0 = np.zeros((N, N, M)) xr = np.random.randn(N, N, M) xp = np.abs(xr) > 3 X0[xp] = np.random.randn(X0[xp].size) S = np.sum(sl.ifftn(sl.fftn(D, (N, N), (0, 1)) * sl.fftn(X0, None, (0, 1)), None, (0, 1)).real, axis=2) lmbda = 1e-2 L = 1e3 opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 2000, 'RelStopTol': 1e-9, 'L': L, 'BackTrack': {'Enabled': False}}) b = cbpdn.ConvBPDN(D, S, lmbda, opt) b.solve() X1 = b.X.squeeze() assert sl.rrs(X0, X1) < 5e-4 Sr = b.reconstruct().squeeze() assert sl.rrs(S, Sr) < 2e-4
def test_09(self): N = 64 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) X0 = np.zeros((N, N, M)) xr = np.random.randn(N, N, M) xp = np.abs(xr) > 3 X0[xp] = np.random.randn(X0[xp].size) S = np.sum(sl.ifftn(sl.fftn(D, (N, N), (0, 1)) * sl.fftn(X0, None, (0, 1)), None, (0, 1)).real, axis=2) lmbda = 1e-4 rho = 3e-3 alpha = 6 opt = parcbpdn.ParConvBPDN.Options({'Verbose': False, 'MaxMainIter': 1000, 'RelStopTol': 1e-3, 'rho': rho, 'alpha': alpha, 'AutoRho': {'Enabled': False}}) b = parcbpdn.ParConvBPDN(D, S, lmbda, opt=opt) b.solve() X1 = b.Y.squeeze() assert sl.rrs(X0, X1) < 5e-5 Sr = b.reconstruct().squeeze() assert sl.rrs(S, Sr) < 1e-4
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
def T_ConvFISTA_precompute(Gram, Achapy, Z_init, L, lbd, beta, maxit, tol=1e-5, verbose=False): """ Minimization of the sub-block of Z Gram: Gram matrix Achapy: vector A * y Z_init: initialization L: Lipschitz constant lbd, beta: hyerparameters """ K, N_i, R = Z_init.shape pobj = [] time0 = time.time() Zpred = Z_init.copy() Xfista = Z_init.copy() t = 1 ite = 0. tol_it = tol + 1. while (tol_it > tol) and (ite < maxit): if verbose: print(ite) Zpred_old = Zpred.copy() # DFT of the activations Xfistachap = fftn(Xfista, axes=[1]) # Vectorization xfistachap = np.reshape(Xfistachap, (K, N_i * R), order='F').ravel() # Computation of the gradient gradf = (Gram.dot(xfistachap) - Achapy) + 2 * beta * xfistachap # Descent step xfistachap = np.array(xfistachap - gradf / L) # Matricization Xfistachap = xfistachap.reshape(K, N_i * R).reshape((K, N_i, R), order='F') # IDFT of the activations Xfista = np.real(ifftn(Xfistachap, axes=[1])) # Soft-thresholding Zpred = np.sign(Xfista) * np.fmax(abs(Xfista) - lbd / L, 0.) # Nesterov Momentum t0 = t t = (1. + np.sqrt(1. + 4. * t**2)) / 2. Xfista = Zpred + ((t0 - 1.) / t) * (Zpred - Zpred_old) # Stopping criterion tol_it = np.max(abs(Zpred_old - Zpred)) this_pobj = tol_it.copy() ite += 1 pobj.append((time.time() - time0, this_pobj)) print('last iteration:', ite) times, pobj = map(np.array, zip(*pobj)) return Zpred, times, pobj