-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_demo.py
More file actions
76 lines (63 loc) · 2.28 KB
/
run_demo.py
File metadata and controls
76 lines (63 loc) · 2.28 KB
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
import numpy as np
import os
from numpy import fft
import torch
from skimage.metrics import structural_similarity as compute_ssim
from scipy.io import loadmat
import utils
import model
import time
import scipy.io as sio
from skimage.io import imsave
import random
os.environ["CUDA_VISIBLE_DEVICES"] = '1'
DEVICE = torch.device('cuda:{}'.format(str(0) if torch.cuda.is_available() else 'cpu'))
## Seed setting
seed_value = 3407
np.random.seed(seed_value)
random.seed(seed_value)
os.environ['PYTHONHASHSEED'] = str(seed_value)
torch.manual_seed(seed_value)
torch.cuda.manual_seed(seed_value)
torch.cuda.manual_seed_all(seed_value)
torch.backends.cudnn.deterministic = True
## Load fully sampled k-space data
fpath ='./data/brain.mat'
f = sio.loadmat(fpath)
data_cpl = f['Y'][:]
Nchl,Nrd,Npe = data_cpl.shape
## Load unersampling mask
maskpath = './mask/mask.mat'
mask = sio.loadmat(maskpath)
SamMask = mask['mask'][:]
## Set the path of saving results
outpath = './results'
if not os.path.exists(outpath):
os.mkdir(outpath)
## Parameter settings
w0 = 20
lamda = 0.5
fn=lambda x: utils.normalize01(np.abs(x))
## Calculate the sum-of-square ground truth image
img_all = fft.fftshift(fft.ifft2(fft.fftshift(data_cpl,axes=(-1,-2)),axes=(-1,-2)),axes=(-1,-2))
gt = np.sqrt(np.sum(np.abs(img_all)**2,0))
## Perform undersampling k-space
tstKsp = data_cpl.transpose(1,2,0)
tstDsKsp = tstKsp*SamMask
## Normalize the undersampled k-space
zf_coil_img = fft.ifft2(tstDsKsp,axes=(0,1))
NormFactor = np.max(np.sqrt(np.sum(np.abs(zf_coil_img)**2,axis=2)))
tstDsKsp = tstDsKsp/NormFactor
## Reconstruct the MR image
time_all_start = time.time()
pre_img, pre_img_dc, pre_ksp = model.Recon(tstDsKsp,SamMask,DEVICE,w0=w0,TV_weight=lamda,PolyOrder=15,MaxIter=1000,LrImg = 1e-4)
normOrg = fn(gt)
normRec = fn(pre_img_dc)
# Note that the psnr and ssim here are computed on the whole image including the background region.
# This is different from the results reported in the paper.
psnrRec = utils.myPSNR(normOrg,normRec)
ssimRec = compute_ssim(normRec,normOrg,data_range=1,gaussian_weights=True)
print('{1:.4f} {0:.3f}'.format(psnrRec,ssimRec))
## Save the results
imsave(outpath + '/' + 'gt.png',normOrg)
imsave(outpath + '/' + 'recon.png',normRec)