ProfFan / gtsam

GTSAM is a library of C++ classes that implement smoothing and mapping (SAM) in robotics and vision, using factor graphs and Bayes networks as the underlying computing paradigm rather than sparse matrices.
http://gtsam.org
Other
0 stars 0 forks source link

pybind11 needs special attention for binding STL types #1

Closed ProfFan closed 4 years ago

ProfFan commented 5 years ago
Python 3.7.4 (default, Sep  7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtsam_py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: generic_type: type "FactorIndexSet" is already registered!

https://pybind11.readthedocs.io/en/stable/upgrade.html#local-stl-container-bindings

We need to recognize that these types are actually std::sets, et cetera, and use py::bind_vector and opaque bindings.

Reference

https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html

ProfFan commented 5 years ago

@matthewsklar If you already done this, please push to GH.

ProfFan commented 5 years ago

Compiles on my machine:

cmake -DBOOST_ROOT=YOURBOOSTROOT -Dpybind11_DIR=<> -DGTSAM_TYPEDEF_POINTS_TO_VECTORS=ON -DGTSAM_INSTALL_PYTHON_TOOLBOX=ON ..

ProfFan commented 5 years ago

After some magic (deleting FactorIndexSet) it works:

Python 3.7.4 (default, Sep  7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gtsam_py import gtsam
>>> import numpy as np
>>> expected = gtsam.Cal3Unified(100 + 2, 105 + 3, 0.0 + 4, 320 + 5, 240 + 6,
...                                      1e-3 + 7, 2.0*1e-3 + 8, 3.0*1e-3 + 9, 4.0*1e-3 + 10, 0.1 + 1)
>>> K = gtsam.Cal3Unified(100, 105, 0.0, 320, 240,
...                               1e-3, 2.0*1e-3, 3.0*1e-3, 4.0*1e-3, 0.1)
>>> d = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 1], order='F')
>>> actual = K.retract(d)
>>> actual.equals(expected, 0.001)
True
>>>

PoseSLAM2

Python 3.7.4 (default, Sep  7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gtsam_py import gtsam
>>> import numpy as np
>>> graph = gtsam.NonlinearFactorGraph()
>>> priorMean = gtsam.Pose2(0.0, 0.0, 0.0)
>>> priorNoise = gtsam.noiseModel_Diagonal.Sigmas(np.array([0.3, 0.3, 0.1]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'gtsam_py.gtsam' has no attribute 'noiseModel_Diagonal'
>>> priorNoise = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.3, 0.3, 0.1]))
>>> graph.add(gtsam.PriorFactorPose2(1, priorMean, priorNoise))
>>> odometryNoise = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.2, 0.2, 0.1]))
>>> graph.add(gtsam.BetweenFactorPose2(
...             1, 2, gtsam.Pose2(2.0, 0.0, 0.0), odometryNoise))
>>> graph.add(gtsam.BetweenFactorPose2(
...             2, 3, gtsam.Pose2(2.0, 0.0, pi / 2), odometryNoise))
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'pi' is not defined
>>> from math import pi
>>> graph.add(gtsam.BetweenFactorPose2(
...             2, 3, gtsam.Pose2(2.0, 0.0, pi / 2), odometryNoise))
>>> graph.add(gtsam.BetweenFactorPose2(
...             3, 4, gtsam.Pose2(2.0, 0.0, pi / 2), odometryNoise))
>>> graph.add(gtsam.BetweenFactorPose2(
...             4, 5, gtsam.Pose2(2.0, 0.0, pi / 2), odometryNoise))
>>> model = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.2, 0.2, 0.1]))
>>> graph.add(gtsam.BetweenFactorPose2(5, 2, gtsam.Pose2(2.0, 0.0, pi / 2), model))
>>> initialEstimate = gtsam.Values()
>>> initialEstimate.insert(1, gtsam.Pose2(0.5, 0.0, 0.2))
>>> initialEstimate.insert(2, gtsam.Pose2(2.3, 0.1, -0.2))
>>> initialEstimate.insert(3, gtsam.Pose2(4.1, 0.1, pi / 2))
>>> initialEstimate.insert(4, gtsam.Pose2(4.0, 2.0, pi))
>>> initialEstimate.insert(5, gtsam.Pose2(2.1, 2.1, -pi / 2))
>>> optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initialEstimate)
>>> result = optimizer.optimize
optimizer.optimize(        optimizer.optimizeSafely(
>>> result = optimizer.optimizeSafely()
>>> marginals = gtsam.Marginals(graph, result)
>>> P = marginals.marginalCovariance(1)
>>> pose_1 = re
repr(      result     return     reversed(
>>> pose_1 = result.at
result.atCal3Bundler(      result.atDouble(           result.atPoint3(           result.atRot3(
result.atCal3DS2(          result.atEssentialMatrix(  result.atPose2(            result.atVector(
result.atCal3_S2(          result.atMatrix(           result.atPose3(
result.atConstantBias(     result.atPoint2(           result.atRot2(
>>> pose_1 = result.atPo
result.atPoint2(  result.atPoint3(  result.atPose2(   result.atPose3(
>>> pose_1 = result.atPo
result.atPoint2(  result.atPoint3(  result.atPose2(   result.atPose3(
>>> pose_1 = result.atPose2(1)
>>> pose_1.
pose_1.Adjoint(           pose_1.adjointTranspose(  pose_1.inverse(           pose_1.theta(
pose_1.AdjointMap(        pose_1.adjoint_(          pose_1.localCoordinates(  pose_1.transformFrom(
pose_1.Expmap(            pose_1.bearing(           pose_1.matrix(            pose_1.transformTo(
pose_1.ExpmapDerivative(  pose_1.between(           pose_1.print_(            pose_1.translation(
pose_1.Logmap(            pose_1.compose(           pose_1.range(             pose_1.wedge(
pose_1.LogmapDerivative(  pose_1.equals(            pose_1.retract(           pose_1.x(
pose_1.adjointMap_(       pose_1.identity(          pose_1.rotation(          pose_1.y(
>>> s=""
>>> pose_1.print_(s)
(-3.62586e-13, -1.35991e-12, -5.28358e-13)
>>> pose_1.equals(gtsam.Pose2(), 1e-4)
True
>>>
matthewsklar commented 5 years ago

I'm running the cmake you gave and am getting 2 errors

CMake Error at cmake/GtsamPybindWrap.cmake:44 (target_link_libraries): The keyword signature for target_link_libraries has already been used with the target "gtsam_py". All uses of target_link_libraries with a target must be either all-keyword or all-plain.

The uses of the keyword signature are here:

and

CMake Error at CMakeLists.txt:581 (print_config_flag): print_config_flag Function invoked with incorrect arguments for function named: print_config_flag

ProfFan commented 5 years ago

ahh let me check, i think i have a change I forgot to push

ProfFan commented 5 years ago

done :) @matthewsklar

ProfFan commented 5 years ago

We also need to add statements to make the STL types opaque in pybind11.

ProfFan commented 5 years ago

@matthewsklar I guess you are on the wrong branch, it's fan/dev/wrapper.

matthewsklar commented 5 years ago

When I run cmake -Dpybind11_DIR=<path to pybind11 cmake> -DGTSAM_TYPEDEF_POINTS_TO_VECTORS=ON -DGTSAM_INSTALL_PYTHON_TOOLBOX=ON .. It seems to run fine (no errors in build output)

However, when I try to use gtsam_py in python I get

[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gtsam_py import gtsam
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gtsam_py'

My cmake output is


-- The C compiler identification is GNU 8.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- GTSAM_SOURCE_ROOT_DIR: [/home/matthew/Documents/GT/GTSAM/gtsam]
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Boost version: 1.67.0
-- Found the following Boost libraries:
--   serialization
--   system
--   filesystem
--   thread
--   program_options
--   date_time
--   timer
--   chrono
--   regex
--   atomic
-- GTSAM_BOOST_LIBRARIES: optimized;/usr/lib/x86_64-linux-gnu/libboost_serialization.so;optimized;/usr/lib/x86_64-linux-gnu/libboost_system.so;optimized;/usr/lib/x86_64-linux-gnu/libboost_filesystem.so;optimized;/usr/lib/x86_64-linux-gnu/libboost_thread.so;optimized;/usr/lib/x86_64-linux-gnu/libboost_date_time.so;optimized;/usr/lib/x86_64-linux-gnu/libboost_regex.so;debug;/usr/lib/x86_64-linux-gnu/libboost_serialization.so;debug;/usr/lib/x86_64-linux-gnu/libboost_system.so;debug;/usr/lib/x86_64-linux-gnu/libboost_filesystem.so;debug;/usr/lib/x86_64-linux-gnu/libboost_thread.so;debug;/usr/lib/x86_64-linux-gnu/libboost_date_time.so;debug;/usr/lib/x86_64-linux-gnu/libboost_regex.so
Ignoring Boost restriction on optional lvalue assignment from rvalues
-- Could NOT find TBB (missing: TBB_INCLUDE_DIRS TBB_LIBRARIES tbb tbbmalloc) (Required is at least version "4.4")
-- Could NOT find MKL (missing: MKL_INCLUDE_DIR MKL_LIBRARIES) 
-- Found OpenMP_C: -fopenmp (found version "4.5") 
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 
-- Found OpenMP: TRUE (found version "4.5")  
-- Found Eigen version: 3.3.7
-- Found PythonInterp: /usr/bin/python3.7 (found version "3.7.3") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.7m.so
-- Performing Test HAS_CPP14_FLAG
-- Performing Test HAS_CPP14_FLAG - Success
-- Performing Test HAS_FLTO
-- Performing Test HAS_FLTO - Success
-- LTO enabled
Python Wrap target: gtsam_py LIBS: gtsam DEP: gtsam;gtsam_header
-- Building 3rdparty
-- Looking for execinfo.h
-- Looking for execinfo.h - found
-- Looking for getline
-- Looking for getline - found
-- checking for thread-local storage - found
-- Could NOT find GeographicLib (missing: GeographicLib_LIBRARY_DIRS GeographicLib_LIBRARIES GeographicLib_INCLUDE_DIRS) 
-- Building base
-- Building geometry
-- Building inference
-- Building symbolic
-- Building discrete
-- Building linear
-- Building nonlinear
-- Building sam
-- Building sfm
-- Building slam
-- Building smart
-- Building navigation
-- GTSAM Version: 4.0.0
-- Install prefix: /usr/local
-- Building GTSAM - shared: ON
-- Building base_unstable
-- Building geometry_unstable
-- Building linear_unstable
-- Building discrete_unstable
-- Building dynamics_unstable
-- Building nonlinear_unstable
-- Building slam_unstable
-- Building partition_unstable
-- GTSAM_UNSTABLE Version: 4.0.0
-- Install prefix: /usr/local
-- Wrote /home/matthew/Documents/GT/GTSAM/gtsam/build/GTSAMConfig.cmake
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.13") found components:  doxygen missing components:  dot
-- ===============================================================
-- ================  Configuration Options  ======================
--   CMAKE_CXX_COMPILER_ID type     : GNU
--   CMAKE_CXX_COMPILER_VERSION     : 8.3.0
--   CMake version                  : 3.13.4
--   CMake generator                : Unix Makefiles
--   CMake build tool               : /usr/bin/make
-- Build flags                                               
--   Build Tests                    : Enabled
--   Build examples with 'make all' : Enabled
--   Build timing scripts with 'make all': Disabled
--   Build Docs                     : Enabled
--   Build shared GTSAM libraries   : Enabled
--   Put build type in library name : Enabled
--   Build libgtsam_unstable        : Enabled
--   Build for native architecture  : Enabled
--   Build type                     : Release
--   C compilation flags            :  -O3 -DNDEBUG
--   C++ compilation flags          :  -O3 -DNDEBUG
--  GTSAM_COMPILE_FEATURES_PUBLIC                    : cxx_std_11
--  GTSAM_COMPILE_OPTIONS_PRIVATE                    : -Wall;$<$<CONFIG:Debug>:-g;-fno-inline>;$<$<CONFIG:Release>:-O3>;$<$<CONFIG:Timing>:-g;-O3>;$<$<CONFIG:Profiling>:-O3>;$<$<CONFIG:RelWithDebInfo>:-g;-O3>;-Wno-unused-local-typedefs
--  GTSAM_COMPILE_OPTIONS_PUBLIC                     : -march=native
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE                : $<$<CONFIG:Debug>:_DEBUG;EIGEN_INITIALIZE_MATRICES_BY_NAN>;$<$<CONFIG:Release>:NDEBUG>;$<$<CONFIG:Timing>:NDEBUG;ENABLE_TIMING>;$<$<CONFIG:Profiling>:NDEBUG>;$<$<CONFIG:RelWithDebInfo>:NDEBUG>
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC                 : BOOST_OPTIONAL_ALLOW_BINDING_TO_RVALUES;BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
--  GTSAM_COMPILE_OPTIONS_PRIVATE_DEBUG              : -g;-fno-inline
--  GTSAM_COMPILE_OPTIONS_PUBLIC_DEBUG               : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_DEBUG          : _DEBUG;EIGEN_INITIALIZE_MATRICES_BY_NAN
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_DEBUG           : 
--  GTSAM_COMPILE_OPTIONS_PRIVATE_RELEASE            : -O3
--  GTSAM_COMPILE_OPTIONS_PUBLIC_RELEASE             : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_RELEASE        : NDEBUG
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_RELEASE         : 
--  GTSAM_COMPILE_OPTIONS_PRIVATE_TIMING             : -g;-O3
--  GTSAM_COMPILE_OPTIONS_PUBLIC_TIMING              : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_TIMING         : NDEBUG;ENABLE_TIMING
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_TIMING          : 
--  GTSAM_COMPILE_OPTIONS_PRIVATE_PROFILING          : -O3
--  GTSAM_COMPILE_OPTIONS_PUBLIC_PROFILING           : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_PROFILING      : NDEBUG
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_PROFILING       : 
--  GTSAM_COMPILE_OPTIONS_PRIVATE_RELWITHDEBINFO     : -g;-O3
--  GTSAM_COMPILE_OPTIONS_PUBLIC_RELWITHDEBINFO      : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_RELWITHDEBINFO : NDEBUG
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_RELWITHDEBINFO  : 
--  GTSAM_COMPILE_OPTIONS_PRIVATE_MINSIZEREL         : 
--  GTSAM_COMPILE_OPTIONS_PUBLIC_MINSIZEREL          : 
--  GTSAM_COMPILE_DEFINITIONS_PRIVATE_MINSIZEREL     : 
--  GTSAM_COMPILE_DEFINITIONS_PUBLIC_MINSIZEREL      : 
--   Use System Eigen               : OFF (Using version: 3.3.7)
--   Use Intel TBB                  : TBB not found
--   Eigen will use MKL             : MKL not found
--   Eigen will use MKL and OpenMP  : OpenMP found but GTSAM_WITH_EIGEN_MKL is disabled
--   Default allocator              : STL
--   Build with ccache              : No
-- Packaging flags                                               
--   CPack Source Generator         : TGZ
--   CPack Generator                : TGZ
-- GTSAM flags                                               
--   Quaternions as default Rot3     : Disabled
--   Runtime consistency checking    : Disabled
--   Rot3 retract is full ExpMap     : Disabled
--   Pose3 retract is full ExpMap    : Disabled
--   Deprecated in GTSAM 4 allowed   : Enabled
--   Point3 is typedef to Vector3    : Enabled
--   Metis-based Nested Dissection   : Enabled
--   Use tangent-space preintegration: Enabled
--   Build Wrap                     : Enabled
-- MATLAB toolbox flags                                      
--   Install matlab toolbox         : Disabled
-- Python module flags                                       
--   Build python module            : Enabled
--   Python version                 : 
-- Python toolbox flags                                      
--   Install Python toolbox         : Enabled
-- ===============================================================
CMake Warning at CMakeLists.txt:605 (message):
  TBB 4.4 or newer was not found - this is ok, but note that GTSAM
  parallelization will be disabled.  Set GTSAM_WITH_TBB to 'Off' to avoid
  this warning.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/matthew/Documents/GT/GTSAM/gtsam/build```
ProfFan commented 5 years ago

See Slack,

If you can compile it, go to build/wrap, and run cp gtsam_pyDebug.cpython-37m-darwin.so gtsam_py.cpython-37m-darwin.so Then PYTHONPATH=$PWD python should give you a python shell with GTSAM from gtsam_py import gtsam, and you will have GTSAM in Python :slightly_smiling_face:

ProfFan commented 5 years ago

Did you run make?