Memorizing/Jetson

Jetson Orin Nano에서 dlib 라이브러리 C++에서 빌드하기

Mingi Kim 2024. 12. 19. 18:53

파이썬을 이용해서 face_recognition, dlib을 이용해서 얼굴인식 프로젝트를 수행한 후, 그것을 C++로 옮기는 과정에서 dlib 라이브러리 빌드 오류가 발생했다. 

정확히는 얼굴 인식 모델을 불러올 때, cuda 버전의 불일치로 인해서 발생하는 오류 였다. 파이썬에서는 문제 없이 잘 되는데 오류가 발생해서 굉장히 곤란했다.

void FaceRecognitionWorker::loadModels() {
    faceDetector = get_frontal_face_detector();
    deserialize("../asset/dlib_face_recognition_resnet_model_v1.dat") >> faceRecognitionModel;

}
terminate called after throwing an instance of 'dlib::cuda_error'
  what():  Error while calling cudaMallocHost(&data, new_size*sizeof(float)) in file /home/mingi/workspace/dlib-second/dlib/dlib/cuda/gpu_data.cpp:211. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.

위의 에러를 해결하기 위해 dlib을 다시 빌드하기를 수십번.. 아무리 cuda architecture를 설정해주어도 50으로 고정되어서 나오는 것이었다..

mingi@mingi-desktop:~/workspace/dlib-second/dlib/build$ cmake -G Ninja       -DCMAKE_BUILD_TYPE=Release       -DDLIB_USE_CUDA=ON       -DCMAKE_CUDA_ARCHITECTURES=87       -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda       -DCUDA_HOST_COMPILER=/usr/bin/g++       ..
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CMake version: 3.26.4
-- Compiling dlib version: 19.24.99
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- 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  
-- Found X11: /usr/include   
-- Looking for XOpenDisplay in /usr/lib/aarch64-linux-gnu/libX11.so;/usr/lib/aarch64-linux-gnu/libXext.so
-- Looking for XOpenDisplay in /usr/lib/aarch64-linux-gnu/libX11.so;/usr/lib/aarch64-linux-gnu/libXext.so - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Found system copy of libpng: /usr/lib/aarch64-linux-gnu/libpng.so;/usr/lib/aarch64-linux-gnu/libz.so
-- Found system copy of libjpeg: /usr/lib/aarch64-linux-gnu/libjpeg.so
-- Searching for JPEG XL
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for modules 'libjxl;libjxl_cms;libjxl_threads'
--   No package 'libjxl' found
--   No package 'libjxl_cms' found
--   No package 'libjxl_threads' found
 *****************************************************************************
 *** No JPEG XL libraries found.                                           ***
 *** On Ubuntu 23.04 and newer you can install them by executing           ***
 ***    sudo apt install libjxl-dev                                        ***
 ***                                                                       ***
 *** Otherwise, you can find precompiled packages here:                    ***
 ***    https://github.com/libjxl/libjxl/releases                          ***
 *****************************************************************************
-- Searching for BLAS and LAPACK
-- Searching for BLAS and LAPACK
-- Checking for module 'cblas'
--   No package 'cblas' found
-- Checking for module 'lapack'
--   Found lapack, version 0.3.8+ds
-- Looking for cblas_ddot
-- Looking for cblas_ddot - not found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Found OpenBLAS library
-- Looking for sgetrf_single
-- Looking for sgetrf_single - found
-- Using OpenBLAS's built in LAPACK
-- Looking for cblas_ddot
-- Looking for cblas_ddot - found
-- Looking for sgesv
-- Looking for sgesv - not found
-- Looking for sgesv_
-- Looking for sgesv_ - not found
-- Found CUDA: /usr/local/cuda (found suitable version "11.8", minimum required is "7.5") 
-- Looking for cuDNN install...
-- Found cuDNN: /usr/lib/aarch64-linux-gnu/libcudnn.so
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- Building a cuDNN test project to check if you have the right version of cuDNN installed...
-- Enabling CUDA support for dlib.  DLIB WILL USE CUDA, compute capabilities: 50
-- Searching for FFMPEG/LIBAV
-- Checking for modules 'libavdevice;libavfilter;libavformat;libavcodec;libswresample;libswscale;libavutil'
--   Found libavdevice, version 58.8.100
--   Found libavfilter, version 7.57.100
--   Found libavformat, version 58.29.100
--   Found libavcodec, version 58.54.100
--   Found libswresample, version 3.5.100
--   Found libswscale, version 5.5.100
--   Found libavutil, version 56.31.100
-- Found FFMPEG/LIBAV via pkg-config in /usr/lib/aarch64-linux-gnu
-- Configuring done (44.6s)
-- Generating done (0.0s)
-- Build files have been written to: /home/mingi/workspace/dlib-second/dlib/build

하나씩 원인을 찾아보던 중 dlib에서 사용하는 CMakeLists.txt 파일을 뒤져보게 되었는데.. 아래의 부분이 50으로 고정되어있는 것을 발견했다..! 아래의 50이라는 부분을 87로 고치니 문제를 해결했다..

set(DLIB_USE_CUDA_COMPUTE_CAPABILITIES 50 CACHE STRING ${DLIB_USE_CUDA_COMPUTE_CAPABILITIES_STR})