openpose icon indicating copy to clipboard operation
openpose copied to clipboard

Including TensorFlow to OpenPose C++ project

Open dradamPK opened this issue 2 years ago • 1 comments

I'm trying to rewrite my Python Openpose project to C++ because it's much faster. As for now, everything works fine but I'm facing one problem, how to include cppflow2 and libtensorflow2 library to Openpose Cmake file? To compile raw TensorFlow example code I just type a simple command line as follow:

g++ -std=c++17 -o load_model.out -I /home/dradam/libtensorflow2/include/ -I/home/dradam/cppflow/include/ load_model.cpp -ltensorflow

and the example program compiles and runs fine.

My question is how can I add to Openpose Cmake file those libraries that I use in the above command line? Or maybe there is different way to compile custom user Openpose C++ code with TensorFlow?

dradamPK avatar Apr 22 '22 13:04 dradamPK

I found Cmake compilation example for Tensorflow at https://serizba.github.io/cppflow/quickstart.html#using-cmake. Based on that knowledge I changed CmakeList.txt (that one in dir openpose_root/examples/user_code) to this:

# Uncomment these lines with your custom file names
set(USER_CODE_FILES
    op_tf.cpp
    tf_model.cpp
    load_model.cpp)

foreach(USER_CODE_FILE ${USER_CODE_FILES})

  get_filename_component(SOURCE_NAME ${USER_CODE_FILE} NAME_WE)

  if (UNIX OR APPLE)
    set(EXE_NAME "${SOURCE_NAME}.bin")
  elseif (WIN32)
    set(EXE_NAME "UserCustomCode")
  endif ()

  ### c++ standard
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)

  ### find tensorflow library
  find_library(TENSORFLOW_LIB tensorflow HINT /home/dradam/libtensorflow2/lib)
  

  message(STATUS "Adding Example ${EXE_NAME}")
  add_executable(${EXE_NAME} ${USER_CODE_FILE})

  ### include tensorflow and cppflow
  target_include_directories(${EXE_NAME} PRIVATE /home/dradam/cppflow/include /home/dradam/libtensorflow2/include)

  ### link tensorflow
  target_link_libraries(${EXE_NAME} openpose ${examples_3rdparty_libraries} "${TENSORFLOW_LIB}")

endforeach()

Then I make all Openpose from

cd openpose_root/build && make -j20

Making process goes fine without any warnings or errors. All my custom Openpose C++ codes work fine. But when I'm trying to run Tensorflow test code tf_model.cpp:

#include <tensorflow/c/c_api.h>
#include <iostream>

using namespace std;

int main() {
  cout << "Hello from TensorFlow C++ library version" << TF_Version() << endl; // Print TF version
  return 0;
}

I get an error like this:

dradam@dradam:~/openpose$ ./build/examples/user_code/tf_model.bin 
Segmentation fault (core dumped)
dradam@dradam:~/openpose$ 

The tf_model code works fine outside Openpose:

dradam@dradam:~$ g++ -std=c++17 -o tf_model.bin -I /home/dradam/libtensorflow2/include/ -I/home/dradam/cppflow/include/ tf_model.cpp -ltensorflow
dradam@dradam:~$ ./tf_model.bin 
Hello from TensorFlow C++ library version2.7.0-dev20211101
dradam@dradam:~$ 

Any suggestions on how to solve this issue "core dumped"? Maybe someone has already run Tensorflow with Openpose successfully in C++?

dradamPK avatar Apr 23 '22 09:04 dradamPK