原帖 https://www.cnblogs.com/king-lps/p/9031568.html, Linux平台下。
方法1、2没有大问题,只需要注意模块名的一致性
方法3:
setup2.py 需要添加numpy库。见无法打开包括文件: “numpy/arrayobject.h”: No such file or directory
from distutils.core import setup from Cython.Build import cythonize import numpy as np setup( name = 'nms_module', ext_modules = cythonize('nums_py2.pyx'), include_dirs=[np.get_include()] )
nums_py2.pyx, line 29将 np.int_t(整型)改为 np.intp_t(长整型)。见问题7;关于 np.int_t 的更多介绍,见MSeifert的回答。
方法4,使用cuda加速
这个是最麻烦的,涉及在Windows上的编译问题。
- git 下载 windows的编译文件,并进入lib目录
- 修改setup_cuda.py中的第33行,改为你的安装地址
- 检查环境变量——’rc.exe’, ‘nvcc.exe’, CUDAHOME等等
- 执行
python setup_cuda.py build
- 不出意外的话,会报错
LINK : fatal error LNK1181: cannot open input file 'ID=2.obj'
, 参照 TinusChen的回答
a. 在第81行添加print(cmd)
打印真正执行的命令
b. 出错后,复制最后一条命令,并删除’ID=2,’,手动执行
c. 复制生成的nms_gpu的pyd文件至nms目录
本机测试的结果
- pure python
thresh=0.7, time wastes:0.0356
thresh=0.8, time wastes:0.1411
thresh=0.9, time wastes:0.5836 - pure python to cython without redefine variables
thresh=0.7, time wastes:0.0349
thresh=0.8, time wastes:0.1391
thresh=0.9, time wastes:0.5836 - cython, redefine variables
thresh=0.7, time wastes:0.0016
thresh=0.8, time wastes:0.0020
thresh=0.9, time wastes:0.0028 - cython with cuda
thresh=0.7, time wastes:0.0126
thresh=0.8, time wastes:0.0066
thresh=0.9, time wastes:0.0066