def solve_for_label(j=0,lmbda=0.001,opt=opt): Y_j = Y_opt[:, [j]] # Initialise and run BPDN object for best lmbda b = bpdn.BPDN(V_m, Y_j, lmbda, opt) A_j = b.solve() # print("BPDN solve time: %.2fs" % b.timer.elapsed('solve')) return A_j
def xstep(self): """Randomly choose max_slices number of slices to update.""" self.extra_timer.start('xstep') signal = self.Y - self.U signal = signal.transpose((1, 0, 2)) # [K, n, N] -> [n, K, N] -> [n, K*N] signal = signal.reshape(signal.shape[0], -1) selected = self.get_next_batch() if not hasattr(self, '_X_bpdn_cache'): self._X_bpdn_cache = np.zeros( (self.D.shape[1], signal.shape[-1]), dtype=self.dtype ) signal = signal[:, selected] opt = copy.deepcopy(self.opt['BPDN']) opt['Y0'] = self._X_bpdn_cache[:, selected] solver = bpdn.BPDN(self.D, signal, lmbda=self.lmbda/self.rho, opt=opt) X = solver.solve() self._X_bpdn_cache[:, selected] = X self.X = self.X.transpose(1, 0, 2) self.X = self.X.reshape(self.X.shape[0], -1) self.X[:, selected] = X self.X = self.X.reshape( self.X.shape[0], self.Y.shape[0], self.Y.shape[2] ).transpose(1, 0, 2) self.extra_timer.stop('xstep')
def test_18(self): N = 64 M = 2 * N L = 8 np.random.seed(12345) D = np.random.randn(N, M) x0 = np.zeros((M, 1)) si = np.random.permutation(list(range(0, M - 1))) x0[si[0:L]] = np.random.randn(L, 1) s = D.dot(x0) lmbda = 5e-2 opt = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 300, 'RelStopTol': 1e-5, 'AutoRho': { 'Enabled': False } }) bp = bpdn.BPDN(D, s, lmbda=lmbda, opt=opt) Xp = bp.solve() epsilon = np.linalg.norm(D.dot(Xp) - s) opt = bpdn.MinL1InL2Ball.Options({ 'Verbose': False, 'MaxMainIter': 300, 'RelStopTol': 1e-5, 'rho': 2e1, 'AutoRho': { 'Enabled': False } }) bc = bpdn.MinL1InL2Ball(D, s, epsilon=epsilon, opt=opt) Xc = bc.solve() assert np.linalg.norm(Xp - Xc) / np.linalg.norm(Xp) < 1e-3 assert np.abs(np.linalg.norm(Xp, 1) - np.linalg.norm(Xc, 1)) < 1e-3
def __init__(self, D0, S, lmbda=None, opt=None): """ Initialise a BPDNDictLearn object with problem size and options. Parameters ---------- D0 : array_like, shape (N, M) Initial dictionary matrix S : array_like, shape (N, K) Signal vector or matrix lmbda : float Regularisation parameter opt : :class:`BPDNDictLearn.Options` object Algorithm options """ if opt is None: opt = BPDNDictLearn.Options() self.opt = opt # Normalise dictionary according to D update options D0 = cmod.getPcn(opt['CMOD', 'ZeroMean'])(D0) # Modify D update options to include initial values for Y and U Nc = D0.shape[1] opt['CMOD'].update({'Y0' : D0, 'U0' : np.zeros((S.shape[0], Nc))}) # Create X update object xstep = bpdn.BPDN(D0, S, lmbda, opt['BPDN']) # Create D update object Nm = S.shape[1] dstep = cmod.CnstrMOD(xstep.Y, S, (Nc, Nm), opt['CMOD']) # Configure iteration statistics reporting isc = dictlrn.IterStatsConfig( isfld = ['Iter', 'ObjFun', 'DFid', 'RegL1', 'Cnstr', 'XPrRsdl', 'XDlRsdl', 'XRho', 'DPrRsdl', 'DDlRsdl', 'DRho', 'Time'], isxmap = {'ObjFun' : 'ObjFun', 'DFid' : 'DFid', 'RegL1' : 'RegL1', 'XPrRsdl' : 'PrimalRsdl', 'XDlRsdl' : 'DualRsdl', 'XRho' : 'Rho'}, isdmap = {'Cnstr' : 'Cnstr', 'DPrRsdl' : 'PrimalRsdl', 'DDlRsdl' : 'DualRsdl', 'DRho' : 'Rho'}, evlmap = {}, hdrtxt = ['Itn', 'Fnc', 'DFid', u('ℓ1'), 'Cnstr', 'r_X', 's_X', u('ρ_X'), 'r_D', 's_D', u('ρ_D')], hdrmap = {'Itn' : 'Iter', 'Fnc' : 'ObjFun', 'DFid' : 'DFid', u('ℓ1') : 'RegL1', 'Cnstr' : 'Cnstr', 'r_X' : 'XPrRsdl', 's_X' : 'XDlRsdl', u('ρ_X') : 'XRho', 'r_D' : 'DPrRsdl', 's_D' : 'DDlRsdl', u('ρ_D') : 'DRho'} ) # Call parent constructor super(BPDNDictLearn, self).__init__(xstep, dstep, opt, isc)
def test_02(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) try: b = bpdn.BPDN(D, s) b.solve() except Exception as e: print(e) assert (0)
def test_01(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) lmbda = 1e-1 try: b = bpdn.BPDN(D, s, lmbda) b.solve() except Exception as e: print(e) assert (0)
def test_17(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) lmbda = 1e-1 opt = bpdn.BPDN.Options({'Verbose': False, 'MaxMainIter': 10}) b = bpdn.BPDN(D, s, lmbda, opt) bp = pickle.dumps(b) c = pickle.loads(bp) Xb = b.solve() Xc = c.solve() assert (linalg.norm(Xb - Xc) == 0.0)
def test_03(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) try: opt = bpdn.BPDN.Options({ 'Verbose': True, 'MaxMainIter': 20, 'AutoRho': { 'StdResiduals': True } }) b = bpdn.BPDN(D, s, lmbda=1.0, opt=opt) b.solve() except Exception as e: print(e) assert (0)
def test_04(self): N = 8 M = 8 D = np.random.randn(N, M) s = np.random.randn(N, 1) try: opt = bpdn.BPDN.Options({ 'FastSolve': True, 'Verbose': False, 'MaxMainIter': 10, 'AutoRho': { 'Enabled': False } }) b = bpdn.BPDN(D, s, lmbda=1.0, opt=opt) b.solve() except Exception as e: print(e) assert (0)
def test_05(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) dt = np.float16 opt = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 20, 'AutoRho': { 'Enabled': True }, 'DataType': dt }) b = bpdn.BPDN(D, s, lmbda=1.0, opt=opt) b.solve() assert (b.X.dtype == dt) assert (b.Y.dtype == dt) assert (b.U.dtype == dt)
def test_03(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) try: opt = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 20, 'LinSolveCheck': True, 'AutoRho': { 'StdResiduals': True } }) b = bpdn.BPDN(D, s, lmbda=1.0, opt=opt) b.solve() except Exception as e: print(e) assert (0) assert (np.array(b.getitstat().XSlvRelRes).max() < 1e-5)
def test_20(self): N = 8 M = 16 D = np.random.randn(N, M) s = np.random.randn(N, 1) lmbda = 1e-1 opt = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 10, 'Callback': CallbackTest, 'RelaxParam': 1.0 }) b = bpdn.BPDN(D, s, lmbda, opt=opt) assert (b.getitstat() is None) b.solve() assert (b.runtime > 0.0) assert (b.k == 7) assert (b.var_x() is not None) assert (b.var_y() is not None) assert (b.var_u() is not None)
def test_08(self): N = 64 M = 2 * N L = 4 np.random.seed(12345) D = np.random.randn(N, M) x0 = np.zeros((M, 1)) si = np.random.permutation(list(range(0, M - 1))) x0[si[0:L]] = np.random.randn(L, 1) s0 = D.dot(x0) lmbda = 5e-3 opt = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 5e-4 }) b = bpdn.BPDN(D, s0, lmbda, opt) b.solve() x1 = b.Y assert (np.abs(b.itstat[-1].ObjFun - 0.012009) < 1e-5) assert (np.abs(b.itstat[-1].DFid - 1.9636082e-06) < 1e-5) assert (np.abs(b.itstat[-1].RegL1 - 2.401446) < 1e-5) assert (linalg.norm(x1 - x0) < 1e-3)
def xstep(self): r"""Minimize with respect to :math:`x`. This has the form: .. math:: f(x)=\sum_i\left(\lambda\|x_i\|_1+\frac{\rho}{2}\|D_l x_i - y_i + u_i\|_2^2\right). This could be solved in parallel over all slice indices i and all batch indices k (implicit in the above form). """ self.extra_timer.start('xstep') signal = self.Y - self.U signal = signal.transpose((1, 0, 2)) # [K, n, N] -> [n, K, N] -> [n, K*N] signal = signal.reshape(signal.shape[0], -1) opt = copy.deepcopy(self.opt['BPDN']) opt['Y0'] = getattr(self, '_X_bpdn_cache', None) solver = bpdn.BPDN(self.D, signal, lmbda=self.lmbda/self.rho, opt=opt) self.X = solver.solve() self._X_bpdn_cache = copy.deepcopy(self.X) self.X = self.X.reshape( self.X.shape[0], self.Y.shape[0], self.Y.shape[2] ).transpose((1, 0, 2)) self.extra_timer.stop('xstep')
} }) d = bpdndl.BPDNDictLearn(D0, train_dataset.squeeze(1).T, config.lambda_l, opt) d.solve() D1 = d.getdict() plt.figure(figsize=(10, 5 * config.embed_size)) for i in range(config.embed_size): plt.subplot(config.embed_size, 1, i + 1) plt.plot(D1[:, i], label="basis function n".format(i)) plt.legend() plt.savefig(os.path.join(results_folder, "basis_functions.jpg")) plt.close('all') r = bpdn.BPDN(D1, val_dataset.squeeze(1).T, config.lambda_r) reconstruction = (D1 @ r.solve()).T plt.figure(figsize=(10, 5 * 32)) for i in range(32): plt.subplot(32, 1, i + 1) j = np.random.choice(len(val_dataset)) plt.plot(val_dataset[j, 0, :], label="true") plt.plot(reconstruction[j, :], label="pred") plt.legend() plt.savefig(os.path.join(results_folder, "reconstruction.jpg")) plt.close('all') mse = np.linalg.norm( (val_dataset.squeeze(1) - reconstruction), ord=2)**2 / len(val_dataset)
Set options for ADMM solver. """ opt_admm = abpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-3, 'AutoRho': { 'RsdlTarget': 1.0 } }) """ Initialise and run ADMM solver object. """ ba = abpdn.BPDN(D, s, lmbda, opt_admm) xa = ba.solve() print("ADMM BPDN solve time: %.2fs" % ba.timer.elapsed('solve')) """ Set options for PGM solver. """ opt_pgm = pbpdn.BPDN.Options({ 'Verbose': True, 'MaxMainIter': 50, 'L': 9e2, 'Backtrack': BacktrackRobust() }) """ Initialise and run PGM solver.
def gengraphs(pth): """ Generate call graph images when necessary. Parameter pth is the path to the directory in which images are to be created. """ srcmodflt = '^sporco.admm' srcqnmflt = r'^((?!<locals>|__new|_name_nested).)*$' dstqnmflt = r'^((?!<locals>|__new|_name_nested).)*$' fnmsub = ('^sporco.admm.', '') grpflt = r'^[^\.]*.[^\.]*' lnkpfx = '../../modules/' lnksub = (r'^([^\.]*).([^\.]*)(?:(.__init__|.__call__)|(.[^\.]*))', lnkpfx + r'sporco.admm.\1.html#sporco.admm.\1.\2\4') fntsz = 9 fntfm = 'Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans' kwargs = {'fntsz': fntsz, 'fntfm': fntfm, 'rmsz': True} ct = jonga.CallTracer(srcmodflt=srcmodflt, srcqnmflt=srcqnmflt, dstqnmflt=dstqnmflt, fnmsub=fnmsub, grpflt=grpflt, lnksub=lnksub) # Make destination directory if it doesn't exist if not os.path.exists(pth): os.makedirs(pth, exist_ok=True) import numpy as np np.random.seed(12345) #### bpdn module from sporco.admm import bpdn mdnm = 'sporco.admm.bpdn' D = np.random.randn(8, 16) s = np.random.randn(8, 1) lmbda = 0.1 ## BPDN class opt = bpdn.BPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'bpdn_init.svg', **kwargs): b = bpdn.BPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'bpdn_solve.svg', **kwargs): b.solve() ## BPDNJoint class opt = bpdn.BPDNJoint.Options({'Verbose': False, 'MaxMainIter': 1}) mu = 0.01 with CallGraph(ct, mdnm, pth, 'bpdnjnt_init.svg', **kwargs): b = bpdn.BPDNJoint(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'bpdnjnt_solve.svg', **kwargs): b.solve() ## ElasticNet class opt = bpdn.ElasticNet.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'elnet_init.svg', **kwargs): b = bpdn.ElasticNet(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'elnet_solve.svg', **kwargs): b.solve() # BPDNProjL1 class opt = bpdn.BPDNProjL1.Options({'Verbose': False, 'MaxMainIter': 1}) gamma = 2.0 with CallGraph(ct, mdnm, pth, 'bpdnprjl1_init.svg', **kwargs): b = bpdn.BPDNProjL1(D, s, gamma, opt) with CallGraph(ct, mdnm, pth, 'bpdnprjl1_solve.svg', **kwargs): b.solve() ## MinL1InL2Ball class opt = bpdn.MinL1InL2Ball.Options({'Verbose': False, 'MaxMainIter': 1}) epsilon = 1.0 with CallGraph(ct, mdnm, pth, 'bpdnml1l2_init.svg', **kwargs): b = bpdn.MinL1InL2Ball(D, s, epsilon, opt) with CallGraph(ct, mdnm, pth, 'bpdnml1l2_solve.svg', **kwargs): b.solve() #### cbpdn module from sporco.admm import cbpdn mdnm = 'sporco.admm.cbpdn' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 ## ConvBPDN class opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdn_init.svg', **kwargs): b = cbpdn.ConvBPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'cbpdn_solve.svg', **kwargs): b.solve() ## ConvBPDNJoint class opt = cbpdn.ConvBPDNJoint.Options({'Verbose': False, 'MaxMainIter': 1}) mu = 0.01 with CallGraph(ct, mdnm, pth, 'cbpdnjnt_init.svg', **kwargs): b = cbpdn.ConvBPDNJoint(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnjnt_solve.svg', **kwargs): b.solve() ## ConvElasticNet class opt = cbpdn.ConvElasticNet.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'celnet_init.svg', **kwargs): b = cbpdn.ConvElasticNet(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'celnet_solve.svg', **kwargs): b.solve() ## ConvBPDNGradReg class opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdngrd_init.svg', **kwargs): b = cbpdn.ConvBPDNGradReg(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdngrd_solve.svg', **kwargs): b.solve() ## ConvBPDNProjL1 class opt = cbpdn.ConvBPDNProjL1.Options({'Verbose': False, 'MaxMainIter': 1}) gamma = 0.5 with CallGraph(ct, mdnm, pth, 'cbpdnprjl1_init.svg', **kwargs): b = cbpdn.ConvBPDNProjL1(D, s, gamma, opt) with CallGraph(ct, mdnm, pth, 'cbpdnprjl1_solve.svg', **kwargs): b.solve() ## ConvMinL1InL2Ball class opt = cbpdn.ConvMinL1InL2Ball.Options({'Verbose': False, 'MaxMainIter': 1}) epsilon = 0.5 with CallGraph(ct, mdnm, pth, 'cbpdnml1l2_init.svg', **kwargs): b = cbpdn.ConvMinL1InL2Ball(D, s, epsilon, opt) with CallGraph(ct, mdnm, pth, 'cbpdnml1l2_solve.svg', **kwargs): b.solve() ## ConvBPDNMaskDcpl class opt = cbpdn.ConvBPDNMaskDcpl.Options({'Verbose': False, 'MaxMainIter': 1}) W = np.ones(s.shape) with CallGraph(ct, mdnm, pth, 'cbpdnmd_init.svg', **kwargs): b = cbpdn.ConvBPDNMaskDcpl(D, s, lmbda, W, opt) with CallGraph(ct, mdnm, pth, 'cbpdnmd_solve.svg', **kwargs): b.solve() ## ConvL1L1Grd class opt = cbpdn.ConvL1L1Grd.Options({'Verbose': False, 'MaxMainIter': 1}) mu = 1e-2 with CallGraph(ct, mdnm, pth, 'cl1l1grd_init.svg', **kwargs): b = cbpdn.ConvL1L1Grd(D, s, lmbda, mu, W, opt) with CallGraph(ct, mdnm, pth, 'cl1l1grd_solve.svg', **kwargs): b.solve() #### cbpdntv module from sporco.admm import cbpdntv mdnm = 'sporco.admm.cbpdntv' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 mu = 0.01 ## ConvBPDNScalarTV class opt = cbpdntv.ConvBPDNScalarTV.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'cbpdnstv_init.svg', **kwargs): b = cbpdntv.ConvBPDNScalarTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnstv_solve.svg', **kwargs): b.solve() ## ConvBPDNVectorTV class opt = cbpdntv.ConvBPDNVectorTV.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'cbpdnvtv_init.svg', **kwargs): b = cbpdntv.ConvBPDNVectorTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnvtv_solve.svg', **kwargs): b.solve() ## ConvBPDNRecTV class opt = cbpdntv.ConvBPDNRecTV.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdnrtv_init.svg', **kwargs): b = cbpdntv.ConvBPDNRecTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnrtv_solve.svg', **kwargs): b.solve() #### cbpdnin module from sporco.admm import cbpdnin mdnm = 'sporco.admm.cbpdnin' D = np.random.randn(4, 4, 32) s = np.random.randn(8, 8) lmbda = 0.1 mu = 0.01 Wg = np.append(np.eye(16), np.eye(16), axis=-1) ## ConvBPDNInhib class opt = cbpdnin.ConvBPDNInhib.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdnin_init.svg', **kwargs): b = cbpdnin.ConvBPDNInhib(D, s, Wg, Whn=4, lmbda=lmbda, mu=mu, gamma=None, opt=opt) with CallGraph(ct, mdnm, pth, 'cbpdnin_solve.svg', **kwargs): b.solve() #### cmod module from sporco.admm import cmod mdnm = 'sporco.admm.cmod' X = np.random.randn(8, 16) S = np.random.randn(8, 16) ## CnstrMOD class opt = cmod.CnstrMOD.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cmod_init.svg', **kwargs): b = cmod.CnstrMOD(X, S, opt=opt) with CallGraph(ct, mdnm, pth, 'cmod_solve.svg', **kwargs): b.solve() #### ccmod module from sporco.admm import ccmod mdnm = 'sporco.admm.ccmod' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) dsz = (4, 4, 1) ## ConvCnstrMOD_IterSM class opt = ccmod.ConvCnstrMOD_IterSM.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodism_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_IterSM(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodism_solve.svg', **kwargs): b.solve() ## ConvCnstrMOD_CG class opt = ccmod.ConvCnstrMOD_CG.Options({ 'Verbose': False, 'MaxMainIter': 1, 'CG': { 'MaxIter': 1 } }) with CallGraph(ct, mdnm, pth, 'ccmodcg_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_CG(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodcg_solve.svg', **kwargs): b.solve() ## ConvCnstrMOD_Consensus class opt = ccmod.ConvCnstrMOD_Consensus.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodcnsns_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_Consensus(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodcnsns_solve.svg', **kwargs): b.solve() #### ccmodmd module from sporco.admm import ccmodmd mdnm = 'sporco.admm.ccmodmd' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) W = np.array([1.0]) dsz = (4, 4, 1) ## ConvCnstrMODMaskDcpl_IterSM class opt = ccmodmd.ConvCnstrMODMaskDcpl_IterSM.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodmdism_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_IterSM(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdism_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMaskDcpl_CG class opt = ccmodmd.ConvCnstrMODMaskDcpl_CG.Options({ 'Verbose': False, 'MaxMainIter': 1, 'CG': { 'MaxIter': 1 } }) with CallGraph(ct, mdnm, pth, 'ccmodmdcg_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_CG(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdcg_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMaskDcpl_Consensus class opt = ccmodmd.ConvCnstrMODMaskDcpl_Consensus.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodmdcnsns_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_Consensus(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdcnsns_solve.svg', **kwargs): b.solve() #### tvl1 module from sporco.admm import tvl1 mdnm = 'sporco.admm.tvl1' s = np.random.randn(16, 16) lmbda = 0.1 ## TVL1Denoise class opt = tvl1.TVL1Denoise.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'tvl1den_init.svg', **kwargs): b = tvl1.TVL1Denoise(s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl1den_solve.svg', **kwargs): b.solve() ## TVL1Deconv class opt = tvl1.TVL1Deconv.Options({'Verbose': False, 'MaxMainIter': 1}) h = np.random.randn(3, 3) with CallGraph(ct, mdnm, pth, 'tvl1dcn_init.svg', **kwargs): b = tvl1.TVL1Deconv(h, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl1dcn_solve.svg', **kwargs): b.solve() #### tvl2 module from sporco.admm import tvl2 mdnm = 'sporco.admm.tvl2' s = np.random.randn(16, 16) lmbda = 0.1 ## TVL2Denoise class opt = tvl2.TVL2Denoise.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'tvl2den_init.svg', **kwargs): b = tvl2.TVL2Denoise(s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl2den_solve.svg', **kwargs): b.solve() ## TVL2Deconv class opt = tvl2.TVL2Deconv.Options({'Verbose': False, 'MaxMainIter': 1}) h = np.random.randn(3, 3) with CallGraph(ct, mdnm, pth, 'tvl2dcn_init.svg', **kwargs): b = tvl2.TVL2Deconv(h, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl2dcn_solve.svg', **kwargs): b.solve() srcmodflt = '^sporco.fista' fnmsub = ('^sporco.fista.', '') lnksub = (r'^([^\.]*).([^\.]*)(?:(.__init__|.__call__)|(.[^\.]*))', lnkpfx + r'sporco.fista.\1.html#sporco.fista.\1.\2\4') ct = jonga.CallTracer(srcmodflt=srcmodflt, srcqnmflt=srcqnmflt, dstqnmflt=dstqnmflt, fnmsub=fnmsub, grpflt=grpflt, lnksub=lnksub) #### fista.cbpdn module from sporco.fista import cbpdn mdnm = 'sporco.fista.cbpdn' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 ## ConvBPDN class opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'fista_cbpdn_init.svg', **kwargs): b = cbpdn.ConvBPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'fista_cbpdn_solve.svg', **kwargs): b.solve() #### fista.ccmod module from sporco.fista import ccmod mdnm = 'sporco.fista.ccmod' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) dsz = (4, 4, 1) ## ConvCnstrMOD class opt = ccmod.ConvCnstrMOD.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'ccmodfista_init.svg', **kwargs): b = ccmod.ConvCnstrMOD(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodfista_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMask class opt = ccmod.ConvCnstrMODMask.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'ccmodmdfista_init.svg', **kwargs): b = ccmod.ConvCnstrMODMask(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdfista_solve.svg', **kwargs): b.solve() srcmodflt = '^sporco.dictlrn' fnmsub = ('^sporco.dictlrn.', '') lnksub = (r'^([^\.]*).([^\.]*)(?:(.__init__|.__call__)|(.[^\.]*))', lnkpfx + r'sporco.dictlrn.\1.html#sporco.dictlrn.\1.\2\4') ct = jonga.CallTracer(srcmodflt=srcmodflt, srcqnmflt=srcqnmflt, dstqnmflt=dstqnmflt, fnmsub=fnmsub, grpflt=grpflt, lnksub=lnksub) #### bpdndl module from sporco.dictlrn import bpdndl mdnm = 'sporco.dictlrn.bpdndl' D0 = np.random.randn(8, 8) S = np.random.randn(8, 16) lmbda = 0.1 ## BPDNDictLearn class opt = bpdndl.BPDNDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'bpdndl_init.svg', **kwargs): b = bpdndl.BPDNDictLearn(D0, S, lmbda, opt) with CallGraph(ct, mdnm, pth, 'bpdndl_solve.svg', **kwargs): b.solve() #### cbpdndl module from sporco.dictlrn import cbpdndl mdnm = 'sporco.dictlrn.cbpdndl' D0 = np.random.randn(4, 4, 16) s = np.random.randn(8, 8, 10) lmbda = 0.1 ## ConvBPDNDictLearn class opt = cbpdndl.ConvBPDNDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'cbpdndl_init.svg', **kwargs): b = cbpdndl.ConvBPDNDictLearn(D0, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'cbpdndl_solve.svg', **kwargs): b.solve() #### cbpdndlmd module from sporco.dictlrn import cbpdndlmd mdnm = 'sporco.dictlrn.cbpdndlmd' ## ConvBPDNMaskDcplDictLearn class W = np.array([1.0]) opt = cbpdndlmd.ConvBPDNMaskDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'cbpdnmddl_init.svg', **kwargs): b = cbpdndlmd.ConvBPDNMaskDictLearn(D0, s, lmbda, W, opt) with CallGraph(ct, mdnm, pth, 'cbpdnmddl_solve.svg', **kwargs): b.solve()
'rho': 50.0 * lmbda + 0.5 }) optd = cmod.CnstrMOD.Options({ 'Verbose': False, 'MaxMainIter': 1, 'rho': S.shape[1] / 200.0 }) # Normalise dictionary according to D update options D0 = cmod.getPcn(optd['ZeroMean'])(D0) # Update D update options to include initial values for Y and U optd.update({'Y0': D0, 'U0': np.zeros((S.shape[0], D0.shape[1]))}) # Create X update object xstep = bpdn.BPDN(D0, S, lmbda, optx) # Create D update object dstep = cmod.CnstrMOD(None, S, (D0.shape[1], S.shape[1]), optd) # Create DictLearn object opt = dictlrn.DictLearn.Options({'Verbose': True, 'MaxMainIter': 100}) d = dictlrn.DictLearn(xstep, dstep, opt) Dmx = d.solve() print("DictLearn solve time: %.2fs" % d.timer.elapsed('solve')) # Display dictionaries D1 = Dmx.reshape((8, 8, D0.shape[1])) D0 = D0.reshape(8, 8, D0.shape[-1]) fig1 = plot.figure(1, figsize=(14, 7)) plot.subplot(1, 2, 1)
def gengraphs(pth, nopyfftw): """ Generate call graph images when necessary. Parameter pth is the path to the directory in which images are to be created. Parameter nopyfftw is a flag indicating whether it is necessary to avoid using pyfftw. """ srcmodflt = '^sporco.admm' srcqnmflt = r'^((?!<locals>|__new|_name_nested).)*$' dstqnmflt = r'^((?!<locals>|__new|_name_nested).)*$' fnmsub = ('^sporco.admm.', '') grpflt = r'^[^\.]*.[^\.]*' lnksub = (r'^([^\.]*).(.*)', r'../../sporco.admm.\1.html#sporco.admm.\1.\2') fntsz = 9 fntfm = 'Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans' kwargs = {'fntsz': fntsz, 'fntfm': fntfm, 'rmsz': True} ct = jonga.CallTracer(srcmodflt=srcmodflt, srcqnmflt=srcqnmflt, dstqnmflt=dstqnmflt, fnmsub=fnmsub, grpflt=grpflt, lnksub=lnksub) # Make destination directory if it doesn't exist if not os.path.exists(pth): os.makedirs(pth, exist_ok=True) # Handle environment in which pyfftw is unavailable if nopyfftw: import numpy.fft as npfft import sporco.linalg as spl def empty(shape, dtype, order='C', n=None): return np.zeros(shape, dtype=dtype) spl.pyfftw_empty_aligned = empty def rfftn_empty(shape, axes, dtype, order='C', n=None): ashp = list(shape) raxis = axes[-1] ashp[raxis] = ashp[raxis] // 2 + 1 cdtype = spl.complex_dtype(dtype) return np.zeros(ashp, dtype=cdtype) spl.pyfftw_rfftn_empty_aligned = rfftn_empty spl.fftn = npfft.fftn spl.ifftn = npfft.ifftn spl.rfftn = npfft.rfftn spl.irfftn = npfft.irfftn import numpy as np np.random.seed(12345) #### bpdn module from sporco.admm import bpdn mdnm = 'sporco.admm.bpdn' D = np.random.randn(8, 16) s = np.random.randn(8, 1) lmbda = 0.1 ## BPDN class opt = bpdn.BPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'bpdn_init.svg', **kwargs): b = bpdn.BPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'bpdn_solve.svg', **kwargs): b.solve() ## BPDNJoint class opt = bpdn.BPDNJoint.Options({'Verbose': False, 'MaxMainIter': 1}) mu = 0.01 with CallGraph(ct, mdnm, pth, 'bpdnjnt_init.svg', **kwargs): b = bpdn.BPDNJoint(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'bpdnjnt_solve.svg', **kwargs): b.solve() ## ElasticNet class opt = bpdn.ElasticNet.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'elnet_init.svg', **kwargs): b = bpdn.ElasticNet(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'elnet_solve.svg', **kwargs): b.solve() # BPDNProjL1 class opt = bpdn.BPDNProjL1.Options({'Verbose': False, 'MaxMainIter': 1}) gamma = 2.0 with CallGraph(ct, mdnm, pth, 'bpdnprjl1_init.svg', **kwargs): b = bpdn.BPDNProjL1(D, s, gamma, opt) with CallGraph(ct, mdnm, pth, 'bpdnprjl1_solve.svg', **kwargs): b.solve() ## MinL1InL2Ball class opt = bpdn.MinL1InL2Ball.Options({'Verbose': False, 'MaxMainIter': 1}) epsilon = 1.0 with CallGraph(ct, mdnm, pth, 'bpdnml1l2_init.svg', **kwargs): b = bpdn.MinL1InL2Ball(D, s, epsilon, opt) with CallGraph(ct, mdnm, pth, 'bpdnml1l2_solve.svg', **kwargs): b.solve() #### cbpdn module from sporco.admm import cbpdn mdnm = 'sporco.admm.cbpdn' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 ## ConvBPDN class opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdn_init.svg', **kwargs): b = cbpdn.ConvBPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'cbpdn_solve.svg', **kwargs): b.solve() ## ConvBPDNJoint class opt = cbpdn.ConvBPDNJoint.Options({'Verbose': False, 'MaxMainIter': 1}) mu = 0.01 with CallGraph(ct, mdnm, pth, 'cbpdnjnt_init.svg', **kwargs): b = cbpdn.ConvBPDNJoint(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnjnt_solve.svg', **kwargs): b.solve() ## ConvElasticNet class opt = cbpdn.ConvElasticNet.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'celnet_init.svg', **kwargs): b = cbpdn.ConvElasticNet(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'celnet_solve.svg', **kwargs): b.solve() ## ConvBPDNGradReg class opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdngrd_init.svg', **kwargs): b = cbpdn.ConvBPDNGradReg(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdngrd_solve.svg', **kwargs): b.solve() ## ConvBPDNProjL1 class opt = cbpdn.ConvBPDNProjL1.Options({'Verbose': False, 'MaxMainIter': 1}) gamma = 0.5 with CallGraph(ct, mdnm, pth, 'cbpdnprjl1_init.svg', **kwargs): b = cbpdn.ConvBPDNProjL1(D, s, gamma, opt) with CallGraph(ct, mdnm, pth, 'cbpdnprjl1_solve.svg', **kwargs): b.solve() ## ConvMinL1InL2Ball class opt = cbpdn.ConvMinL1InL2Ball.Options({'Verbose': False, 'MaxMainIter': 1}) epsilon = 0.5 with CallGraph(ct, mdnm, pth, 'cbpdnml1l2_init.svg', **kwargs): b = cbpdn.ConvMinL1InL2Ball(D, s, epsilon, opt) with CallGraph(ct, mdnm, pth, 'cbpdnml1l2_solve.svg', **kwargs): b.solve() ## ConvBPDNMaskDcpl class opt = cbpdn.ConvBPDNMaskDcpl.Options({'Verbose': False, 'MaxMainIter': 1}) W = np.ones(s.shape) with CallGraph(ct, mdnm, pth, 'cbpdnmd_init.svg', **kwargs): b = cbpdn.ConvBPDNMaskDcpl(D, s, lmbda, W, opt) with CallGraph(ct, mdnm, pth, 'cbpdnmd_solve.svg', **kwargs): b.solve() #### cbpdntv module from sporco.admm import cbpdntv mdnm = 'sporco.admm.cbpdntv' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 mu = 0.01 ## ConvBPDNScalarTV class opt = cbpdntv.ConvBPDNScalarTV.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'cbpdnstv_init.svg', **kwargs): b = cbpdntv.ConvBPDNScalarTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnstv_solve.svg', **kwargs): b.solve() ## ConvBPDNVectorTV class opt = cbpdntv.ConvBPDNVectorTV.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'cbpdnvtv_init.svg', **kwargs): b = cbpdntv.ConvBPDNVectorTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnvtv_solve.svg', **kwargs): b.solve() ## ConvBPDNRecTV class opt = cbpdntv.ConvBPDNRecTV.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cbpdnrtv_init.svg', **kwargs): b = cbpdntv.ConvBPDNRecTV(D, s, lmbda, mu, opt) with CallGraph(ct, mdnm, pth, 'cbpdnrtv_solve.svg', **kwargs): b.solve() #### cmod module from sporco.admm import cmod mdnm = 'sporco.admm.cmod' X = np.random.randn(8, 16) S = np.random.randn(8, 16) ## CnstrMOD class opt = cmod.CnstrMOD.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'cmod_init.svg', **kwargs): b = cmod.CnstrMOD(X, S, opt=opt) with CallGraph(ct, mdnm, pth, 'cmod_solve.svg', **kwargs): b.solve() #### ccmod module from sporco.admm import ccmod mdnm = 'sporco.admm.ccmod' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) dsz = (4, 4, 1) ## ConvCnstrMOD_IterSM class opt = ccmod.ConvCnstrMOD_IterSM.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodism_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_IterSM(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodism_solve.svg', **kwargs): b.solve() ## ConvCnstrMOD_CG class opt = ccmod.ConvCnstrMOD_CG.Options({ 'Verbose': False, 'MaxMainIter': 1, 'CG': { 'MaxIter': 1 } }) with CallGraph(ct, mdnm, pth, 'ccmodcg_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_CG(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodcg_solve.svg', **kwargs): b.solve() ## ConvCnstrMOD_Consensus class opt = ccmod.ConvCnstrMOD_Consensus.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodcnsns_init.svg', **kwargs): b = ccmod.ConvCnstrMOD_Consensus(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodcnsns_solve.svg', **kwargs): b.solve() #### ccmodmd module from sporco.admm import ccmodmd mdnm = 'sporco.admm.ccmodmd' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) W = np.array([1.0]) dsz = (4, 4, 1) ## ConvCnstrMODMaskDcpl_IterSM class opt = ccmodmd.ConvCnstrMODMaskDcpl_IterSM.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodmdism_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_IterSM(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdism_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMaskDcpl_CG class opt = ccmodmd.ConvCnstrMODMaskDcpl_CG.Options({ 'Verbose': False, 'MaxMainIter': 1, 'CG': { 'MaxIter': 1 } }) with CallGraph(ct, mdnm, pth, 'ccmodmdcg_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_CG(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdcg_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMaskDcpl_Consensus class opt = ccmodmd.ConvCnstrMODMaskDcpl_Consensus.Options({ 'Verbose': False, 'MaxMainIter': 1 }) with CallGraph(ct, mdnm, pth, 'ccmodmdcnsns_init.svg', **kwargs): b = ccmodmd.ConvCnstrMODMaskDcpl_Consensus(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdcnsns_solve.svg', **kwargs): b.solve() #### bpdndl module from sporco.admm import bpdndl mdnm = 'sporco.admm.bpdndl' D0 = np.random.randn(8, 8) S = np.random.randn(8, 16) lmbda = 0.1 ## BPDNDictLearn class opt = bpdndl.BPDNDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'bpdndl_init.svg', **kwargs): b = bpdndl.BPDNDictLearn(D0, S, lmbda, opt) with CallGraph(ct, mdnm, pth, 'bpdndl_solve.svg', **kwargs): b.solve() #### cbpdndl module from sporco.admm import cbpdndl mdnm = 'sporco.admm.cbpdndl' D0 = np.random.randn(4, 4, 16) s = np.random.randn(8, 8, 10) lmbda = 0.1 ## ConvBPDNDictLearn class opt = cbpdndl.ConvBPDNDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'cbpdndl_init.svg', **kwargs): b = cbpdndl.ConvBPDNDictLearn(D0, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'cbpdndl_solve.svg', **kwargs): b.solve() ## ConvBPDNMaskDcplDictLearn class W = np.array([1.0]) opt = cbpdndl.ConvBPDNMaskDcplDictLearn.Options({ 'Verbose': False, 'MaxMainIter': 1, 'AccurateDFid': True }) with CallGraph(ct, mdnm, pth, 'cbpdnmddl_init.svg', **kwargs): b = cbpdndl.ConvBPDNMaskDcplDictLearn(D0, s, lmbda, W, opt) with CallGraph(ct, mdnm, pth, 'cbpdnmddl_solve.svg', **kwargs): b.solve() #### tvl1 module from sporco.admm import tvl1 mdnm = 'sporco.admm.tvl1' s = np.random.randn(16, 16) lmbda = 0.1 ## TVL1Denoise class opt = tvl1.TVL1Denoise.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'tvl1den_init.svg', **kwargs): b = tvl1.TVL1Denoise(s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl1den_solve.svg', **kwargs): b.solve() ## TVL1Deconv class opt = tvl1.TVL1Deconv.Options({'Verbose': False, 'MaxMainIter': 1}) h = np.random.randn(3, 3) with CallGraph(ct, mdnm, pth, 'tvl1dcn_init.svg', **kwargs): b = tvl1.TVL1Deconv(h, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl1dcn_solve.svg', **kwargs): b.solve() #### tvl2 module from sporco.admm import tvl2 mdnm = 'sporco.admm.tvl2' s = np.random.randn(16, 16) lmbda = 0.1 ## TVL2Denoise class opt = tvl2.TVL2Denoise.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'tvl2den_init.svg', **kwargs): b = tvl2.TVL2Denoise(s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl2den_solve.svg', **kwargs): b.solve() ## TVL2Deconv class opt = tvl2.TVL2Deconv.Options({'Verbose': False, 'MaxMainIter': 1}) h = np.random.randn(3, 3) with CallGraph(ct, mdnm, pth, 'tvl2dcn_init.svg', **kwargs): b = tvl2.TVL2Deconv(h, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'tvl2dcn_solve.svg', **kwargs): b.solve() srcmodflt = '^sporco.fista' fnmsub = ('^sporco.fista.', '') lnksub = (r'^([^\.]*).(.*)', r'../../sporco.fista.\1.html#sporco.fista.\1.\2') ct = jonga.CallTracer(srcmodflt=srcmodflt, srcqnmflt=srcqnmflt, dstqnmflt=dstqnmflt, fnmsub=fnmsub, grpflt=grpflt, lnksub=lnksub) #### fista.cbpdn module from sporco.fista import cbpdn mdnm = 'sporco.fista.cbpdn' D = np.random.randn(4, 4, 16) s = np.random.randn(8, 8) lmbda = 0.1 ## ConvBPDN class opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'fista_cbpdn_init.svg', **kwargs): b = cbpdn.ConvBPDN(D, s, lmbda, opt) with CallGraph(ct, mdnm, pth, 'fista_cbpdn_solve.svg', **kwargs): b.solve() #### fista.ccmod module from sporco.fista import ccmod mdnm = 'sporco.fista.ccmod' X = np.random.randn(8, 8, 1, 2, 1) S = np.random.randn(8, 8, 2) dsz = (4, 4, 1) ## ConvCnstrMOD class opt = ccmod.ConvCnstrMOD.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'ccmodfista_init.svg', **kwargs): b = ccmod.ConvCnstrMOD(X, S, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodfista_solve.svg', **kwargs): b.solve() ## ConvCnstrMODMaskDcpl class opt = ccmod.ConvCnstrMODMask.Options({'Verbose': False, 'MaxMainIter': 1}) with CallGraph(ct, mdnm, pth, 'ccmodmdfista_init.svg', **kwargs): b = ccmod.ConvCnstrMODMask(X, S, W, dsz=dsz, opt=opt) with CallGraph(ct, mdnm, pth, 'ccmodmdfista_solve.svg', **kwargs): b.solve()
def train(self): noisyPatches, gtPatches = mutils.get_training_patches( self.data_dir, self.dataset, self.patch_size, self.overlap_size) # subtract the mean from bothdata SL = np.reshape(noisyPatches, [noisyPatches.shape[0], self.patch_size**2]) mSL = np.expand_dims(np.mean(SL, axis=1), axis=1) rmSL = np.tile(mSL, [1, SL.shape[1]]) SL = SL - rmSL SH = np.reshape(gtPatches, [gtPatches.shape[0], self.patch_size**2]) mSH = np.expand_dims(np.mean(SH, axis=1), axis=1) rmSH = np.tile(mSH, [1, SH.shape[1]]) SH = SH - rmSH # make sure patch is stored in column vector SL = np.transpose(SL, [1, 0]) SH = np.transpose(SH, [1, 0]) hDim = SH.shape[0] lDim = SL.shape[0] # normalize signals SL = cmod.normalise(SL) SH = cmod.normalise(SH) # Joint training S = np.concatenate((SL, SH), axis=0) # S = cmod.normalise(S) # initialize dictionary np.random.seed(1133221) DL = np.random.randn(SL.shape[0], self.dict_size) DH = np.random.randn(SH.shape[0], self.dict_size) # join learning dict D0 = np.concatenate((DL, DH), axis=0) # X and D update options lmbda = 0.1 optx = bpdn.BPDN.Options({ 'Verbose': False, 'MaxMainIter': 1, 'rho': 50.0 * lmbda + 0.5 }) optd = cmod.CnstrMOD.Options({ 'Verbose': False, 'MaxMainIter': 1, 'rho': S.shape[1] / 200.0 }) # update D update options optd.update({'Y0': D0, 'U0': np.zeros((S.shape[0], D0.shape[1]))}) # create X update object xstep = bpdn.BPDN(D0, S, lmbda, optx) # create D update object dstep = cmod.CnstrMOD(None, S, (D0.shape[1], S.shape[1]), optd) # create dict learn object opt = dictlrn.DictLearn.Options({'Verbose': True, 'MaxMainIter': 2}) d = dictlrn.DictLearn(xstep=xstep, dstep=dstep, opt=opt) Dmx = d.solve() print("DictLearn solv time: %.2fs" % d.timer.elapsed('solve')) # get dictionary DL1 = Dmx[0:self.patch_size**2, :] DL1 = DL1.reshape(self.patch_size, self.patch_size, DL1.shape[1]) DH1 = Dmx[self.patch_size**2:, :] DH1 = DH1.reshape(self.patch_size, self.patch_size, DH1.shape[1]) itsx = xstep.getitstat() itsd = dstep.getitstat() # write logs mutils.write_logs(itsx, itsd, d.timer.elapsed('solve'), self.output_dir, 'logs_%d_5' % self.dict_size) # write dictionary mutils.write_dicts(DL1, DH1, self.output_dir, 'dicts_%d_5' % self.dict_size)
""" Set solver options. """ lmbda = 1e-1 opt = bpdn.BPDN.Options({'Verbose': True, 'MaxMainIter': 250, 'RelStopTol': 3e-3, 'AuxVarObj': False, 'AutoRho': {'Enabled': False}, 'rho': 1e1*lmbda}) """ Initialise the :class:`.admm.bpdn.BPDN` object and call the ``solve`` method. """ b = bpdn.BPDN(D, blocks, lmbda, opt) X = b.solve() """ The denoised estimate of the image is by aggregating the block reconstructions from the coefficient maps. """ imgd_mean = util.average_blocks(np.dot(D, X).reshape(blksz + (-1,)) + blockmeans, img.shape, stpsz) imgd_median = util.combine_blocks(np.dot(D, X).reshape(blksz + (-1,)) + blockmeans, img.shape, stpsz, np.median) """ Display solve time and denoising performance.
Load initial dictionary. """ D0 = util.convdicts()['G:8x8x64'] D0 = np.reshape(D0, (np.prod(D0.shape[0:2]), D0.shape[2])) """ Compute sparse representation on current dictionary. """ lmbda = 0.1 opt = bpdn.BPDN.Options({ 'Verbose': True, 'MaxMainIter': 200, 'RelStopTol': 1e-3 }) b = bpdn.BPDN(D0, S, lmbda, opt) X = b.solve() """ Update dictionary for training image set. """ opt = cmod.CnstrMOD.Options({ 'Verbose': True, 'MaxMainIter': 100, 'RelStopTol': 1e-3, 'rho': 4e2 }) c = cmod.CnstrMOD(X, S, None, opt) D1 = c.solve() print("CMOD solve time: %.2fs" % c.timer.elapsed('solve')) """
return np.sum(np.abs(x - x0)) # Parallel evalution of error function on lmbda grid lrng = np.logspace(1, 2, 20) sprm, sfvl, fvmx, sidx = util.grid_search(evalerr, (lrng, )) lmbda = sprm[0] print('Minimum ℓ1 error: %5.2f at 𝜆 = %.2e' % (sfvl, lmbda)) """ Once the best $\lambda$ has been determined, run BPDN with verbose display of ADMM iteration statistics. """ # Initialise and run BPDN object for best lmbda opt['Verbose'] = True b = bpdn.BPDN(D, s, lmbda, opt) x = b.solve() print("BPDN solve time: %.2fs" % b.timer.elapsed('solve')) """ Plot comparison of reference and recovered representations. """ plot.plot(np.hstack((x0, x)), title='Sparse representation', lgnd=['Reference', 'Reconstructed']) """ Plot lmbda error curve, functional value, residuals, and rho """ its = b.getitstat()
def evalerr(prm): lmbda = prm[0] b = bpdn.BPDN(D, s, lmbda, opt) x = b.solve() return np.sum(np.abs(x - x0))