#!/usr/bin/env python # coding: utf-8 # Convolutional Dictionary Learning with Spatial Mask # =================================================== # # This example demonstrates the use of [cbpdndlmd.ConvBPDNMaskDictLearn](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.cbpdndlmd.html#sporco.dictlrn.cbpdndlmd.ConvBPDNMaskDictLearn) for convolutional dictionary learning with a spatial mask, from a set of colour training images [[51]](http://sporco.rtfd.org/en/latest/zreferences.html#id54). The dictionary learning algorithm is based on the hybrid mask decoupling / ADMM consensus dictionary update [[26]](http://sporco.rtfd.org/en/latest/zreferences.html#id25). # In[1]: from __future__ import print_function from builtins import input import pyfftw # See https://github.com/pyFFTW/pyFFTW/issues/40 import numpy as np from sporco.admm import tvl2 from sporco.dictlrn import cbpdndl from sporco.dictlrn import cbpdndlmd from sporco import util from sporco import signal from sporco import plot plot.config_notebook_plotting() # Load training images. # In[2]: exim = util.ExampleImages(scaled=True, zoom=0.25) S1 = exim.image('barbara.png', idxexp=np.s_[10:522, 100:612]) S2 = exim.image('kodim23.png', idxexp=np.s_[:, 60:572]) S = np.stack((S1, S2), axis=3) # Construct initial dictionary. # In[3]: np.random.seed(12345) D0 = np.random.randn(8, 8, 3, 32) # Create random mask and apply to training images. # In[4]: frc = 0.5 W = signal.rndmask(S.shape[0:3] + (1,), frc, dtype=np.float32) Sw = W * S # $\ell_2$-TV denoising with a spatial mask as a non-linear lowpass filter. # In[5]: lmbda = 0.1 opt = tvl2.TVL2Denoise.Options({'Verbose': False, 'MaxMainIter': 200, 'DFidWeight': W, 'gEvalY': False, 'AutoRho': {'Enabled': True}}) b = tvl2.TVL2Denoise(Sw, lmbda, opt, caxis=2) sl = b.solve() sh = Sw - sl # CDL without a spatial mask using [dictlrn.cbpdndl.ConvBPDNDictLearn](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.cbpdndl.html#sporco.dictlrn.cbpdndl.ConvBPDNDictLearn). (Note that [prlcnscdl.ConvBPDNMaskDcplDictLearn_Consensus](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.prlcnscdl.html#sporco.dictlrn.prlcnscdl.ConvBPDNMaskDcplDictLearn_Consensus) solves the same problem, but is substantially faster on a multi-core architecture.) # In[6]: lmbda = 0.05 opt1 = cbpdndl.ConvBPDNDictLearn.Options({'Verbose': True, 'MaxMainIter': 200, 'AccurateDFid': True, 'CBPDN': {'rho': 50.0*lmbda + 0.5}, 'CCMOD': {'rho': 3e2}}, dmethod='cns') d1 = cbpdndl.ConvBPDNDictLearn(D0, sh, lmbda, opt1, dmethod='cns') D1 = d1.solve() # Reconstruct from the CDL solution without a spatial mask. # In[7]: sr1 = d1.reconstruct().squeeze() + sl # CDL with a spatial mask using [cbpdndlmd.ConvBPDNMaskDictLearn](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.cbpdndlmd.html#sporco.dictlrn.cbpdndlmd.ConvBPDNMaskDictLearn). # In[8]: opt2 = cbpdndlmd.ConvBPDNMaskDictLearn.Options({'Verbose': True, 'MaxMainIter': 200, 'AccurateDFid': True, 'CBPDN': {'rho': 50.0*lmbda + 0.5}, 'CCMOD': {'rho': 1.0}}, dmethod='cns') d2 = cbpdndlmd.ConvBPDNMaskDictLearn(D0, sh, lmbda, W, opt2, dmethod='cns') D2 = d2.solve() # Reconstruct from the CDL solution with a spatial mask. # In[9]: sr2 = d2.reconstruct().squeeze() + sl # Compare dictionaries. # In[10]: fig = plot.figure(figsize=(14, 7)) plot.subplot(1, 2, 1) plot.imview(util.tiledict(D1.squeeze()), fig=fig, title='Without Mask Decoupling') plot.subplot(1, 2, 2) plot.imview(util.tiledict(D2.squeeze()), fig=fig, title='With Mask Decoupling') fig.show() # Display reference and training images. # In[11]: fig = plot.figure(figsize=(14, 14)) plot.subplot(2, 2, 1) plot.imview(S[...,0], title='Reference', fig=fig) plot.subplot(2, 2, 2) plot.imview(Sw[...,0], title='Test', fig=fig) plot.subplot(2, 2, 3) plot.imview(S[...,1], title='Reference', fig=fig) plot.subplot(2, 2, 4) plot.imview(Sw[...,1], title='Test', fig=fig) fig.show() # Compare reconstructed images. # In[12]: fig = plot.figure(figsize=(14, 14)) plot.subplot(2, 2, 1) plot.imview(sr1[...,0], title='Without Mask Decoupling', fig=fig) plot.subplot(2, 2, 2) plot.imview(sr2[...,0], title='With Mask Decoupling', fig=fig) plot.subplot(2, 2, 3) plot.imview(sr1[...,1], title='Without Mask Decoupling', fig=fig) plot.subplot(2, 2, 4) plot.imview(sr2[...,1], title='With Mask Decoupling', fig=fig) fig.show()