Linear Operator#

Open In Colab

[1]:
# uncomment the following line to install dprox if your are in online google colab notebook
# !pip install dprox
[2]:
import dprox as dp
from dprox.contrib import fspecial_gaussian
from dprox.utils import to_torch_tensor, imshow
from scipy.misc import face

x = dp.Variable()
point_spread_function = fspecial_gaussian(15, 5)
op = dp.conv(x, point_spread_function)

K = dp.CompGraph(op)
K.visualize()
K.sanity_check()  # implementation sanity check (please refer to the supplement D.3 for details)

img = to_torch_tensor(face(), batch=True)
out = K.forward(img)

print(img.shape)
imshow(out, titles=['blurry measurement'])
../_images/tutorials_linear_operator_2_0.svg
Sanity check passed, diff=0.03125 rel_diff=6.135499575066206e-08
torch.Size([1, 3, 768, 1024])
../_images/tutorials_linear_operator_2_2.png
[3]:
import torch

x = dp.Variable()
y = 3 * (x - torch.tensor([2, 2, 2]))

K = dp.CompGraph(y)
K.visualize()

input = torch.tensor([1, 2, 3])
print(dp.eval(y, input, zero_out_constant=False))

print(y.variables)
print(y.constants)

x.value = torch.tensor([1, 2, 3])
print(y.value)
print(y.offset)
../_images/tutorials_linear_operator_3_0.svg
tensor([-3,  0,  3])
[Variable(id=854080d0-33a2-11ee-8a7e-2cf05dadd712, shape=None, value=None)]
[Constant(value=somevalue)]
tensor([-3,  0,  3])
tensor([-6, -6, -6])
[4]:

x = dp.Variable() K = dp.CompGraph(dp.grad(x, dim=1) + dp.grad(x, dim=2)) K.sanity_check() img = to_torch_tensor(face(), batch=True) print(img.shape) outputs = K.forward(img) imshow(outputs*255)
Sanity check passed, diff=0.03125 rel_diff=6.149233655605713e-08
torch.Size([1, 3, 768, 1024])
../_images/tutorials_linear_operator_4_1.png
[5]:

x1 = dp.Variable() x2 = dp.Variable() K = dp.CompGraph(x1 + x2) K.visualize() v1 = torch.randn((4, 4), requires_grad=True) v2 = torch.randn((4, 4)) outputs = K.forward(v1, v2) print(outputs) loss = torch.mean(outputs[0]) loss.backward() print(v1.grad)
../_images/tutorials_linear_operator_5_0.svg
tensor([[-2.0644, -1.2812, -1.3720, -0.2061],
        [-3.8320, -1.7413, -1.0122,  2.3179],
        [-0.0968,  1.2712, -0.8897,  1.1791],
        [-2.2427,  2.9101,  1.0075,  0.7626]], grad_fn=<AddBackward0>)
tensor([[0.2500, 0.2500, 0.2500, 0.2500],
        [0.0000, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0000, 0.0000]])
[6]:
x = dp.Variable()
K = dp.CompGraph(dp.vstack([dp.mosaic(x), dp.grad(x)]))

K.visualize()

img = to_torch_tensor(face(), batch=True)
print(img.shape)

outputs = K.forward(img)
inputs = K.adjoint(outputs)
print(inputs.shape)
# K.sanity_check()
../_images/tutorials_linear_operator_6_0.svg
torch.Size([1, 3, 768, 1024])
torch.Size([1, 3, 768, 1024])
[8]:
from dprox import *
from dprox.utils import *
from dprox.contrib import *

img = sample('face')
psf = point_spread_function(15, 5)
b = blurring(img, psf)

x = Variable()
data_term = sum_squares(conv(x, psf) - b)
reg_term = deep_prior(x, denoiser='ffdnet_color')
reg2 = nonneg(x)
K = CompGraph(vstack([fn.linop for fn in [data_term, reg_term, reg2]]))
K.visualize()
../_images/tutorials_linear_operator_7_0.svg