Ejemplo n.º 1
0
 def test_23(self):
     t = util.Timer()
     t.start()
     with util.ContextTimer(t, action='StopStart'):
         t0 = t.elapsed()
     t.stop()
     assert t.elapsed() >= 0.0
Ejemplo n.º 2
0
    def __new__(cls, *args, **kwargs):
        """Create a DictLearn object and start its initialisation timer."""

        instance = super(DictLearn, cls).__new__(cls)
        instance.timer = util.Timer(['init', 'solve', 'solve_wo_eval'])
        instance.timer.start('init')
        return instance
Ejemplo n.º 3
0
 def test_21(self):
     t = util.Timer('a')
     t.start(['a', 'b'])
     t.reset('a')
     assert t.elapsed('a') == 0.0
     t.reset('all')
     assert t.elapsed('b') == 0.0
Ejemplo n.º 4
0
    def __new__(cls, *args, **kwargs):
        """Create a FISTA object and start its initialisation timer."""

        instance = super(FISTA, cls).__new__(cls)
        instance.timer = util.Timer(['init', 'solve', 'solve_wo_func',
                                     'solve_wo_rsdl', 'solve_wo_btrack'])
        instance.timer.start('init')
        return instance
Ejemplo n.º 5
0
 def test_19(self):
     t = util.Timer()
     t.start()
     t0 = t.elapsed()
     t.stop()
     t1 = t.elapsed()
     assert t0 >= 0.0
     assert t1 >= t0
     assert len(t.__str__()) > 0
     assert len(t.labels()) > 0
Ejemplo n.º 6
0
 def test_17(self):
     t = util.Timer()
     t.start()
     t0 = t.elapsed()
     t.stop()
     t1 = t.elapsed()
     assert (t0 >= 0.0)
     assert (t1 >= t0)
     assert (len(t.__str__()) > 0)
     assert (len(t.labels()) > 0)
Ejemplo n.º 7
0
 def test_20(self):
     t = util.Timer('a')
     t.start(['a', 'b'])
     t0 = t.elapsed('a')
     t.stop('a')
     t.stop('b')
     t.stop(['a', 'b'])
     assert t.elapsed('a') >= 0.0
     assert t.elapsed('b') >= 0.0
     assert t.elapsed('a', total=False) == 0.0
Ejemplo n.º 8
0
    def __init__(self, xstep, dstep, opt=None, isc=None):
        """
        Initialise a DictLearn object with problem size and options.

        Parameters
        ----------
        xstep : bpdn (or similar interface) object
          Object handling X update step
        dstep : cmod (or similar interface) object
          Object handling D update step
        opt : :class:`DictLearn.Options` object
          Algorithm options
        isc : :class:`DictLearn.IterStatsConfig` object
          Iteration statistics and header display configuration
        """

        self.runtime = 0.0
        self.timer = util.Timer()

        if opt is None:
            opt = DictLearn.Options()
        self.opt = opt

        if isc is None:
            isc = IterStatsConfig(
                isfld = ['Iter', 'ObjFunX', 'XPrRsdl', 'XDlRsdl', 'XRho',
                         'ObjFunD', 'DPrRsdl', 'DDlRsdl', 'DRho', 'Time'],
                isxmap = {'ObjFunX' : 'ObjFun', 'XPrRsdl' : 'PrimalRsdl',
                          'XDlRsdl' : 'DualRsdl', 'XRho' : 'Rho'},
                isdmap = {'ObjFunD' : 'DFid', 'DPrRsdl' : 'PrimalRsdl',
                          'DDlRsdl' : 'DualRsdl', 'DRho' : 'Rho'},
                evlmap = {},
                hdrtxt = ['Itn', 'FncX', 'r_X', 's_X', u('ρ_X'),
                          'FncD', 'r_D', 's_D', u('ρ_D')],
                hdrmap = {'Itn' : 'Iter', 'FncX' : 'ObjFunX',
                          'r_X' : 'XPrRsdl', 's_X' : 'XDlRsdl',
                          u('ρ_X') : 'XRho', 'FncD' : 'ObjFunD',
                          'r_D' : 'DPrRsdl', 's_D' : 'DDlRsdl',
                          u('ρ_D') : 'DRho'}
            )
        self.isc = isc

        self.xstep = xstep
        self.dstep = dstep

        self.itstat = []
        self.j = 0

        # Increment `runtime` to reflect object initialisation
        # time. The timer object is reset to avoid double-counting of
        # elapsed time if a similar increment is applied in a derived
        # class __init__.
        self.runtime += self.timer.elapsed(reset=True)
Ejemplo n.º 9
0
    def __init__(self, D, S, lmbda=None, opt=None, dimK=None, dimN=2):
        r"""We use the same layout as ConvBPDN as input and output, but for
        internal computation we use a differnt layout.

        Internal Parameters
        -------------------
        X: [K, m, N]
          Convolutional representation of the input signal. m is the size
          of atom in a dictionary, K is the batch size of input signals,
          and N is the number of slices extracted from each signal (usually
          number of pixels in an image).
        Y: [K, n, N]
          Splitted variable with contraint :math:`D_l x_i - y_i = 0`.
          n represents the size of each slice.
        U: [K, n, N]
          Dual variable with the same size as Y.
        """
        if opt is None:
            opt = ConvBPDNSlice.Options()
        # Set dtype attribute based on S.dtype and opt['DataType']
        self.set_dtype(opt, S.dtype)
        if not hasattr(self, 'cri'):
            self.cri = cr.CSC_ConvRepIndexing(D, S, dimK=dimK, dimN=dimN)
        self.boundary = opt['Boundary']
        # Number of elements of sparse representation x is invariant to
        # slice/FFT solvers.
        Nx = np.product(self.cri.shpX)
        # NOTE: To incorporate with im2slices/slices2im, where each slice
        # is organized as in_channels x patch_size x patch_size, the dictionary
        # is first transposed to [in_channels, patch_size, patch_size, out_channels],
        # and then reshape to 2-D (N, M).
        self.setdict(D)
        # Externally the input signal should have a data layout as
        # S(N0, N1, ..., C, K).
        # First convert to common pytorch Variable layout.
        # [H, W, C, K, 1] -> [K, C, H, W]
        self.S = np.asarray(S.reshape(self.cri.shpS), dtype=S.dtype)
        self.S = self.S.squeeze(-1).transpose((3, 2, 0, 1))
        # [K, n, N]
        self.S_slice = self.im2slices(self.S)
        self.lmbda = lmbda
        # Set penalty parameter if not set
        self.set_attr('rho', opt['rho'], dval=(50.0*self.lmbda + 1.0),
                      dtype=self.dtype)
        super().__init__(Nx, self.S_slice.shape, self.S_slice.shape,
                         S.dtype, opt)
        self.X = np.zeros(
            (self.S_slice.shape[0], self.D.shape[-1], self.Y.shape[-1]),
            dtype=self.dtype
        )
        self.extra_timer = su.Timer(['xstep', 'ystep'])
Ejemplo n.º 10
0
 def __init__(self, D, S, lmbda=None, opt=None, dimK=None, dimN=2):
     if opt is None:
         opt = ConvBPDNSliceTwoBlockCnstrnt.Options()
     # Set dtype attribute based on S.dtype and opt['DataType']
     self.set_dtype(opt, S.dtype)
     if not hasattr(self, 'cri'):
         self.cri = cr.CSC_ConvRepIndexing(D, S, dimK=dimK, dimN=dimN)
     self.lmbda = self.dtype.type(lmbda)
     # Set penalty parameter if not set
     self.set_attr('rho', opt['rho'], dval=(50.0*self.lmbda + 1.0),
                   dtype=self.dtype)
     # Set xi if not set
     self.set_attr('tau_xi', opt['AutoRho', 'RsdlTarget'],
                   dval=(1.0+18.3**(np.log10(self.lmbda)+1.0)),
                   dtype=self.dtype)
     # set boundary condition
     self.set_attr('boundary', opt['Boundary'], dval='circulant_back',
                   dtype=None)
     # set weight factor between two constraints
     self.set_attr('gamma', opt['Gamma'], dval=1., dtype=self.dtype)
     self.setdict(D)
     # Number of elements of sparse representation x is invariant to
     # slice/FFT solvers.
     Nx = np.product(self.cri.shpX)
     # Externally the input signal should have a data layout as
     # S(N0, N1, ..., C, K).
     # First convert to common pytorch Variable layout.
     # [H, W, C, K, 1] -> [K, C, H, W]
     self.S = np.asarray(S.reshape(self.cri.shpS), dtype=S.dtype)
     self.S = self.S.squeeze(-1).transpose((3, 2, 0, 1))
     # [K, n, N]
     self.S_slice = self.im2slices(self.S)
     yshape = (self.S_slice.shape[0], self.D.shape[0] + self.D.shape[1],
               self.S_slice.shape[-1])
     super().__init__(Nx, yshape, 1, self.D.shape[0], S.dtype, opt)
     self.X = np.zeros_like(self._Y1, dtype=self.dtype)
     self.extra_timer = su.Timer(['xstep', 'ystep'])
Ejemplo n.º 11
0

# Load dictionary
D = util.convdicts()['G:12x12x72']


# Set up ConvBPDN options
lmbda = 1e-2
opt = cbpdn.ConvBPDN.Options({'Verbose': True, 'MaxMainIter': 200,
                    'HighMemSolve': True, 'RelStopTol': 5e-3,
                    'AuxVarObj': False, 'AutoRho': {'Enabled': False},
                    'rho': 5.0})


# Time CUDA cbpdn solve
t = util.Timer()
with util.ContextTimer(t):
    X = cucbpdn.cbpdn(D, sh, lmbda, opt)
print("Image size: %d x %d" % sh.shape)
print("GPU ConvBPDN solve time: %.2fs" % t.elapsed())


# Reconstruct the image from the sparse representation
shr = np.sum(spl.fftconv(D, X), axis=2)
imgr = sl + shr


#Display representation and reconstructed image.
fig = plot.figure(figsize=(14, 14))
plot.subplot(2, 2, 1)
plot.imview(sl, fig=fig, title='Lowpass component')
Ejemplo n.º 12
0
 def __new__(cls, *args, **kwargs):
     instance = super(OnlineDictLearnDenseSurrogate, cls).__new__(cls)
     instance.timer = su.Timer(['init', 'solve', 'solve_wo_eval', 'hessian',
                                'xstep', 'dstep'])
     instance.timer.start('init')
     return instance
Ejemplo n.º 13
0
 def test_22(self):
     t = util.Timer()
     with util.ContextTimer(t):
         t0 = t.elapsed()
     assert t.elapsed() >= 0.0
Ejemplo n.º 14
0
# Set up ConvBPDN options
lmbda = 1e-2
opt = cbpdn.ConvBPDN.Options({
    'Verbose': False,
    'MaxMainIter': 0,
    'HighMemSolve': True,
    'LinSolveCheck': True,
    'RelStopTol': 1e-5,
    'AuxVarObj': False,
    'AutoRho': {
        'Enabled': False
    }
})

# Compute initialisation time: solve with 0 iterations
t0 = util.Timer()
with util.ContextTimer(t0):
    X = cucbpdn.cbpdn(D, sh, lmbda, opt)

# Solve with Niter iterations
Niter = 200
opt['MaxMainIter'] = Niter
t1 = util.Timer()
with util.ContextTimer(t1):
    X = cucbpdn.cbpdn(D, sh, lmbda, opt)

# Print run time information
print("GPU ConvBPDN init time: %.3fs" % t0.elapsed())
print("GPU ConvBPDN solve time per iteration: %.3fs" % (t1.elapsed() / Niter))
Ejemplo n.º 15
0
 def __new__(cls, *args, **kwargs):
     instance = super(OnlineSliceDictLearn, cls).__new__(cls)
     instance.timer = su.Timer(
         ['init', 'solve', 'solve_wo_eval', 'xstep', 'dstep'])
     instance.timer.start('init')
     return instance
Ejemplo n.º 16
0
    'GradWeight': wgr,
    'AutoRho': {
        'Enabled': False,
        'StdResiduals': False
    }
})
"""
Construct :class:`.admm.cbpdn.AddMaskSim` wrapper for :class:`.admm.cbpdn.ConvBPDNGradReg` and solve via wrapper. If the ``sporco-cuda`` extension is installed and a GPU is available, use the CUDA implementation of this combination.
"""

if cuda.device_count() > 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()
Ejemplo n.º 17
0
    def __init__(self, Nx, yshape, ushape, dtype, opt=None):
        """
        Initialise an ADMM object with problem size and options.

        Parameters
        ----------
        Nx : int
          Size of variable :math:`\mathbf{x}` in objective function
        yshape : tuple of ints
          Shape of working variable Y (the auxiliary variable)
        ushape : tuple of ints
          Shape of working variable U (the scaled dual variable)
        dtype : data-type
          Data type for working variables (overridden by 'DataType' option)
        opt : :class:`ADMM.Options` object
          Algorithm options
        """

        if opt is None:
            opt = ADMM.Options()
        if not isinstance(opt, ADMM.Options):
            raise TypeError("Parameter opt must be an instance of ADMM.Options")

        # Management of timing of object initialisation is the
        # responsibility of derived classes. Timing of :meth:`solve`
        # is managed by this class.
        self.runtime = 0.0
        self.timer = util.Timer()

        self.opt = opt
        self.Nx = Nx
        # Working variable U has the same dimensionality as constant c
        # in the constraint Ax + By = c
        self.Nc = np.product(ushape)

        # DataType option overrides data type inferred from __init__
        # parameters of derived class
        self.set_dtype(opt, dtype)

        # Initialise attributes representing penalty parameter and other
        # parameters
        self.set_attr('rho', opt['rho'], dval=1.0, dtype=self.dtype)
        self.set_attr('rho_tau', opt['AutoRho', 'Scaling'], dval=2.0,
                      dtype=self.dtype)
        self.set_attr('rho_mu', opt['AutoRho', 'RsdlRatio'], dval=10.0,
                      dtype=self.dtype)
        self.set_attr('rho_xi', opt['AutoRho', 'RsdlTarget'], dval=1.0,
                      dtype=self.dtype)
        self.set_attr('rlx', opt['RelaxParam'], dval=1.0, dtype=self.dtype)


        # Initialise working variable Y
        if  self.opt['Y0'] is None:
            self.Y = self.yinit(yshape)
        else:
            self.Y = self.opt['Y0'].astype(self.dtype, copy=True)
        self.Yprev = self.Y.copy()

        # Initialise working variable U
        if  self.opt['U0'] is None:
            self.U = self.uinit(ushape)
        else:
            self.U = self.opt['U0'].astype(self.dtype, copy=True)

        self.itstat = []
        self.k = 0