748

insightface人脸识别算法的应用

乐果   发表于   2024 年 05 月 08 日 标签:pythonai

在研究 stable-diffusion 的一些 ai 功能时,发现了 insightface 这个开源的人脸检测识别、比对算法模型库。

github 源码

python 类库 : https://pypi.org/project/insightface/

在对它应用时发现一些环境兼容性的问题,因此作一下笔记。

insightface 安装

insightface 目前 python 官方的版本是 0.7.3

pip install insightface

命令即可安装

环境兼容性问题

由于 0.7.3 版本大概两年前,彼时 numpy 版本应该是 1.22.3
因此它使用了 numpy.ini 这个属性,但目前 numpy 版本已经迭代到 1.26 以上, numpy.intNumPy 1.20中已弃用,在NumPy 1.24中已被删除,所以没有numpy.int

因此,insightface 实际使用时会报错:

Traceback (most recent call last):
  File "/data/work/py/sd-api/main.py", line 80, in <module>
    start(sys.argv[1:])
  File "/data/work/py/sd-api/main.py", line 72, in start
    img2img.img2img(filename)
  File "/data/work/py/sd-api/img2img.py", line 150, in img2img
    cv2.imwrite(faceSaveName, face_analyser.draw_on(faceCheckImg, faces))
  File "/home/xiao/anaconda3/envs/sd/lib/python3.10/site-packages/insightface/app/face_analysis.py", line 84, in draw_on
    box = face.bbox.astype(np.int)
  File "/home/xiao/anaconda3/envs/sd/lib/python3.10/site-packages/numpy/__init__.py", line 324, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'int'.
`np.int` was a deprecated alias for the builtin `int`. To avoid this error in existing code, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'inf'?

解决办法,要么根据提示,将代码中对应的 numpy.ini 修改成 numpy.ini_, 即修改 insightface 源码。

另一种办法是降低 NumPy 包的版本,降低到 1.22.3

个人建议修改 insightface 源码即可,因为只有 insightface/app/face_analysis.py 文件中两处使用了 numpy.ini

使用Gpu出现的问题

因为 insightface 算法模型对算力有一定要求,可以使用英伟达Gpu。

在之前的笔记中,已经记录过安装 CUDA 踩过的坑。

显卡驱动

nvidia-smi
Wed May  8 15:15:41 2024       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.171.04             Driver Version: 535.171.04   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 3090        Off | 00000000:01:00.0 Off |                  N/A |
| 30%   33C    P8              29W / 350W |   8856MiB / 24576MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      1187      G   /usr/lib/xorg/Xorg                            6MiB |
|    0   N/A  N/A     43163      C   python                                     8838MiB |
+---------------------------------------------------------------------------------------+

cuda安装

例如,我安装的是 11.8 版本

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run

显卡驱动已安装的情况下,只选择安装cudnn,安装成功提示:

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-11.8/

Please make sure that
 -   PATH includes /usr/local/cuda-11.8/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-11.8/lib64, or, add /usr/local/cuda-11.8/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.8/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 520.00 is required for CUDA 11.8 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log

按提示,加入环境变量(编辑 /etc/profile 追加):

export PATH=/usr/local/cuda-11.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH

验证:

nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

cuDNN下载

下载地址

找到对应的系统版本下载解压后,将 include 下的头文件拷贝到 /usr/local/cuda-11.8/include 目录下, 将 lib 下的动态类库文件拷贝到 /usr/local/cuda-11.8/lib64 目录下。

onnxruntime-gpu

可以查看当前 onnxruntime-gpu 是否安装以及它的版本:

pip list

如果有安装(或安装有 onnxruntime ),建议卸载重新安装:

pip uninstall onnxruntime onnxruntime-gpu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple onnxruntime-gpu==1.15.1

验证:

python
>>> import onnxruntime
>>> onnxruntime.get_device()
'GPU'  #表示GPU可用
>>> onnxruntime.get_available_providers()
['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']

Ok,环境问题解决后 insightface 就可以使用了。

效果:

乐果   发表于   2024 年 05 月 08 日 标签:pythonai

0

文章评论