LibTorch for object detection - ashBabu/Utilities GitHub Wiki
LibTorch object detection C++ tutorial is provided here
Sometimes a very strange error occurs when some libraries (for example yaml-cpp
) is used together with libtorch
. The error says undefined reference to ...
. This link gives a solution to this. What I did was to download the (cxx11 ABI)
version from here. This corresponds to LibTorch with cuda 11.3
. The CMakeLists.txt
then became
cmake_minimum_required(VERSION 3.8.0)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CUDA_COMPILER /usr/local/cuda-11.7/bin/nvcc)
#set(OpenGL_GL_PREFERENCE GLVND)
#set(OpenGL_GL_PREFERENCE LEGACY)
project(object_detection LANGUAGES CXX CUDA)
enable_language(CUDA)
#set(CMAKE_PREFIX_PATH "/home/ash/Ash/repo/yaml-cpp/build")
#find_package(yaml-cpp REQUIRED)
#find_library(Yaml-cpp NAMES yaml-cpp HINTS "/home/ash/Ash/repo/yaml-cpp/build")
#message(STATUS "yaml-cpp_INCLUDE_DIRS = ${YAML_CPP_INCLUDE_DIR}")
#message(STATUS "yaml-cpp_LIBS = ${YAML_CPP_LIBRARIES}")
#set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(Threads REQUIRED) # for threading
#find_package(OpenGL)
include_directories(
include
${OpenCV_INCLUDE_DIRS}
# /home/ash/Ash/repo/librealsense/examples/
${OPENGL_INCLUDE_DIRS}
# /home/ash/Ash/libtorch-1.11.0+cu113/libtorch
# "/home/ash/Ash/repo/yaml-cpp/include/yaml-cpp"
# ${YAML_CPP_INCLUDE_DIR}
)
find_package(realsense2 REQUIRED)
set(CMAKE_PREFIX_PATH "/home/ash/Ash/libtorch-1.11.0+cu113/libtorch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
#find_package( OpenCV 3.2 REQUIRED )
set(CMAKE_PREFIX_PATH "/home/ash/Ash/repo/opencv/build")
find_package( OpenCV 4.6.0 REQUIRED )
message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
add_executable(
${PROJECT_NAME}
src/Utils.cpp
src/detector/detector.cpp
src/detector/main.cpp
# src/detector/realsense.cpp
)
target_link_libraries(
${PROJECT_NAME}
# ${YAML_CPP_LIBRARIES}
${DEPENDENCIES}
${OpenCV_LIBS}
${TORCH_LIBRARIES}
${realsense2_LIBRARY}
# Threads::Threads
# glfw
# GL
# GLU
)
#target_include_directories(${TORCH_INCLUDE_DIRS})
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
####################################
#### Gtest ##################
####################################
enable_testing()
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
######## 3 #########
find_package(realsense2 REQUIRED)
add_executable(
depth_from_pixel_test
test/depth_from_pixel_test.cpp
)
include_directories(
# ${YAML_CPP_INCLUDE_DIR}
)
target_link_libraries(
depth_from_pixel_test
gtest
${OpenCV_LIBS}
${realsense2_LIBRARY}
)
add_test(depth_from_pixel_test depth_from_pixel_test)