Hi everybody, just started trying out ceres, looks great!

So, I'm trying to use it to solve a function parametrization problem. 

I've seen that in all the examples the modeling is more or less completely 
self-contained into the cost function. But in my case the modeling phase 
was yet done and I was looking for a way to re-use that without rewriting 
everything.

In my case I've got class, say the Model class, which incorporates all my 
modeling, with function members for setting the parameters etc... etc...

My class is templated so it can easily accept the ceres::Jet class as 
template argument. 


template<T = double>
class Model()
{
public:
T operator() (Eigen::Matrix<T,...> x)
{
//[...] compute y for the given x, using the parameters setted with pars an 
other internal parameters that I am not trying to refine with the solver 
[...]
return y
}

void setParameters(Eigen::Matrix<T,... > pars) {...}
...
}


This is my first temptative to define a cost function to be used with ceres 
(it is simplified from actual code so I am no sure it is formally correct, 
but it should give you an idea):


struct Residual {
    Residual(double x, double y, const Eigen::MatrixXd &knots_): x_(x), y_(y
), knots_(knots_)
    {

    }

    template <typename T> bool operator()(const T* const coeffs, T* residual
) const
    {
        Eigen::Matrix<T, 6, 1> c = Eigen::Map<const Eigen::Matrix<T, 6, 1>>(
coeffs);

        Model<T> model;  // create a new model object. This is not really 
good!

        model.setParameters(c);
        model.setKnots(knots_.cast<T>()); // knots are model parameters 
that are not optimized!
                                          // so everytime I have to re-set 
them
      
        Eigen::Matrix<T, 1, 1> eigx;
        eigx << T(x_);
        T predicted = model(eigx); // evaluating the model here

        residual[0] = T(y_) - predicted; // and computing the residual

        return true;
    }

private:
    const double x_;
    const double y_;

    Eigen::MatrixXd knots_;// these are internal parameters not to be 
optimized
};


Then I add a series of residual blocks to the problem, as is don in the 
curve fitting example, one for each observation... but this imply that 
every time the cost function is evaluated a new Model<T> object is build up 
(and it is pretty expensive to be done...) and this will happen for each 
residual block added.

I could rewrite my cost function so that it will be added to the problem 
only once, returning an array of residuals. But I will still have the 
problem that a new Model object is created at each function  call.

what would be the preferred way of doing that?

Any idea/help/suggestion? I like the idea to keep the classes defining the 
model separated by the solver - with no ceres-related code, so that they 
can be reused in other parts of my program for other tasks without creating 
confusion. Is it even possible?

Many thanks
Luca



-- 
-- 
Check the OTB FAQ at
http://www.orfeo-toolbox.org/FAQ.html

You received this message because you are subscribed to the Google
Groups "otb-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/otb-users?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"otb-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to