-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlola_CS.py
133 lines (96 loc) · 3.65 KB
/
lola_CS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
import os
import numpy as np
import scipy.fftpack as spfft
import scipy.ndimage as spimg
import matplotlib.pyplot as plt
from pylbfgs import owlqn
import imageio
# Fraction to scale the original image
SCALE = 1.0
# Fraction of the scaled image to randomly sample
SAMPLE = 0.1
# Coeefficient for the L1 norm of variables (see OWL-QN algorithm)
ORTHANTWISE_C = 5
def dct2(x):
"""Return 2D discrete cosine transform.
"""
return spfft.dct(
spfft.dct(x.T, norm='ortho', axis=0).T, norm='ortho', axis=0)
def idct2(x):
"""Return inverse 2D discrete cosine transform.
"""
return spfft.idct(
spfft.idct(x.T, norm='ortho', axis=0).T, norm='ortho', axis=0)
def progress(x, g, fx, xnorm, gnorm, step, k, ls):
"""Just display the current iteration.
"""
print('Iteration {}'.format(k))
return 0
_image_dims = None # track target image dimensions here
_ri_vector = None # reference the random sampling indices here
_b_vector = None # reference the sampled vector b here
def evaluate(x, g, step):
"""An in-memory evaluation callback.
"""
# we want to return two things:
# (1) the norm squared of the residuals, sum((Ax-b).^2), and
# (2) the gradient 2*A'(Ax-b)
# expand x columns-first
x2 = x.reshape((_image_dims[1], _image_dims[0])).T
# Ax is just the inverse 2D dct of x2
Ax2 = idct2(x2)
# stack columns and extract samples
Ax = Ax2.T.flat[_ri_vector].reshape(_b_vector.shape)
# calculate the residual Ax-b and its 2-norm squared
Axb = Ax - _b_vector
fx = np.sum(np.power(Axb, 2))
# project residual vector (k x 1) onto blank image (ny x nx)
Axb2 = np.zeros(x2.shape)
Axb2.T.flat[_ri_vector] = Axb # fill columns-first
# A'(Ax-b) is just the 2D dct of Axb2
AtAxb2 = 2 * dct2(Axb2)
AtAxb = AtAxb2.T.reshape(x.shape) # stack columns
# copy over the gradient vector
np.copyto(g, AtAxb)
return fx
_A_matrix = None # reference the dct matrix operator A here
def main():
global _b_vector, _A_matrix, _image_dims, _ri_vector
# read image in grayscale, then downscale it
Xorig = imageio.imread('LOLA.jpg', as_gray=True, pilmode='L')
X = spimg.zoom(Xorig, SCALE)
ny, nx = X.shape
# take random samples of image, store them in a vector b
k = round(nx * ny * SAMPLE)
ri = np.random.choice(nx*ny, k, replace=False) # random sample of indices
b = X.T.flat[ri].astype(float) # important: cast to 64 bit
# This method evaluates the objective function sum((Ax-b).^2) and its
# gradient without ever actually generating A (which can be massive)
# Our ability to do this stems from our knowledge that Ax is just the
# sampled idct2 of the spectral image (x in matrix form).
# save image dims, sampling vector, and b vector and to global vars
_image_dims = (ny, nx)
_ri_vector = ri
_b_vector = np.expand_dims(b, axis=1)
# perform the L1 minimization in memory
Xat2 = owlqn(nx*ny, evaluate, progress, ORTHANTWISE_C)
# transform the output back into the spatial domain
Xat = Xat2.reshape(nx, ny).T # stack columns
Xa = idct2(Xat)
# create images of mask (for visualization)
mask = np.zeros(X.shape)
mask.T.flat[ri] = 255
Xm = 255 * np.ones(X.shape)
Xm.T.flat[ri] = X.T.flat[ri]
# display the result
f, ax = plt.subplots(1, 3, figsize=(14, 4))
ax[0].imshow(X, cmap='gray', interpolation='none')
ax[0].set_axis_off()
ax[1].imshow(Xm, cmap='gray', interpolation='none')
ax[1].set_axis_off()
ax[2].imshow(Xa, cmap='gray', interpolation='none')
ax[2].set_axis_off()
plt.show()
if __name__ == '__main__':
main()