Skip to content

Commit

Permalink
MTN use benchopt 1.5 API
Browse files Browse the repository at this point in the history
  • Loading branch information
mathurinm authored Jul 17, 2024
2 parents 7af8c49 + 0c67f0a commit 740550b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion config_medium.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ objective:
- TV2D[data_fit=huber,delta=0.9,isotropy=isotropic,reg=0.02]
dataset:
- Cartoon[size_blur=27,std_blur=2.0,std_noise=0.02,subsampling=10,type_A=deblurring,type_n=[gaussian, laplace]]
- Cartoon[size_blur=27,std_blur=2.0,std_noise=0.3,subsampling=10,type_A=denoising,type_n=[gaussian, laplace]
- Cartoon[size_blur=27,std_blur=2.0,std_noise=0.3,subsampling=10,type_A=denoising,type_n=[gaussian, laplace]]
solver:
- ADMM[gamma=0.1]
- Chambolle-Pock PD-split[eta=1.0,ratio=10.0]
Expand Down
26 changes: 10 additions & 16 deletions objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@


class Objective(BaseObjective):
min_benchopt_version = "1.3"
min_benchopt_version = "1.5"
name = "TV2D"

parameters = {'reg': [0.02],
'delta': [0.9],
'isotropy': ["anisotropic", "isotropic"],
'data_fit': ["lsq", "huber"]}

def __init__(self, reg=0.02, delta=0.1,
isotropy="anisotropic", data_fit="lsq"):
self.reg = reg
self.delta = delta
self.isotropy = isotropy
self.data_fit = data_fit
parameters = {
'reg': [0.02],
'delta': [0.9],
'isotropy': ["anisotropic", "isotropic"],
'data_fit': ["lsq", "huber"]
}

def set_data(self, A, y):
self.A = A
self.y = y
self.reg = self.reg

def compute(self, u):
def evaluate_result(self, u):
R = self.y - self.A @ u # residuals

if self.data_fit == "lsq":
Expand All @@ -43,8 +37,8 @@ def compute(self, u):

return loss + self.reg * penalty

def get_one_solution(self):
return np.zeros(self.y.shape)
def get_one_result(self):
return dict(u=np.zeros(self.y.shape))

def get_objective(self):
return dict(A=self.A,
Expand Down
7 changes: 4 additions & 3 deletions solvers/ADMM.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def set_objective(self, A, reg, delta, data_fit, y, isotropy):
def run(self, callback):
n, m = self.y.shape
# initialisation
self.u = np.zeros((n, m))
u = np.zeros((n, m))
zh = np.zeros((n, m)) # we consider non-cyclic finite difference
zv = np.zeros((n, m))
Expand All @@ -79,7 +80,7 @@ def run(self, callback):
self.A @ x.reshape((n, m)))
- gamma * div(grad(x.reshape((n, m)))[0],
grad(x.reshape((n, m)))[1]))
while callback(u):
while callback():
if self.data_fit == 'lsq':
u_tmp = (Aty + div(muh, muv) - gamma * div(zh, zv)).flatten()
u, _ = cg(AtA_gDtD, u_tmp, x0=u.flatten(), tol=tol_cg)
Expand All @@ -105,7 +106,7 @@ def jac(u):
zv = (gv * gamma + muv - zv) / gamma
muh += gamma * (gh - zh)
muv += gamma * (gv - zv)
self.u = u
self.u = u

def get_result(self):
return self.u
return dict(u=self.u)
7 changes: 4 additions & 3 deletions solvers/ChambollePockPDSplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def run(self, callback):
# Init variables
n, m = self.y.shape
u = np.zeros((n, m))
self.u = np.zeros((n, m))
vh = np.zeros((n, m)) # we consider non-cyclic finite difference
vv = np.zeros((n, m))
w = np.zeros((n, m))
Expand All @@ -54,7 +55,7 @@ def run(self, callback):
'isotropic': dual_prox_tv_iso,
}.get(self.isotropy, dual_prox_tv_aniso)

while callback(u):
while callback():
u_old = u
gh, gv = grad(u_bar)
vh, vv = proj(vh + sigma_v * gh,
Expand All @@ -73,7 +74,7 @@ def run(self, callback):
# grad.T = -div, hence + sign
u = u + tau * div(vh, vv) - tau * self.A.T @ w
u_bar = u + self.eta * (u - u_old)
self.u = u
self.u = u

def get_result(self):
return self.u
return dict(u=self.u)
7 changes: 4 additions & 3 deletions solvers/CondatVu.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ def run(self, callback):
eta = self.eta
# initialisation
u = np.zeros((n, m))
self.u = np.zeros((n, m))
vh = np.zeros((n, m)) # we consider non-cyclic finite difference
vv = np.zeros((n, m))
proj = {
'anisotropic': dual_prox_tv_aniso,
'isotropic': dual_prox_tv_iso,
}.get(self.isotropy, dual_prox_tv_aniso)

while callback(u):
while callback():
u_tmp = (u - tau * grad_F(self.y, self.A, u,
self.data_fit, self.delta)
+ tau * div(vh, vv))
Expand All @@ -63,7 +64,7 @@ def run(self, callback):
u = eta * u_tmp + (1 - eta) * u
vh = eta * vh_tmp + (1 - eta) * vh
vv = eta * vv_tmp + (1 - eta) * vv
self.u = u
self.u = u

def get_result(self):
return self.u
return dict(u=self.u)
9 changes: 5 additions & 4 deletions solvers/DPGD.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def run(self, callback):
n, m = self.y.shape
# initialisation
u = np.zeros((n, m))
self.u = np.zeros((n, m))
v = np.zeros((n, m))
vh = np.zeros((n, m)) # we consider non-cyclic finite difference
vv = np.zeros((n, m))
Expand All @@ -54,7 +55,7 @@ def run(self, callback):
Aty = self.A.T @ self.y
AtA = LinearOperator(shape=(n * m, n * m),
matvec=lambda x: self.A.T @ (
self.A @ x.reshape((n, m))))
self.A @ x.reshape((n, m))))
proj = {
'anisotropic': dual_prox_tv_aniso,
'isotropic': dual_prox_tv_iso,
Expand All @@ -68,7 +69,7 @@ def run(self, callback):
vv_acc = vv.copy()

t_new = 1
while callback(u):
while callback():
if self.use_acceleration:
t_old = t_new
t_new = (1 + np.sqrt(1 + 4 * t_old ** 2)) / 2
Expand All @@ -95,7 +96,7 @@ def run(self, callback):
u_tmp = (Aty + div(vh, vv)).flatten()
u, _ = cg(AtA, u_tmp, x0=u.flatten(), tol=tol_cg)
u = u.reshape((n, m))
self.u = u
self.u = u

def get_result(self):
return self.u
return dict(u=self.u)
9 changes: 5 additions & 4 deletions solvers/PGD.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Solver(BaseSolver):
# We need blas devel to get the include file for BLAS/LAPACK operations
requirements = ["blas-devel", 'pip:prox-tv']

stopping_strategy = "callback"
sampling_strategy = "callback"

parameters = {'prox_tv_method': [
"dr",
Expand All @@ -44,11 +44,12 @@ def run(self, callback):
n, m = self.y.shape
stepsize = 1. / (get_l2norm(self.A) ** 2)
u = np.zeros((n, m))
self.u = np.zeros((n, m))
u_acc = u.copy()
u_old = u.copy()

t_new = 1
while callback(u):
while callback():
if self.use_acceleration:
t_old = t_new
t_new = (1 + np.sqrt(1 + 4 * t_old ** 2)) / 2
Expand All @@ -60,7 +61,7 @@ def run(self, callback):
self.reg * stepsize, method=self.prox_tv_method)
if self.use_acceleration:
u_acc[:] = u + (t_old - 1.) / t_new * (u - u_old)
self.u = u
self.u = u

def get_result(self):
return self.u
return dict(u=self.u)

0 comments on commit 740550b

Please sign in to comment.