-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtf_utils_subspace.py
More file actions
154 lines (106 loc) · 4.16 KB
/
tf_utils_subspace.py
File metadata and controls
154 lines (106 loc) · 4.16 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
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
import tensorflow as tf
import math
import os
import parser_ops
import UnrollNet_subspace
parser = parser_ops.get_parser()
args = parser.parse_args()
def test_graph(directory):
"""
This function creates a test graph for testing
"""
tf.reset_default_graph()
sens_mapsP = tf.placeholder(tf.complex64, shape=(None, args.ncoil_GLOB, args.nrow_GLOB, args.ncol_GLOB), name='sens_maps')
basisP = tf.placeholder(tf.float32, shape=(None, args.netl_GLOB * args.necho_GLOB, args.nbasis_GLOB), name='basis')
stkP = tf.placeholder(tf.float32, shape=(None, args.nrow_GLOB, args.ncol_GLOB, 1, args.nbasis_GLOB, args.nbasis_GLOB), name='stk')
trn_maskP = tf.placeholder(tf.complex64, shape=(None, args.nrow_GLOB, args.ncol_GLOB, args.netl_GLOB * args.necho_GLOB), name='trn_mask')
loss_maskP = tf.placeholder(tf.complex64, shape=(None, args.nrow_GLOB, args.ncol_GLOB, args.netl_GLOB * args.necho_GLOB), name='loss_mask')
nw_inputP = tf.placeholder(tf.float32, shape=(None, args.nrow_GLOB, args.ncol_GLOB, args.nbasis_GLOB * 2), name='nw_input')
nw_output, nw_kspace_output, x0, all_intermediate_outputs, mu = \
UnrollNet_subspace.UnrolledNet(nw_inputP, sens_mapsP, basisP, stkP, trn_maskP, loss_maskP).model
nw_output = tf.identity(nw_output, name='nw_output')
nw_kspace_output = tf.identity(nw_kspace_output, name='nw_kspace_output')
all_intermediate_outputs = tf.identity(all_intermediate_outputs, name='all_intermediate_outputs')
x0 = tf.identity(x0, name='x0')
mu = tf.identity(mu, name='mu')
saver = tf.train.Saver()
sess_test_filename = os.path.join(directory, 'model_test')
with tf.Session(config=tf.ConfigProto()) as sess:
sess.run(tf.global_variables_initializer())
saved_test_model = saver.save(sess, sess_test_filename, latest_filename='checkpoint_test')
print('\n Test graph is generated and saved at: ' + saved_test_model)
return True
def tf_complex2real(input_data):
"""
Parameters
----------
input_data : nrow x ncol.
Returns
-------
outputs concatenated real and imaginary parts as nrow x ncol x 2
"""
return tf.stack([tf.real(input_data), tf.imag(input_data)], axis=-1)
def tf_real2complex(input_data):
"""
Parameters
----------
input_data : nrow x ncol x 2
Returns
-------
merges concatenated channels and outputs complex image of size nrow x ncol.
"""
return tf.complex(input_data[..., 0], input_data[..., 1])
def tf_fftshift_flip2D(input_data, axes=1):
"""
Parameters
----------
input_data : ncoil x nrow x ncol
axes : The default is 1.
------
"""
nx = math.ceil(args.nrow_GLOB / 2)
ny = math.ceil(args.ncol_GLOB / 2)
if axes == 1:
first_half = tf.identity(input_data[:, :nx, :])
second_half = tf.identity(input_data[:, nx:, :])
elif axes == 2:
first_half = tf.identity(input_data[:, :, :ny])
second_half = tf.identity(input_data[:, :, ny:])
else:
raise ValueError('Invalid axes for fftshift')
return tf.concat([second_half, first_half], axis=axes)
def tf_ifftshift_flip2D(input_data, axes=1):
"""
Parameters
----------
input_data : ncoil x nrow x ncol
axes : The default is 1.
------
"""
nx = math.floor(args.nrow_GLOB / 2)
ny = math.floor(args.ncol_GLOB / 2)
if axes == 1:
first_half = tf.identity(input_data[:, :nx, :])
second_half = tf.identity(input_data[:, nx:, :])
elif axes == 2:
first_half = tf.identity(input_data[:, :, :ny])
second_half = tf.identity(input_data[:, :, ny:])
else:
raise ValueError('Invalid axes for ifftshift')
return tf.concat([second_half, first_half], axis=axes)
def tf_fftshift(input_x, axes=1):
"""
Parameters
----------
input_x : ncoil x nrow x ncol
axes : The default is 1.
"""
return tf_fftshift_flip2D(tf_fftshift_flip2D(input_x, axes=1), axes=2)
def tf_ifftshift(input_x, axes=1):
"""
Parameters
----------
input_x : ncoil x nrow x ncol
axes : The default is 1.
"""
return tf_ifftshift_flip2D(tf_ifftshift_flip2D(input_x, axes=1), axes=2)