Serialization of gtsam classes such as gtsam::NonlinearFactorGraph
Just a question: Is there a minimal working example for the serialization of e.g. gtsam::NonlinearFactorGraph?
With
gtsam::NonlinearFactorGraph factors;
gtsam::Symbol x1('x',1);
gtsam::Pose2 prior(0.0, 0.0, 0.0);
gtsam::noiseModel::Diagonal::shared_ptr priorNoise =
gtsam::noiseModel::Diagonal::Sigmas(gtsam::Vector3(0.3, 0.3, 0.1));
factors.add(gtsam::PriorFactor<gtsam::Pose2>(x1, prior, priorNoise));
gtsam::serializeToFile(factors, "factor");
I get
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): unregistered class - derived class not registered or exported
Same with gtsam::Values.
What am I missing? Thanks a lot.
Comments (12)
-
-
Hi @hitimo , that's a pitfall when using boost serialization for derived class. You need to call BOOST_CLASS_EXPORT_GUID to regester the derived class before trying to serialize it. See http://www.boost.org/doc/libs/1_32_0/libs/serialization/doc/special.html for explanation and gtsam/navigation/tests/testImuFactorSerialization.cpp as an example.
-
- changed status to resolved
Question answered
-
reporter @jdong37 Thanks for the help.
-
I add
BOOST_CLASS_EXPORT_GUID(gtsam::NonlinearFactorGraph, "NonlinearFactorGraph")
also try:
BOOST_CLASS_EXPORT(gtsam::NonlinearFactorGraph) BOOST_CLASS_EXPORT_IMPLEMENT(gtsam::NonlinearFactorGraph)
and it say the same error:
terminate called after throwing an instance of 'boost::archive::archive_exception' what(): unregistered class - derived class not registered or exported
can you resolve it? @dellaert @jdong37
I am using gtsam 4.0.0 (from devel branch). -
Hi @talregev you need export the factor classes you use, not the factor graph class. For example, if you use Prior factor for Vector3 in the graph, try
typedef PriorFactor<Vector3> PriorFactorVector3; BOOST_CLASS_EXPORT_GUID(PriorFactorVector3, "gtsam::PriorFactorVector3");
-
it doesn't work. I will copy my program:
#include <gtsam/slam/PoseTranslationPrior.h> #include <gtsam/base/serialization.h> #include <gtsam/base/serializationTestHelpers.h> #include <gtsam/geometry/Pose3.h> #include <gtsam/geometry/Pose2.h> #include <gtsam/slam/BetweenFactor.h> #include <gtsam/slam/PriorFactor.h> #include <gtsam/inference/Symbol.h> #include <gtsam/nonlinear/NonlinearFactorGraph.h> typedef gtsam::PriorFactor<gtsam::Pose2> PriorFactorPose2; typedef gtsam::BetweenFactor<gtsam::Pose2> BetweenFactorPose2; BOOST_CLASS_EXPORT_GUID(PriorFactorPose2,"gtsam::PriorFactorPose2") BOOST_CLASS_EXPORT_GUID(BetweenFactorPose2, "gtsam::BetweenFactorPose2") int main(int argc, char** argv) { gtsam::Key i1 = gtsam::symbol('x',1); gtsam::Key i2 = gtsam::symbol('x',2); gtsam::Key i3 = gtsam::symbol('x',3); gtsam::Key j1 = gtsam::symbol('l',1); gtsam::Key j2 = gtsam::symbol('l',2); gtsam::NonlinearFactorGraph graph; gtsam::Pose2 priorMean = gtsam::Pose2(0.0, 0.0, 0.0); gtsam::Vector sigma_p(3); sigma_p << 0.3, 0.3, 0.1; gtsam::SharedNoiseModel priorNoise = gtsam::noiseModel::Diagonal::Sigmas(sigma_p); PriorFactorPose2 prior = PriorFactorPose2(i1, priorMean, priorNoise); graph.add(prior); gtsam::Pose2 odometry = gtsam::Pose2(2.0, 0.0, 0.0); gtsam::Vector sigma(3); sigma << 0.2, 0.2, 0.2; gtsam::SharedNoiseModel odometryNoise = gtsam::noiseModel::Diagonal::Sigmas(sigma); graph.add(BetweenFactorPose2(i1, i2, odometry, odometryNoise)); graph.add(BetweenFactorPose2(i2, i3, odometry, odometryNoise)); std::string serialized; serialized = gtsam::serialize(graph); std::cout << serialized; return 0; }
-
do you have a tests cpp or example that i can compile?
-
i copy all definition from testSerializationSLAM.cpp then it work :\
-
I got the same error there. I try
BOOST_CLASS_EXPORT_GUID
and all definition from testSerializationSLAM.cpp but error still in gtsam::Values and gtsam::NonlinearFactorGraph. Anyone can help me ? -
You need to make sure all factors you want to export are registered. Can’t be of much help beyond this. PS we migrated to GitHub years ago, might want to re-open an issue there if it still does not work, although this might be more for the google user group.
-
Start from this source code:
https://github.com/borglab/gtsam/blob/develop/tests/testSerializationSLAM.cpp
Make sure of adding boost macros for every single class and template instance you use in your program! It’s really hard to debug since boost error messages doesn’t help, but in the end it works
- Log in to comment
@jdong37 has experience with this.