-
Notifications
You must be signed in to change notification settings - Fork 2
/
inferenceSimulated.py
408 lines (313 loc) · 16 KB
/
inferenceSimulated.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
import torch
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import torch.nn.functional as F
from data import *
from reconAlgos import *
from modelClasses import *
from trainerClasses import *
import gc
import time
import copy
# Settings for inference:
gpuNo = 0 # use GPU ID
pSNRval = 10 # simulation pSNR
initializationTo = 1 # initialize ADMM to least squares input
# 0: zeros
# 1: least squares input
# 2: Regularized least squares input
# run inference for these techniques:
# L1, TV, L1_TV are the conventional hand-crafted regularizers.
# other ones include folder names under "training/denoiser/", "training/dcDenoiser/", "training/deqmpi/", "training/admld", "training/deqmpi"
# after "+" one can include l1, tv for a linear combination of plug-and-play and l1 and/or tv.
descriptorsHere = [\
"L1",\
"TV",\
"L1_TV",\
"ppmpi_lr_0.001_wd_0_bs_64_mxNs_0.1_fixNs_1_data_mnNs_0_nF12_nB4_lieb4_gr12_rMn0.5_1.0+ ",\
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_10.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_15.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_20.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_25.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_30.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_35.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
"DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_40.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
]
descriptorsLD = [\
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_10.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_15.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_20.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_25.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_30.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_35.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
# "DeqMPI_1D_ds_lr_0.001_wd_0.0_bs_64_pSNR_40.0_fixNs_1_Nit_5_nF12_nB4_lieb4_gr12_lmb100.0_rMn0.5_1.0_mtx_pMatinHouse.mat_svd_250_LnF_8_LnB_1_nN_1_cr_0", \
]
nbOfSingulars = 220
inverseCrime = True
inhouseLoadStr = "inhouseData/expMatinHouse"
# inhousePhantomLoadStr = "inhouseData/expPhantominHouse.mat"
torch.cuda.set_device(gpuNo)
print(torch.cuda.get_device_name(gpuNo))
n1 = 26
n2 = 13
sysMtxRef = loadMtxExp(inhouseLoadStr).reshape(-1, n1 * n2)
if inverseCrime: # bicubic upsampling followed by downsampling
interpolater = loadmat('interpExp2.mat')['interpolater']
sysMtxHRint2 = sysMtxRef @ torch.from_numpy(interpolater).float().cuda()
dataGenMtx = sysMtxHRint2
sysMtx = F.avg_pool2d(sysMtxHRint2.reshape(sysMtxRef.shape[0], 2 * n1, 2 * n2), 2).reshape(sysMtxRef.shape[0], -1)
else:
dataGenMtx = sysMtxRef
sysMtx = sysMtxRef
U, S, Vh = torch.linalg.svd(
sysMtx, full_matrices=False)
nbSvd = nbOfSingulars
U_ = U[:, :nbSvd]
S_ = S[:nbSvd]
Vh_ = Vh[:nbSvd, :]
V_ = Vh_.T
# B = U_.T @ sysMtx
theSys = U_.T @ sysMtx
# Load Phantom Data
# loadPreProcessed = 0
testDataHR = (MRAdatasetH5NoScale("datasets/testPatches.h5", prefetch=True)).data
testData = transformDataset(testDataHR, [26, 13], [0.5, 1], [0, 0])
underlyingImage = testData[:,:,:,:].squeeze()
myDataNsless = getNoisyData(testData, 0, sysMtxRef)
myDataLRinvCR = getNoisyData(testData, 0, sysMtx)
stdScl = 0.41
stdVal = 10**(-pSNRval / 20) * stdScl# = std * (myDatNsless.shape[0:3]*)
# underlyingEpsilon = stdVal * (nbOfSingulars)**(1/2)
underlyingEpsilon = stdVal * (myDataNsless.shape[2])**(1/2)
myDataGen = getNoisyData(testData, stdVal, sysMtxRef)
print("Data without inverse crime SNR: ", 20 * torch.log10(torch.norm(myDataNsless) / torch.norm(myDataGen - myDataNsless)))
print("Data with inverse crime SNR: ", 20 * torch.log10(torch.norm(myDataLRinvCR) / torch.norm(myDataGen - myDataLRinvCR)))
datatC, lsqrInp = admmInputGenerator(myDataGen, U_, S_, V_, testData.shape)
Nbatch = datatC.shape[0]
sclScale = 1
imgSize = [n1, n2]
theWholeSize = ((-1, 1, *imgSize))
outImgs = list()
outDiags = list()
outMaxs = list()
outNetworkNorms = list()
outNetworkNrmses = list()
outPsnrs = list()
outHfens = list()
outL1Obj = list()
outTVobj = list()
outCritobj = list()
outX1 = list()
outX2 = list()
outName = list()
inPsnrs = list()
outNumels = list()
pSNRexp = 15 # 23
U, S, V = U_.clone(), S_.clone(), V_.clone()
simMtx2 = sysMtx
AtC = simMtx2.reshape(-1, n1*n2)
if underlyingEpsilon is not None:
epsilonVal = underlyingEpsilon
else:
epsilonVal = torch.norm(datatC, dim=1) * 10**(-pSNRexp/20)
if isinstance(epsilonVal, float) or epsilonVal.numel() == 1:
epsilonVal = float(epsilonVal)
epsilon = epsilonVal
refVals = underlyingImage.reshape(Nbatch, -1)
print("Epsilon Value: ", epsilon)
print("Standard Deviation Value: ", epsilon / (datatC.numel())**(1/2))
datatC, lsqrInp = admmInputGenerator(myDataGen, U, S, V, theWholeSize)
datatC = datatC.reshape(Nbatch, -1)
if initializationTo == 0:
x_in = torch.zeros_like(underlyingImage)
elif initializationTo == 1:
x_in = lsqrInp.reshape(-1, n1, n2) #torch.linalg.lstsq(AtC, datatC.T).solution.T.reshape(-1, n1, n2)
else:
x_in = F.linear(F.linear(datatC, AtC.T), torch.inverse(
1 * torch.eye(n1 * n2).type_as(AtC.T) + AtC.T @ AtC)).reshape(-1, n1, n2)
inPsnrs.append(psnr(refVals, x_in.reshape(Nbatch, -1)))
updateStep = 1
verboseIn = 100
mu1 = 1
mu2 = 10
mu3 = 50
MaxIter = 200
theADMMclass = ADMMfncs(AtC, MaxIter, verboseIn, imgSize)
datatC = myDataGen.reshape(Nbatch, -1)
compMtx = AtC
Madmm = theADMMclass.MtC
descriptors = descriptorsHere
for i, descriptor in enumerate(descriptors):
if descriptor[:5] == 'ADMLD':
testMode = 4 # ADMLD
elif descriptor[:6] == 'DeqMPI':
testMode = 5 # DeqMPI
else:
testMode = 0 # plug & play
print(descriptor, "test Mode: ",testMode)
if testMode == 0: # plug & play
if not ("L1" in descriptor or "TV" in descriptor):
model = getModel(descriptor[:-3])
muScaleIter = 1
mu2Scale = 1
fnc1 = ppFnc2dnm(1/mu1, model, imgSize)
fnc2 = softTV(1/30, imgSize, 10)
fnc3 = softT(1/20)
fncUse = None
synCaller = False
if 'tv' in descriptors[i]:
fncUse = fnc2
elif 'l1' in descriptors[i]:
fncUse = fnc3
elif 'lS' in descriptors[i]:
fncUse = softTpos(1/20)
synCaller = True
if synCaller:
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj, x1, x2 = theADMMclass.ADMMreconDualSynthesis(
datatC, fnc1, fncUse, epsilon, refVals, mu2Scale=mu2Scale, muScaleIter=muScaleIter, x_in=x_in)
outX1.append(x1)
outX2.append(x2)
else:
if fncUse is None:
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.afterFnc(
datatC, fnc1, fnc1, 0, epsilon, refVals, mu2Scale=1, muScaleIter=muScaleIter, x_in=x_in)
else:
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.ADMMreconDual(
datatC, fnc1, fncUse, epsilon, refVals, mu2Scale=mu2Scale, muScaleIter=muScaleIter, x_in=x_in)
else:
muScaleIter = 70
if not "TV" in descriptor:
fnc3 = softT(1/(100 / 2))
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.afterFnc(
datatC, fnc3, fnc3, 0, epsilon, refVals, mu2Scale=1, muScaleIter=muScaleIter, x_in=x_in)
elif not "L1" in descriptor:
fnc2 = softTV(1/(500 / 2), imgSize, 10)
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.afterFnc(
datatC, fnc2, fnc2, 0, epsilon, refVals, mu2Scale=1, muScaleIter=muScaleIter, x_in=x_in)
else:
fnc3 = softTpos((1 - 0.1) / 10)
fnc2 = softTV(0.1 / 10, imgSize, 10)
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.ADMMreconDual(
datatC, fnc2, fnc3, epsilon, refVals, mu2Scale=1, muScaleIter=muScaleIter, x_in=x_in)
elif (testMode == 4) or (testMode == 5): # ADMM unrolled
if testMode == 4:
theMd, _ = getModelForADMMLD(descriptor)
elif testMode == 5:
numIter = 25
theMd, _ = getModelForImplicitLD(descriptor, numIter)
Vt = V_.T
if datatC.shape[1] == U_.shape[1]:
compData = datatC
else:
compData = F.linear(datatC.reshape(Nbatch, -1), U_.T)
theWholeSize = ((-1, 1, *imgSize))
x_rec = torch.zeros_like(refVals).reshape(theWholeSize)
valInp = F.linear(compData / (S_ + 1e-4), V_).reshape(theWholeSize)
batch_size_val = 256
iii = 0
compData = datatC
if (testMode == 5): # run
Nimg = n1 * n2
d0, d2 = torch.zeros_like(valInp).reshape(Nbatch, -1), torch.zeros_like(datatC)
theIn = torch.cat((valInp.reshape(Nbatch, -1), d0.reshape(Nbatch, -1), d2.reshape(Nbatch, -1)), dim = 1 )
while(iii < Nbatch - (Nbatch % batch_size_val)):
imgSize2 = (batch_size_val, *theWholeSize[1:])
theFixedPts = (compData[iii:iii+batch_size_val], compMtx, Madmm, epsilonVal, imgSize2)
x_rec[iii:iii+batch_size_val] = theMd(theIn[iii:iii+batch_size_val], theFixedPts)[:, :Nimg].reshape(imgSize2)
iii += batch_size_val
imgSize2 = (compData[iii:].shape[0], *theWholeSize[1:])
theFixedPts = (compData[iii:], compMtx, Madmm, epsilonVal, imgSize2)
x_rec[iii:] = theMd(theIn[iii:], theFixedPts)[:, :Nimg].reshape(imgSize2)
elif (testMode == 4):
while(iii < valInp.shape[0]-(valInp.shape[0] % batch_size_val)):
x_rec[iii:iii+batch_size_val, :, :, :] = theMd(compData[iii:iii+batch_size_val], compMtx, 1, valInp[iii:iii+batch_size_val], Madmm, epsilonVal)
iii += batch_size_val
x_rec[iii:] = theMd(compData[iii:], compMtx, 1, valInp[iii:], Madmm, epsilonVal)
x_rec = x_rec.reshape(Nbatch, -1)
if testMode > 0:
outDiag, outMax, outPsnr, outHfen, l1Obj, TVobj = theADMMclass.calculateDiagnoseVals(x_rec, refVals)
outDiag /= float(torch.norm(refVals))
outHfen /= theADMMclass.hfenFnc(refVals)
critObj = torch.sqrt(torch.sum((F.linear(x_rec, AtC) - datatC).reshape(Nbatch, -1).abs() ** 2, dim = 1)).detach().cpu().numpy() / epsilon
outImgs.append(x_rec)
outDiags.append(outDiag)
outMaxs.append(outMax)
outPsnrs.append(outPsnr)
outHfens.append(outHfen)
outL1Obj.append(l1Obj)
outTVobj.append(TVobj)
outCritobj.append(critObj)
if testMode == 0:
outNetworkNorms.append(inpOutNorms)
outNetworkNrmses.append(inpOutNrmses)
for i, descriptor in enumerate(descriptorsLD):
if descriptor[:5] == 'ADMLD':
testMode = 4 # ADMLD
elif descriptor[:6] == 'DeqMPI':
testMode = 5 # Implicit
print(descriptor, "test Mode: ",testMode)
if (testMode == 4) or (testMode == 5): # ADMM unrolled
if testMode == 4:
theMd, _ = getModelForADMMLD(descriptor)
elif testMode == 5:
numIter = 25
theMd2, _ = getModelForImplicitLD(descriptor, numIter)
theMd = theMd2.f
fnc1 = ppFnc2dnm(1, theMd.sharedNet, imgShape = imgSize)
fncLD = theMd.consistencyNet
x_rec, outDiag, outPsnr, outHfen, inpOutNorms, inpOutNrmses, outMax, l1Obj, TVobj, critObj = theADMMclass.afterFncLD(
datatC, fnc1, fnc1, 0, epsilon, refVals, mu2Scale=1, muScaleIter=1, consistencyFnc = fncLD, x_in=x_in)
x_rec = x_rec.reshape(Nbatch, -1)
outImgs.append(x_rec)
outDiags.append(outDiag)
outMaxs.append(outMax)
outPsnrs.append(outPsnr)
outHfens.append(outHfen)
outL1Obj.append(l1Obj)
outTVobj.append(TVobj)
outCritobj.append(critObj)
if testMode == 0:
outNetworkNorms.append(inpOutNorms)
outNetworkNrmses.append(inpOutNrmses)
if pSNRval < 10:
lambdaDeger = 10
elif pSNRval < 30:
lambdaDeger = 1
elif pSNRval < 40:
lambdaDeger = 0.1
else:
lambdaDeger = 0.01
maxIt = 10
xART, diagOutVals = ART(sysMtx, myDataGen.reshape(myDataGen.shape[0], -1), maxIter = maxIt, lambdaVal = lambdaDeger, order = None, energy = None, underlyingImage = underlyingImage)
def selFnc(x):
return x[0, 1:]
def selFnc(x):
if len(x.shape) > 1:
return x[:, 1:].mean(0)
elif len(x.shape) == 1:
return x.mean(0).repeat(MaxIter - 1)
else:
return x.repeat(MaxIter - 1)
namesOrdered = copy.deepcopy([*descriptors, *descriptorsLD])
for d, e, epsVls in zip(outPsnrs, namesOrdered, outCritobj):
dSel = selFnc(d)
dFin = dSel[-1]
dMax = dSel.max()
epsVl = round(selFnc(epsVls)[-1], 2)
print('Final pSNR: ', round(dFin, 2), ' max pSNR: ', round(dMax, 2), ', crit: ', epsVl ,', method: {0:60}'.format(e))
preProcessValues = lambda x: x.reshape(1, 3730, -1).repeat( 1 + 199 * (2 - len(x.shape)) , 2)
saveImgs = np.concatenate([indivImg.unsqueeze(0).cpu().numpy() for indivImg in outImgs])
saveDiags = np.concatenate([preProcessValues(indivImg) for indivImg in outDiags])
savePsnrs = np.concatenate([preProcessValues(indivImg) for indivImg in outPsnrs])
saveHfens = np.concatenate([preProcessValues(indivImg) for indivImg in outHfens])
saveL1Obj = np.concatenate([preProcessValues(indivImg) for indivImg in outL1Obj])
saveTVobj = np.concatenate([preProcessValues(indivImg) for indivImg in outTVobj])
saveCritobj = np.concatenate([preProcessValues(indivImg) for indivImg in outCritobj])
saveNetworkNorms = np.concatenate([preProcessValues(indivImg) for indivImg in outNetworkNorms])
saveNetworkNrmses = np.concatenate([preProcessValues(indivImg) for indivImg in outNetworkNrmses])
# saveMaxs = np.concatenate([preProcessValues(indivImg) for indivImg in outMaxs])
savemat('Simulated{0}dBresults.mat'.format(pSNRval), {'descriptors':[*descriptors, *descriptorsLD], 'saveImgs': saveImgs, 'saveDiags': saveDiags.astype(np.float32), 'savePsnrs': savePsnrs.astype(np.float32), 'saveHfens': saveHfens.astype(np.float32),
'saveL1Obj': saveL1Obj.astype(np.float32), 'saveTVobj': saveTVobj.astype(np.float32), 'saveCritobj': saveCritobj.astype(np.float32), 'saveNetworkNorms': saveNetworkNorms, 'saveNetworkNrmses': saveNetworkNrmses, 'xART': xART.cpu().detach().numpy(), 'ARTdiags': diagOutVals})
savemat('Simulated{0}dBresultsOnlyPsnr.mat'.format(pSNRval), {'descriptors':[*descriptors, *descriptorsLD], 'savePsnrs': savePsnrs.astype(np.float32), 'ARTdiags': diagOutVals})
savemat('Simulated{0}dBdata.mat'.format(pSNRval), {'myDataGen': myDataGen.cpu().detach().numpy(), 'testData': testData.cpu().detach().numpy()})