Blame examples/fields_of_experts.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: strandmark@google.com (Petter Strandmark)
Packit ea1746
//
Packit ea1746
// Class for loading the data required for descibing a Fields of Experts (FoE)
Packit ea1746
// model. The Fields of Experts regularization consists of terms of the type
Packit ea1746
//
Packit ea1746
//   alpha * log(1 + (1/2)*sum(F .* X)^2),
Packit ea1746
//
Packit ea1746
// where F is a d-by-d image patch and alpha is a constant. This is implemented
Packit ea1746
// by a FieldsOfExpertsSum object which represents the dot product between the
Packit ea1746
// image patches and a FieldsOfExpertsLoss which implements the log(1 + (1/2)s)
Packit ea1746
// part.
Packit ea1746
//
Packit ea1746
// [1] S. Roth and M.J. Black. "Fields of Experts." International Journal of
Packit ea1746
//     Computer Vision, 82(2):205--229, 2009.
Packit ea1746
Packit ea1746
#ifndef CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
Packit ea1746
#define CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
Packit ea1746
Packit ea1746
#include <iostream>
Packit ea1746
#include <vector>
Packit ea1746
Packit ea1746
#include "ceres/loss_function.h"
Packit ea1746
#include "ceres/cost_function.h"
Packit ea1746
#include "ceres/sized_cost_function.h"
Packit ea1746
Packit ea1746
#include "pgm_image.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace examples {
Packit ea1746
Packit ea1746
// One sum in the FoE regularizer. This is a dot product between a filter and an
Packit ea1746
// image patch. It simply calculates the dot product between the filter
Packit ea1746
// coefficients given in the constructor and the scalar parameters passed to it.
Packit ea1746
class FieldsOfExpertsCost : public ceres::CostFunction {
Packit ea1746
 public:
Packit ea1746
  explicit FieldsOfExpertsCost(const std::vector<double>& filter);
Packit ea1746
  // The number of scalar parameters passed to Evaluate must equal the number of
Packit ea1746
  // filter coefficients passed to the constructor.
Packit ea1746
  virtual bool Evaluate(double const* const* parameters,
Packit ea1746
                        double* residuals,
Packit ea1746
                        double** jacobians) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const std::vector<double>& filter_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// The loss function used to build the correct regularization. See above.
Packit ea1746
//
Packit ea1746
//   f(x) = alpha_i * log(1 + (1/2)s)
Packit ea1746
//
Packit ea1746
class FieldsOfExpertsLoss : public ceres::LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit FieldsOfExpertsLoss(double alpha) : alpha_(alpha) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const double alpha_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// This class loads a set of filters and coefficients from file. Then the users
Packit ea1746
// obtains the correct loss and cost functions through NewCostFunction and
Packit ea1746
// NewLossFunction.
Packit ea1746
class FieldsOfExperts {
Packit ea1746
 public:
Packit ea1746
  // Creates an empty object with size() == 0.
Packit ea1746
  FieldsOfExperts();
Packit ea1746
  // Attempts to load filters from a file. If unsuccessful it returns false and
Packit ea1746
  // sets size() == 0.
Packit ea1746
  bool LoadFromFile(const std::string& filename);
Packit ea1746
Packit ea1746
  // Side length of a square filter in this FoE. They are all of the same size.
Packit ea1746
  int Size() const {
Packit ea1746
    return size_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Total number of pixels the filter covers.
Packit ea1746
  int NumVariables() const {
Packit ea1746
    return size_ * size_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Number of filters used by the FoE.
Packit ea1746
  int NumFilters() const {
Packit ea1746
    return num_filters_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Creates a new cost function. The caller is responsible for deallocating the
Packit ea1746
  // memory. alpha_index specifies which filter is used in the cost function.
Packit ea1746
  ceres::CostFunction* NewCostFunction(int alpha_index) const;
Packit ea1746
  // Creates a new loss function. The caller is responsible for deallocating the
Packit ea1746
  // memory. alpha_index specifies which filter this loss function is for.
Packit ea1746
  ceres::LossFunction* NewLossFunction(int alpha_index) const;
Packit ea1746
Packit ea1746
  // Gets the delta pixel indices for all pixels in a patch.
Packit ea1746
  const std::vector<int>& GetXDeltaIndices() const {
Packit ea1746
    return x_delta_indices_;
Packit ea1746
  }
Packit ea1746
  const std::vector<int>& GetYDeltaIndices() const {
Packit ea1746
    return y_delta_indices_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  // The side length of a square filter.
Packit ea1746
  int size_;
Packit ea1746
  // The number of different filters used.
Packit ea1746
  int num_filters_;
Packit ea1746
  // Pixel offsets for all variables.
Packit ea1746
  std::vector<int> x_delta_indices_, y_delta_indices_;
Packit ea1746
  // The coefficients in front of each term.
Packit ea1746
  std::vector<double> alpha_;
Packit ea1746
  // The filters used for the dot product with image patches.
Packit ea1746
  std::vector<std::vector<double> > filters_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace examples
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_