简介
从原insightface上clone下来的RetinaFace在win10平台上(python3.6)运行,想要只使用mxnet的CPU版,遇到一些问题,这里记录下来供有需求的同学一起讨论。
代码clone,CPU版mxnet安装
代码网址:RetinaFace,目标是安装相关依赖,跑通test.py测试文件。
安装CPU版mxnet:
- 在pypi.org上找到mxnet包;
- 选择对应python的版本;
- 使用pip安装;
采坑经历
安装完毕,修改test.py里面对应模型文件(在git上下载)路径和测试图片,运行,提示错误:
Traceback (most recent call last):
File "D:/PycharmProject/facereg/insightface/RetinaFace/test.py", line 19, in <module>
from retinaface import RetinaFace
File "D:\PycharmProject\facereg\insightface\RetinaFace\retinaface.py", line 13, in <module>
from rcnn.processing.bbox_transform import clip_boxes
File "D:\PycharmProject\facereg\insightface\RetinaFace\rcnn\processing\bbox_transform.py", line 4, in <module>
from ..cython.bbox import bbox_overlaps_cython
ModuleNotFoundError: No module named 'rcnn.cython.bbox'
打开\rcnn\processing\bbox_transform.py查看发现:
from ..cython.bbox import bbox_overlaps_cython
查看对应路径下发现包含了对应的.pyx文件:
百度大致意思是说必须要把pyx编译为其他文件才可以运行。
这里也踩了很多坑:
- 百度有给出RCNN的同样的错误,可以直接修改setup.py这个文件,没成功解决。
- 忽然想到git上有提示要运行make的,去到对应路径下make(win上必须安装minGW配置环境变量后才可以make),发现依然有这个错误,然后打开Makefile查看发现是Linux下的Makefile文件,因此直接运行setup.py文件,又提示错误:
AttributeError: 'MSVCCompiler' object has no attribute 'compiler_so'
,找解决办法,无果。 - 期间出现了一个错误提示:
Unable to find vcvarsall.bat
,大致意思上是说在win下编译,缺少这个文件,百度说让安装对应版本的VS,我电脑上已经安装过VS2017和VS2015了,对应的这个文件也有,但是一直提示该错误。
到这里感觉没办法进行下去了,感觉哪里出了问题。
问题解决
期间一直找方法,都有问题,其中百度了两个文章(1,2),感觉还是在setup.py文件上出了问题,可能因为原来的文件是在Linux下的,于是按照这两个里面的思路,将rcnn/Cython/setup.py改为:
try:
from setuptools import setup
from setuptools import Extension
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
###这里的try...except...是百度的网上的帖子,必须加上,否则会出现Unable to find vcvarsall.bat错误。
try:
numpy_include = np.get_include()
except AttributeError:
numpy_include = np.get_numpy_include()
ext_modules = [
Extension(
"bbox",
["bbox.pyx"],
extra_compile_args=["/openmp"],
include_dirs=[numpy_include]
),
Extension(
"anchors",
["anchors.pyx"],
extra_compile_args=["/openmp"],
include_dirs=[numpy_include]
),
Extension(
"cpu_nms",
["cpu_nms.pyx"],
extra_compile_args=["/openmp"],
include_dirs = [numpy_include]
),
]
setup(
name='frcnn_cython',
ext_modules=ext_modules,
# inject our custom trigger
# cmdclass={'build_ext': custom_build_ext},
cmdclass={'build_ext': build_ext},
)
按照Makefile里的命令运行:python setup.py build_ext --inplace
,发现成功生成了.c文件和.pyd文件:
之后满怀期待的去重新运行test.py文件,依然出错:
C:\Jenkins\workspace\mxnet-tag\mxnet\src\storage\storage.cc:119: Compile with USE_CUDA=1 to enable GPU usage
发现是代码中默认使用了GPU的方式运行,我们安装的是CPU的,所以当然不能成功。找到对应代码更改参数:
gpuid = -1 ###禁止使用GPU
detector = RetinaFace(r'D:\PycharmProject\facereg\insightface\RetinaFace\models\R50', 0, gpuid, 'net3')
运行,又又又出错:
Traceback (most recent call last):
File "D:/PycharmProject/facereg/insightface/RetinaFace/test.py", line 49, in <module>
faces, landmarks = detector.detect(img, thresh, scales=scales, do_flip=flip)
File "D:\PycharmProject\facereg\insightface\RetinaFace\retinaface.py", line 361, in detect
keep = self.nms(pre_det)
File "D:\PycharmProject\facereg\insightface\RetinaFace\rcnn\processing\nms.py", line 17, in _nms
return cpu_nms(dets, thresh)
File "cpu_nms.pyx", line 25, in cpu_nms.cpu_nms
ValueError: Buffer dtype mismatch, expected 'int_t' but got 'long long'
大意是在cpu_nms中出现了数据不匹配的情况,dbug代码,发现是因为cpu_nms里cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]
的函数nparray.argsort()默认返回的是int64类型的数组,而数据类型定义为np.int_t型,因此更改这行代码为:
cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1].astype(np.int32)
重新运行python setup.py build_ext --inplace
编译,finally,成功运行出了示例图像: