Skip to content

Commit

Permalink
Fixed smooth gradient method parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
daurer committed Mar 5, 2024
1 parent 1375372 commit a472521
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
9 changes: 4 additions & 5 deletions ptypy/accelerate/base/engines/ML_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ML_serial(ML):
"""
Defaults:
[smooth_gradient.method]
[smooth_gradient_method]
default = convolution
type = str
help = Method to be used for smoothing the gradient, choose between ```convolution``` or ```fft```.
Expand Down Expand Up @@ -153,12 +153,12 @@ def engine_prepare(self):
self.ML_model.prepare()

def _get_smooth_gradient(self, data, sigma):
if self.p.smooth_gradient.method == "convolution":
if self.p.smooth_gradient_method == "convolution":
return complex_gaussian_filter(data, sigma)
elif self.p.smooth_gradient_method == "fft":
return complex_gaussian_filter_fft(data, sigma)
else:
raise NotImplementedError("smooth_gradient.method can only be ```convolution``` or ```fft```.")
raise NotImplementedError("smooth_gradient_method should be ```convolution``` or ```fft```.")

def _replace_ob_grad(self):
new_ob_grad = self.ob_grad_new
Expand Down Expand Up @@ -246,8 +246,7 @@ def engine_iterate(self, num=1):
# Smoothing preconditioner
if self.smooth_gradient:
for name, s in self.ob_h.storages.items():
# s.data[:] -= self._get_smooth_gradient(self.ob_grad.storages[name].data, self.smooth_gradient.sigma)
s.data[:] -= self._get_smooth_gradient_fft(self.ob_grad.storages[name].data, self.smooth_gradient.sigma)
s.data[:] -= self._get_smooth_gradient(self.ob_grad.storages[name].data, self.smooth_gradient.sigma)
else:
self.ob_h -= self.ob_grad

Expand Down
11 changes: 5 additions & 6 deletions ptypy/accelerate/cuda_pycuda/array_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,12 @@ def allocate(self, shape, sigma=1.):

def _compute_kernel(self, shape, sigma):
# Create kernel
self.sigma = sigma
if len(sigma) == 1:
sigma = np.array(sigma)
if sigma.ndim <= 1:
sigma = np.array([sigma, sigma])
elif len(sigma) == 2:
sigma = np.array(sigma)
else:
raise NotImplementedError("Only batches of 2D arrays allowed!")
if sigma.ndim > 2:
raise NotImplementedError("Only batches of 2D arrays allowed!")
self.sigma = sigma
return gaussian_kernel_2d(shape, sigma[0], sigma[1]).astype(self.dtype)

def filter(self, data, sigma=None):
Expand Down
13 changes: 6 additions & 7 deletions ptypy/accelerate/cuda_pycuda/engines/ML_pycuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def engine_initialize(self):
self.qu_htod = cuda.Stream()
self.qu_dtoh = cuda.Stream()

if self.p.smooth_gradient.method == "convolution":
if self.p.smooth_gradient_method == "convolution":
self.GSK = GaussianSmoothingKernel(queue=self.queue)
self.GSK.tmp = None

if self.p.smooth_gradient.method == "fft":
if self.p.smooth_gradient_method == "fft":
self.FGSK = FFTGaussianSmoothingKernel(queue=self.queue)

# Real/Fourier Support Kernel
Expand Down Expand Up @@ -259,14 +259,14 @@ def _set_pr_ob_ref_for_data(self, dev='gpu', container=None, sync_copy=False):
self._set_pr_ob_ref_for_data(dev=dev, container=container, sync_copy=sync_copy)

def _get_smooth_gradient(self, data, sigma):
if self.p.smooth_gradient.method == "convolution":
if self.p.smooth_gradient_method == "convolution":
if self.GSK.tmp is None:
self.GSK.tmp = gpuarray.empty(data.shape, dtype=np.complex64)
self.GSK.convolution(data, [sigma, sigma], tmp=self.GSK.tmp)
elif self.p.smooth_gradient.method == "fft":
elif self.p.smooth_gradient_method == "fft":
self.FGSK.filter(data, sigma)
else:
raise NotImplementedError("smooth_gradient.method can only be ```convolution``` or ```fft```.")
raise NotImplementedError("smooth_gradient_method should be ```convolution``` or ```fft```.")
return data

def _replace_ob_grad(self):
Expand All @@ -275,8 +275,7 @@ def _replace_ob_grad(self):
if self.smooth_gradient:
self.smooth_gradient.sigma *= (1. - self.p.smooth_gradient_decay)
for name, s in new_ob_grad.storages.items():
#s.gpu = self._get_smooth_gradient(s.gpu, self.smooth_gradient.sigma)
s.gpu = self._get_smooth_gradient_fft(s.gpu, self.smooth_gradient.sigma)
s.gpu = self._get_smooth_gradient(s.gpu, self.smooth_gradient.sigma)

return self._replace_grad(self.ob_grad, new_ob_grad)

Expand Down

0 comments on commit a472521

Please sign in to comment.