<jupyter_start><jupyter_text>Online Convolutional Dictionary Learning (CuPy Version)=======================================================This example demonstrates the use of [onlinecdl.OnlineConvBPDNDictLearn](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.onlinecdl.htmlsporco.dictlrn.onlinecdl.OnlineConvBPDNDictLearn) for learning a convolutional dictionary from a set of training images. The dictionary is learned using the online dictionary learning algorithm proposed in [[33]](http://sporco.rtfd.org/en/latest/zreferences.htmlid33). This variant of the example uses the GPU accelerated version of [onlinecdl](http://sporco.rtfd.org/en/latest/modules/sporco.dictlrn.onlinecdl.htmlmodule-sporco.dictlrn.onlinecdl) within the [sporco.cupy](http://sporco.rtfd.org/en/latest/modules/sporco.cupy.htmlmodule-sporco.cupy) subpackage.<jupyter_code>from __future__ import print_function
from builtins import input

import numpy as np

from sporco import util
from sporco import signal
from sporco import plot
plot.config_notebook_plotting()
from sporco.cupy import (cupy_enabled, np2cp, cp2np, select_device_by_load,
                         gpu_info)
from sporco.cupy.dictlrn import onlinecdl<jupyter_output><empty_output><jupyter_text>Load training images.<jupyter_code>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])
S3 = exim.image('monarch.png', idxexp=np.s_[:, 160:672])
S4 = exim.image('sail.png', idxexp=np.s_[:, 210:722])
S5 = exim.image('tulips.png', idxexp=np.s_[:, 30:542])
S = np.stack((S1, S2, S3, S4, S5), axis=3)<jupyter_output><empty_output><jupyter_text>Highpass filter training images.<jupyter_code>npd = 16
fltlmbd = 5
sl, sh = signal.tikhonov_filter(S, fltlmbd, npd)<jupyter_output><empty_output><jupyter_text>Construct initial dictionary.<jupyter_code>np.random.seed(12345)
D0 = np.random.randn(8, 8, 3, 64)<jupyter_output><empty_output><jupyter_text>Set regularization parameter and options for dictionary learning solver.<jupyter_code>lmbda = 0.2
opt = onlinecdl.OnlineConvBPDNDictLearn.Options({
                'Verbose': True, 'ZeroMean': False, 'eta_a': 10.0,
                'eta_b': 20.0, 'DataType': np.float32,
                'CBPDN': {'rho': 5.0, 'AutoRho': {'Enabled': True},
                    'RelaxParam': 1.8, 'RelStopTol': 1e-7, 'MaxMainIter': 50,
                    'FastSolve': False, 'DataType': np.float32}})<jupyter_output><empty_output><jupyter_text>Create solver object and solve.<jupyter_code>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'))<jupyter_output>Running on GPU 0 (GeForce RTX 2080 Ti)<jupyter_text>Display initial and final dictionaries.<jupyter_code>D1 = D1.squeeze()
fig = plot.figure(figsize=(14, 7))
plot.subplot(1, 2, 1)
plot.imview(util.tiledict(D0), title='D0', fig=fig)
plot.subplot(1, 2, 2)
plot.imview(util.tiledict(D1), title='D1', fig=fig)
fig.show()<jupyter_output><empty_output><jupyter_text>Get iterations statistics from solver object and plot functional value.<jupyter_code>its = d.getitstat()
fig = plot.figure(figsize=(7, 7))
plot.plot(np.vstack((its.DeltaD, its.Eta)).T, xlbl='Iterations',
          lgnd=('Delta D', 'Eta'), fig=fig)
fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15583048170571204, 'fraction_numerical': 0.03185048697025533, 'mean_word_length': 5.408094435075886, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '520940', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1390, 'n_words': 275, 'path': 'cdl/onlinecdl_clr_cupy.ipynb', 'repo_name': 'bwohlberg/sporco-notebooks', 'size': '3799'}