AutoDifferentiation of a Sampled function

730 views
Skip to first unread message

Manohar Kuse

unread,
Aug 5, 2016, 1:19:08 AM8/5/16
to Ceres Solver
I have a cost function which is a sampled function. The function is R^2 --> R (stored as a 2d matrix). However as I look up the values
in operator() it gives the following compiler error: The relevant part of the error is as follows:

/home/mpkusex/ros_workspaces/robotic_vision/src/edge_alignment/src/SolveEA.cpp:349:1:   required from here
/home/mpkusex/ros_workspaces/robotic_vision/src/edge_alignment/include/EAResidue.h:151:60: error: no match for call to ‘(const MatrixXd {aka const Eigen::Matrix<double, -1, -1>}) (ceres::Jet<double, 7>&, ceres::Jet<double, 7>&)’
             double readVal = now_dist_transform_eig( _v, _u);


I am aware that ceres version 1.11 
  1. has CubicInterpolator and BiCubicInterpolator to allow smooth interpolation of sampled functions and integration with automatic differentiation.
    I cannot quite figure out how to get it working.
Looking for your reply..!

My class :
class EAResidue {
public:
    /// Set the constants of the problem
    /// @param list_edge_ref : a list of 3d points in reference image 3xN
    /// @param now_dist_transform_eig : distance transform of now frame as an image (row x cols)
    EAResidue( MatrixXd& __list_edge_ref, MatrixXd& __now_dist_transform_eig, Matrix3d& K )
    {
        list_edge_ref = __list_edge_ref;
        now_dist_transform_eig = __now_dist_transform_eig;

        fx = ( K(0,0) );
        fy = ( K(1,1) );
        cx = ( K(0,2) );
        cy = ( K(1,2) );
    }


    template <typename T>
    bool operator()(const T* const Q, const T* const t, T* residual) const
    {
        T R[9];
        ceres::QuaternionToRotation(Q, R); //use `ceres::QuaternionRotatePoint`

        for( int i =0 ; i<list_edge_ref.cols() ; i++ )
        {
            T _x = T(list_edge_ref(0,i));
            T _y = T(list_edge_ref(1,i));
            T _z = T(list_edge_ref(2,i));

            // xd = R*x + t
            T _xd = t[0] + R[0] * _x + R[3] * _y + R[6] * _z;
            T _yd = t[1] + R[1] * _x + R[4] * _y + R[7] * _z;
            T _zd = t[2] + R[2] * _x + R[5] * _y + R[8] * _z;

            // de-homegenous and project
            T _u = T(fx) * _xd / _zd + T(cx);
            T _v = T(fy) * _yd / _zd + T(cy);

            double readVal = now_dist_transform_eig( _v, _u); 
//            residual[i] = T( now_dist_transform_eig( (int)_v, (int)_u) ); // <<<<<< This line is the trouble. 

        }

        return true;
    }

private:
    MatrixXd list_edge_ref;
    MatrixXd now_dist_transform_eig;
    double fx, fy, cx, cy;
};

Sameer Agarwal

unread,
Aug 5, 2016, 1:22:48 AM8/5/16
to Ceres Solver
The problem is the line 

double readVal = now_dist_transform_eig( _v, _u);

you are using values of type T (which in this case are Jets) to index into a matrix.

is now_dist_transform_eig the matrix you want to interpolate ?

Sameer

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/c56bceb3-8004-4237-8b49-7761ee47cd04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manohar Kuse

unread,
Aug 5, 2016, 1:24:44 AM8/5/16
to ceres-...@googlegroups.com
Yes. I need to interpolate that matrix. 
I should be using Grid2D I think, but cannot figure out how.

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CABqdRUA9NW2HSb-qX-%2Bpjcf%3DuP40kS5WZ4jh2AkDzMOGi7t0-g%40mail.gmail.com.

Sameer Agarwal

unread,
Aug 5, 2016, 1:25:22 AM8/5/16
to ceres-...@googlegroups.com
have you looked at the example code or the tests?

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CAP%2Bw43vWDnQPPo6C-ToPFR_%3DuuzQFRPVyi6_TLB3j7EX173_ug%40mail.gmail.com.

Manohar Kuse

unread,
Aug 5, 2016, 1:30:31 AM8/5/16
to ceres-...@googlegroups.com

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CABqdRUBCrHWPiTEVm039VBLrEO9EfVDW7N3fhSZuk%2BSJJ8BwFA%40mail.gmail.com.

Sameer Agarwal

unread,
Aug 5, 2016, 1:37:40 AM8/5/16
to ceres-...@googlegroups.com

MatrixXd a;
// Fill a somehow.

Grid2D<double, 1> grid(a.data(), a.rows(), a.cols());
BiCubicInterpolator<Grid2D<double, 1> > interpolated_a(grid);

now your cost function should use the interpolated_a instead of a.

class EAResidue {
public:
     EAResidue( MatrixXd& list_edge_ref, BiCubicInterpolator<Grid2D<double, 1> >& interpolated_a) 
: list_edge_ref(list_edge_ref), interpolated_a(interpolated_a) {}
    template <typename T>
    bool operator()(const T* const Q, const T* const t, T* residual) const
    {
/// some calculation
               T _u = T(fx) * _xd / _zd + T(cx);
         T _v = T(fy) * _yd / _zd + T(cy);
       interpolated_a.Evaluate(_u, _v, residuals + i);
        return true;
    }

private:
    MatrixXd list_edge_ref;
    const BiCubicInterpolator<Grid2D<double, 1> >& interpolated_a;
    double fx, fy, cx, cy;
};

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CAP%2Bw43tHqZeLEC6omWptpnHqStr%3D7RUp3b%2B-zKBqM6RtjTVPjw%40mail.gmail.com.

Manohar Kuse

unread,
Aug 5, 2016, 2:52:47 AM8/5/16
to ceres-...@googlegroups.com
Perfect...! Exactly what I was looking for..

Thanks :)

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CABqdRUDbBJn44Y6kucO6-dMLJ%2BoKcKtvnmuBw6PPEELspc-_Xw%40mail.gmail.com.

Manohar Kuse

unread,
Jul 26, 2018, 5:54:08 AM7/26/18
to Ceres Solver
 I am trying to create a 2D grid. 

e_disTrans is a MatrixXd of size 500x500. 
I am trying to create a Grid2D as you had suggested (long ago :P). 

```
ceres::Grid2D::Grid2D<double,1> grid( e_disTrans.data(), e_disTrans.rows(), e_disTrans.cols() );
```
I get a compilation issue, 
```
/home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp: In function ‘int main()’:
/home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp:103:12: error: ‘template<class T, int kDataDimension, bool kRowMajor, bool kInterleaved> struct ceres::Grid2D’ used without template parameters
     ceres::Grid2D::Grid2D<double,1> grid( e_disTrans.data(), e_disTrans.rows(), e_disTrans.cols() );
            ^
/home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp:103:27: error: expected primary-expression before ‘double’
     ceres::Grid2D::Grid2D<double,1> grid( e_disTrans.data(), e_disTrans.rows(), e_disTrans.cols() );
                           ^
CMakeFiles/edge_align.dir/build.make:62: recipe for target 'CMakeFiles/edge_align.dir/standalone_edge_align.cpp.o' failed
```

Has the usage changed? How can I fix this? 

To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/JBPk9Aj0qio/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.

I

Manohar Kuse

unread,
Jul 26, 2018, 6:01:40 AM7/26/18
to Ceres Solver
I tried this from one of the examples: 

const double data[] = {1.0, 3.0, -1.0, 4.0,
                       
3.6, 2.1,  4.2, 2.0,
                       
2.0, 1.0,  3.1, 5.2};
    ceres
::Grid2D<double, 1>  grid(data, 3, 4);


I realized ceres::Grid2D is not a namespace. 

Still get the following compilation error: 

/home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp: In function int main()’:
/home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp:111:46: error: no matching function for call to ceres::Grid2D<double, 1>::Grid2D(const double [12], int, int)’
     ceres
::Grid2D<double, 1>  grid(data, 3, 4);
                                             
^
In file included from /home/mpkuse/Downloads/edge_alignment/standalone/standalone_edge_align.cpp:16:0:
/usr/local/include/ceres/cubic_interpolation.h:390:3: note: candidate: ceres::Grid2D<T, kDataDimension, kRowMajor, kInterleaved>::Grid2D(const T*, int, int, int, int) [with T = double; int kDataDimension = 1; bool kRowMajor = true; bool kInterleaved = true]
   
Grid2D(const T* data,
   
^
/usr/local/include/ceres/cubic_interpolation.h:390:3: note:   candidate expects 5 arguments, 3 provided
/usr/local/include/ceres/cubic_interpolation.h:386:8: note: candidate: constexpr ceres::Grid2D<double, 1>::Grid2D(const ceres::Grid2D<double, 1>&)
 
struct Grid2D {
       
^
/usr/local/include/ceres/cubic_interpolation.h:386:8: note:   candidate expects 1 argument, 3 provided
/usr/local/include/ceres/cubic_interpolation.h:386:8: note: candidate: constexpr ceres::Grid2D<double, 1>::Grid2D(ceres::Grid2D<double, 1>&&)
/usr/local/include/ceres/cubic_interpolation.h:386:8: note:   candidate expects 1 argument, 3 provided
CMakeFiles/edge_align.dir/build.make:62: recipe for target 'CMakeFiles/edge_align.dir/standalone_edge_align.cpp.o'
failed
make
[2]: *** [CMakeFiles/edge_align.dir/standalone_edge_align.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/edge_align.dir/all' failed
make
[1]: *** [CMakeFiles/edge_align.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make
: *** [all] Error 2



Manohar Kuse

unread,
Jul 26, 2018, 6:04:50 AM7/26/18
to Ceres Solver

I managed to get it working!
Sorry to bother though. 

The solution was :


 
const double data[] = {1.0, 3.0, -1.0, 4.0,
                     
3.6, 2.1,  4.2, 2.0,
                   
2.0, 1.0,  3.1, 5.2};

 ceres
::Grid2D<double, 1>  grid(data, 0,3, 0,4); //< Interface changed.

The interface for Grid2D has changed I guess, need to specify the start and ends of rows and cols. 
Please correct me If i interpret it wrongly. 

Thanks

Sameer Agarwal

unread,
Jul 26, 2018, 9:18:30 AM7/26/18
to ceres-...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages