示例#1
0
 def test_13(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     mu = 1e-2
     Wl1 = np.random.randn(1, 1, M).astype(np.float32)
     Wl1i = np.concatenate((Wl1, np.ones(Wl1.shape[0:-1] + (1,))),
                           axis=-1)
     Wgrdi = np.hstack((np.ones(M,), np.zeros((1,))))
     opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': False,
             'MaxMainIter': 50, 'AutoRho': {'Enabled': False}})
     opt['L1Weight'] = Wl1i
     opt['GradWeight'] = Wgrdi
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg, D, s, msk, lmbda, mu, opt)
     X1 = b.solve()
     opt['L1Weight'] = Wl1
     opt['GradWeight'] = 1.0
     X2 = cucbpdn.cbpdngrdmsk(D, s, msk, lmbda, mu, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#2
0
 def test_14(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     mu = 1e-2
     # Create a random ℓ2 of gradient term weighting array. There is no
     # need to extend this array to account for the AMS impulse filter
     # since this is taken care of automatically by cucbpdn.cbpdngrdmsk
     Wgrd = np.random.randn(M).astype(np.float32)
     # Append a zero entry to the GradWeight array, corresponding to
     # the impulse filter appended to the dictionary by cbpdn.AddMaskSim,
     # since this is not done automatically by cbpdn.AddMaskSim
     Wgrdi = np.hstack((Wgrd, np.zeros((1,))))
     opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': False,
             'MaxMainIter': 50, 'AutoRho': {'Enabled': False}})
     opt['GradWeight'] = Wgrdi
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg, D, s, msk, lmbda, mu, opt)
     X1 = b.solve()
     opt['GradWeight'] = Wgrd
     X2 = cucbpdn.cbpdngrdmsk(D, s, msk, lmbda, mu, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#3
0
 def test_11(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     # Create a random ℓ1 term weighting array. There is no need to
     # extend this array to account for the AMS impulse filter since
     # this is taken care of automatically by cucbpdn.cbpdnmsk
     Wl1 = np.random.randn(1, 1, M).astype(np.float32)
     # Append a zero entry to the L1Weight array, corresponding to
     # the impulse filter appended to the dictionary by cbpdn.AddMaskSim,
     # since this is not done automatically by cbpdn.AddMaskSim
     Wl1i = np.concatenate((Wl1, np.zeros(Wl1.shape[0:-1] + (1,))),
                           axis=-1)
     opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 50,
                                   'AutoRho': {'Enabled': False}})
     opt['L1Weight'] = Wl1i
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, msk, lmbda, opt=opt)
     X1 = b.solve()
     opt['L1Weight'] = Wl1
     X2 = cucbpdn.cbpdnmsk(D, s, msk, lmbda, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#4
0
 def test_12(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     mu = 1e-2
     # Since cucbpdn.cbpdngrdmsk automatically ensures that the ℓ2 of
     # gradient term is not applied to the AMS impulse filter, while
     # cbpdn.AddMaskSim does not, we have to pass a GradWeight array
     # with a zero entry corresponding to the AMS impulse filter to
     # cbpdn.AddMaskSim
     Wgrdi = np.hstack((np.ones(M,), np.zeros((1,))))
     opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': False,
             'MaxMainIter': 50, 'AutoRho': {'Enabled': False}})
     opt['GradWeight'] = Wgrdi
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg, D, s, msk, lmbda, mu, opt)
     X1 = b.solve()
     opt['GradWeight'] = 1.0
     X2 = cucbpdn.cbpdngrdmsk(D, s, msk, lmbda, mu, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#5
0
 def test_23(self):
     N = 16
     Nd = 5
     M = 4
     D = np.random.randn(Nd, Nd, M)
     s = np.random.randn(N, N)
     w = np.ones(s.shape)
     lmbda = 1e-1
     try:
         b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, w, lmbda)
         b.solve()
     except Exception as e:
         print(e)
         assert (0)
示例#6
0
 def test_10(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 50,
                                   'AutoRho': {'Enabled': False}})
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, msk, lmbda, opt=opt)
     X1 = b.solve()
     X2 = cucbpdn.cbpdnmsk(D, s, msk, lmbda, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#7
0
 def test_29(self):
     N = 16
     Nd = 5
     M = 4
     D = np.random.randn(Nd, Nd, M)
     s = np.random.randn(N, N)
     w = np.ones(s.shape)
     dt = np.float32
     opt = cbpdn.ConvBPDN.Options({'Verbose' : False, 'MaxMainIter' : 20,
                              'AutoRho' : {'Enabled' : True},
                              'DataType' : dt})
     lmbda = 1e-1
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, w, lmbda, opt=opt)
     b.solve()
     assert(b.cbpdn.X.dtype == dt)
     assert(b.cbpdn.Y.dtype == dt)
     assert(b.cbpdn.U.dtype == dt)
示例#8
0
 def test_13(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     mu = 1e-2
     # Create a random ℓ1 term weighting array. There is no need to
     # extend this array to account for the AMS impulse filter since
     # this is taken care of automatically by cucbpdn.cbpdngrdmsk
     Wl1 = np.random.randn(1, 1, M).astype(np.float32)
     # Append a zero entry to the L1Weight array, corresponding to
     # the impulse filter appended to the dictionary by cbpdn.AddMaskSim,
     # since this is not done automatically by cbpdn.AddMaskSim
     Wl1i = np.concatenate((Wl1, np.zeros(Wl1.shape[0:-1] + (1, ))),
                           axis=-1)
     # Since cucbpdn.cbpdngrdmsk automatically ensures that the ℓ2 of
     # gradient term is not applied to the AMS impulse filter, while
     # cbpdn.AddMaskSim does not, we have to pass a GradWeight array
     # with a zero entry corresponding to the AMS impulse filter to
     # cbpdn.AddMaskSim
     Wgrdi = np.hstack((np.ones(M, ), np.zeros((1, ))))
     opt = cbpdn.ConvBPDNGradReg.Options({
         'Verbose': False,
         'MaxMainIter': 50,
         'AutoRho': {
             'Enabled': False
         }
     })
     opt['L1Weight'] = Wl1i
     opt['GradWeight'] = Wgrdi
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg, D, s, msk, lmbda, mu, opt)
     X1 = b.solve()
     opt['L1Weight'] = Wl1
     opt['GradWeight'] = 1.0
     X2 = cucbpdn.cbpdngrdmsk(D, s, msk, lmbda, mu, opt)
     assert (sm.mse(X1, X2) < 1e-10)
示例#9
0
 def test_11(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)
     frc = 0.5
     msk = su.rndmask(s.shape, frc, dtype=np.float32)
     s *= msk
     lmbda = 1e-1
     Wl1 = np.random.randn(1, 1, M).astype(np.float32)
     Wl1i = np.concatenate((Wl1, np.ones(Wl1.shape[0:-1] + (1,))),
                           axis=-1)
     opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 50,
                                   'AutoRho': {'Enabled': False}})
     opt['L1Weight'] = Wl1i
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, msk, lmbda, opt=opt)
     X1 = b.solve()
     opt['L1Weight'] = Wl1
     X2 = cucbpdn.cbpdnmsk(D, s, msk, lmbda, opt)
     assert(sm.mse(X1, X2) < 1e-10)
示例#10
0
    opt['L1Weight'] = wl1
    opt['GradWeight'] = wgr
    ams = None
    print('%s GPU found: running CUDA solver' % cuda.device_name())
    tm = util.Timer()
    with sys_pipes(), util.ContextTimer(tm):
        X = cuda.cbpdngrdmsk(Di, imgwp, mskp, lmbda, mu, opt)
    t = tm.elapsed()
    imgr = crop(np.sum(linalg.fftconv(Di, X), axis=-1))
else:
    opt['L1Weight'] = wl1i
    opt['GradWeight'] = wgri
    ams = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg,
                           Di,
                           imgwp,
                           mskp,
                           lmbda,
                           mu,
                           opt=opt)
    X = ams.solve().squeeze()
    t = ams.timer.elapsed('solve')
    imgr = crop(ams.reconstruct().squeeze())
"""
Display solve time and reconstruction performance.
"""

print("AddMaskSim wrapped ConvBPDN solve time: %.2fs" % t)
print("Corrupted image PSNR: %5.2f dB" % metric.psnr(img, imgw))
print("Recovered image PSNR: %5.2f dB" % metric.psnr(img, imgr))
"""
Display reference, test, and reconstructed image
示例#11
0
    }
})
"""
Construct :class:`.admm.cbpdn.AddMaskSim` wrapper for :class:`.admm.cbpdn.ConvBPDN` and solve via wrapper. This example could also have made use of :class:`.admm.cbpdn.ConvBPDNMaskDcpl` (see example `cbpdn_md_gry`), which has similar performance in this application, but :class:`.admm.cbpdn.AddMaskSim` has the advantage of greater flexibility in that the wrapper can be applied to a variety of CSC solver objects. If the ``sporco-cuda`` extension is installed and a GPU is available, use the CUDA implementation of this combination.
"""

if cuda.device_count() > 0:
    ams = None
    print('%s GPU found: running CUDA solver' % cuda.device_name())
    tm = util.Timer()
    with sys_pipes(), util.ContextTimer(tm):
        X = cuda.cbpdnmsk(D, sh, mskp, lmbda, opt)
    t = tm.elapsed()
    imgr = crop(sl + np.sum(fftconv(D, X), axis=-1))
else:
    ams = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, sh, mskp, lmbda, opt=opt)
    X = ams.solve()
    t = ams.timer.elapsed('solve')
    imgr = crop(sl + ams.reconstruct().squeeze())
"""
Display solve time and reconstruction performance.
"""

print("AddMaskSim wrapped ConvBPDN solve time: %.2fs" % t)
print("Corrupted image PSNR: %5.2f dB" % metric.psnr(img, imgw))
print("Recovered image PSNR: %5.2f dB" % metric.psnr(img, imgr))
"""
Display reference, test, and reconstructed image
"""

fig = plot.figure(figsize=(21, 7))
示例#12
0
opt = cbpdn.ConvBPDN.Options({
    'Verbose': True,
    'MaxMainIter': 500,
    'HighMemSolve': True,
    'RelStopTol': 1e-3,
    'AuxVarObj': False,
    'RelaxParam': 1.8,
    'rho': 1e2 * lmbda,
    'AutoRho': {
        'Enabled': False,
        'StdResiduals': True
    }
})

# Construct cbpdn.AddMaskSim wrapper for cbpdn.ConvBPDN
ams = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, S, W, lmbda, opt=opt)

# Call solve via wrapper
X = ams.solve()
print("AddMaskSim wrapped ConvBPDN solve time: %.2fs" %
      ams.timer.elapsed('solve'))

# Reconstruct representation
Sr = ams.reconstruct().squeeze()
print("                   reconstruction PSNR: %.2fdB\n" % sm.psnr(S, Sr))

# Display representation and reconstructed image
fig1 = plot.figure(1, figsize=(14, 14))
plot.subplot(2, 2, 1)
plot.imview(np.squeeze(np.sum(abs(X), axis=ams.cri.axisM)),
            fgrf=fig1,
示例#13
0
wgrd = np.ones((D.shape[-1]+1,), dtype=np.float32)
wgrd[-1] = 0.0


# Set up ConvBPDNGradReg options
lmbda = 1e-2
mu = 1e-3
opt = cbpdn.ConvBPDNGradReg.Options({'Verbose': True, 'MaxMainIter': 20,
                    'HighMemSolve': True, 'LinSolveCheck': False,
                    'RelStopTol': 2e-3, 'AuxVarObj': False,
                    'AutoRho': {'Enabled': False}})


# Initialise and run AddMaskSim/ConvBPDNGradReg object
opt['GradWeight'] = wgrd
b = cbpdn.AddMaskSim(cbpdn.ConvBPDNGradReg, D, shw, msk, lmbda, mu, opt)
X1 = b.solve()
print("AddMaskSim/ConvBPDNGradReg solve time: %.2fs" %
      b.timer.elapsed('solve'))


# Time CUDA AddMaskSim/ConvBPDNGradReg solve
opt['GradWeight'] = 1.0
t = util.Timer()
with util.ContextTimer(t):
    X2 = cucbpdn.cbpdngrdmsk(D, shw, msk, lmbda, mu, opt)
print("GPU AddMaskSim/ConvBPDNGradReg solve time: %.2fs" % t.elapsed())
print("GPU time improvement factor: %.1f" % (b.timer.elapsed('solve') /
                                             t.elapsed()))

示例#14
0
    'AutoRho': {
        'Enabled': False,
    }
})

# Normalise dictionary according to Y update options
D0n = ccmod.getPcn0(optd['ZeroMean'], D0.shape, dimN=2, dimC=0)(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.AddMaskSim(cbpdn.ConvBPDN, D0n, shp, W, lmbda, opt=optx)

# Create D update object
dstep = ccmod.ConvCnstrMOD(None, shp, 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')
示例#15
0
lmbda = 1e-2
opt = cbpdn.ConvBPDN.Options({
    'Verbose': True,
    'MaxMainIter': 20,
    'HighMemSolve': True,
    'LinSolveCheck': False,
    'RelStopTol': 2e-3,
    'AuxVarObj': False,
    'rho': 1.5e0,
    'AutoRho': {
        'Enabled': False
    }
})

# Initialise and run AddMaskSim/ConvBPDN object
b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, shw, msk, lmbda, opt=opt)
X1 = b.solve()
print("AddMaskSim/ConvBPDN solve time: %.2fs" % b.timer.elapsed('solve'))

# Time CUDA AddMaskSim/ConvBPDN solve
t = util.Timer()
with util.ContextTimer(t):
    X2 = cucbpdn.cbpdnmsk(D, shw, msk, lmbda, opt)

# Solve time comparison
print("GPU AddMaskSim/ConvBPDN solve time: %.2fs" % t.elapsed())
print("GPU time improvement factor: %.1f" %
      (b.timer.elapsed('solve') / t.elapsed()))

# Compare CPU and GPU solutions
print("CPU solution:  min: %.4e  max: %.4e   l1: %.4e" %
示例#16
0
    def solve(self, S, W=None):
        """Solve for given signal S, optionally with mask W."""
        self.cri = cr.CSC_ConvRepIndexing(self.D.squeeze()[:, :, None, None,
                                                           ...],
                                          S[:, :, None, None, ...],
                                          dimK=None,
                                          dimN=4)

        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 = cucbpdn.cbpdn(self.getdict(),
                              S.squeeze(),
                              self.lmbda,
                              opt=copt)
            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:
            if W is None:
                xstep = cbpdn.ConvBPDN(self.getdict(), S, self.lmbda, opt=copt)
                xstep.solve()
                X = np.asarray(xstep.getcoef().reshape(self.cri.shpX),
                               dtype=self.dtype)
            else:
                xstep = cbpdn.AddMaskSim(cbpdn.ConvBPDN,
                                         self.getdict(),
                                         S,
                                         W,
                                         self.lmbda,
                                         opt=copt)
                X = xstep.solve()
                X = np.asarray(X.reshape(self.cri.shpX), dtype=self.dtype)
                # The additive component is removed from masked signal
                add_cpnt = reconstruct_additive_component(xstep)
                S -= add_cpnt.reshape(S.shape)

        self.timer.stop('xstep')

        # update At and Bt
        self.timer.start('hessian')
        patches = self.im2slices(S)
        self.update_At(X)
        self.update_Bt(X, patches)
        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']:
            fopt['RelStopTol'] = \
                self.dtype.type(self.opt['CCMOD', 'RelStopTol']/(1.+self.j))
        self.timer.start('dstep')
        dstep = StripeSliceFISTA(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()