-
Notifications
You must be signed in to change notification settings - Fork 21
/
GD.py
executable file
·207 lines (152 loc) · 5.5 KB
/
GD.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
from PIL import Image
import yaml
def loaddata(show_im=True):
psf = Image.open(psfname)
psf = np.array(psf, dtype='float32')
data = Image.open(imgname)
data = np.array(data, dtype='float32')
"""In the picamera, there is a non-trivial background
(even in the dark) that must be subtracted"""
bg = np.mean(psf[5:15,5:15])
psf -= bg
data -= bg
"""Resize to a more manageable size to do reconstruction on.
Because resizing is downsampling, it is subject to aliasing
(artifacts produced by the periodic nature of sampling). Demosaicing is an attempt
to account for/reduce the aliasing caused. In this application, we do the simplest
possible demosaicing algorithm: smoothing/blurring the image with a box filter"""
def resize(img, factor):
num = int(-np.log2(factor))
for i in range(num):
img = 0.25*(img[::2,::2,...]+img[1::2,::2,...]+img[::2,1::2,...]+img[1::2,1::2,...])
return img
psf = resize(psf, f)
data = resize(data, f)
""" nmormalizing copy from shreyas"""
psf /= np.linalg.norm(psf.ravel())
data /= np.linalg.norm(data.ravel())
if show_im:
fig1 = plt.figure()
plt.imshow(psf, cmap='gray')
plt.title('PSF')
plt.show()
fig2 = plt.figure()
plt.imshow(data, cmap='gray')
plt.title('Raw data')
plt.show()
return psf, data
def initMatrices(h):
pixel_start = (np.max(h) + np.min(h))/2
x = np.ones(h.shape)*pixel_start
init_shape = h.shape
padded_shape = [nextPow2(2*n - 1) for n in init_shape]
starti = (padded_shape[0]- init_shape[0])//2
endi = starti + init_shape[0]
startj = (padded_shape[1]//2) - (init_shape[1]//2)
endj = startj + init_shape[1]
hpad = np.zeros(padded_shape)
hpad[starti:endi, startj:endj] = h
H = fft.fft2(hpad, norm="ortho")
Hadj = np.conj(H)
def crop(X):
return X[starti:endi, startj:endj]
def pad(v):
vpad = np.zeros(padded_shape).astype(np.complex64)
vpad[starti:endi, startj:endj] = v
return vpad
utils = [crop, pad]
v = np.real(pad(x))
return H, Hadj, v, utils
def nextPow2(n):
return int(2**np.ceil(np.log2(n)))
def grad(Hadj, H, vk, b, crop, pad):
Av = calcA(H, vk, crop)
diff = Av - b
return np.real(calcAHerm(Hadj, diff, pad))
def calcA(H, vk, crop):
Vk = fft.fft2(vk, norm="ortho")
return crop(fft.ifftshift(fft.ifft2(H*Vk, norm="ortho")))
def calcAHerm(Hadj, diff, pad):
xpad = pad(diff)
X = fft.fft2(xpad, norm="ortho")
return fft.ifftshift(fft.ifft2(Hadj*X, norm="ortho"))
def grad_descent(h, b):
H, Hadj, v, utils = initMatrices(h)
crop = utils[0]
pad = utils[1]
alpha = np.real(2/(np.max(Hadj * H)))
iterations = 0
def non_neg(xi):
xi = np.maximum(xi,0)
return xi
#proj = lambda x: x #Do no projection
proj = non_neg #Enforce nonnegativity at every gradient step. Comment out as needed.
parent_var = [H, Hadj, b, crop, pad, alpha, proj]
vk = v
#### uncomment for Nesterov momentum update ####
#p = 0
#mu = 0.9
################################################
#### uncomment for FISTA update ################
tk = 1
xk = v
################################################
for iterations in range(iters):
# uncomment for regular GD update
#vk = gd_update(vk, parent_var)
# uncomment for Nesterov momentum update
#vk, p = nesterov_update(vk, p, mu, parent_var)
# uncomment for FISTA update
vk, tk, xk = fista_update(vk, tk, xk, parent_var)
if iterations % disp_pic == 0:
print(iterations)
image = proj(crop(vk))
f = plt.figure(1)
plt.imshow(image, cmap='gray')
plt.title('Reconstruction after iteration {}'.format(iterations))
plt.show()
return proj(crop(vk))
def gd_update(vk, parent_var):
H, Hadj, b, crop, pad, alpha, proj = parent_var
gradient = grad(Hadj, H, vk, b, crop, pad)
vk -= alpha*gradient
vk = proj(vk)
return xk
def nesterov_update(vk, p, mu, parent_var):
H, Hadj, b, crop, pad, alpha, proj = parent_var
p_prev = p
gradient = grad(Hadj, H, vk, b, crop, pad)
p = mu*p - alpha*gradient
vk += -mu*p_prev + (1+mu)*p
vk = proj(vk)
return vk, p
def fista_update(vk, tk, xk, parent_var):
H, Hadj, b, crop, pad, alpha, proj = parent_var
x_k1 = xk
gradient = grad(Hadj, H, vk, b, crop, pad)
vk -= alpha*gradient
xk = proj(vk)
t_k1 = (1+np.sqrt(1+4*tk**2))/2
vk = xk+(tk-1)/t_k1*(xk - x_k1)
tk = t_k1
return vk, tk, xk
if __name__ == "__main__":
### Reading in params from config file (don't mess with parameter names!)
params = yaml.load(open("gd_config.yml"))
for k,v in params.items():
exec(k + "=v")
psf, data = loaddata()
final_im = grad_descent(psf, data)
print(iters)
plt.imshow(final_im, cmap='gray')
plt.title('Final reconstruction after {} iterations'.format(iters))
plt.show()
saveim = input('Save final image? (y/n) ')
if saveim == 'y':
filename = input('Name of file: ')
plt.imshow(final_im, cmap='gray')
plt.axis('off')
plt.savefig(filename+'.png', bbox_inches='tight')