def solve_cs(ori_img, denoiser, reshape_img_fun, compress_ratio, noise_mean=0, noise_std=0., alpha=0.3, lambda_l1=0.1, max_iter=100, solver_tol=1e-2): import cs as problem x_shape = ori_img.shape (A_fun, AT_fun, A) = problem.setup(x_shape, compress_ratio) y, noise = add_noise.exe(A_fun(ori_img), noise_mean=noise_mean, noise_std=noise_std) info = {'ori_img': ori_img, 'y': y, 'noise': noise, 'compress_ratio': compress_ratio, 'noise_std': noise_std, 'alpha': alpha, 'max_iter': max_iter, 'solver_tol': solver_tol, 'lambda_l1': lambda_l1} # save the problem base_folder = '%s/cs_ratio%.2f_std%.2f' % (result_folder, compress_ratio, noise_std) if not os.path.exists(base_folder): os.makedirs(base_folder) filename = '%s/settings.mat' % base_folder sp.io.savemat(filename, info) filename = '%s/ori_img.jpg' % base_folder sp.misc.imsave(filename, sp.misc.imresize((reshape_img(np.clip(ori_img, 0.0, 1.0))*255).astype(np.uint8), 4.0, interp='nearest')) if run_ours: # ours folder = '%s/ours_alpha%f' % (base_folder, alpha) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver.solve(y, A_fun, AT_fun, denoiser, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha, max_iter=max_iter, solver_tol=solver_tol) save_results(folder, infos, x, z, u) if run_l1: # wavelet l1 folder = '%s/l1_lambdal1%f_alpha%f' % (base_folder, lambda_l1, alpha_l1) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver_l1.solve(y, A_fun, AT_fun, lambda_l1, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha_l1, max_iter=max_iter_l1, solver_tol=solver_tol_l1) save_results(folder, infos, x, z, u) z1 = reshape_img(np.clip(z, 0.0, 1.0)) ori_img1 = reshape_img(np.clip(ori_img, 0.0, 1.0)) psnr = 10*np.log10( 1.0 /np.linalg.norm(z1-ori_img1)**2*np.prod(z1.shape)) img = Image.fromarray( sp.misc.imresize(np.uint8(z1*255), 4.0, interp='nearest' ) ) draw = ImageDraw.Draw(img) #font = ImageFont.truetype(font='tnr.ttf', size=50) #draw.text((135, 200), "%.2f"%psnr, (255,255,255), font=font) filename = '%s/z.jpg' % folder img.save(filename)
def solve_denoising_dropping(ori_img, denoiser, reshape_img_fun, drop_prob=0.3, noise_mean=0, noise_std=0.1, alpha=0.3, lambda_l1=0.1, max_iter=100, solver_tol=1e-2): import inpaint as problem x_shape = ori_img.shape (A_fun, AT_fun, mask) = problem.setup(x_shape, drop_prob=drop_prob) y, noise = add_noise.exe(A_fun(ori_img), noise_mean=noise_mean, noise_std=noise_std) if show_img_progress: fig = plt.figure('denoise') plt.gcf().clear() fig.canvas.set_window_title('denoise') plt.subplot(1,2,1) plt.imshow(reshape_img_fun(ori_img), interpolation='nearest') plt.title('ori_img') plt.subplot(1,2,2) plt.imshow(reshape_img_fun(y), interpolation='nearest') plt.title('y') plt.pause(0.00001) info = {'ori_img': ori_img, 'y': y, 'noise': noise, 'mask': mask, 'drop_prob': drop_prob, 'noise_std': noise_std, 'alpha': alpha, 'max_iter': max_iter, 'solver_tol': solver_tol, 'lambda_l1': lambda_l1} # save the problem base_folder = '%s/denoise_ratio%.2f_std%.2f' % (result_folder, drop_prob, noise_std) if not os.path.exists(base_folder): os.makedirs(base_folder) filename = '%s/settings.mat' % base_folder sp.io.savemat(filename, info) filename = '%s/y.jpg' % base_folder sp.misc.imsave(filename, sp.misc.imresize((reshape_img(np.clip(y, 0.0, 1.0))*255).astype(np.uint8), 4.0, interp='nearest')) filename = '%s/ori_img.jpg' % base_folder sp.misc.imsave(filename, sp.misc.imresize((reshape_img(np.clip(ori_img, 0.0, 1.0))*255).astype(np.uint8), 4.0, interp='nearest')) if run_ours: # ours folder = '%s/ours_alpha%f' % (base_folder, alpha) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver.solve(y, A_fun, AT_fun, denoiser, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha, max_iter=max_iter, solver_tol=solver_tol) save_results(folder, infos, x, z, u) if run_l1: # wavelet l1 folder = '%s/l1_lambdal1%f_alpha%f' % (base_folder, lambda_l1, alpha_l1) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver_l1.solve(y, A_fun, AT_fun, lambda_l1, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha_l1, max_iter=max_iter_l1, solver_tol=solver_tol_l1) save_results(folder, infos, x, z, u) z1 = reshape_img(np.clip(z, 0.0, 1.0)) ori_img1 = reshape_img(np.clip(ori_img, 0.0, 1.0)) psnr = 10*np.log10( 1.0 /np.linalg.norm(z1-ori_img1)**2*np.prod(z1.shape)) img = Image.fromarray( sp.misc.imresize(np.uint8(z1*255), 4.0, interp='nearest' ) ) draw = ImageDraw.Draw(img) #font = ImageFont.truetype(font='tnr.ttf', size=50) #draw.text((135, 200), "%.2f"%psnr, (255,255,255), font=font) filename = '%s/z.jpg' % folder img.save(filename)
def solve_superres(ori_img, denoiser, reshape_img_fun, resize_ratio=0.5, noise_mean=0, noise_std=0., alpha=0.3, lambda_l1=0.1, max_iter=100, solver_tol=1e-2): import superres as problem x_shape = ori_img.shape (A_fun, AT_fun) = problem.setup(x_shape, resize_ratio=resize_ratio) y, noise = add_noise.exe(A_fun(ori_img), noise_mean=noise_mean, noise_std=noise_std) bicubic_img = cv2.resize(y[0], dsize=(ori_img.shape[1], ori_img.shape[2]), interpolation=cv2.INTER_CUBIC) if show_img_progress: fig = plt.figure('superres') plt.gcf().clear() fig.canvas.set_window_title('superres') plt.subplot(1, 3, 1) plt.imshow(reshape_img_fun(ori_img), interpolation='nearest') plt.title('ori_img') plt.subplot(1, 3, 2) plt.imshow(reshape_img_fun(y), interpolation='nearest') plt.title('y') plt.subplot(1, 3, 3) plt.imshow(np.clip(bicubic_img, 0, 255), interpolation='nearest') plt.title('bicubic') plt.pause(0.00001) bicubic_img = bicubic_img.astype(float) / 255.0 l2_dis = np.mean(np.square(ori_img[0] - bicubic_img)) print('bicubic err = %f' % (l2_dis)) info = { 'ori_img': ori_img, 'y': y, 'bicubic': bicubic_img, 'noise': noise, 'resize_ratio': resize_ratio, 'noise_std': noise_std, 'alpha': alpha, 'max_iter': max_iter, 'solver_tol': solver_tol, 'lambda_l1': lambda_l1 } # save the problem base_folder = '%s/superres_ratio%.2f_std%.2f' % ( result_folder, resize_ratio, noise_std) if not os.path.exists(base_folder): os.makedirs(base_folder) filename = '%s/settings.mat' % base_folder sp.io.savemat(filename, info) filename = '%s/y.jpg' % base_folder cv2.imwrite( filename, cv2.resize( (reshape_img(np.clip(y, 0.0, 1.0)) * 255).astype(np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) filename = '%s/ori_img.jpg' % base_folder cv2.imwrite( filename, cv2.resize((reshape_img(np.clip(ori_img, 0.0, 1.0)) * 255).astype( np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) filename = '%s/bicubic_img.jpg' % base_folder cv2.imwrite( filename, cv2.resize((bicubic_img * 255).astype(np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) if run_ours: # ours folder = '%s/ours_alpha%f' % (base_folder, alpha) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver.solve(y, A_fun, AT_fun, denoiser, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha, max_iter=max_iter, solver_tol=solver_tol) save_results(folder, infos, x, z, u) if run_l1: # wavelet l1 folder = '%s/l1_lambdal1%f_alpha%f' % (base_folder, lambda_l1, alpha_l1) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver_l1.solve(y, A_fun, AT_fun, lambda_l1, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha_l1, max_iter=max_iter_l1, solver_tol=solver_tol_l1) save_results(folder, infos, x, z, u) z1 = reshape_img(np.clip(z, 0.0, 1.0)) ori_img1 = reshape_img(np.clip(ori_img, 0.0, 1.0)) psnr = 10 * np.log10( 1.0 / np.linalg.norm(z1 - ori_img1)**2 * np.prod(z1.shape)) img = Image.fromarray( cv2.resize(np.uint8(z1 * 255), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) draw = ImageDraw.Draw(img) #font = ImageFont.truetype(font='tnr.ttf', size=50) #draw.text((135, 200), "%.2f"%psnr, (255,255,255), font=font) filename = '%s/z.jpg' % folder img.save(filename)
def solve_cs(ori_img, denoiser, reshape_img_fun, compress_ratio, noise_mean=0, noise_std=0., alpha=0.3, lambda_l1=0.1, max_iter=100, solver_tol=1e-2): import cs as problem x_shape = ori_img.shape (A_fun, AT_fun, A) = problem.setup(x_shape, compress_ratio) y, noise = add_noise.exe(A_fun(ori_img), noise_mean=noise_mean, noise_std=noise_std) info = { 'ori_img': ori_img, 'y': y, 'noise': noise, 'compress_ratio': compress_ratio, 'noise_std': noise_std, 'alpha': alpha, 'max_iter': max_iter, 'solver_tol': solver_tol, 'lambda_l1': lambda_l1 } # save the problem base_folder = '%s/cs_ratio%.2f_std%.2f' % (result_folder, compress_ratio, noise_std) if not os.path.exists(base_folder): os.makedirs(base_folder) filename = '%s/settings.mat' % base_folder sp.io.savemat(filename, info) filename = '%s/ori_img.jpg' % base_folder cv2.imwrite( filename, cv2.resize((reshape_img(np.clip(ori_img, 0.0, 1.0)) * 255).astype( np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) if run_ours: # ours folder = '%s/ours_alpha%f' % (base_folder, alpha) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver.solve(y, A_fun, AT_fun, denoiser, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha, max_iter=max_iter, solver_tol=solver_tol) save_results(folder, infos, x, z, u) if run_l1: # wavelet l1 folder = '%s/l1_lambdal1%f_alpha%f' % (base_folder, lambda_l1, alpha_l1) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver_l1.solve(y, A_fun, AT_fun, lambda_l1, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha_l1, max_iter=max_iter_l1, solver_tol=solver_tol_l1) save_results(folder, infos, x, z, u) z1 = reshape_img(np.clip(z, 0.0, 1.0)) ori_img1 = reshape_img(np.clip(ori_img, 0.0, 1.0)) psnr = 10 * np.log10( 1.0 / np.linalg.norm(z1 - ori_img1)**2 * np.prod(z1.shape)) img = Image.fromarray( cv2.resize(np.uint8(z1 * 255), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) draw = ImageDraw.Draw(img) #font = ImageFont.truetype(font='tnr.ttf', size=50) #draw.text((135, 200), "%.2f"%psnr, (255,255,255), font=font) filename = '%s/z.jpg' % folder img.save(filename)
def solve_inpaint_block(ori_img, denoiser, reshape_img_fun, box_size=1, total_box=1, noise_mean=0, noise_std=0., alpha=0.3, lambda_l1=0.1, max_iter=100, solver_tol=1e-2): import inpaint_block as problem x_shape = ori_img.shape (A_fun, AT_fun, mask) = problem.setup(x_shape, box_size=box_size, total_box=total_box) y, noise = add_noise.exe(A_fun(ori_img), noise_mean=noise_mean, noise_std=noise_std) if show_img_progress: fig = plt.figure('inpaint') plt.gcf().clear() fig.canvas.set_window_title('inpaint') plt.subplot(1, 2, 1) plt.imshow(reshape_img_fun(ori_img), interpolation='nearest') plt.title('ori_img') plt.subplot(1, 2, 2) plt.imshow(reshape_img_fun(y), interpolation='nearest') plt.title('y') plt.pause(0.00001) info = { 'ori_img': ori_img, 'y': y, 'noise': noise, 'mask': mask, 'box_size': box_size, 'total_box': total_box, 'noise_std': noise_std, 'alpha': alpha, 'max_iter': max_iter, 'solver_tol': solver_tol, 'lambda_l1': lambda_l1 } # save the problem base_folder = '%s/inpaint_bs%d_tb%d_std%.2f' % ( result_folder, box_size, total_box, noise_std) if not os.path.exists(base_folder): os.makedirs(base_folder) filename = '%s/settings.mat' % base_folder sp.io.savemat(filename, info) filename = '%s/y.jpg' % base_folder cv2.imwrite( filename, cv2.resize( (reshape_img(np.clip(y, 0.0, 1.0)) * 255).astype(np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) filename = '%s/ori_img.jpg' % base_folder cv2.imwrite( filename, cv2.resize((reshape_img(np.clip(ori_img, 0.0, 1.0)) * 255).astype( np.uint8), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) if run_ours: # ours folder = '%s/ours_alpha%f' % (base_folder, alpha) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver.solve(y, A_fun, AT_fun, denoiser, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha, max_iter=max_iter, solver_tol=solver_tol) save_results(folder, infos, x, z, u) if run_l1: # wavelet l1 folder = '%s/l1_lambdal1%f_alpha%f' % (base_folder, lambda_l1, alpha_l1) if not os.path.exists(folder): os.makedirs(folder) (x, z, u, infos) = solver_l1.solve(y, A_fun, AT_fun, lambda_l1, reshape_img_fun, folder, show_img_progress=show_img_progress, alpha=alpha_l1, max_iter=max_iter_l1, solver_tol=solver_tol_l1) save_results(folder, infos, x, z, u) z1 = reshape_img(np.clip(z, 0.0, 1.0)) ori_img1 = reshape_img(np.clip(ori_img, 0.0, 1.0)) psnr = 10 * np.log10( 1.0 / np.linalg.norm(z1 - ori_img1)**2 * np.prod(z1.shape)) img = Image.fromarray( cv2.resize(np.uint8(z1 * 255), None, fx=1.0, fy=1.0, interpolation=cv2.INTER_NEAREST)) draw = ImageDraw.Draw(img) #font = ImageFont.truetype(font='tnr.ttf', size=50) #draw.text((135, 200), "%.2f"%psnr, (255,255,255), font=font) filename = '%s/z.jpg' % folder img.save(filename)