![](/img/trans.png)
[英]pass parameters of double but get Jet<double,6>when using ceres solver
[英]Ceres solver number of parameters question
我写了一个像这样的函子:
class Functor
{
...
static CostFunction* create()
{
return new AutoDiffCostFunction<Functor, N_RES, N_PARAMS>(new Functor());
}
...
}
我称之为:
std::vector<double> params(N_PARAMS, 0);
CostFunction* costFunc = Functor::create();
problem.AddResidualBlock(costFunc, nullptr, params.data());
奇怪的是,这在N_PARAMS=236
时N_PARAMS=236
,但适用于6
、 237
(我试过)。 错误消息是段错误。 而且我之前用过Ceres Solver,从来没有遇到过这样的问题。
Ceres Solver 中的参数数量有限制吗?
==========================================更新======== ============ 我是这样写的:
class ReprojError {
public:
ReprojError(
const std::vector<std::vector<Eigen::Vector3d>>& pt2d,
const std::vector<Eigen::Matrix<double, 3, 4>>& vmProjs) :
m_pt2d(pt2d), m_vmProjs(vmProjs) { }
template<typename _Tp>
bool operator () (const _Tp* const pts, _Tp* aResiduals) const
{
for(int i = 0; i < 24; ++i)
{
std::cout << "?" << std::endl;
if(m_pt2d[i].empty())
{
for(int j = 0; j < 276; ++j)
{
aResiduals[i * 276 * 2 + j * 2] = _Tp(0);
aResiduals[i * 276 * 2 + j * 2 + 1] = _Tp(0);
}
}
else
{
auto proj = m_vmProjs[i];
for(int j = 0; j < 276; ++j)
{
auto X = pts[j * 3];
auto Y = pts[j * 3 + 1];
auto Z = pts[j * 3 + 2];
auto newX = (_Tp)proj(0, 0) * X + (_Tp)proj(0, 1) * Y + (_Tp)proj(0, 2) * Z + (_Tp)proj(0, 3);
auto newY = (_Tp)proj(1, 0) * X + (_Tp)proj(1, 1) * Y + (_Tp)proj(1, 2) * Z + (_Tp)proj(1, 3);
auto newZ = (_Tp)proj(2, 0) * X + (_Tp)proj(2, 1) * Y + (_Tp)proj(2, 2) * Z + (_Tp)proj(2, 3);
aResiduals[i * 276 * 2 + j * 2] = newX / newZ - m_pt2d[i][j](0);
aResiduals[i * 276 * 2 + j * 2 + 1] = newY / newZ - m_pt2d[i][j](1);
}
}
}
return true;
}
static ceres::CostFunction* create(
const std::vector<std::vector<Eigen::Vector3d>>& pt2d,
const std::vector<Eigen::Matrix<double, 3, 4>>& vmProjs)
{
return (new ceres::AutoDiffCostFunction<ReprojError, 24 * 2 * 276, 276 * 3>(
new ReprojError(pt2d, vmProjs)));
}
private:
const std::vector<std::vector<Eigen::Vector3d>>& m_pt2d;
const std::vector<Eigen::Matrix<double, 3, 4>>& m_vmProjs;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
并定义了:
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Vector3d)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Matrix<double, 3, 4>)
和 '?' 没有输出,这意味着 operator() 没有被执行。
没有这样的参数限制。 在调试模式下编译代码以查看发生了什么会很有帮助。
也就是说,你为什么要把所有这些都放在一个单一的成本函数中,为什么不为每个观察都有一个成本函数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.