-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGaussPCC.py
158 lines (130 loc) · 6.41 KB
/
GaussPCC.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
##############################################################
#
# GaussPCC:
# Simple partial coherence correction module
# which assumes a Gaussian partial coherence
# function in 3D.
#
# Siddharth Maddali
# Argonne National Laboratory
# Oct 2019
# 6xlq96aeq@relay.firefox.com
#
##############################################################
import numpy as np
import tensorflow as tf
#tf.enable_eager_execution()
try:
from pyfftw.interfaces.numpy_fft import fftshift, fftn, ifftn
except:
from numpy.fft import fftshift, fftn, ifftn
from tqdm import tqdm
class Mixin: # inherited by Phaser module
def _ModProjectPC( self ):
self._cImage_f = tf.signal.fft3d( self._cImage )
self._patt = tf.signal.fft3d( tf.cast( tf.abs( self._cImage_f )**2, dtype=tf.complex64 ) )
self._pcoh_est = tf.sqrt( tf.cast( tf.abs( tf.signal.ifft3d( self._patt * self._kernel_f ) ), dtype=tf.complex64 ) )
self._cImage.assign(
tf.signal.ifft3d(
self._cImage_f * self._modulus / self._pcoh_est
)
)
return
def PCC( self, n_iterations, show_progress=False ):
self._pccSolver._setCoherentEstimate( ( tf.abs( tf.signal.fft3d( self._cImage ) )**2 ).numpy() )
self._pccSolver.Deblur( iterations=n_iterations, show_progress=show_progress )
self._pccSolver._setupAuxiliary()
self._pccSolver._updateBlurKernel()
self._kernel_f = self._pccSolver.getBlurKernel()
return
class PCSolver( tf.Module ):
def __init__( self, measured_intensity, gpack ):
self._shape = gpack[ 'array_shape' ]
self._modulus_measured = tf.constant( np.sqrt( measured_intensity ), dtype=tf.float32 )
pts = self._setupDomain( gpack=gpack )
self._setupConstants( pts )
self._setCoherentEstimate( np.absolute( fftn( gpack[ 'cImage' ] ) )**2 )
self._setupVariables()
self._setupAuxiliary()
self._updateBlurKernel()
self._setupOptimizer( learning_rate=0.01, momentum=0.99 )
return
def _setupOptimizer( self, learning_rate, momentum ):
self._optimizer = tf.optimizers.Adam( learning_rate=learning_rate )
return
def _setCoherentEstimate( self, intensity ):
self._cohEst_f = tf.constant( fftn( intensity ), dtype=tf.complex64 )
return
def getBlurKernel( self ):
self._updateBlurKernel()
return tf.constant( np.absolute( self._blurKernel_f.numpy() ), dtype=tf.complex64 )
def _setupDomain( self, gpack ):
x, y, z = tuple( fftshift( this ) for this in np.meshgrid( *[ np.arange( -n//2., n//2. ) for n in gpack[ 'support' ].shape ] ) )
pts = np.concatenate( tuple( this.reshape( 1, -1 ) for this in [ x, y, z ] ), axis=0 )
if isinstance( gpack[ 'pcc_params' ], type( None ) ):
self.parm_list = 0.5, 0.5, 0.5, 0., 0., 0.
else:
self.parm_list = tuple( gpack[ 'pcc_params' ] )
return pts
def _resetParameterList( self, arr ):
self.parm_list = tuple( arr )
self._setupVariables()
self._setupAuxiliary()
self._updateBlurKernel()
return
def _setupConstants( self, pts ):
self._q = tf.constant( pts, dtype=tf.float32 )
self._v0, self._v1, self._v2 = tuple(
tf.constant( np.roll( np.array( [ 1., 0., 0. ] ).reshape( -1, 1 ), shift=n, axis=0 ), dtype=tf.float32 )
for n in [ 0, 1, 2 ]
)
self._nskew0 = tf.constant( np.array( [ [ 0., 0., 0. ], [ 0., 0., -1. ], [ 0., 1., 0. ] ] ), dtype=tf.float32 )
self._nskew1 = tf.constant( np.array( [ [ 0., 0., 1. ], [ 0., 0., 0. ], [ -1., 0., 0. ] ] ), dtype=tf.float32 )
self._nskew2 = tf.constant( np.array( [ [ 0., -1., 0. ], [ 1., 0., 0. ], [ 0., 0., 0. ] ] ), dtype=tf.float32 )
self._I = tf.eye( 3 )
return
def _setupAuxiliary( self ):
self._mD = tf.linalg.diag( self.trainable_variables[0][:3] )
self._n0 = tf.sin( np.pi*self.trainable_variables[0][4] ) * tf.cos( 2.*np.pi*self.trainable_variables[0][5] )
self._n1 = tf.sin( np.pi*self.trainable_variables[0][4] ) * tf.sin( 2.*np.pi*self.trainable_variables[0][5] )
self._n2 = tf.cos( np.pi*self.trainable_variables[0][4] )
self._n = self._n0*self._v0 + self._n1*self._v1 + self._n2*self._v2
self._nskew = self._n0*self._nskew0 + self._n1*self._nskew1 + self._n2*self._nskew2
self._R = tf.cos( np.pi*self.trainable_variables[0][3] )*self._I +\
tf.sin( np.pi*self.trainable_variables[0][3] )*self._nskew +\
( 1. - tf.cos( np.pi*self.trainable_variables[0][3] ) )*tf.matmul( self._n, tf.transpose( self._n ) )
self._C = tf.matmul( self._R, tf.matmul( tf.matmul( self._mD, self._mD ), tf.transpose( self._R ) ) )
return
def _setupVariables( self ):
self._vars = tf.Variable( np.array( self.parm_list ), dtype=tf.float32 )
return
#@tf.function # don't do this, it messes with eager execution
def _updateBlurKernel( self ):
self._blurKernel = tf.reshape(
tf.exp( -0.5 * tf.reduce_sum( self._q * tf.matmul( self._C, self._q ), axis=0 ) ),
shape=self._shape
) * tf.reduce_prod( self.trainable_variables[0][:3] ) / ( ( 2. * np.pi )**( 3./2. ) )
self._blurKernel_f = tf.signal.fft3d( tf.cast( self._blurKernel, dtype=tf.complex64 ) )
return
#@tf.function
def Predict( self ):
self._setupAuxiliary()
self._updateBlurKernel()
self._modulus_estimated = tf.sqrt( tf.abs( tf.signal.ifft3d( self._cohEst_f * self._blurKernel_f ) ) )
return
#@tf.function
def Objective( self ):
return tf.reduce_mean( ( self._modulus_measured - self._modulus_estimated )**2 )
def Deblur( self, iterations, show_progress=False ):
if show_progress:
allIterations = tqdm( list( range( iterations ) ), desc='PCC' )
else:
allIterations = list( range( iterations ) )
for n in allIterations:
with tf.GradientTape( persistent=True ) as tape:
tape.watch( self.trainable_variables[0] )
self.Predict()
objfun = self.Objective()
gradient = tape.gradient( objfun, self.trainable_variables )
self._optimizer.apply_gradients( zip( gradient, self.trainable_variables ) )
return