/
Framework.py
313 lines (275 loc) · 12.5 KB
/
Framework.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
import numpy as np
import os
import platform
import h5py
import odl
from abc import ABC, abstractmethod
import tensorflow as tf
from Operators import Load_PAT2D_data as PATdata, fastPAT_withAdjoint as fpat
# abstract class to wrap up the occuring operators in numpy. Can be turned into odl operator using as_odl_operator
class np_operator(ABC):
linear = True
def __init__(self, input_dim, output_dim):
self.input_dim = input_dim
self.output_dim = output_dim
@abstractmethod
def evaluate(self, y):
pass
@abstractmethod
def differentiate(self, point, direction):
pass
#### methods to turn numpy operator into corresponding odl operator
class deriv_op_adj(odl.Operator):
def __init__(self, inp_sp, out_sp, np_model, point):
self.linear=np_model.linear
self.model = np_model
self.point = point
self.out_sp = out_sp
super(deriv_op_adj, self).__init__(inp_sp, out_sp, linear=True)
def _call(self, x):
der = self.model.differentiate(self.point, x)
return self.out_sp.element(der)
@property
def adjoint(self):
if not self.linear:
raise TypeError('Non linear operators do not admit adjoint')
else:
return as_odl_operator(self.model)
class deriv_op(odl.Operator):
def __init__(self, inp_sp, out_sp, np_model, point):
self.model = np_model
self.point = point
self.inp_sp = inp_sp
self.out_sp = out_sp
super(deriv_op, self).__init__(inp_sp, out_sp, linear=True)
def _call(self, x):
return self.out_sp.element(self.model.evaluate(x))
@property
def adjoint(self):
return deriv_op_adj(inp_sp=self.out_sp, out_sp=self.inp_sp, np_model=self.model, point=self.point)
class as_odl_operator(odl.Operator):
def _call(self, x):
return self.output_space.element(self.np_model.evaluate(x))
def derivative(self, point):
return deriv_op(inp_sp=self.input_space, out_sp=self.output_space, np_model=self.np_model, point=point)
def __init__(self, np_model):
self.linear = np_model.linear
self.np_model = np_model
idim = np_model.input_dim
left = int(idim[0] / 2)
right = int(idim[1] / 2)
self.input_space = odl.uniform_discr([-left, -right], [left, right], [idim[0], idim[1]],
dtype='float32')
odim = np_model.output_dim
left = int(odim[0] / 2)
right = int(odim[1] / 2)
self.output_space = odl.uniform_discr([-left, -right], [left, right], [odim[0], odim[1]],
dtype='float32')
super(as_odl_operator, self).__init__(self.input_space, self.output_space)
@property
def adjoint(self):
if not self.linear:
raise TypeError('Non linear operators do not admit adjoint')
else:
return deriv_op_adj(inp_sp=self.input_space, out_sp=self.output_space, np_model=self.np_model, point=0)
# the approximated PAT operator as np_operator
class approx_PAT_operator(np_operator):
def __init__(self, PAT_OP, input_dim, output_dim):
self.PAT_OP = PAT_OP
super(approx_PAT_operator, self).__init__(input_dim, output_dim)
def evaluate(self, y):
if len(y.shape) == 4:
res = np.zeros(shape=(y.shape[0], self.output_dim[0], self.output_dim[1], 1))
for k in range(y.shape[0]):
res[k,...,0] = self.PAT_OP.kspace_forward(y[k,...,0])
elif len(y.shape) == 2:
res = self.PAT_OP.kspace_forward(y)
else:
raise ValueError
return res
def differentiate(self, point, direction):
return self.PAT_OP.kspace_adjoint(direction)
def inverse(self, y):
if len(y.shape) == 4:
res = np.zeros(shape=(y.shape[0], self.input_dim[0], self.input_dim[1], 1))
for k in range(y.shape[0]):
res[k,...,0] = self.PAT_OP.kspace_inverse(y[k,...,0])
elif len(y.shape) == 2:
res = self.PAT_OP.kspace_inverse(y)
else:
raise ValueError
return res
class approx_PAT_matrix(np_operator):
def __init__(self, matrix_path, input_dim, output_dim):
fData = h5py.File(matrix_path, 'r')
inData = fData.get('Aapprox')
rows = inData.shape[0]
cols = inData.shape[1]
print(rows, cols)
self.m = np.matrix(inData)
self.input_sq = input_dim[0]*input_dim[1]
self.output_sq = output_dim[0]*output_dim[1]
super(approx_PAT_matrix, self).__init__(input_dim, output_dim)
# computes the matrix multiplication with matrix
def evaluate(self, y):
y = np.flipud(np.asarray(y))
if len(y.shape) == 4:
# res = np.zeros(shape=(y.shape[0], self.output_dim[0], self.output_dim[1], 1))
# for k in range(y.shape[0]):
# res[k,...,0] = np.reshape(np.matmul(self.m, np.reshape(y[k,...,0], self.input_sq)),
# [self.output_dim[0], self.output_dim[1]])
# return res
batch = y.shape[0]
flat = np.reshape(y, (batch, self.input_sq))
res = np.transpose(np.matmul(self.m, np.transpose(flat)))
return np.reshape(np.asarray(res), (batch, self.output_dim[0], self.output_dim[1], 1))
elif len(y.shape) == 2:
res = np.reshape(np.matmul(self.m, np.reshape(y, self.input_sq)), [self.output_dim[0], self.output_dim[1]])
else:
raise ValueError
return res
# matrix multiplication with the adjoint of the matrix
def differentiate(self, point, direction):
if len(direction.shape) == 4:
# res = np.zeros(shape=(direction.shape[0], self.input_dim[0], self.input_dim[1], 1))
# for k in range(direction.shape[0]):
# res[k,...,0] = np.flipud(np.reshape(np.matmul(np.transpose(self.m),
# np.reshape(np.asarray(direction[k,...,0]), self.output_sq)),
# [self.input_dim[0], self.input_dim[1]]))
batch = direction.shape[0]
inp = np.reshape(np.asarray(direction), (batch, self.output_sq))
res = np.transpose(np.matmul(np.transpose(self.m), np.transpose(inp)))
return np.flipud(np.reshape(np.asarray(res), [batch, self.input_dim[0], self.input_dim[1], 1]))
elif len(direction.shape) == 2:
res = np.flipud(np.reshape(np.matmul(np.transpose(self.m), np.reshape(np.asarray(direction), self.output_sq)),
[self.input_dim[0], self.input_dim[1]]))
else:
raise ValueError
return res
# Finds the inverse
def inverse(self, y):
if len(y.shape) == 4:
res = np.zeros(shape=(y.shape[0], self.input_dim[0], self.input_dim[1], 1))
for k in range(y.shape[0]):
inp = np.reshape(np.asarray(y[k, ...,0]), self.output_sq)
sol = np.linalg.solve(self.m, inp)
res[k, ..., 0] = np.flipud(np.reshape(sol, [self.input_dim[0], self.input_dim[1]]))
elif len(y.shape) == 2:
inp = np.reshape(np.asarray(y), self.output_sq)
sol = np.linalg.solve(self.m, inp)
res = np.flipud(np.reshape(sol, [self.input_dim[0], self.input_dim[1]]))
else:
raise ValueError
return res
# the exact PAT as numpy operator
class exact_PAT_operator(np_operator):
def __init__(self, matrix_path, input_dim, output_dim):
fData = h5py.File(matrix_path, 'r')
inData = fData.get('A')
rows = inData.shape[0]
cols = inData.shape[1]
print(rows, cols)
self.m = np.matrix(inData)
self.input_sq = input_dim[0]*input_dim[1]
self.output_sq = output_dim[0]*output_dim[1]
super(exact_PAT_operator, self).__init__(input_dim, output_dim)
# computes the matrix multiplication with matrix
def evaluate(self, y):
y = np.flipud(np.asarray(y))
if len(y.shape) == 4:
# res = np.zeros(shape=(y.shape[0], self.output_dim[0], self.output_dim[1], 1))
# for k in range(y.shape[0]):
# res[k,...,0] = np.reshape(np.matmul(self.m, np.reshape(y[k,...,0], self.input_sq)),
# [self.output_dim[0], self.output_dim[1]])
batch = y.shape[0]
flat = np.reshape(y, (batch, self.input_sq))
res = np.transpose(np.matmul(self.m, np.transpose(flat)))
return np.reshape(np.asarray(res), (batch, self.output_dim[0], self.output_dim[1], 1))
elif len(y.shape) == 2:
res = np.reshape(np.matmul(self.m, np.reshape(y, self.input_sq)), [self.output_dim[0], self.output_dim[1]])
else:
raise ValueError
return res
# matrix multiplication with the adjoint of the matrix
def differentiate(self, point, direction):
if len(direction.shape) == 4:
res = np.zeros(shape=(direction.shape[0], self.input_dim[0], self.input_dim[1], 1))
# for k in range(direction.shape[0]):
# # res[k,...,0] = np.flipud(np.reshape(np.matmul(np.transpose(self.m),
# # np.reshape(np.asarray(direction[k,...,0]), self.output_sq)),
# # [self.input_dim[0], self.input_dim[1]]))
batch = direction.shape[0]
inp = np.reshape(np.asarray(direction), (batch, self.output_sq))
res = np.transpose(np.matmul(np.transpose(self.m), np.transpose(inp)))
return np.flipud(np.reshape(np.asarray(res), [batch, self.input_dim[0], self.input_dim[1], 1]))
elif len(direction.shape) == 2:
res = np.flipud(np.reshape(np.matmul(np.transpose(self.m), np.reshape(np.asarray(direction), self.output_sq)),
[self.input_dim[0], self.input_dim[1]]))
else:
raise ValueError
return res
# The model correction operator as numpy operator
class model_correction(np_operator):
linear = False
# categorizes experiments
# makes sure the folders needed for saving the model and logging data are in place
def generate_folders(self, path):
paths = {}
paths['Saves Folder'] = path + 'Data'
paths['Logging Folder'] = path + 'Logs'
for key, value in paths.items():
if not os.path.exists(value):
try:
os.makedirs(value)
except OSError:
pass
print(key + ' created')
# save current model to data
def save(self):
saver = tf.train.Saver()
saver.save(self.sess, self.path+'Data/model', global_step=self.global_step)
print('Progress saved')
# load model from data
def load(self, savepoint=None):
saver = tf.train.Saver()
if os.listdir(self.path+'Data/'):
print(self.path)
if savepoint is None:
saver.restore(self.sess, tf.train.latest_checkpoint(self.path+'Data/'))
else:
saver.restore(self.sess, self.path + 'Data/' + savepoint)
print(f'Restored Savepoint {savepoint}')
print('Save restored')
else:
print('No save found')
# clears computational graph
def end(self):
tf.reset_default_graph()
self.sess.close()
@abstractmethod
def get_network(self, channels):
pass
@staticmethod
# puts numpy array in form that can be fed into the graph
def feedable_format(array):
dim = len(array.shape)
changed = False
if dim == 2:
array = np.expand_dims(np.expand_dims(array, axis=0), axis=-1)
changed = True
elif not dim == 4:
raise ValueError
return array, changed
def __init__(self, path, data_sets, experiment_name='default_experiment'):
self.experiment_name = experiment_name
self.image_size = (64,64)
self.measurement_size = (64,64)
super(model_correction, self).__init__(self.measurement_size, self.measurement_size)
self.data_sets = data_sets
self.raw_path = path
self.path = path+self.experiment_name+'/'
self.generate_folders(self.path)
self.UNet = self.get_network(channels=1)
# start tensorflow sesssion
self.sess = tf.InteractiveSession()
self.global_step = tf.Variable(0, name='global_step', trainable=False)