Serialization of gtsam classes such as gtsam::NonlinearFactorGraph

Issue #307 resolved
Timo Hinzmann created an issue

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)

  1. Tal Regev

    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).

  2. Jing Dong

    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");
    
  3. Tal Regev

    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;
    }
    
  4. Life Zero

    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 ?

  5. Frank Dellaert

    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.

  6. Log in to comment