blob: 61ea2c6fa14fc58d11b568cbd36099092b7fc392 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: strandmark@google.com (Petter Strandmark)
30//
31// Denoising using Fields of Experts and the Ceres minimizer.
32//
33// Note that for good denoising results the weighting between the data term
34// and the Fields of Experts term needs to be adjusted. This is discussed
35// in [1]. This program assumes Gaussian noise. The noise model can be changed
36// by substituing another function for QuadraticCostFunction.
37//
38// [1] S. Roth and M.J. Black. "Fields of Experts." International Journal of
39// Computer Vision, 82(2):205--229, 2009.
40
41#include <algorithm>
42#include <cmath>
43#include <iostream>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080044#include <random>
Austin Schuh70cc9552019-01-21 19:46:48 -080045#include <sstream>
46#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080047#include <vector>
Austin Schuh70cc9552019-01-21 19:46:48 -080048
49#include "ceres/ceres.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080050#include "fields_of_experts.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080051#include "gflags/gflags.h"
52#include "glog/logging.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080053#include "pgm_image.h"
54
55DEFINE_string(input, "", "File to which the output image should be written");
56DEFINE_string(foe_file, "", "FoE file to use");
57DEFINE_string(output, "", "File to which the output image should be written");
58DEFINE_double(sigma, 20.0, "Standard deviation of noise");
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080059DEFINE_string(trust_region_strategy,
60 "levenberg_marquardt",
61 "Options are: levenberg_marquardt, dogleg.");
62DEFINE_string(dogleg,
63 "traditional_dogleg",
64 "Options are: traditional_dogleg,"
65 "subspace_dogleg.");
66DEFINE_string(linear_solver,
67 "sparse_normal_cholesky",
68 "Options are: "
69 "sparse_normal_cholesky and cgnr.");
70DEFINE_string(preconditioner,
71 "jacobi",
72 "Options are: "
73 "identity, jacobi, subset");
74DEFINE_string(sparse_linear_algebra_library,
75 "suite_sparse",
76 "Options are: suite_sparse, cx_sparse and eigen_sparse");
77DEFINE_double(eta,
78 1e-2,
79 "Default value for eta. Eta determines the "
80 "accuracy of each linear solve of the truncated newton step. "
81 "Changing this parameter can affect solve performance.");
82DEFINE_int32(num_threads, 1, "Number of threads.");
83DEFINE_int32(num_iterations, 10, "Number of iterations.");
84DEFINE_bool(nonmonotonic_steps,
85 false,
86 "Trust region algorithm can use"
87 " nonmonotic steps.");
88DEFINE_bool(inner_iterations,
89 false,
90 "Use inner iterations to non-linearly "
91 "refine each successful trust region step.");
92DEFINE_bool(mixed_precision_solves, false, "Use mixed precision solves.");
93DEFINE_int32(max_num_refinement_iterations,
94 0,
95 "Iterative refinement iterations");
96DEFINE_bool(line_search,
97 false,
98 "Use a line search instead of trust region "
Austin Schuh70cc9552019-01-21 19:46:48 -080099 "algorithm.");
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100DEFINE_double(subset_fraction,
101 0.2,
102 "The fraction of residual blocks to use for the"
103 " subset preconditioner.");
Austin Schuh70cc9552019-01-21 19:46:48 -0800104
105namespace ceres {
106namespace examples {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800107namespace {
Austin Schuh70cc9552019-01-21 19:46:48 -0800108
109// This cost function is used to build the data term.
110//
111// f_i(x) = a * (x_i - b)^2
112//
113class QuadraticCostFunction : public ceres::SizedCostFunction<1, 1> {
114 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800115 QuadraticCostFunction(double a, double b) : sqrta_(std::sqrt(a)), b_(b) {}
Austin Schuh70cc9552019-01-21 19:46:48 -0800116 virtual bool Evaluate(double const* const* parameters,
117 double* residuals,
118 double** jacobians) const {
119 const double x = parameters[0][0];
120 residuals[0] = sqrta_ * (x - b_);
121 if (jacobians != NULL && jacobians[0] != NULL) {
122 jacobians[0][0] = sqrta_;
123 }
124 return true;
125 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800126
Austin Schuh70cc9552019-01-21 19:46:48 -0800127 private:
128 double sqrta_, b_;
129};
130
131// Creates a Fields of Experts MAP inference problem.
132void CreateProblem(const FieldsOfExperts& foe,
133 const PGMImage<double>& image,
134 Problem* problem,
135 PGMImage<double>* solution) {
136 // Create the data term
137 CHECK_GT(FLAGS_sigma, 0.0);
138 const double coefficient = 1 / (2.0 * FLAGS_sigma * FLAGS_sigma);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800139 for (int index = 0; index < image.NumPixels(); ++index) {
140 ceres::CostFunction* cost_function = new QuadraticCostFunction(
141 coefficient, image.PixelFromLinearIndex(index));
142 problem->AddResidualBlock(
143 cost_function, NULL, solution->MutablePixelFromLinearIndex(index));
Austin Schuh70cc9552019-01-21 19:46:48 -0800144 }
145
146 // Create Ceres cost and loss functions for regularization. One is needed for
147 // each filter.
148 std::vector<ceres::LossFunction*> loss_function(foe.NumFilters());
149 std::vector<ceres::CostFunction*> cost_function(foe.NumFilters());
150 for (int alpha_index = 0; alpha_index < foe.NumFilters(); ++alpha_index) {
151 loss_function[alpha_index] = foe.NewLossFunction(alpha_index);
152 cost_function[alpha_index] = foe.NewCostFunction(alpha_index);
153 }
154
155 // Add FoE regularization for each patch in the image.
156 for (int x = 0; x < image.width() - (foe.Size() - 1); ++x) {
157 for (int y = 0; y < image.height() - (foe.Size() - 1); ++y) {
158 // Build a vector with the pixel indices of this patch.
159 std::vector<double*> pixels;
160 const std::vector<int>& x_delta_indices = foe.GetXDeltaIndices();
161 const std::vector<int>& y_delta_indices = foe.GetYDeltaIndices();
162 for (int i = 0; i < foe.NumVariables(); ++i) {
163 double* pixel = solution->MutablePixel(x + x_delta_indices[i],
164 y + y_delta_indices[i]);
165 pixels.push_back(pixel);
166 }
167 // For this patch with coordinates (x, y), we will add foe.NumFilters()
168 // terms to the objective function.
169 for (int alpha_index = 0; alpha_index < foe.NumFilters(); ++alpha_index) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800170 problem->AddResidualBlock(
171 cost_function[alpha_index], loss_function[alpha_index], pixels);
Austin Schuh70cc9552019-01-21 19:46:48 -0800172 }
173 }
174 }
175}
176
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800177void SetLinearSolver(Solver::Options* options) {
178 CHECK(StringToLinearSolverType(FLAGS_linear_solver,
179 &options->linear_solver_type));
180 CHECK(StringToPreconditionerType(FLAGS_preconditioner,
181 &options->preconditioner_type));
182 CHECK(StringToSparseLinearAlgebraLibraryType(
183 FLAGS_sparse_linear_algebra_library,
184 &options->sparse_linear_algebra_library_type));
185 options->use_mixed_precision_solves = FLAGS_mixed_precision_solves;
186 options->max_num_refinement_iterations = FLAGS_max_num_refinement_iterations;
187}
188
189void SetMinimizerOptions(Solver::Options* options) {
190 options->max_num_iterations = FLAGS_num_iterations;
191 options->minimizer_progress_to_stdout = true;
192 options->num_threads = FLAGS_num_threads;
193 options->eta = FLAGS_eta;
194 options->use_nonmonotonic_steps = FLAGS_nonmonotonic_steps;
195 if (FLAGS_line_search) {
196 options->minimizer_type = ceres::LINE_SEARCH;
197 }
198
199 CHECK(StringToTrustRegionStrategyType(FLAGS_trust_region_strategy,
200 &options->trust_region_strategy_type));
201 CHECK(StringToDoglegType(FLAGS_dogleg, &options->dogleg_type));
202 options->use_inner_iterations = FLAGS_inner_iterations;
203}
204
Austin Schuh70cc9552019-01-21 19:46:48 -0800205// Solves the FoE problem using Ceres and post-processes it to make sure the
206// solution stays within [0, 255].
207void SolveProblem(Problem* problem, PGMImage<double>* solution) {
208 // These parameters may be experimented with. For example, ceres::DOGLEG tends
209 // to be faster for 2x2 filters, but gives solutions with slightly higher
210 // objective function value.
211 ceres::Solver::Options options;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800212 SetMinimizerOptions(&options);
213 SetLinearSolver(&options);
Austin Schuh70cc9552019-01-21 19:46:48 -0800214 options.function_tolerance = 1e-3; // Enough for denoising.
215
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800216 if (options.linear_solver_type == ceres::CGNR &&
217 options.preconditioner_type == ceres::SUBSET) {
218 std::vector<ResidualBlockId> residual_blocks;
219 problem->GetResidualBlocks(&residual_blocks);
220
221 // To use the SUBSET preconditioner we need to provide a list of
222 // residual blocks (rows of the Jacobian). The denoising problem
223 // has fairly general sparsity, and there is no apriori reason to
224 // select one residual block over another, so we will randomly
225 // subsample the residual blocks with probability subset_fraction.
226 std::default_random_engine engine;
227 std::uniform_real_distribution<> distribution(0, 1); // rage 0 - 1
228 for (auto residual_block : residual_blocks) {
229 if (distribution(engine) <= FLAGS_subset_fraction) {
230 options.residual_blocks_for_subset_preconditioner.insert(
231 residual_block);
232 }
233 }
234 }
235
Austin Schuh70cc9552019-01-21 19:46:48 -0800236 ceres::Solver::Summary summary;
237 ceres::Solve(options, problem, &summary);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800238 std::cout << summary.FullReport() << "\n";
Austin Schuh70cc9552019-01-21 19:46:48 -0800239
240 // Make the solution stay in [0, 255].
241 for (int x = 0; x < solution->width(); ++x) {
242 for (int y = 0; y < solution->height(); ++y) {
243 *solution->MutablePixel(x, y) =
244 std::min(255.0, std::max(0.0, solution->Pixel(x, y)));
245 }
246 }
247}
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800248
249} // namespace
Austin Schuh70cc9552019-01-21 19:46:48 -0800250} // namespace examples
251} // namespace ceres
252
253int main(int argc, char** argv) {
254 using namespace ceres::examples;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800255 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
Austin Schuh70cc9552019-01-21 19:46:48 -0800256 google::InitGoogleLogging(argv[0]);
257
258 if (FLAGS_input.empty()) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800259 std::cerr << "Please provide an image file name using -input.\n";
Austin Schuh70cc9552019-01-21 19:46:48 -0800260 return 1;
261 }
262
263 if (FLAGS_foe_file.empty()) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800264 std::cerr << "Please provide a Fields of Experts file name using -foe_file."
265 "\n";
Austin Schuh70cc9552019-01-21 19:46:48 -0800266 return 1;
267 }
268
269 // Load the Fields of Experts filters from file.
270 FieldsOfExperts foe;
271 if (!foe.LoadFromFile(FLAGS_foe_file)) {
272 std::cerr << "Loading \"" << FLAGS_foe_file << "\" failed.\n";
273 return 2;
274 }
275
276 // Read the images
277 PGMImage<double> image(FLAGS_input);
278 if (image.width() == 0) {
279 std::cerr << "Reading \"" << FLAGS_input << "\" failed.\n";
280 return 3;
281 }
282 PGMImage<double> solution(image.width(), image.height());
283 solution.Set(0.0);
284
285 ceres::Problem problem;
286 CreateProblem(foe, image, &problem, &solution);
287
288 SolveProblem(&problem, &solution);
289
290 if (!FLAGS_output.empty()) {
291 CHECK(solution.WriteToFile(FLAGS_output))
292 << "Writing \"" << FLAGS_output << "\" failed.";
293 }
294
295 return 0;
296}