-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.py
471 lines (380 loc) · 17 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
file : main.py
author: Xiaohan Chen
email : [email protected]
last_modified: 2018-10-13
Main script. Start running model from main.py.
"""
import os , sys
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # BE QUIET!!!!
# timing
import time
from datetime import timedelta
from config import get_config
import utils.prob as problem
import utils.data as data
import utils.train as train
import numpy as np
import tensorflow as tf
try :
from PIL import Image
from sklearn.feature_extraction.image \
import extract_patches_2d, reconstruct_from_patches_2d
except Exception as e :
pass
def imread_CS_py(im_fn, patch_size, stride):
im_org = np.array (Image.open (im_fn), dtype='float32')
H, W = im_org.shape
num_rpatch = (H - patch_size + stride - 1) // stride + 1
num_cpatch = (W - patch_size + stride - 1) // stride + 1
H_pad = patch_size + (num_rpatch - 1) * stride
W_pad = patch_size + (num_cpatch - 1) * stride
im_pad = np.zeros ((H_pad, W_pad), dtype=np.float32)
im_pad [:H, :W] = im_org
return im_org, H, W, im_pad, H_pad, W_pad
def img2col_py(im_pad, patch_size, stride):
[H, W] = im_pad.shape
num_rpatch = (H - patch_size) / stride + 1
num_cpatch = (W - patch_size) / stride + 1
num_patches = int (num_rpatch * num_cpatch)
img_col = np.zeros ([patch_size**2, num_patches])
count = 0
for x in range(0, H-patch_size+1, stride):
for y in range(0, W-patch_size+1, stride):
img_col[:, count] = im_pad[x:x+patch_size, y:y+patch_size].reshape([-1])
count = count + 1
return img_col
def col2im_CS_py(X_col, patch_size, stride, H, W, H_pad, W_pad):
X0_rec = np.zeros ((H_pad, W_pad))
counts = np.zeros ((H_pad, W_pad))
k = 0
for x in range(0, H_pad-patch_size+1, stride):
for y in range(0, W_pad-patch_size+1, stride):
X0_rec[x:x+patch_size, y:y+patch_size] += X_col[:,k].\
reshape([patch_size, patch_size])
counts[x:x+patch_size, y:y+patch_size] += 1
k = k + 1
X0_rec /= counts
X_rec = X0_rec[:H, :W]
return X_rec
def setup_model (config , **kwargs) :
untiedf = 'u' if config.untied else 't'
coordf = 'c' if config.coord else 's'
"""LISTA"""
if config.net == 'LISTA' :
config.model = ("LISTA_T{T}_lam{lam}_{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, untiedf=untiedf,
coordf=coordf, exp_id=config.exp_id))
from models.LISTA import LISTA
model = LISTA (kwargs ['A'], T=config.T, lam=config.lam,
untied=config.untied, coord=config.coord,
scope=config.scope)
"""LAMP"""
if config.net == 'LAMP' :
config.model = ("LAMP_T{T}_lam{lam}_{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, untiedf=untiedf,
coordf=coordf, exp_id=config.exp_id))
from models.LAMP import LAMP
model = LAMP (kwargs ['A'], T=config.T, lam=config.lam,
untied=config.untied, coord=config.coord,
scope=config.scope)
"""LIHT"""
if config.net == 'LIHT' :
from models.LIHT import LIHT
model = LIHT (p, T=config.T, lam=config.lam, y_=p.y_ , x0_=None ,
untied=config.untied , cord=config.coord)
"""LISTA-CP"""
if config.net == 'LISTA_cp' :
config.model = ("LISTA_cp_T{T}_lam{lam}_{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, untiedf=untiedf,
coordf=coordf, exp_id=config.exp_id))
from models.LISTA_cp import LISTA_cp
model = LISTA_cp (kwargs ['A'], T=config.T, lam=config.lam,
untied=config.untied, coord=config.coord,
scope=config.scope)
"""LISTA-SS"""
if config.net == 'LISTA_ss' :
config.model = ("LISTA_ss_T{T}_lam{lam}_p{p}_mp{mp}_"
"{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, p=config.percent,
mp=config.max_percent, untiedf=untiedf,
coordf=coordf, exp_id=config.exp_id))
from models.LISTA_ss import LISTA_ss
model = LISTA_ss (kwargs ['A'], T=config.T, lam=config.lam,
percent=config.percent, max_percent=config.max_percent,
untied=config.untied , coord=config.coord,
scope=config.scope)
"""LISTA-CPSS"""
if config.net == 'LISTA_cpss' :
config.model = ("LISTA_cpss_T{T}_lam{lam}_p{p}_mp{mp}_"
"{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, p=config.percent,
mp=config.max_percent, untiedf=untiedf,
coordf=coordf, exp_id=config.exp_id))
from models.LISTA_cpss import LISTA_cpss
model = LISTA_cpss (kwargs ['A'], T=config.T, lam=config.lam,
percent=config.percent, max_percent=config.max_percent,
untied=config.untied , coord=config.coord,
scope=config.scope)
"""LISTA-CS"""
if config.net == 'LISTA_cs' :
config.model = ("LISTA_cs_T{T}_lam{lam}_llam{llam}_"
"{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, llam=config.lasso_lam,
untiedf=untiedf, coordf=coordf,
exp_id=config.exp_id))
from models.LISTA_cs import LISTA_cs
model = LISTA_cs (kwargs ['Phi'], kwargs ['D'], T=config.T,
lam=config.lam, untied=config.untied,
coord=config.coord, scope=config.scope)
"""LISTA-SS-CS"""
if config.net == 'LISTA_ss_cs' :
config.model = ("LISTA_ss_cs_T{T}_lam{lam}_p{p}_mp{mp}_llam{llam}_"
"{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, p=config.percent,
mp=config.max_percent, llam=config.lasso_lam,
untiedf=untiedf, coordf=coordf,
exp_id=config.exp_id))
from models.LISTA_ss_cs import LISTA_ss_cs
model = LISTA_ss_cs (kwargs ['Phi'], kwargs ['D'], T=config.T,
lam=config.lam, percent=config.percent,
max_percent=config.max_percent,
untied=config.untied, coord=config.coord,
scope=config.scope)
"""LISTA-CPSS-CS"""
if config.net == 'LISTA_cpss_cs' :
config.model = ("LISTA_cpss_cs_T{T}_lam{lam}_p{p}_mp{mp}_llam{llam}_"
"{untiedf}_{coordf}_{exp_id}"
.format (T=config.T, lam=config.lam, p=config.percent,
mp=config.max_percent, llam=config.lasso_lam,
untiedf=untiedf, coordf=coordf,
exp_id=config.exp_id))
from models.LISTA_cpss_cs import LISTA_cpss_cs
model = LISTA_cpss_cs (kwargs ['Phi'], kwargs ['D'], T=config.T,
lam=config.lam, percent=config.percent,
max_percent=config.max_percent,
untied=config.untied, coord=config.coord,
scope=config.scope)
config.modelfn = os.path.join (config.expbase, config.model)
config.resfn = os.path.join (config.resbase, config.model)
print ("model disc:", config.model)
return model
############################################################
###################### Training #######################
############################################################
def run_train (config) :
if config.task_type == 'sc':
run_sc_train (config)
elif config.task_type == 'cs':
run_cs_train (config)
def run_sc_train (config) :
"""Load problem."""
if not os.path.exists (config.probfn):
raise ValueError ("Problem file not found.")
else:
p = problem.load_problem (config.probfn)
"""Set up model."""
model = setup_model (config, A=p.A)
"""Set up input."""
config.SNR = np.inf if config.SNR == 'inf' else float (config.SNR)
y_, x_, y_val_, x_val_ = (
train.setup_input_sc (
config.test, p, config.tbs, config.vbs, config.fixval,
config.supp_prob, config.SNR, config.magdist, **config.distargs))
"""Set up training."""
stages = train.setup_sc_training (
model, y_, x_, y_val_, x_val_, None,
config.init_lr, config.decay_rate, config.lr_decay)
tfconfig = tf.ConfigProto (allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
with tf.Session (config=tfconfig) as sess:
# graph initialization
sess.run (tf.global_variables_initializer ())
# start timer
start = time.time ()
# train model
model.do_training (sess, stages, config.modelfn, config.scope,
config.val_step, config.maxit, config.better_wait)
# end timer
end = time.time ()
elapsed = end - start
print ("elapsed time of training = " + str (timedelta (seconds=elapsed)))
def run_cs_train (config) :
"""Load dictionary and sensing matrix."""
Phi = np.load (config.sensing) ['A']
D = np.load (config.dict)
"""Set up model."""
model = setup_model (config, Phi=Phi, D=D)
"""Set up inputs."""
y_, f_, y_val_, f_val_ = train.setup_input_cs (config.train_file,
config.val_file,
config.tbs, config.vbs)
"""Set up training."""
stages = train.setup_cs_training (
model, y_, f_, y_val_, f_val_, None, config.init_lr, config.decay_rate,
config.lr_decay, config.lasso_lam)
"""Start training."""
tfconfig = tf.ConfigProto (allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
with tf.Session (config=tfconfig) as sess:
# graph initialization
sess.run (tf.global_variables_initializer ())
# start timer
start = time.time ()
# train model
model.do_training (sess, stages, config.modelfn, config.scope,
config.val_step, config.maxit, config.better_wait)
# end timer
end = time.time ()
elapsed = end - start
print ("elapsed time of training = " + str (timedelta (seconds=elapsed)))
############################################################
###################### Testing ########################
############################################################
def run_test (config):
if config.task_type == 'sc':
run_sc_test (config)
elif config.task_type == 'cs':
run_cs_test (config)
def run_sc_test (config) :
"""
Test model.
"""
"""Load problem."""
if not os.path.exists (config.probfn):
raise ValueError ("Problem file not found.")
else:
p = problem.load_problem (config.probfn)
"""Load testing data."""
xt = np.load (config.xtest)
"""Set up input for testing."""
config.SNR = np.inf if config.SNR == 'inf' else float (config.SNR)
input_, label_ = (
train.setup_input_sc (config.test, p, xt.shape [1], None, False,
config.supp_prob, config.SNR,
config.magdist, **config.distargs))
"""Set up model."""
model = setup_model (config , A=p.A)
xhs_ = model.inference (input_, None, False)
"""Create session and initialize the graph."""
tfconfig = tf.ConfigProto (allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
with tf.Session (config=tfconfig) as sess:
# graph initialization
sess.run (tf.global_variables_initializer ())
# load model
model.load_trainable_variables (sess , config.modelfn)
nmse_denom = np.sum (np.square (xt))
supp_gt = xt != 0
lnmse = []
lspar = []
lsperr = []
lflspo = []
lflsne = []
# test model
for xh_ in xhs_ :
xh = sess.run (xh_ , feed_dict={label_:xt})
# nmse:
loss = np.sum (np.square (xh - xt))
nmse_dB = 10.0 * np.log10 (loss / nmse_denom)
print (nmse_dB)
lnmse.append (nmse_dB)
supp = xh != 0.0
# intermediate sparsity
spar = np.sum (supp , axis=0)
lspar.append (spar)
# support error
sperr = np.logical_xor(supp, supp_gt)
lsperr.append (np.sum (sperr , axis=0))
# false positive
flspo = np.logical_and (supp , np.logical_not (supp_gt))
lflspo.append (np.sum (flspo , axis=0))
# false negative
flsne = np.logical_and (supp_gt , np.logical_not (supp))
lflsne.append (np.sum (flsne , axis=0))
res = dict (nmse=np.asarray (lnmse),
spar=np.asarray (lspar),
sperr=np.asarray (lsperr),
flspo=np.asarray (lflspo),
flsne=np.asarray (lflsne))
np.savez (config.resfn , **res)
# end of test
def run_cs_test (config) :
"""Load dictionary and sensing matrix."""
Phi = np.load (config.sensing) ['A']
D = np.load (config.dict)
# loading compressive sensing settings
M = Phi.shape [0]
F = Phi.shape [1]
N = D.shape [1]
assert M == config.M and F == config.F and N == config.N
patch_size = int (np.sqrt (F))
assert patch_size ** 2 == F
"""Set up model."""
model = setup_model (config, Phi=Phi, D=D)
"""Inference."""
y_ = tf.placeholder (shape=(M, None), dtype=tf.float32)
_, fhs_ = model.inference (y_, None)
"""Start testing."""
tfconfig = tf.ConfigProto (allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
with tf.Session (config=tfconfig) as sess:
# graph initialization
sess.run (tf.global_variables_initializer ())
# load model
model.load_trainable_variables (sess , config.modelfn)
# calculate average NMSE and PSRN on test images
test_dir = './data/test_images/'
test_files = os.listdir (test_dir)
avg_nmse = 0.0
avg_psnr = 0.0
overlap = 0
stride = patch_size - overlap
if 'joint' in config.net :
D = sess.run (model.D_)
for test_fn in test_files :
# read in image
test_fn = os.path.join (test_dir, test_fn)
test_im, H, W, test_im_pad, H_pad, W_pad = \
imread_CS_py (test_fn, patch_size, stride)
test_fs = img2col_py (test_im_pad, patch_size, stride)
# remove dc from features
test_dc = np.mean (test_fs, axis=0, keepdims=True)
test_cfs = test_fs - test_dc
test_cfs = np.asarray (test_cfs) / 255.0
# sensing signals
test_ys = np.matmul (Phi, test_cfs)
num_patch = test_ys.shape [1]
rec_cfs = sess.run (fhs_ [-1], feed_dict={y_: test_ys})
print (rec_cfs.shape)
rec_fs = rec_cfs * 255.0 + test_dc
# patch-level NMSE
patch_err = np.sum (np.square (rec_fs - test_fs))
patch_denom = np.sum (np.square (test_fs))
avg_nmse += 10.0 * np.log10 (patch_err / patch_denom)
rec_im = col2im_CS_py (rec_fs, patch_size, stride,
H, W, H_pad, W_pad)
# image-level PSNR
image_mse = np.mean (np.square (rec_im - test_im))
avg_psnr += 10.0 * np.log10 (255.**2 / image_mse)
num_test_ims = len (test_files)
print ('Average Patch-level NMSE is {}'.format (avg_nmse / num_test_ims))
print ('Average Image-level PSNR is {}'.format (avg_psnr / num_test_ims))
# end of cs_testing
############################################################
####################### Main #########################
############################################################
def main ():
# parse configuration
config, _ = get_config()
# set visible GPUs
os.environ['CUDA_VISIBLE_DEVICES'] = config.gpu
if config.test:
run_test (config)
else:
run_train (config)
# end of main
if __name__ == "__main__":
main ()