def background_substraction(X, algorithm, use_gpu=True, **kwargs): fg, bg = None, None if algorithm == 'RTPCA': X = torch.from_numpy(X) if torch.cuda.is_available() and use_gpu: X = X.cuda() bgs = RTPCA(reg_E=0.001, n_iter_max=100, backend='pytorch') # pytorch bg, fg = bgs(X) bg, fg = bg.cpu().numpy(), fg.cpu().numpy() elif algorithm == 'RPCA_sporco': from sporco import cupy if cupy.have_cupy and use_gpu: opt = RPCA_sporco_gpu.Options(dict(Verbose=True, MaxMainIter=20)) X = cupy.np2cp(X) bgs = RPCA_sporco_gpu(X, opt=opt) bg, fg = bgs.solve() bg = cupy.cp2np(bg) fg = cupy.cp2np(fg) else: opt = RPCA_sporco.Options(dict(Verbose=True, MaxMainIter=100)) bgs = RPCA_sporco(X, opt=opt) bg, fg = bgs.solve() elif algorithm == 'GoDec': bgs = GoDec(X, rank=2, max_iter=2, **kwargs) bg, fg = bgs() # bg, fg = godec_original(X, r=2, k=int(np.prod(X.shape)/50), q=0, max_iter=20) else: NotImplementedError return fg, bg
def csc(imgs, D, args, lmbda=None, opt=None): if lmbda is None: lmbda = args.lmbda if opt is None: opt = sporco.admm.cbpdn.ConvBPDN.Options({ 'Verbose': False, 'MaxMainIter': args.nInnerIter, 'HighMemSolve': True, 'RelStopTol': 5e-3, 'AuxVarObj': False }) s = np.transpose(imgs[..., 0], (1, 2, 0)) sl, sh = util.tikhonov_filter(s, args.lmbdaPre) ys = [] coefs = [] for i in range(sh.shape[-1]): coef = sporco.cuda.cbpdn(D, sh[..., i], lmbda, opt, dev=args.device) y = np.sum(cp2np(sporco.cupy.linalg.fftconv(np2cp(D), np2cp(coef))), -1) + sl[..., i] coefs.append(coef) ys.append(y[np.newaxis, ..., np.newaxis]) return np.concatenate(ys, 0), np.array(coefs)
stimulus_ra = np.asarray(stimulus_ra_new) d = online_cdl.OnlineConvBPDNDictLearn(np2cp(np.load('Data_81sets/v_ra')), lmbda, opt, dimK=0, dimN=1) d.init_vars(data_ra[0], dimK=0) d.display_start() for it in range(len(data_ra)): d.solve(np2cp(data_ra[it]), dimK=0) d.solve(np2cp(stimulus_ra[it]), dimK=0) d.display_end() D1 = cp2np(d.getdict()) print("OnlineConvBPDNDictLearn solve time: %.2fs" % d.timer.elapsed('solve')) pickle.dump(D1, open('OUTPUT_CUDA_RA', 'wb')) ''' # ========================================================================================================== # THIS SECTION IS TO BE USED TO CONCATENATE MULTIPLE INPUTS ACROSS MULTIPLE DATA FILES---------------------- data_ra, data_sa, data_pc = data1.neuralresponse(indices1) x,y,z=data_ra.shape,data_sa.shape,data_pc.shape stimulus_ra, stimulus_sa, stimulus_pc = data1.stimulus(indices1) a, b, c = stimulus_ra.shape, stimulus_pc.shape, stimulus_sa.shape data_ra1, data_sa1, data_pc1 = data2.neuralresponse(indices2)
if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info: print('Running on GPU %d (%s)\n' % (id, info[id].name)) b = pdcsc.ConvProdDictL1L1GrdJoint(np2cp(D), np2cp(B), np2cp(pad(imgn)), lmbda, mu, opt=opt, dimK=0) X = cp2np(b.solve()) """ The denoised estimate of the image is just the reconstruction from all coefficient maps. """ imgdp = cp2np(b.reconstruct().squeeze()) imgd = crop(imgdp) """ Display solve time and denoising performance. """ print("ConvProdDictL1L1GrdJoint solve time: %5.2f s" % b.timer.elapsed('solve')) print("Noisy image PSNR: %5.2f dB" % psnr(img, imgn)) print("Denoised image PSNR: %5.2f dB" % psnr(img, imgd)) """
W = fft.irfftn( np.conj(fft.rfftn(D[..., np.newaxis, :], imgnph.shape[0:2], (0, 1))) * fft.rfftn(imgnph[..., np.newaxis], None, (0, 1)), imgnph.shape[0:2], (0, 1)) W = 1.0 / (np.maximum(np.abs(W), 1e-8)) lmbda = 1.5e-2 mu = 2.7e-1 opt = cbpdn.ConvBPDNJoint.Options({ 'Verbose': True, 'MaxMainIter': 250, 'HighMemSolve': True, 'RelStopTol': 3e-3, 'AuxVarObj': False, 'L1Weight': cp2np(W), 'AutoRho': { 'Enabled': False }, 'rho': 1e3 * lmbda }) """ Initialise a ``sporco.cupy`` version of a :class:`.admm.cbpdn.ConvBPDNJoint` object and call the ``solve`` method. """ if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info:
""" Initialise the :class:`.admm.pdcsc.ConvProdDictL1L1Grd` object and call the ``solve`` method. """ if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info: print('Running on GPU %d (%s)\n' % (id, info[id].name)) b = pdcsc.ConvProdDictL1L1Grd(np2cp(D), np2cp(B), np2cp(pad(imgn)), lmbda, mu, opt=opt, dimK=0) X = cp2np(b.solve()) """ The denoised estimate of the image is just the reconstruction from all coefficient maps. """ imgdp = cp2np(b.reconstruct().squeeze()) imgd = crop(imgdp) """ Display solve time and denoising performance. """ print("ConvProdDictL1L1Grd solve time: %5.2f s" % b.timer.elapsed('solve'))
""" Create solver object and solve, returning the the denoised image ``imgr``. """ if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info: print('Running on GPU %d (%s)\n' % (id, info[id].name)) b = tvl1.TVL1Denoise(np2cp(imgn), lmbda, opt) imgr = cp2np(b.solve()) """ Display solve time and denoising performance. """ print("TVL1Denoise solve time: %5.2f s" % b.timer.elapsed('solve')) print("Noisy image PSNR: %5.2f dB" % metric.psnr(img, imgn)) print("Denoised image PSNR: %5.2f dB" % metric.psnr(img, imgr)) """ Display reference, corrupted, and denoised images. """
""" Initialise and run CSC solver. """ if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info: print('Running on GPU %d (%s)\n' % (id, info[id].name)) b = pdcsc.ConvProdDictBPDN(np2cp(D), np2cp(B), np2cp(shc), lmbda, opt, dimK=0) X = cp2np(b.solve()) print("ConvProdDictBPDN solve time: %.2fs" % b.timer.elapsed('solve')) """ Compute partial and full reconstructions from sparse representation $X$ with respect to convolutional dictionary $D$ and standard dictionary $B$. The partial reconstructions are $DX$ and $XB$, and the full reconstruction is $DXB$. """ DX = fft.fftconv(D[..., np.newaxis, np.newaxis, :], X, axes=(0, 1)) XB = linalg.dot(B, X, axis=2) shr = cp2np(b.reconstruct().squeeze()) imgr = slc + shr print("Reconstruction PSNR: %.2fdB\n" % metric.psnr(img, imgr)) """
""" Set solver options. See Section 8 of :cite:`wohlberg-2017-convolutional2` for details of construction of $\ell_1$ weighting matrix $W$. """ imgnpl, imgnph = spl.tikhonov_filter(pad(imgn), fltlmbd, npd) W = spl.irfftn(np.conj(spl.rfftn(D[..., np.newaxis, :], imgnph.shape[0:2], (0, 1))) * spl.rfftn(imgnph[..., np.newaxis], None, (0, 1)), imgnph.shape[0:2], (0, 1)) W = 1.0/(np.maximum(np.abs(W), 1e-8)) lmbda = 1.5e-2 mu = 2.7e-1 opt = cbpdn.ConvBPDNJoint.Options({'Verbose': True, 'MaxMainIter': 250, 'HighMemSolve': True, 'RelStopTol': 3e-3, 'AuxVarObj': False, 'L1Weight': cp2np(W), 'AutoRho': {'Enabled': False}, 'rho': 1e3*lmbda}) """ Initialise a ``sporco.cupy`` version of a :class:`.admm.cbpdn.ConvBPDNJoint` object and call the ``solve`` method. """ if not cupy_enabled(): print('CuPy/GPU device not available: running without GPU acceleration\n') else: id = select_device_by_load() info = gpu_info() if info: print('Running on GPU %d (%s)\n' % (id, info[id].name))
""" Wg = np.eye(24) Wg = np.repeat(Wg, M, axis=1) if CUPY: D = np2cp(D) Wg = np2cp(Wg) audio = np2cp(audio) b = cbpdnin.ConvBPDNInhib(D.T, audio, Wg, int( 0.20*16000), None, lmbda, mu, gamma, opt, dimK=None, dimN=1) X = b.solve() if CUPY: X = cp2np(X) print("ConvBPDN solve time: %.2fs" % b.timer.elapsed('solve')) """ Reconstruct image from sparse representation. """ recon = b.reconstruct().squeeze() if CUPY: audio = cp2np(audio) recon = cp2np(recon) print("Reconstruction PSNR: %.2fdB\n" % sm.psnr(audio, recon))
estimator = admm.bpdn.BPDN(v_ra.T, s.T, 0.6) u_ra = estimator.solve() # print(u_ra.shape,"U shape") x = np2cp(u_ra) u.append(x) time_stop = timeit.timeit() print(time_stop - time_start, "Done creating Us") time_start = timeit.timeit() for p in u: A0 += p @ p.T B0 += u_ra @ s # FEATURES X NEURONS for j in range(v_ra.shape[1]): if A0[j][j] == 0: print(A0[j][j] == 0, "!") exit() vv = (1 / A0[j][j]) * (B0[:, j] - (v_ra.T @ A0[:, j]) + v_ra[:, j] * A0[j][j]) v_ra[:, j] = vv / cp.linalg.norm(vv) time_stop = timeit.timeit() print(time_stop - time_start, lll, "-th iteration done") print(cp.linalg.norm(v_ra.T @ u[0] - data_ra[0].T)) pickle.dump(cp2np(v_ra), open('V_RA', 'wb')) k = v_ra.T @ u[0] k[k > 0.4] = 1 k[k <= 0.4] = 0 plt.plot(k) plt.show()