def test_10cplx(self): N = 64 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) + 1j * np.random.randn(Nd, Nd, M) X0 = np.zeros((N, N, M)) + 1j * 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) + 1j * np.random.randn(X0[xp].size)) S = np.sum(fftconv(D, X0, axes=(0, 1)), 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 rrs(X0, X1) < 5e-5 Sr = b.reconstruct().squeeze() assert rrs(S, Sr) < 1e-4
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 xstep(self, S, lmbda, dimK): """Solve CSC problem for training data `S`.""" if self.opt['CUDA_CBPDN']: Z = np.stack([ cucbpdn.cbpdn(self.D.squeeze(), S[..., i], lmbda, self.opt['CBPDN']) for i in range(S.shape[-1]) ], axis=-2) Z = Z.reshape(self.cri.Nv + (1, self.cri.K, self.cri.M,)) self.Z[:] = np.asarray(Z, dtype=self.dtype) self.Zf = sl.rfftn(self.Z, self.cri.Nv, self.cri.axisN) self.Sf = sl.rfftn(S.reshape(self.cri.shpS), self.cri.Nv, self.cri.axisN) self.xstep_itstat = None elif self.opt['PAR_CBPDN']: popt = parcbpdn.ParConvBPDN.Options(dict(self.opt['CBPDN'])) xstep = parcbpdn.ParConvBPDN(self.D.squeeze(), S, lmbda, opt=popt, dimK=dimK, dimN=self.cri.dimN) xstep.solve() self.Sf = xstep.Sf self.setcoef(xstep.getcoef()) self.xstep_itstat = xstep.itstat[-1] if len(xstep.itstat) > 0 \ else None else: # Create X update object (external representation is expected!) xstep = cbpdn.ConvBPDN(self.D.squeeze(), S, lmbda, self.opt['CBPDN'], dimK=dimK, dimN=self.cri.dimN) xstep.solve() self.Sf = xstep.Sf self.setcoef(xstep.getcoef()) self.xstep_itstat = xstep.itstat[-1] if len(xstep.itstat) > 0 \ else None
def xstep(self, S, lmbda, dimK): """Solve CSC problem for training data `S`.""" if self.opt['CUDA_CBPDN']: Z = cuda.cbpdn(self.D.squeeze(), S[..., 0], lmbda, self.opt['CBPDN']) Z = Z.reshape(self.cri.Nv + ( 1, 1, self.cri.M, )) self.Z[:] = np.asarray(Z, dtype=self.dtype) self.Zf = sl.rfftn(self.Z, self.cri.Nv, self.cri.axisN) self.Sf = sl.rfftn(S.reshape(self.cri.shpS), self.cri.Nv, self.cri.axisN) self.xstep_itstat = None else: # Create X update object (external representation is expected!) xstep = cbpdn.ConvBPDN(self.D.squeeze(), S, lmbda, self.opt['CBPDN'], dimK=dimK, dimN=self.cri.dimN) xstep.solve() self.Sf = xstep.Sf self.setcoef(xstep.getcoef()) self.xstep_itstat = xstep.itstat[-1] if xstep.itstat else None
def test_22(self): N = 32 M = 4 Nd = 8 D = np.random.randn(Nd, Nd, M) D /= np.sqrt(np.sum(D**2, axis=(0, 1))) 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.fftconv(D, X0), axis=2) lmbda = 1e-3 opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-5, 'rho': 5e-1, 'AutoRho': {'Enabled': False}}) bp = cbpdn.ConvBPDN(D, S, lmbda, opt) Xp = bp.solve() epsilon = np.linalg.norm(bp.reconstruct(Xp).squeeze() - S) opt = cbpdn.ConvMinL1InL2Ball.Options({'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-5, 'rho': 2e2, 'RelaxParam': 1.0, 'AutoRho': {'Enabled': False}}) bc = cbpdn.ConvMinL1InL2Ball(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.ravel(), 1) - np.linalg.norm(Xc.ravel(), 1)) < 1e-3)
def learn_codes(signal, atom_dictionary, penalty=PENALTY): """Return the sparse codes""" opt_sc = get_opt_sc() return ( cbpdn.ConvBPDN( D=atom_dictionary, # learned dictionary S=atleast_2d(signal), # signal at hand lmbda=penalty, # sparsity penalty opt=opt_sc, # options for the optimizations ).solve().squeeze())
def xinit(self, S): init = cbpdn.ConvBPDN(self.getdict().squeeze(), S, self.lmbda, self.opt['CBPDN']) init.solve() # (H, W, 1, K, M) X = init.getcoef() assert X.shape[2] == 1 X = X.reshape(-1, X.shape[-2], X.shape[-1]) X = X.transpose(1, 2, 0) return X
def test_01(self): N = 16 Nd = 5 Cs = 3 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N, Cs) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda, dimK=0) assert b.cri.dimC == 1 assert b.cri.dimK == 0
def test_03(self): N = 16 Nd = 5 Cd = 3 M = 4 D = np.random.randn(Nd, Nd, Cd, M) s = np.random.randn(N, N, Cd) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda) assert b.cri.dimC == 1 assert b.cri.dimK == 0
def test_05(self): N = 16 Nd = 5 K = 2 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N, K) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda) assert b.cri.dimC == 0 assert b.cri.dimK == 1
def test_09(self): N = 16 Nd = 5 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N) try: b = cbpdn.ConvBPDN(D, s) b.solve() except Exception as e: print(e) assert 0
def test_04(self): N = 16 Nd = 5 Cd = 3 K = 5 M = 4 D = np.random.randn(Nd, Nd, Cd, M) s = np.random.randn(N, N, Cd, K) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda) assert(b.cri.dimC == 1) assert(b.cri.dimK == 1)
def test_02(self): N = 16 Nd = 5 Cs = 3 K = 5 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N, Cs, K) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda) assert b.cri.dimC == 1 assert b.cri.dimK == 1
def test_08(self): N = 16 Nd = 5 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N) lmbda = 1e-1 try: b = cbpdn.ConvBPDN(D, s, lmbda) b.solve() except Exception as e: print(e) assert (0)
def test_01(self): Nr = 32 Nc = 31 Nd = 5 M = 4 D = np.random.randn(Nd, Nd, M).astype(np.float32) s = np.random.randn(Nr, Nc).astype(np.float32) lmbda = 1e-1 opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 50, 'AutoRho': {'Enabled': False}}) b = cbpdn.ConvBPDN(D, s, lmbda, opt) X1 = b.solve() X2 = cucbpdn.cbpdn(D, s, lmbda, opt) assert(sm.mse(X1, X2) < 1e-10)
def test_12(self): N = 16 Nd = 5 Cs = 3 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N, Cs) lmbda = 1e-1 try: b = cbpdn.ConvBPDN(D, s, lmbda, dimK=0) b.solve() except Exception as e: print(e) assert (0)
def test_34(self): N = 16 Nd = 5 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N) lmbda = 1e-1 opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 10}) b = cbpdn.ConvBPDN(D, s, lmbda, opt) bp = pickle.dumps(b) c = pickle.loads(bp) Xb = b.solve() Xc = c.solve() assert np.linalg.norm(Xb - Xc) == 0.0
def calcXr(cri, Dr, Sr, lmbda=5e-2): opt = cbpdn.ConvBPDN.Options({ 'Verbose': True, 'MaxMainIter': 200, 'RelStopTol': 5e-3, 'AuxVarObj': False }) b = cbpdn.ConvBPDN(Dr.squeeze(), Sr.squeeze(), lmbda, opt, dimK=cri.dimK, dimN=cri.dimN) Xr = b.solve() return Xr
def test_08(self): N = 16 Nd = 5 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N) lmbda = 1e-1 try: opt = cbpdn.ConvBPDN.Options({'LinSolveCheck' : True}) b = cbpdn.ConvBPDN(D, s, lmbda, opt=opt) b.solve() except Exception as e: print(e) assert(0) assert(np.array(b.getitstat().XSlvRelRes).max() < 1e-5)
def findHeatmaps(self, img): #Opzioni e soluzione dell'algoritmo di CSC lmbda = 5e-2 opt = cbpdn.ConvBPDN.Options({ 'Verbose': True, 'MaxMainIter': 100, 'RelStopTol': 5e-3, 'AuxVarObj': False }) csc = cbpdn.ConvBPDN(self.D, img, lmbda, opt, dimK=0) X = csc.solve() print(X.shape) print("ConvBPDN solve time: %.2fs" % csc.timer.elapsed('solve')) #rec = csc.reconstruct().squeeze() return X
def run_cbpdn(D, sl, sh, lmbda, test_blob=None, opt=None): """Run ConvBPDN solver and compute statistics.""" opt = cbpdn.ConvBPDN.Options(opt) if test_blob is None: test_blob = sl + sh solver = cbpdn.ConvBPDN(D, sh, lmbda, opt=opt) solver.solve() fnc = solver.getitstat().ObjFun[-1] shr = solver.reconstruct().reshape(sl.shape) imgr = sl + shr psnr = 0. for idx in range(sh.shape[-1]): psnr += sm.psnr(test_blob[..., idx], imgr[..., idx], rng=1.) psnr /= test_blob.shape[-1] if _cfg.VERBOSE: print('.', end='', flush=True) return fnc, psnr
def test_06(self): N = 16 Nd = 5 K = 2 M = 4 D = np.random.randn(Nd, Nd, M) s = np.random.randn(N, N, K) dt = np.float32 opt = cbpdn.ConvBPDN.Options({'Verbose' : False, 'MaxMainIter' : 20, 'AutoRho' : {'Enabled' : True}, 'DataType' : dt}) lmbda = 1e-1 b = cbpdn.ConvBPDN(D, s, lmbda, opt=opt) b.solve() assert(b.X.dtype == dt) assert(b.Y.dtype == dt) assert(b.U.dtype == dt)
def recons(name): if not os.path.exists(os.path.join(out_path, name)): impath = os.path.join(im_path, name) im = get_im(impath) sl, sh = util.tikhonov_filter(im, fltlmbd, npd) opt = cbpdn.ConvBPDN.Options({ 'Verbose': False, 'MaxMainIter': 200, 'RelStopTol': 5e-3, 'AuxVarObj': False }) b = cbpdn.ConvBPDN(basis, sh, lmbda, opt) X = b.solve() print("ConvBPDN solve time: %.2fs" % b.timer.elapsed('solve')) shr = b.reconstruct().squeeze() imgr = sl + shr cv2.imwrite(os.path.join(out_path, name), 255 * imgr / imgr.max()) else: # print(name) pass
}, 'rho': 4e2 * lmbda }) opt3 = cbpdntv.ConvBPDNVectorTV.Options({ 'Verbose': True, 'MaxMainIter': 250, 'HighMemSolve': True, 'RelStopTol': 3e-3, 'AuxVarObj': True, 'L1Weight': W, 'AutoRho': { 'Enabled': False }, 'rho': 4e2 * lmbda }) a = cbpdn.ConvBPDN(D, pad(imgnh), lmbda, opt1, dimK=0) b = cbpdntv.ConvBPDNScalarTV(D, pad(imgnh), lmbda, mu, opt2) c = cbpdntv.ConvBPDNVectorTV(D, pad(imgnh), lmbda, mu, opt3) X1 = a.solve() X2 = b.solve() X3 = c.solve() imgdp1 = a.reconstruct().squeeze() imgd1 = np.clip(crop(imgdp1) + imgnl, 0, 1) imgdp2 = b.reconstruct().squeeze() imgd2 = np.clip(crop(imgdp2) + imgnl, 0, 1) imgdp3 = c.reconstruct().squeeze() imgd3 = np.clip(crop(imgdp3) + imgnl, 0, 1) print("ConvBPDN solve time: %5.2f s" % b.timer.elapsed('solve')) print("Noisy image PSNR: %5.2f dB" % sm.psnr(img, imgn))
S7 = sh7.reshape(N * N) # S6 = tesS[0].reshape(N*N) # S7 = tesS[1].reshape(N*N) TESTS = [S6, S7] # %% # d_0 = np.random.normal(-1, 1, (M, d_size, d_size)) from sporco.admm import cbpdn lmbda = 0.048 opt = cbpdn.ConvBPDN.Options({ 'Verbose': True, 'MaxMainIter': 500, 'RelStopTol': 5e-3, 'AuxVarObj': False }) b0 = cbpdn.ConvBPDN(D1, sh6, lmbda, opt, dimK=0) X6 = b0.solve() b1 = cbpdn.ConvBPDN(D1, sh7, lmbda, opt, dimK=0) X7 = b1.solve() shr6 = b0.reconstruct().squeeze() imgr6 = sl6 + shr6 print("Reconstruction PSNR: %.2fdB\n" % sm.psnr(tesS[0], imgr6)) shr7 = b1.reconstruct().squeeze() imgr7 = sl7 + shr7 print("Reconstruction PSNR: %.2fdB\n" % sm.psnr(tesS[1], imgr7)) # %% fig = plot.figure(figsize=(14, 7)) plot.subplot(1, 2, 1) plot.imview(np.sum(abs(X6), axis=b0.cri.axisM).squeeze(), cmap=plot.cm.Blues, title='Sparse representation',
'RelStopTol': 5e-3, 'AuxVarObj': False }) """ If GPU available, run CUDA ConvBPDN solver, otherwise run standard Python version. """ if cuda.device_count() > 0: print('%s GPU found: running CUDA solver' % cuda.device_name()) tm = util.Timer() with sys_pipes(), util.ContextTimer(tm): X = cuda.cbpdn(D, sh, lmbda, opt) t = tm.elapsed() else: print('GPU not found: running Python solver') c = cbpdn.ConvBPDN(D, sh, lmbda, opt) X = c.solve().squeeze() t = c.timer.elapsed('solve') print('Solve time: %.2f s' % t) """ Reconstruct the image from the sparse representation. """ shr = np.sum(spl.fftconv(D, X), axis=2) imgr = sl + shr print("Reconstruction PSNR: %.2fdB\n" % spm.psnr(img, imgr)) """ Display representation and reconstructed image. """ fig = plot.figure(figsize=(14, 14))
""" Set :class:`.admm.cbpdn.ConvBPDN` solver options. """ lmbda = 5e-2 opt = cbpdn.ConvBPDN.Options({ 'Verbose': True, 'MaxMainIter': 200, 'RelStopTol': 5e-3, 'AuxVarObj': False }) """ Initialise and run CSC solver. """ b = cbpdn.ConvBPDN(D, sh, lmbda, opt, dimK=0) X = b.solve() print("ConvBPDN solve time: %.2fs" % b.timer.elapsed('solve')) """ Reconstruct image from sparse representation. """ shr = b.reconstruct().squeeze() imgr = sl + shr print("Reconstruction PSNR: %.2fdB\n" % sm.psnr(img, imgr)) """ Display low pass component and sum of absolute values of coefficient maps of highpass component. """ fig = plot.figure(figsize=(14, 7)) plot.subplot(1, 2, 1)
'Scaling': 2.0, 'RsdlTarget': 1.0 } }) # Normalise dictionary according to Y update options D0n = ccmod.getPcn0(optd['ZeroMean'], D0.shape, dimN=2, dimC=1)(D0) # Update D update options to include initial values for Y and U optd.update({ 'Y0': ccmod.zpad(ccmod.stdformD(D0n, cri.C, cri.M), cri.Nv), 'U0': np.zeros(cri.shpD) }) # Create X update object xstep = cbpdn.ConvBPDN(D0n, sh, lmbda, optx) # Create D update object dstep = ccmod.ConvCnstrMOD(None, sh, D0.shape, optd) # Create DictLearn object opt = dictlrn.DictLearn.Options({'Verbose': True, 'MaxMainIter': 100}) d = dictlrn.DictLearn(xstep, dstep, opt) D1 = d.solve() print("DictLearn solve time: %.2fs" % d.runtime, "\n") # Display dictionaries D1 = D1.squeeze() fig1 = plot.figure(1, figsize=(14, 7)) plot.subplot(1, 2, 1) plot.imview(util.tiledict(D0), fgrf=fig1, title='D0')
#train_feature = nmf.transform(train_feature); nmf_p.fit(train_feature_p) train_feature_p = nmf_p.transform(train_feature_p) #%% # テスト用データセットの係数算出 print("### テスト用データセットの係数算出 ###") test_opt0 = cbpdn.ConvBPDN.Options({ 'Verbose': False, 'MaxMainIter': opt["Iter"], 'RelStopTol': 5e-3, 'AuxVarObj': False }) b0 = cbpdn.ConvBPDN(np.float32(D0), np.float32(test_data), opt["lmbda"], test_opt0) print("第1層目:フォワードパスの計算中...") test_coef0_ = b0.solve() test_coef0 = np.zeros_like(test_coef0_) for i in range(opt['test_amount']): for j in range(test_coef0_.shape[4]): test_coef0[:, :, 0, i, j] = power_spectrum(test_coef0_[:, :, 0, i, j]) #test_feature = test_coef0_.transpose(3, 0, 1, 2, 4).squeeze().reshape(opt["test_amount"], -1); #test_feature = nmf.transform(test_feature); test_feature_p = test_coef0.transpose(3, 0, 1, 2, 4).squeeze().reshape( opt["test_amount"], -1) test_feature_p = nmf_p.transform(test_feature_p) #%%
def solve(self, S): self.cri = cr.CSC_ConvRepIndexing(self.getdict(), S, dimK=self.cri.dimK, dimN=self.cri.dimN) self.timer.start(['solve', 'solve_wo_eval']) # Initialize with CBPDN self.timer.start('xstep') copt = copy.deepcopy(self.opt['CBPDN']) if self.opt['OCDL', 'CUCBPDN']: X = np.stack([ cucbpdn.cbpdn(self.getdict(), S[..., i].squeeze(), self.lmbda, opt=copt) for i in range(S.shape[-1]) ], axis=-2) X = np.asarray(X.reshape(self.cri.shpX), dtype=self.dtype) elif self.opt['OCDL', 'PARCBPDN']: popt = parcbpdn.ParConvBPDN.Options(dict(self.opt['CBPDN'])) xstep = parcbpdn.ParConvBPDN(self.getdict(), S, self.lmbda, opt=popt, nproc=self.opt['OCDL', 'nproc']) X = xstep.solve() X = np.asarray(X.reshape(self.cri.shpX), dtype=self.dtype) else: xstep = cbpdn.ConvBPDN(self.getdict(), S, self.lmbda, opt=copt) xstep.solve() X = np.asarray(xstep.getcoef().reshape(self.cri.shpX), dtype=self.dtype) self.timer.stop('xstep') # X = np.asarray(xstep.getcoef().reshape(self.cri.shpX), dtype=self.dtype) S = np.asarray(S.reshape(self.cri.shpS), dtype=self.dtype) # update At and Bt # (H, W, 1, K, M) -> (H, W, Hc, Wc, 1, K, M) self.timer.start('hessian') Xe = self.extend_code(X) self.update_At(Xe) self.update_Bt(Xe, S) self.timer.stop('hessian') self.Lmbda = self.dtype.type(self.alpha*self.Lmbda+1) # update dictionary with FISTA fopt = copy.deepcopy(self.opt['CCMOD']) fopt['X0'] = self.D if self.opt['OCDL', 'DiminishingTol']: if self.opt['OCDL', 'MinTol'] is None: min_tol = 0. else: min_tol = self.opt['OCDL', 'MinTol'] fopt['RelStopTol'] = max( self.dtype.type(self.opt['CCMOD', 'RelStopTol']/(1.+self.j)), min_tol ) self.timer.start('dstep') dstep = SpatialFISTA(self.At, self.Bt, opt=fopt) dstep.solve() self.timer.stop('dstep') # set dictionary self.setdict(dstep.getmin()) self.timer.stop('solve_wo_eval') evl = self.evaluate(S, X) self.timer.start('solve_wo_eval') t = self.timer.elapsed(self.opt['IterTimer']) if self.opt['OCDL', 'CUCBPDN']: # this requires a slight modification of dictlrn itst = self.isc.iterstats(self.j, t, None, dstep.itstat[-1], evl) else: itst = self.isc.iterstats(self.j, t, xstep.itstat[-1], dstep.itstat[-1], evl) self.itstat.append(itst) if self.opt['Verbose']: self.isc.printiterstats(itst) self.j += 1 self.timer.stop(['solve', 'solve_wo_eval']) if 0: import matplotlib.pyplot as plt plt.imshow(su.tiledict(self.getdict().squeeze())) plt.show() return self.getdict()