强制 Dlib python 仅使用 CPU 支持进行安装;在具有 Cuda 和所有内容的 GPU 机器上

Force Dlib python to install with CPU support only; on a GPU machine with Cuda and everything

本文关键字:Cuda 机器 GPU python Dlib CPU 安装 支持 强制      更新时间:2023-10-16

我已经在互联网上搜索了它,我所能找到的是人们在使用 Cuda 安装 Dlib 时遇到问题。 我有完全相反的问题。我可以在 Ubuntu (Google Colab( 上成功安装 Dlib 和 Cuda 和 GPU 支持。但我不想这样。 我想强制 Dlib 使用 CPU,因为当我将运行时更改为 TPU 时,将没有 GPU。但是 Dlib 在安装时找到 CUDA,并在安装时添加对 CUDA 的支持,即使运行时设置为"NOT GPU"。

pip install Dlib

默认情况下,这将使用 cuda 安装 Dlib,因为我不知道有任何标志可以禁用 Cuda。 但即使我尝试从以下源代码构建 Dlib:

!git clone https://github.com/davisking/dlib.git
%cd dlib
!mkdir build
%cd build
!cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1 
!cmake --build .
# !sudo make install
# !sudo ldconfig
%cd ..
# !python setup.py build --no DLIB_USE_CUDA
!python setup.py install --no DLIB_USE_CUDA

我可以成功地做到

import Dlib

但是当我使用"face_recogntion"在后端调用 Dlib 时

!pip install face_recognition
import dlib
import face_recognition

它抛出一个错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-52-c42385b122ec> in <module>()
2 get_ipython().system('pip install face_recognition')
3 import dlib
----> 4 import face_recognition
1 frames
/usr/local/lib/python3.6/dist-packages/face_recognition/api.py in <module>()
21 
22 cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
---> 23 cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)
24 
25 face_recognition_model = face_recognition_models.face_recognition_model_location()
RuntimeError: Error while calling cudaGetDevice(&the_device_id) in file /tmp/pip-install-9p_kkgzg/dlib/dlib/cuda/gpu_data.cpp:178. code: 38, reason: no CUDA-capable device is detected

现在我无法理解我错过了什么。 我应该设置什么标志才能强制 DLIB 使用 CPU,甚至即使有 GPU,也不查找 GPU。

如果要直接从pip包安装,请使用以下命令

sudo pip install -v --install-option="--no" --install-option="DLIB_USE_CUDA" dlib

在谷歌合作中,转到工具栏>运行时>更改运行时类型>设置硬件加速器>GPU

如果您正在使用谷歌合作实验室并已安装!pip 在协作中安装face_recognition。 然后,您应该将运行时加速更改为 GPU 以导入face_recognition