Exemplo n.º 1
0
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)
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
Exemplo n.º 3
0
        info = gpu_info()
        if info:
            print('Running on GPU %d (%s)\n' % (id, info[id].name))

    data_ra_new = []
    for i in range(1, int(data_ra.shape[0] / 1000)):
        data_ra_new.append(data_ra[(i - 1) * 1000:i * 1000, :])
    data_ra = np.asarray(data_ra_new)
    print(data_ra.shape, "New data_ra shape")

    stimulus_ra_new = []
    for i in range(1, int(stimulus_ra.shape[0] / 1000)):
        stimulus_ra_new.append(stimulus_ra[(i - 1) * 1000:i * 1000, :])
    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'))
Exemplo n.º 4
0
"""

wgr = np.zeros((D.shape[2]), dtype=np.float32)
wgr[0] = 1.0
"""
Set :class:`.admm.pdcsc.ConvProdDictL1L1GrdJoint` solver options.
"""

opt = pdcsc.ConvProdDictL1L1GrdJoint.Options({
    'Verbose': True,
    'MaxMainIter': 100,
    'RelStopTol': 5e-3,
    'AuxVarObj': False,
    'rho': 1e1,
    'RelaxParam': 1.8,
    'L21Weight': np2cp(wl1),
    'GradWeight': np2cp(wgr)
})
"""
Initialise the :class:`.admm.pdcsc.ConvProdDictL1L1GrdJoint` 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.ConvProdDictL1L1GrdJoint(np2cp(D),
Exemplo n.º 5
0
    },
    '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))

b = cbpdn.ConvBPDNJoint(np2cp(D), np2cp(pad(imgnh)), lmbda, mu, opt, dimK=0)
X = cp2np(b.solve())
"""
The denoised estimate of the image is just the reconstruction from the coefficient maps.
"""

imgdp = cp2np(b.reconstruct().squeeze())
imgd = np.clip(crop(imgdp) + imgnl, 0, 1)
"""
Display solve time and denoising performance.
"""

print("ConvBPDNJoint 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, imgd))
"""
Exemplo n.º 6
0
"""
Set of weights for the $\ell_2$ norm of the gradient to disable regularization of all coefficient maps except for the one corresponding to the impulse filter.
"""

wgr = np.zeros((D.shape[2]), dtype=np.float32)
wgr[0] = 1.0


"""
Set :class:`.admm.pdcsc.ConvProdDictL1L1Grd` solver options.
"""

opt = pdcsc.ConvProdDictL1L1Grd.Options(
    {'Verbose': True, 'MaxMainIter': 100, 'RelStopTol': 5e-3,
     'AuxVarObj': False, 'rho': 1e1, 'RelaxParam': 1.8,
     'L1Weight': np2cp(wl1), 'GradWeight': np2cp(wgr)})


"""
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)),
Exemplo n.º 7
0


"""
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.
"""
Exemplo n.º 8
0
                                      'RelStopTol': 5e-3, 'AuxVarObj': False})


"""
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))

Exemplo n.º 9
0
        'DataType': np.float32
    }
})
"""
Create solver object and solve.
"""

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))

d = onlinecdl.OnlineConvBPDNDictLearn(np2cp(D0), lmbda, opt)

iter = 50
d.display_start()
for it in range(iter):
    img_index = np.random.randint(0, sh.shape[-1])
    d.solve(np2cp(sh[..., [img_index]]))

d.display_end()
D1 = cp2np(d.getdict())
print("OnlineConvBPDNDictLearn solve time: %.2fs" % d.timer.elapsed('solve'))
"""
Display initial and final dictionaries.
"""

D1 = D1.squeeze()
Exemplo n.º 10
0
lmbda = 3e0
mu = 2e1
w1 = np.ones((1, 1, 1, 1, D.shape[-1]), dtype=np.float32)
w1[..., 0:3] = 0.0
wg = np.zeros((D.shape[-1]), dtype=np.float32)
wg[..., 0:3] = 1.0

opt = cbpdn.ConvL1L1Grd.Options({
    'Verbose': True,
    'MaxMainIter': 200,
    'RelStopTol': 5e-3,
    'AuxVarObj': False,
    'rho': 4e1,
    'RelaxParam': 1.8,
    'L1Weight': np2cp(w1),
    'GradWeight': np2cp(wg)
})
"""
Initialise the :class:`.admm.cbpdn.ConvL1L1Grd` 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 = cbpdn.ConvL1L1Grd(np2cp(D), np2cp(pad(imgn)), lmbda, mu, opt=opt, dimK=0)
Exemplo n.º 11
0
"""
The problem is solved using class :class:`.admm.cbpdn.ConvL1L1Grd`, which implements a convolutional sparse coding problem with an $\ell_1$ data fidelity term, an $\ell_1$ regularisation term, and an additional gradient regularization term :cite:`wohlberg-2016-convolutional2`, as defined above. The regularization parameters for the $\ell_1$ and gradient terms are ``lmbda`` and ``mu`` respectively. Setting correct weighting arrays for these regularization terms is critical to obtaining good performance. For the $\ell_1$ norm, the weights on the filters that are intended to represent low frequency components are set to zero (we only want them penalised by the gradient term), and the weights of the remaining filters are set to zero. For the gradient penalty, all weights are set to zero except for those corresponding to the filters intended to represent low frequency components, which are set to unity.
"""

lmbda = 3e0
mu = 2e1
w1 = np.ones((1, 1, 1, 1, D.shape[-1]), dtype=np.float32)
w1[..., 0:3] = 0.0
wg = np.zeros((D.shape[-1]), dtype=np.float32)
wg[..., 0:3] = 1.0

opt = cbpdn.ConvL1L1Grd.Options({'Verbose': True, 'MaxMainIter': 200,
                                'RelStopTol': 5e-3, 'AuxVarObj': False,
                                'rho': 4e1, 'RelaxParam': 1.8,
                                'L1Weight': np2cp(w1),
                                'GradWeight': np2cp(wg)})


"""
Initialise the :class:`.admm.cbpdn.ConvL1L1Grd` 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))
Exemplo n.º 12
0
    }
})

lmbda = 0.1

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))

t = TicToc()
t.tic()
b = comcbpdn.ComplexConvBPDN(np2cp(D0),
                             np2cp(s_noise),
                             lmbda,
                             opt_par,
                             dimK=1,
                             dimN=1)
X = cp2np(b.solve())
rec = cp2np(b.reconstruct().squeeze())
# GPUtil.showUtilization()
t.toc()

plt.subplot(131)
plt.plot(s_clean[:, 1].real)
plt.title('clean signal')
plt.subplot(132)
plt.plot(s_noise[:, 1].real)
Exemplo n.º 13
0
"""
Set solver options. See Section 8 of :cite:`wohlberg-2017-convolutional2` for details of construction of $\ell_1$ weighting matrix $W$.
"""

imgnpl, imgnph = signal.tikhonov_filter(pad(imgn), fltlmbd, npd)
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': np2cp(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))
Exemplo n.º 14
0
mu = 1e1
gamma = 1e0
opt = cbpdnin.ConvBPDNInhib.Options({'Verbose': True, 'MaxMainIter': 500,
                                     'RelStopTol': 5e-2, 'AuxVarObj': False,
                                     'rho': 100, 'AutoRho': {'Enabled': False}})


"""
Initialise and run CSC solver.
"""

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.
Exemplo n.º 15
0
stimulus_ra_new = []
for i in range(1, int(stimulus_ra.shape[0] / 1000)):
    stimulus_ra_new.append(stimulus_ra[(i - 1) * 1000:i * 1000, :])
stimulus_ra = np.asarray(stimulus_ra_new)

stimulus_sa_new = []
for i in range(1, int(stimulus_sa.shape[0] / 1000)):
    stimulus_sa_new.append(stimulus_sa[(i - 1) * 1000:i * 1000, :])
stimulus_sa = np.asarray(stimulus_sa_new)

stimulus_pc_new = []
for i in range(1, int(stimulus_pc.shape[0] / 1000)):
    stimulus_pc_new.append(stimulus_pc[(i - 1) * 1000:i * 1000, :])
stimulus_pc = np.asarray(stimulus_pc_new)

data_ra = np2cp(data_ra)
data_sa = np2cp(data_sa)
data_pc = np2cp(data_pc)
stimulus_ra = np2cp(stimulus_ra)
stimulus_sa = np2cp(stimulus_sa)
stimulus_pc = np2cp(stimulus_pc)

# v_ra=np.load('Data_81sets/v_ra',allow_pickle=True)
# v_sa=np.load('Data_81sets/v_sa',allow_pickle=True)
# v_pc=np.load('Data_81sets/v_pc',allow_pickle=True)
v_ra = cp.eye(data_ra[1].shape[1])
v_sa = cp.eye(data_ra[1].shape[1])
v_pc = cp.eye(data_ra[1].shape[1])

A0 = cp.zeros(v_ra.shape)
B0 = cp.zeros(v_ra.shape)