Blame examples/fields_of_experts.cc

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.
Packit ea1746
Packit ea1746
#include "fields_of_experts.h"
Packit ea1746
Packit ea1746
#include <fstream>
Packit ea1746
#include <cmath>
Packit ea1746
Packit ea1746
#include "pgm_image.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace examples {
Packit ea1746
Packit ea1746
FieldsOfExpertsCost::FieldsOfExpertsCost(const std::vector<double>& filter)
Packit ea1746
    : filter_(filter) {
Packit ea1746
  set_num_residuals(1);
Packit ea1746
  for (int i = 0; i < filter_.size(); ++i) {
Packit ea1746
    mutable_parameter_block_sizes()->push_back(1);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
// This is a dot product between a the scalar parameters and a vector of filter
Packit ea1746
// coefficients.
Packit ea1746
bool FieldsOfExpertsCost::Evaluate(double const* const* parameters,
Packit ea1746
                                   double* residuals,
Packit ea1746
                                   double** jacobians) const {
Packit ea1746
  int num_variables = filter_.size();
Packit ea1746
  residuals[0] = 0;
Packit ea1746
  for (int i = 0; i < num_variables; ++i) {
Packit ea1746
    residuals[0] += filter_[i] * parameters[i][0];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  if (jacobians != NULL) {
Packit ea1746
    for (int i = 0; i < num_variables; ++i) {
Packit ea1746
      if (jacobians[i] != NULL) {
Packit ea1746
        jacobians[i][0] = filter_[i];
Packit ea1746
      }
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
// This loss function builds the FoE terms and is equal to
Packit ea1746
//
Packit ea1746
//   f(x) = alpha_i * log(1 + (1/2)s)
Packit ea1746
//
Packit ea1746
void FieldsOfExpertsLoss::Evaluate(double sq_norm, double rho[3]) const {
Packit ea1746
  const double c = 0.5;
Packit ea1746
  const double sum = 1.0 + sq_norm * c;
Packit ea1746
  const double inv = 1.0 / sum;
Packit ea1746
  // 'sum' and 'inv' are always positive, assuming that 's' is.
Packit ea1746
  rho[0] = alpha_ *  log(sum);
Packit ea1746
  rho[1] = alpha_ * c * inv;
Packit ea1746
  rho[2] = - alpha_ * c * c * inv * inv;
Packit ea1746
}
Packit ea1746
Packit ea1746
FieldsOfExperts::FieldsOfExperts()
Packit ea1746
    :  size_(0), num_filters_(0) {
Packit ea1746
}
Packit ea1746
Packit ea1746
bool FieldsOfExperts::LoadFromFile(const std::string& filename) {
Packit ea1746
  std::ifstream foe_file(filename.c_str());
Packit ea1746
  foe_file >> size_;
Packit ea1746
  foe_file >> num_filters_;
Packit ea1746
  if (size_ < 0 || num_filters_ < 0) {
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
  const int num_variables = NumVariables();
Packit ea1746
Packit ea1746
  x_delta_indices_.resize(num_variables);
Packit ea1746
  for (int i = 0; i < num_variables; ++i) {
Packit ea1746
    foe_file >> x_delta_indices_[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  y_delta_indices_.resize(NumVariables());
Packit ea1746
  for (int i = 0; i < num_variables; ++i) {
Packit ea1746
    foe_file >> y_delta_indices_[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  alpha_.resize(num_filters_);
Packit ea1746
  for (int i = 0; i < num_filters_; ++i) {
Packit ea1746
    foe_file >> alpha_[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  filters_.resize(num_filters_);
Packit ea1746
  for (int i = 0; i < num_filters_; ++i) {
Packit ea1746
    filters_[i].resize(num_variables);
Packit ea1746
    for (int j = 0; j < num_variables; ++j) {
Packit ea1746
      foe_file >> filters_[i][j];
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // If any read failed, return failure.
Packit ea1746
  if (!foe_file) {
Packit ea1746
    size_ = 0;
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // There cannot be anything else in the file. Try reading another number and
Packit ea1746
  // return failure if that succeeded.
Packit ea1746
  double temp;
Packit ea1746
  foe_file >> temp;
Packit ea1746
  if (foe_file) {
Packit ea1746
    size_ = 0;
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
ceres::CostFunction* FieldsOfExperts::NewCostFunction(int alpha_index) const {
Packit ea1746
  return new FieldsOfExpertsCost(filters_[alpha_index]);
Packit ea1746
}
Packit ea1746
Packit ea1746
ceres::LossFunction* FieldsOfExperts::NewLossFunction(int alpha_index) const {
Packit ea1746
  return new FieldsOfExpertsLoss(alpha_[alpha_index]);
Packit ea1746
}
Packit ea1746
Packit ea1746
Packit ea1746
}  // namespace examples
Packit ea1746
}  // namespace ceres