前言

Dlib 是一个 C++ 工具包,被广泛应用于工业和学术界。Dlib 的开源许可允许在任何应用程序中免费使用它。Dlib支持导出其他编程语言如Python的binding。

在Python环境下一般安装dlib很方便,直接使用pip安装即可。但是某些时候由于CUDA支持的问题导致部分模型使用dlib库进行推理会出现异常。

本文简要介绍无CUDA支持的dlib库的安装与使用。

安装

以Ubuntu为例进行说明:

pip安装

pip install dlib

注:使用此方式安装的dlib默认支持CUDA加速(如已安装CUDA和CUDNN)。

>>> import dlib
>>> dlib.DLIB_USE_CUDA
True

但是在使用face_recognition库进行推理的时候,出现了多卡机器上的显存分配异常(如下图中的253MiB的异常显存占用)和illegal memory access was encountered等问题。

|    3   N/A  N/A     46077      C   ...envs/test/bin/python     4409MiB |
|    3   N/A  N/A     46115      C   ...envs/test/bin/python     8569MiB |
|    3   N/A  N/A     46165      C   ...envs/test/bin/python     5822MiB |
|    3   N/A  N/A     46176      C   ...envs/test/bin/python      253MiB |
|    3   N/A  N/A     46186      C   ...envs/test/bin/python      253MiB |
|    3   N/A  N/A     46192      C   ...envs/test/bin/python      253MiB |
|    4   N/A  N/A     46089      C   ...envs/test/bin/python     4409MiB |
|    4   N/A  N/A     46118      C   ...envs/test/bin/python     8569MiB |
|    4   N/A  N/A     46176      C   ...envs/test/bin/python     5569MiB |
|    6   N/A  N/A     46100      C   ...envs/test/bin/python     4409MiB |
|    6   N/A  N/A     46125      C   ...envs/test/bin/python     8569MiB |
|    6   N/A  N/A     46186      C   ...envs/test/bin/python     5569MiB |
|    7   N/A  N/A     46111      C   ...envs/test/bin/python     4409MiB |
|    7   N/A  N/A     46150      C   ...envs/test/bin/python     8569MiB |
while calling cudnnFindConvolutionForwardAlgorithm( context(),
descriptor(data), (const cudnnFilterDescriptor_t)filter_handle,
(const cudnnConvolutionDescriptor_t)conv_handle,
descriptor(dest_desc), num_possible_algorithms, &num_algorithms,
perf_results.data()) in file /tmp/pip-install-fz7s/dlib_537e10d/dlib/cuda/cudnn_dlibapi.cpp:819.
code: 2, reason: CUDA Resources could not be allocated.
6846:CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
6847:For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
6889:2022-11-04 18:08:52,712 CUDA error: an illegal memory access was encountered
6890:CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
6891:For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

进一步分析发现,是由于机器的GPU和显卡驱动以及CUDNN太新,而face_recognition中的部分模型使用dlib导入后,会引发上述异常。

同样在另一台旧机器上则不会出现上述问题,分析发现是同样版本的dlib在那台机器上没有支持CUDA,于是也就不会出现上述显存异常问题。

>>> import dlib
>>> dlib.DLIB_USE_CUDA
False

于是可以考虑编译安装不支持CUDA的dlib以正常使用face_recognition。

源码编译安装

git clone https://github.com/davisking/dlib.git
cd dlib
python setup.py install --no DLIB_USE_CUDA

测试:

>>> import dlib
>>> dlib.DLIB_USE_CUDA
False

使用

import numpy as np
import face_recognitionimg = np.zeros((100,100,3)).astype(np.uint8)face_encodings = face_recognition.face_encodings(img, known_face_locations=[[10, 50, 50, 10]], model="small")

运行正常。

face_encodings = face_recognition.face_encodings(img[:, : , ::-1], known_face_locations=[[10, 50, 50, 10]], model="small")

运行会报错:

Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/data1/miniconda3/envs/torch1.12/lib/python3.7/site-packages/face_recognition/api.py", line 214, in face_encodingsreturn [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]File "/data1/miniconda3/envs/torch1.12/lib/python3.7/site-packages/face_recognition/api.py", line 214, in <listcomp>return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]
TypeError: compute_face_descriptor(): incompatible function arguments. The following argument types are supported:1. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], face: _dlib_pybind11.full_object_detection, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], num_jitters: int = 0) -> _dlib_pybind11.vector3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], faces: _dlib_pybind11.full_object_detections, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors4. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], batch_faces: List[_dlib_pybind11.full_object_detections], num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectorss5. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], num_jitters: int = 0) -> _dlib_pybind11.vectorsInvoked with: <_dlib_pybind11.face_recognition_model_v1 object at 0x7f746912a370>, array([[[0, 0, 0],[0, 0, 0],[0, 0, 0],
img = np.ascontiguousarray(img[:, :, ::-1])
face_encodings = face_recognition.face_encodings(img, known_face_locations=[[10, 50, 50, 10]], model="small")

运行正常。

其他说明

如果在最新机器上使用基于dlib的face_recognition库推理,同时使用tensorflow进行其他模型的推理,则tensorflow可能也会有CUDA相关异常的打印,但是溯源还是由于dlib的CUDA支持不正常引发的问题:

2022-11-04 15:32:20.964078: E tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:723] failed to record completion event; therefore, failed to create inter-stream dependency
2022-11-04 15:32:20.964146: E tensorflow/stream_executor/cuda/cuda_driver.cc:1183] failed to enqueue async memcpy from host to device: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered; GPU dst: 0x7f6b56e84a00; host src: 0x55bf1eaa4cc0; size: 602112=0x93000
2022-11-04 15:32:20.964171: E tensorflow/stream_executor/stream.cc:334] Error recording event in stream: Error recording CUDA event: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered; not marking stream as bad, as the Event object may be at fault. Monitor for further errors.
2022-11-04 15:32:20.964341: E tensorflow/stream_executor/cuda/cuda_event.cc:29] Error polling for event status: failed to query event: CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
2022-11-04 15:32:20.964352: F tensorflow/core/common_runtime/device/device_event_mgr.cc:221] Unexpected Event status: 1

版权说明

本文为原创文章,独家发布在blog.csdn.net/TracelessLe。未经个人允许不得转载。如需帮助请email至tracelessle@163.com或扫描个人介绍栏二维码咨询。

参考资料

[1] dlib C++ Library - Frequently Asked Questions
[2] davisking/dlib: A toolkit for making real world machine learning and data analysis applications in C++
[3] How to force install Dlib with only CPU support on a GPU machine with Cuda enabled · Issue #1885 · davisking/dlib
[4] ageitgey/face_recognition: The world’s simplest facial recognition api for Python and the command line

无CUDA支持的dlib库的安装与使用相关推荐

  1. anaconda+python3.7+win10安装dlib_Windows系统下 Python(Anaconda)的 Dlib库 的安装

    0. 引言 介绍在 Windows  操作系统下,在 Python 的 Anaconda 集成环境中,安装 Dlib 库 : 对于不了解源码编译的,或者利用 cmake 方法失败的,可以尝试下此方法: ...

  2. Py之dlib:Python库之dlib库的简介、安装、使用方法详细攻略

    Py之dlib:Python库之dlib库的简介.安装.使用方法详细攻略 目录 dlib库的简介 dlib库的安装 dlib库的使用函数 0.利用dlib.get_frontal_face_detec ...

  3. dlib 怎么安装vs2017_win10中的dlib库安装过程

    之前试过很多方法结果都失败,最后终于发现一个成功的方法,先记一下以防忘记. 参考:记一次Win10环境python3.7安装dlib模块趟过的坑 由于我是通过Anaconda安装的Python,所以环 ...

  4. 矩池云上编译安装dlib库

    方法一(简单) 矩池云上的k80因为内存问题,请用其他版本的GPU去进行编译,保存环境后再在k80上用. 准备工作 下载dlib的源文件 进入python的官网,点击PyPi选项,搜索dilb,再点击 ...

  5. Python:安装dlib库

    目录: 前言 问题描述 前言 本来安装各种库的方法,我已经写了好几种,甚至于也装上了anaconda 这种神器,但今天还是遇到了一个神奇的问题,因此,特地搜索学习了一下,在此感谢两位博主吧: pyth ...

  6. windows下dlib库简介、安装问题解决及简单小例子 (python)

    一.dlib简介 Dlib是一个现代C++框架,解决包含机器学习算法以及开发复杂软件的实现问题,它被广泛应用在工业和学术研究领域,包括机器人.嵌入式设备.移动手机以及大规模高性能计算环境中,DLib的 ...

  7. sklearn 命令行安装_1.sklearn库的安装

    sklearn库 sklearn是scikit-learn的简称,是一个基于Python的第三方模块.sklearn库集成了一些常用的机器学习方法,在进行机器学习任务时,并不需要实现算法,只需要简单的 ...

  8. Frappe Charts - 免费开源、轻量无依赖的 web 图表库,简单不臃肿,支持搭配 Vue / React 等框架使用

    一个小巧的图表库,基于 SVG 生成图表,使用很简单,推荐给大家. 关于 Frappe Charts Frappe Charts 是一个小巧简单的 JavaScript 图表库,通过简单几个参数,可以 ...

  9. cuda, cudnn的升级,各种深度学习库的安装

    新版本的tensorflow和keras已经要求cudnn v6了,并且将来会升级到cuda9 和 cudnn7.  cudnn7最大的特点是支持group convolution, 这里原先的环境是 ...

最新文章

  1. 系统集成资质培训 - 在线答疑(17:00更新)
  2. java url 处理,URL处理-Java架构师必看
  3. Spring Web Flow 入门demo(二)与业务结合 附源码
  4. PyFoam来实时输出残差
  5. leetcode——Lowest Common Ancestor of a Binary Tree
  6. JS的typeof力所能及已经力所不及
  7. 作业要求 20171130 每周例行报告
  8. hack wifi android,WiFi Hack AIO 2010 - WiFi v1.2
  9. linux设置自启动方式
  10. 哪种存储器是非易失的_非易失性存储器和易失性存储器有什么全部详细资料对比...
  11. GD32f103介绍第一章
  12. python urllib.parse_Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)...
  13. HTML5多媒体(音频、视频播放)
  14. Python实现日周月排行榜
  15. FRP搭建内网穿透(亲测有效)
  16. SQL查询语句的使用
  17. 清明节海报设计软件测试,PS清明节海报设计教程
  18. 手把手教你如何获取微信公众号用户的个人信息(包括OpenId)
  19. 三个值得关注的零撸空投项目,请收藏
  20. Linux 命令(214)—— arpd 命令

热门文章

  1. C++ ofstream和ifstream详细用法
  2. 新未来大学英语视听说教程
  3. php class之入门篇
  4. Java 网络编程学习复习总结(一)-2021.05.28
  5. Rrd 文档 总结(一)
  6. javascript--瀑布流客厅笔记
  7. 公司企业如何建立一个网站?
  8. 小甜点,RecyclerView 之 ItemDecoration 讲解及高级特性实践
  9. learning_curve(学习曲线)
  10. 【数据分析师八大能力】