dlib
GPU
machine learning
Python
deep learning

How to check if dlib is using GPU or not?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Dlib is a C++ toolkit with Python bindings used for face detection, landmark prediction, and other machine learning tasks. It optionally supports CUDA GPU acceleration, but only if compiled from source with CUDA enabled. The pip-installed version does not include CUDA support. To check whether your dlib installation is using the GPU, use dlib.DLIB_USE_CUDA and dlib.cuda.get_num_devices().

Checking GPU Support

python
1import dlib
2
3# Check if dlib was compiled with CUDA support
4print(dlib.DLIB_USE_CUDA)  # True or False
5
6# Check the number of available CUDA devices
7print(dlib.cuda.get_num_devices())  # 0 if no GPU, 1+ if GPU available

If DLIB_USE_CUDA is True and get_num_devices() returns a positive number, dlib is using the GPU.

Quick One-Liner Check

python
import dlib
gpu_available = dlib.DLIB_USE_CUDA and dlib.cuda.get_num_devices() > 0
print(f"Dlib GPU acceleration: {'enabled' if gpu_available else 'disabled'}")

Verifying with a CNN Model

The most practical test is running a CNN-based face detector, which uses the GPU when available:

python
1import dlib
2import time
3
4# Download from: http://dlib.net/files/mmod_human_face_detector.dat.bz2
5cnn_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")
6
7img = dlib.load_rgb_image("test_image.jpg")
8
9start = time.time()
10detections = cnn_detector(img, 1)
11elapsed = time.time() - start
12
13print(f"Detected {len(detections)} faces in {elapsed:.2f}s")
14# GPU: ~0.05s per image, CPU: ~2-5s per image

If detection takes under 0.1 seconds per image, the GPU is being used. CPU-only detection typically takes several seconds.

Installing Dlib with CUDA Support

The pip-installed version (pip install dlib) ships without CUDA. You must compile from source:

bash
1# Prerequisites
2# - NVIDIA GPU with CUDA Compute Capability 3.5+
3# - CUDA Toolkit (10.x, 11.x, or 12.x)
4# - cuDNN (matching your CUDA version)
5# - CMake 3.8+
6
7# Verify CUDA is installed
8nvcc --version
9# nvcc: NVIDIA (R) Cuda compiler driver
10# Cuda compilation tools, release 11.8
11
12# Clone and build dlib
13git clone https://github.com/davisking/dlib.git
14cd dlib
15mkdir build && cd build
16cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
17cmake --build . --config Release
18cd ..
19python setup.py install

Verify the build detected CUDA:

bash
1# During cmake, look for these lines:
2# -- Found CUDA: /usr/local/cuda
3# -- Looking for cuDNN install...
4# -- Found cuDNN: /usr/lib/x86_64-linux-gnu/libcudnn.so
5# -- Building a CUDA+cuDNN+AVX enabled build.

If cmake says "CUDA not found" or "Building a non-CUDA build", CUDA is not properly installed or not in the PATH.

Checking CUDA and cuDNN Versions

bash
1# CUDA version
2nvcc --version
3cat /usr/local/cuda/version.txt
4
5# cuDNN version
6cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
7# Or in Python:
8python3 -c "import torch; print(torch.backends.cudnn.version())"
9
10# NVIDIA driver version
11nvidia-smi

Debugging GPU Not Detected

If DLIB_USE_CUDA is True but get_num_devices() returns 0:

python
1import dlib
2
3print(f"CUDA compiled: {dlib.DLIB_USE_CUDA}")
4print(f"GPU devices: {dlib.cuda.get_num_devices()}")
5
6# Check if the NVIDIA driver is loaded
7import subprocess
8result = subprocess.run(["nvidia-smi"], capture_output=True, text=True)
9print(result.stdout if result.returncode == 0 else "nvidia-smi failed - driver not loaded")
bash
1# Check if the GPU is visible to the system
2lspci | grep -i nvidia
3
4# Check driver status
5nvidia-smi
6
7# Check CUDA runtime
8python3 -c "import ctypes; ctypes.CDLL('libcudart.so')"

Common Pitfalls

  • Pip-installed dlib has no CUDA: Running pip install dlib installs a CPU-only build. There is no pip option for GPU support. You must compile from source with -DDLIB_USE_CUDA=1.
  • CUDA/cuDNN version mismatch: Dlib requires matching CUDA and cuDNN versions. If cmake finds CUDA but not cuDNN (or vice versa), the build silently falls back to CPU. Check cmake output carefully for "Found cuDNN" messages.
  • Outdated NVIDIA drivers: The GPU driver must support your CUDA toolkit version. Run nvidia-smi to check the driver version and verify compatibility with the NVIDIA CUDA compatibility matrix.
  • Docker containers without GPU passthrough: Inside Docker, the GPU is invisible unless you use --gpus all or the NVIDIA Container Toolkit. Dlib will compile with CUDA but get_num_devices() returns 0 at runtime.
  • Conda environment overriding system CUDA: Conda may install its own CUDA libraries that conflict with the system installation. Use conda install -c conda-forge dlib or set CUDA_HOME explicitly before building.

Summary

  • Check GPU support with dlib.DLIB_USE_CUDA and dlib.cuda.get_num_devices()
  • The pip-installed dlib does not include CUDA — compile from source with cmake
  • Verify cmake output shows "Found CUDA" and "Found cuDNN" during the build
  • CNN face detection speed is a practical indicator (GPU is 50-100x faster)
  • Ensure NVIDIA driver, CUDA toolkit, and cuDNN versions are all compatible
  • In Docker, use --gpus all to expose the GPU to the container

Course illustration
Course illustration

All Rights Reserved.