def main(): image = lena().astype(np.float) / 255. h, w = image.shape y_true = image.ravel() noise = np.random.normal(0, 0.1, y_true.shape) y_noisy = y_true + noise grad = image_gradient_operator(h, w) l1 = 0.1 * GroupL1Norm((2, h, w), 0) #l1 = 0.1 * SliceSeparableNorm(L2Norm(), (2, h * w), 1) l2 = L2Distance(y_noisy) obj = lambda x: l1(grad * x) + l2(x) algo = APGM(l2, l1, A=grad, maxiter=50, x0=y_noisy, objectives=ObjectiveLogger(obj), errors=ErrorLogger(y_true)) t0 = time.time() y_denoised = algo.run() print "took %.3f seconds" % (time.time() - t0) pl.figure() pl.gray() for i, (title, v) in enumerate([('ground truth', y_true), ('noisy', y_noisy), ('denoised', y_denoised)]): pl.subplot(2, 3, i + 1) pl.title(title) pl.xticks([]) pl.yticks([]) pl.imshow(v.reshape(image.shape), vmin=0, vmax=1) pl.subplot(2, 3, 4) pl.title('Objective Function') pl.plot(algo.objectives) pl.ylabel('objective') pl.xlabel('iteration') pl.subplot(2, 3, 5) pl.title('Errors') pl.plot(algo.errors) pl.ylabel('error') pl.xlabel('iteration') pl.draw() pl.show()
def main(): image = lena().astype(np.float) / 255. h, w = image.shape y_true = image.ravel() noise = np.random.normal(0, 0.1, y_true.shape) y_noisy = y_true + noise grad = image_gradient_operator(h, w) l1 = 0.1 * GroupL1Norm((2, h, w), 0) #l1 = 0.1 * SliceSeparableNorm(L2Norm(), (2, h * w), 1) l2 = L2Distance(y_noisy) obj = lambda x: l1(grad * x) + l2(x) algo = APGM(l2, l1, A=grad, maxiter=50, x0=y_noisy, objectives=ObjectiveLogger(obj), errors=ErrorLogger(y_true) ) t0 = time.time() y_denoised = algo.run() print "took %.3f seconds" % (time.time() - t0) pl.figure() pl.gray() for i, (title, v) in enumerate([('ground truth', y_true), ('noisy', y_noisy), ('denoised', y_denoised)]): pl.subplot(2, 3, i+1) pl.title(title) pl.xticks([]) pl.yticks([]) pl.imshow(v.reshape(image.shape), vmin=0, vmax=1) pl.subplot(2, 3, 4) pl.title('Objective Function') pl.plot(algo.objectives) pl.ylabel('objective') pl.xlabel('iteration') pl.subplot(2, 3, 5) pl.title('Errors') pl.plot(algo.errors) pl.ylabel('error') pl.xlabel('iteration') pl.draw() pl.show()
b = A.matvec(x) # setup problem argmin_x 0.5 ||A x - b||_2^2 + ||x||_1 l1 = 1e-2 * L1Norm() l2 = DataTerm(A, b) # solve maxiter = 5000 dr = DouglasRachford(l2, l1, gamma=0.1, maxiter=maxiter, objectives=ObjectiveLogger(l1 + l2), errors=ErrorLogger(x)) dr.run() print "reasons for stopping: " + ", ".join(message for cls, message in dr.stopping_reasons) np.testing.assert_array_almost_equal(x, dr.x, decimal=3) apgm = APGM(l2, l1, rho=0.5, maxiter=maxiter, objectives=ObjectiveLogger(l1 + l2), errors=ErrorLogger(x)) apgm.run() print "reasons for stopping: " + ", ".join(message for cls, message in apgm.stopping_reasons) np.testing.assert_array_almost_equal(x, apgm.x, decimal=3) alg1 = ForwardBackward(l1, l2, maxiter=maxiter, objectives=ObjectiveLogger(l1 + l2), errors=ErrorLogger(x)) alg1.run() print "reasons for stopping: " + ", ".join(message for cls, message in alg1.stopping_reasons) np.testing.assert_array_almost_equal(x, alg1.x, decimal=3) xr = forward_backward(l1, l2, maxiter=maxiter) np.testing.assert_array_equal(alg1.x, xr) alg2 = ForwardBackward(l1, l2, maxiter=maxiter, alpha=0.9, objectives=ObjectiveLogger(l1 + l2), errors=ErrorLogger(x)) alg2.run() print "reasons for stopping: " + ", ".join(message for cls, message in alg2.stopping_reasons) np.testing.assert_array_almost_equal(x, alg2.x, decimal=3)