forked from matthiaslee/VMBD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_vmbd.py
executable file
·253 lines (209 loc) · 10.5 KB
/
run_vmbd.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python
# Script for performing spatially-varying online multi-frame
# blind deconvolution
#
# Copyright (C) 2011 Michael Hirsch
# Load other libraries
import pycuda.autoinit
import pycuda.gpuarray as cua
import numpy as np
# pylab is turned off because it takes forever to load
#import pylab as pl
#from pylab import draw, figure, bla bla bla
import os
from shutil import rmtree
from optparse import OptionParser
# Load own libraris
import olaGPU
import imagetools
import gputools
import fitsTools
import stopwatch
import img_scale
def process(opts):
# ============================================================================
# Specify some parameter settings
# ----------------------------------------------------------------------------
# Specify data path and file identifier
DATAPATH = '/DATA/LSST/FITS'
RESPATH = '../../../DATA/results';
BASE_N = 141
FILENAME = lambda i: '%s/v88827%03d-fz.R22.S11.fits' % (DATAPATH,(BASE_N+i))
ID = 'LSST'
# ----------------------------------------------------------------------------
# Specify parameter settings
# General
doshow = opts.doShow # put 1 to show intermediate results
backup = opts.backup # put 1 to write intermediate results to disk
N = opts.N # how many frames to process
N0 = opts.N0 # number of averaged frames for initialisation
# OlaGPU parameters
sf = np.array([40,40]) # estimated size of PSF
csf =(3,3) # number of kernels across x and y direction
overlap = 0.5 # overlap of neighboring patches in percent
# Regularization parameters for kernel estimation
f_alpha = opts.f_alpha # promotes smoothness
f_beta = opts.f_beta # Thikhonov regularization
optiter = opts.optiter # number of iterations for minimization
tol = opts.tol # tolerance for when to stop minimization
# ============================================================================
# Create helper functions for file handling
# # # HACK for chunking into available GPU mem # # #
# - loads one 1kx1k block out of the fits image
xOffset=2000
yOffset=0
chunkSize=1000
yload = lambda i: 1. * fitsTools.readFITS(FILENAME(i), use_mask=True, norm=True)[yOffset:yOffset+chunkSize,xOffset:xOffset+chunkSize]
# ----------------------------------------------------------------------------
# Some more code for backuping the results
# ----------------------------------------------------------------------------
# For backup purposes
EXPPATH = '%s/%s_sf%dx%d_csf%dx%d_maxiter%d_alpha%.2f_beta%.2f' % \
(RESPATH,ID,sf[0],sf[1],csf[0],csf[1],optiter,f_alpha,f_beta)
xname = lambda i: '%s/x_%04d.png' % (EXPPATH,i)
yname = lambda i: '%s/y_%04d.png' % (EXPPATH,i)
fname = lambda i: '%s/f_%04d.png' % (EXPPATH,i)
if os.path.exists(EXPPATH) and opts.overwrite:
try:
rmtree(EXPPATH)
except:
print "[ERROR] removing old results dir:",EXPPATH
exit()
elif os.path.exists(EXPPATH):
print "[ERROR] results directory already exists, please remove or use '-o' to overwrite"
exit()
# Create results path if not existing
try:
os.makedirs(EXPPATH)
except:
print "[ERROR] creating results dir:",EXPPATH
exit()
print 'Results are saved to: \n %s \n' % EXPPATH
# ----------------------------------------------------------------------------
# For displaying intermediate results create target figure
# ----------------------------------------------------------------------------
# Create figure for displaying intermediate results
if doshow:
print "showing intermediate results is currently disabled.."
#pl.figure(1)
#pl.draw()
# ----------------------------------------------------------------------------
# Code for initialising the online multi-frame deconvolution
# ----------------------------------------------------------------------------
# Initialisation of latent image by averaging the first 20 frames
y0 = 0.
for i in np.arange(1,N0):
y0 += yload(i)
y0 /= N0
y_gpu = cua.to_gpu(y0)
# Pad image since we perform deconvolution with valid boundary conditions
x_gpu = gputools.impad_gpu(y_gpu, sf-1)
# Create windows for OlaGPU
sx = y0.shape + sf - 1
sf2 = np.floor(sf/2)
winaux = imagetools.win2winaux(sx, csf, overlap)
# ----------------------------------------------------------------------------
# Loop over all frames and do online blind deconvolution
# ----------------------------------------------------------------------------
import time as t
ti = t.clock()
t1 = stopwatch.timer()
t2 = stopwatch.timer()
t3 = stopwatch.timer()
t4 = stopwatch.timer()
t4.start()
for i in np.arange(1,N+1):
print 'Processing frame %d/%d \r' % (i,N)
# Load next observed image
t3.start()
y = yload(i)
print "TIMER load:", t3.elapsed()
# Compute mask for determining saturated regions
mask_gpu = 1. * cua.to_gpu(y < 1.)
y_gpu = cua.to_gpu(y)
# ------------------------------------------------------------------------
# PSF estimation
# ------------------------------------------------------------------------
# Create OlaGPU instance with current estimate of latent image
t2.start()
X = olaGPU.OlaGPU(x_gpu,sf,'valid',winaux=winaux)
print "TIMER GPU: ", t2.elapsed()
t1.start()
# PSF estimation for given estimate of latent image and current observation
f = X.deconv(y_gpu, mode = 'lbfgsb', alpha = f_alpha, beta = f_beta,
maxfun = optiter, verbose = 10)
print "TIMER Optimization: ", t1.elapsed()
fs = f[0]
# Normalize PSF kernels to sum up to one
fs = gputools.normalize(fs)
# ------------------------------------------------------------------------
# Latent image estimation
# ------------------------------------------------------------------------
# Create OlaGPU instance with estimated PSF
t2.start()
F = olaGPU.OlaGPU(fs,sx,'valid',winaux=winaux)
# Latent image estimation by performing one gradient descent step
# multiplicative update is used which preserves positivity
factor_gpu = F.cnvtp(mask_gpu*y_gpu)/(F.cnvtp(mask_gpu*F.cnv(x_gpu))+tol)
gputools.cliplower_GPU(factor_gpu, tol)
x_gpu = x_gpu * factor_gpu
x_max = x_gpu.get()[sf[0]:-sf[0],sf[1]:-sf[1]].max()
gputools.clipupper_GPU(x_gpu, x_max)
print "TIMER GPU: ", t2.elapsed()
# ------------------------------------------------------------------------
# For backup intermediate results
# ------------------------------------------------------------------------
if backup or i == N:
# Write intermediate results to disk incl. input
y_img = y_gpu.get()*1e5
fitsTools.asinhScale(y_img, 450, -50, minCut=0.0, maxCut=40000, fname=yname(i))
# Crop image to input size
xi = (x_gpu.get()[sf2[0]:-sf2[0],sf2[1]:-sf2[1]] / x_max)*1e5
fitsTools.fitsStats(xi)
fitsTools.asinhScale(xi, 450, -50, minCut=0.0, maxCut=40000, fname=xname(i))
# Concatenate PSF kernels for ease of visualisation
f = imagetools.gridF(fs,csf)
f = f*1e5
fitsTools.asinhScale(f, 450, -50, minCut=0.0, maxCut=40000, fname=fname(i))
# ------------------------------------------------------------------------
# For displaying intermediate results
# ------------------------------------------------------------------------
'''
if np.mod(i,1) == 0 and doshow:
pl.figure(1)
pl.subplot(121)
# what is SY?
pl.imshow(imagetools.crop(x_gpu.get(),sy,np.ceil(sf/2)),'gray')
pl.title('x after %d observations' % i)
pl.subplot(122)
pl.imshow(y_gpu.get(),'gray')
pl.title('y(%d)' % i)
pl.draw()
pl.figure(2)
pl.title('PSF(%d)' % i)
imagetools.cellplot(fs, winaux.csf)
tf = t.clock()
print('Time elapsed after %d frames %.3f' % (i,(tf-ti)))
'''
tf = t.clock()
print('Time elapsed for total image sequence %.3f' % (tf-ti))
# ----------------------------------------------------------------------------
print "TOTAL: %.3f" % (t4.elapsed())
print "OptimizeCPUtime %.3f %.3f" % (t1.getTotal(), 100*(t1.getTotal()/t4.getTotal()))
print "GPUtime %.3f %.3f" % (t2.getTotal(), 100*(t2.getTotal()/t4.getTotal()))
print "LoadTime %.3f %.3f" % (t3.getTotal(), 100*(t3.getTotal()/t4.getTotal()))
if __name__ == "__main__":
optparser = OptionParser()
optparser.add_option("-s","--doShow", action="store_true", dest="doShow", default=False, help="show output at every timestep (Default: False)")
optparser.add_option("-b","--backup", action="store_true", dest="backup", default=False, help="write intermediate results to disk (Default: False)")
optparser.add_option("-l","--log", action="store_true", dest="log", default=False, help="write log to file (Default: False)")
optparser.add_option("-o","--overwrite", action="store_true", dest="overwrite", default=False, help="overwrite existing results (Default: False)")
optparser.add_option("-n","--nFrames", dest="N", default=100, type="int", help="Number of frames to process (Default: 100)")
optparser.add_option("-a","--nAveFrames", dest="N0", default=20, type="int", help="Number of frames averaged for initialization (Default: 20)")
optparser.add_option("--f_alpha", dest="f_alpha", default=0.0, type="float", help="promotes smoothness (Default: 0.0)")
optparser.add_option("--f_beta", dest="f_beta", default=0.1, type="float", help="Thikhonov regularization (Default: 0.1)")
optparser.add_option("--optiter", dest="optiter", default=50, type="float", help="number of iterations (Default: 50)")
optparser.add_option("--tol", dest="tol", default=1e-10, type="float", help="tolerance for when to stop minimization (Default: 1e-10)")
(opts,args) = optparser.parse_args()
print "Set Parameters:", opts, args, "\n"
process(opts)