-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGPUModule_Core.py
157 lines (131 loc) · 5.31 KB
/
GPUModule_Core.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
###########################################################
#
# GPUModule_Core:
# Core definitions and operations for Phaser GPU
#
# Siddharth Maddali
# Argonne National Laboratory
# January 2020
#
###########################################################
import tensorflow as tf
import numpy as np
import functools as ftools
import PostProcessing as post
from scipy.spatial.transform import Rotation
try:
from pyfftw.interfaces.numpy_fft import fftshift
except:
from numpy.fft import fftshift
class Mixin:
def ImportCore( self, varDict ):
self._modulus = tf.constant( varDict[ 'modulus' ], dtype=tf.complex64 )
self._support = tf.Variable( varDict[ 'support' ], dtype=tf.complex64 )
self._support_comp = tf.Variable( 1. - varDict[ 'support' ], dtype=tf.complex64 )
self._beta = tf.constant( varDict[ 'beta' ], dtype=tf.complex64 )
self._cImage = tf.Variable( varDict[ 'cImage' ], dtype=tf.complex64 )
self._cachedImage = tf.Variable( np.zeros( varDict[ 'cImage' ].shape ), dtype=tf.complex64 )
self._modulus_sum = tf.reduce_sum( self._modulus )
self._cImage_fft_mod = tf.Variable( tf.abs( tf.signal.fft3d( self._cImage ) ) )
self.BinaryErosion = self.__GPUErosion__
self._error = []
self._UpdateError()
x, y, z = np.meshgrid( *[ np.arange( -n//2., n//2. ) for n in varDict[ 'support' ].shape ] )
self._rsquared = tf.constant(
ftools.reduce( lambda a, b: a+b, [ fftshift( this )**2 for this in [ x, y, z ] ] ),
dtype=tf.complex64
)
# used for GPU shrinkwrap
return
def resetImage( self, cImg, fSup, reset_error=True ):
self._cImage = tf.Variable( fftshift( cImg ), dtype=tf.complex64 )
self._support = tf.Variable( fftshift( fSup ), dtype=tf.complex64 )
if reset_error:
self._error = []
return
def resetSolver( self, fData, cImg, fSup ):
self._modulus = tf.constant( fftshift( fData ), dtype=tf.complex64 )
self.resetImage( cImg, fSup )
return
def resetParameterList( self, arr ):
self._pccSolver._resetParameterList( arr )
return
return
def Modulus( self ):
return np.absolute( tf.signal.fftshift( tf.signal.fft3d( self._cImage ) ).numpy() )
def _UpdateError( self ):
self._error.append(
tf.reduce_sum(
( self._cImage_fft_mod - tf.abs( self._modulus ) )**2
).numpy()
)
return
def _UpdateMod( self ):
self._cImage_fft_mod.assign( tf.abs( tf.signal.fft3d( self._cImage ) ) )
return
def UpdateSupport( self, support ):
self._support.assign( tf.cast( support, dtype=tf.complex64 ) )
self._support_comp.assign( 1. - self._support )
return
def _CacheImage( self ):
self._cachedImage.assign( self._cImage )
return
def _UpdateHIOStep( self ):
self._cImage.assign(
( self._support * self._cImage ) +\
self._support_comp * ( self._cachedImage - self._beta * self._cImage )
)
return
# GPU-specific shrinkwrap routine
def Shrinkwrap( self, sigma, thresh ):
kernel = 1. / ( sigma * np.sqrt( 2. * np.pi ) ) * tf.exp( -0.5 * self._rsquared / ( sigma**2 ) )
kernel_ft = tf.signal.fft3d( kernel )
ampl_ft = tf.signal.fft3d( tf.cast( tf.abs( self._cImage ), dtype=tf.complex64 ) )
#blurred = tf.signal.fftshift( tf.abs( tf.signal.ifft3d( kernel_ft * ampl_ft ) ) )
blurred = tf.abs( tf.signal.ifft3d( kernel_ft * ampl_ft ) )
new_support = tf.where( blurred > thresh * tf.reduce_max( blurred ), 1., 0. )
self.UpdateSupport( new_support )
return
def _ModProject( self ):
self._cImage.assign(
tf.signal.ifft3d( self._modulus * tf.exp( 1.j * tf.cast(
tf.math.angle( tf.signal.fft3d( self._cImage ) ),
dtype=tf.complex64
)
) )
)
return
def _SupProject( self ):
self._cImage.assign( self._cImage * self._support )
return
def _SupReflect( self ):
self._cImage.assign(
2. * ( self._support * self._cImage ) - self._cImage
)
return
def Retrieve( self ):
self.finalImage, self.finalSupport = post.centerObject(
self._cImage.numpy(), np.absolute( self._support.numpy() )
)
if hasattr( self, '_pccSolver' ):
self.pccParameters = self._pccSolver.trainable_variables[0].numpy()
return
def getCovarianceMatrix( self ):
try:
ln = self.pccParameters.size
except NameError: # pccParameters does not exist yet
self.pccParameters = self._pccSolver.trainable_variables[0].numpy()
evalues = np.diag( self.pccParameters[:3] )**2
ang = self.pccParameters[3] # rotation angle in radians
th, ph = tuple( self.pccParameters[4:] )
ax = np.array(
[
np.sin( th ) * np.cos( ph ),
np.sin( th ) * np.sin( ph ),
np.cos( th )
]
)
evectors = Rotation.from_rotvec( ang*ax ).as_matrix()
C = evectors @ evalues @ evectors.T
return C