Skip to content

Instantly share code, notes, and snippets.

@nguyenhoan1988
Created October 19, 2021 22:05
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781 to your computer and use it in GitHub Desktop.
Save nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781 to your computer and use it in GitHub Desktop.
Installing dlib using conda with CUDA enabled

Installing dlib using conda with CUDA enabled

Prerequisite: conda and/or miniconda are already installed

  1. Create a conda environment.
$ conda create -n dlib python=3.8 cmake ipython
  1. Activate the environment.
$ conda activate dlib
  1. Install CUDA and cuDNN with conda using nvidia channel
$ conda install cuda cudnn -c nvidia

Then find the path to the nvcc of this environment. We will use this path for the build step below

$which nvcc
/path/to/your/miniconda3/envs/dlib/bin/
  1. Install dlib. Clone and build dlib from source
$ git clone https://github.com/davisking/dlib.git
$ cd dlib
$ mkdir build
$ cd build
$ cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DCUDAToolkit_ROOT=/path/to/your/miniconda3/envs/dlib/bin/
$ cmake --build .
$ cd ..
$ python setup.py install --set DLIB_USE_CUDA=1
  1. Test dlib
(dlib) $ ipython
Python 3.8.12 (default, Oct 12 2021, 13:49:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dlib

In [2]: dlib.DLIB_USE_CUDA
Out[2]: True

In [3]: print(dlib.cuda.get_num_devices())
1
@ravikiranrao
Copy link

Cleanest installation guide of dlib (with CUDA) on the internet. Thanks a ton.

@sashavor
Copy link

sashavor commented Sep 14, 2022

Is there specific cuda toolkit version that works with dlib? I can't manage to get it to run:

cnn_face_detector = dlib.cnn_face_detection_model_v1('dlib_models/mmod_human_face_detector.dat')
RuntimeError: Error while calling cudaMallocHost(&data, new_size*sizeof(float)) in ./dlib/dlib/cuda/gpu_data.cpp:211. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.

@Amr-YA
Copy link

Amr-YA commented Sep 20, 2022

Surprisingly, this the only way it worked.
Although it froze still while building a sample cuda project and didn't continue till I pressed "space", no idea why or how but it worked.

@sherelynyap
Copy link

helped me a lot, thanks!

@brianholland
Copy link

brianholland commented Oct 12, 2022

Many thanks! Now I'm off to the next glitch:

Same problem, I think, as @sashavor :

dlib '19.24.99', face_recognition_models '0.1.0'
Ubuntu 20.04, python 3.10

import face_recognition_models
import dlib
cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

I get

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In [1], line 4
     2 import dlib
     3 cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
----> 4 cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

RuntimeError: Error while calling cudaMallocHost(&data, new_size*sizeof(float)) in file /home/brian/dlib/dlib/cuda/gpu_data.cpp:211. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.

@EylonSho
Copy link

Many thanks! Now I'm off to the next glitch:

Same problem, I think, as @sashavor :

dlib '19.24.99', face_recognition_models '0.1.0' Ubuntu 20.04, python 3.10

import face_recognition_models
import dlib
cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

I get

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In [1], line 4
     2 import dlib
     3 cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
----> 4 cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

RuntimeError: Error while calling cudaMallocHost(&data, new_size*sizeof(float)) in file /home/brian/dlib/dlib/cuda/gpu_data.cpp:211. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.

Any suggestion for this issue please?

@gipinze
Copy link

gipinze commented Dec 28, 2022

Hi,

Thanks for your explanation, I used it, mixing it with some other I found around internet to make a new one that worked for me

I used my device CUDA,=11.5 and Cudnn=8.3 and runs in Anaconda with python=3.8 as well

The biggest differences is that, I guess due to the time you uploaded this, with VS 2022 is not possible to compile DLIB, so we need to specify the version and not use the GIF_SUPPORT library

Thanks again for your clear explanation

@deerwinter
Copy link

for windows user who met the
import dlib Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\oceanx\AppData\Local\Programs\Python\Python310\lib\site-packages\dlib-19.24.99-py3.10-win-amd64.egg\dlib\__init__.py", line 19, in <module> from _dlib_pybind11 import * ImportError: DLL load failed while importing _dlib_pybind11: The specified module could not be found.
check your __init__.py , make sure its "on"=="on".

@ankit-g
Copy link

ankit-g commented May 2, 2023

I'm getting this error:

Could not load library libcudnn_cnn_train.so.8. Error: /home/ankit/anaconda3/envs/dlib/bin/../lib/libcudnn_ops_train.so.8: undefined symbol: _Z22cudnnGenericOpTensorNdILi3EE13cudnnStatus_tP12cudnnContext16cudnnGenericOp_t21cudnnNanPropagation_tPKdPKvPK17cudnnTensorStructS8_S8_SB_S8_S8_SB_Pv, version libcudnn_ops_infer.so.8

@mtalhaubaid
Copy link

mtalhaubaid commented Jun 13, 2023

I got this message after installing.

Installed c:\users\asdf\anaconda3\envs\dlib\lib\site-packages\dlib-19.24.99-py3.8-win-amd64.egg
Processing dependencies for dlib==19.24.99
Finished processing dependencies for dlib==19.24.99

But when try to test dlib it gives this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ASDF\anaconda3\envs\dlib\lib\site-packages\dlib-19.24.99-py3.8-win-amd64.egg\dlib\__init__.py", line 19, in <module>
    from _dlib_pybind11 import *
ImportError: DLL load failed while importing _dlib_pybind11: The specified module could not be found.
>>> import dlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ASDF\anaconda3\envs\dlib\lib\site-packages\dlib-19.24.99-py3.8-win-amd64.egg\dlib\__init__.py", line 19, in <module>
    from _dlib_pybind11 import *
ImportError: DLL load failed while importing _dlib_pybind11: The specified module could not be found.
>>> import dlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ASDF\anaconda3\envs\dlib\lib\site-packages\dlib-19.24.99-py3.8-win-amd64.egg\dlib\__init__.py", line 19, in <module>
    from _dlib_pybind11 import *
ImportError: DLL load failed while importing _dlib_pybind11: The specified module could not be found.

@DUDUKorte
Copy link

getting this error:

Could not load library libcudnn_cnn_train.so.8. Error: /home/ankit/anaconda3/envs/dlib/bin/../lib/libcudnn_ops_train.so.8: undefined symbol: _Z22cudnnGenericOpTensorNdILi3EE13cudnnStatus_tP12cudnnContext16cudnnGenericOp_t21cudnnNanPropagation_tPKdPKvPK17cudnnTensorStructS8_S8_SB_S8_S8

i got the exactly same error, anyone knows how to fix it???

@bwajster97
Copy link

Does this work on Ubuntu 18.04? Also, can you share the link to install conda?

@mtalhaubaid
Copy link

@bwajster97 I think It should work on ubuntu as well

@lingster
Copy link

lingster commented Feb 8, 2024

if you are running on arch linux and you are getting these errors try temporarily downgrading your cc/gcc compilers like this:

export CXX="/usr/bin/g++-12"
export CC="/usr/bin/gcc-12"

or wherever the correct path you have installed the previous versions of the compiler.

@s2485523800
Copy link

Thanks a lot.
maybe somebody installed cudnn but dlib said not found, you can use " set CMAKE_PREFIX_PATH=path/to/cudnn"( in conda is /Library) to fix this problem

@huhreenee
Copy link

Hi @s2485523800, I am facing this issue, unfortunately, I am not able to set CMAKE_PREFIX_PATH correctly. Could you please help?

@s2485523800
Copy link

Hi @s2485523800, I am facing this issue, unfortunately, I am not able to set CMAKE_PREFIX_PATH correctly. Could you please help?

maybe you can take a photo about your conda env path,and conda list

@semajbaker
Copy link

thanks. Worked for me too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment