【M2Det】编译Cython版本NMS

2024-08-27 18:08
文章标签 编译 版本 nms cython m2det

本文主要是介绍【M2Det】编译Cython版本NMS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

具体参考来自于https://github.com/MrGF/py-faster-rcnn-windows

由于编译gpu版本比较麻烦,所以需要将gpu部分注释掉,只编译cpu即可(GPU版本可以根据本文章顶部链接自行修改)

进入到M2Det/utils目录下,将该目录下的build.py修改为如下形式:

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------import os
from os.path import join as pjoin
import numpy as np
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext#change for windows, by MrX
nvcc_bin = 'nvcc.exe'
lib_dir = 'lib/x64'def find_in_path(name, path):"Find a file in a search path"# adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/for dir in path.split(os.pathsep):binpath = pjoin(dir, name)if os.path.exists(binpath):return os.path.abspath(binpath)return Nonedef locate_cuda():"""Locate the CUDA environment on the systemReturns a dict with keys 'home', 'nvcc', 'include', and 'lib64'and values giving the absolute path to each directory.Starts by looking for the CUDAHOME env variable. If not found, everythingis based on finding 'nvcc' in the PATH."""# first check if the CUDAHOME env variable is in use# if 'CUDAHOME' in os.environ:if True:# home = os.environ['CUDA_PATH']home = r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0"print("home = %s\n" % home)nvcc = pjoin(home, 'bin', nvcc_bin)else:# otherwise, search the PATH for NVCCdefault_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')nvcc = find_in_path(nvcc_bin, os.environ['PATH'] + os.pathsep + default_path)if nvcc is None:raise EnvironmentError('The nvcc binary could not be ''located in your $PATH. Either add it to your path, or set $CUDA_PATH')home = os.path.dirname(os.path.dirname(nvcc))print("home = %s, nvcc = %s\n" % (home, nvcc))cudaconfig = {'home':home, 'nvcc':nvcc,'include': pjoin(home, 'include'),'lib64': pjoin(home, lib_dir)}for k, v in cudaconfig.items():if not os.path.exists(v):raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))return cudaconfigCUDA = locate_cuda()# Obtain the numpy include directory.  This logic works across numpy versions.
try:numpy_include = np.get_include()
except AttributeError:numpy_include = np.get_numpy_include()def customize_compiler_for_nvcc(self):"""inject deep into distutils to customize how the dispatchto gcc/nvcc works.If you subclass UnixCCompiler, it's not trivial to get your subclassinjected in, and still have the right customizations (i.e.distutils.sysconfig.customize_compiler) run on it. So instead of goingthe OO route, I have this. Note, it's kindof like a wierd functionalsubclassing going on."""# tell the compiler it can processes .cu# self.src_extensions.append('.cu')# save references to the default compiler_so and _comple methods# default_compiler_so = self.spawn# default_compiler_so = self.rcsuper = self.compile# now redefine the _compile method. This gets executed for each# object but distutils doesn't have the ability to change compilers# based on source extension: we add it.def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None,extra_postargs=None, depends=None):postfix = os.path.splitext(sources[0])[1]if postfix == '.cu':# use the cuda for .cu files# self.set_executable('compiler_so', CUDA['nvcc'])# use only a subset of the extra_postargs, which are 1-1 translated# from the extra_compile_args in the Extension classpostargs = extra_postargs['nvcc']else:postargs = extra_postargs['gcc']return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)# reset the default compiler_so, which we might have changed for cuda# self.rc = default_compiler_so# inject our redefined _compile method into the classself.compile = compile# run the customize_compiler
class custom_build_ext(build_ext):def build_extensions(self):customize_compiler_for_nvcc(self.compiler)build_ext.build_extensions(self)ext_modules = [Extension("nms.cpu_nms",["nms\\cpu_nms.pyx"],# extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},# include_dirs=[numpy_include]extra_compile_args={'gcc': []},include_dirs=[numpy_include]),# Extension('nms.gpu_nms',#           ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],#           library_dirs=[CUDA['lib64']],#           libraries=['cudart'],#           language='c++',#           runtime_library_dirs=[CUDA['lib64']],#           # this syntax is specific to this build system#           # we're only going to use certain compiler args with nvcc and not with gcc#           # the implementation of this trick is in customize_compiler() below#           extra_compile_args={'gcc': ["-Wno-unused-function"],#                               'nvcc': ['-arch=sm_52',#                                        '--ptxas-options=-v',#                                        '-c',#                                        '--compiler-options',#                                        "'-fPIC'"]},#           include_dirs=[numpy_include, CUDA['include']]#           ),Extension('pycocotools._mask',# sources=['pycocotools/maskApi.c', 'pycocotools/_mask.pyx'],# include_dirs=[numpy_include, 'pycocotools'],# extra_compile_args={#     'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']},sources=['pycocotools\\maskApi.c', 'pycocotools\\_mask.pyx'],include_dirs = [numpy_include, 'pycocotools'],extra_compile_args={'gcc': ['/Qstd=c99']},),
]setup(name='mot_utils',ext_modules=ext_modules,# inject our custom triggercmdclass={'build_ext': custom_build_ext},
)

在cmd终端下,进入到M2Det/utils文件夹下,然后使用命令

python build.py build

即可生成build文件夹。然后将build文件夹下pyd文件复制到对应文件下,然后重命名。 

修改M2Det/utils/nms_wrapper.py文件中,将使用GPU的注释掉,具体如下所示

# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------from .nms.cpu_nms import cpu_nms, cpu_soft_nms
# from .nms.gpu_nms import gpu_nms# def nms(dets, thresh, force_cpu=False):
#     """Dispatch to either CPU or GPU NMS implementations."""
#
#     if dets.shape[0] == 0:
#         return []
#     if cfg.USE_GPU_NMS and not force_cpu:
#         return gpu_nms(dets, thresh, device_id=cfg.GPU_ID)
#     else:
#         return cpu_nms(dets, thresh)def nms(dets, thresh, force_cpu=False):"""Dispatch to either CPU or GPU NMS implementations."""if dets.shape[0] == 0:return []if force_cpu:return cpu_soft_nms(dets, thresh, method = 1)#return cpu_nms(dets, thresh)# return gpu_nms(dets, thresh)return cpu_nms(dets, thresh, method=1)

常见问题

1、setup2.py 需要添加numpy库。见无法打开包括文件: “numpy/arrayobject.h”: No such file or directory

from distutils.core import setup
from Cython.Build import cythonize
import numpy as npsetup(name = 'nms_module',ext_modules = cythonize('nums_py2.pyx'),include_dirs=[np.get_include()])

2、nums_py2.pyx, line 29将 np.int_t(整型)改为 np.intp_t(长整型)。见问题7;关于 np.int_t 的更多介绍,见MSeifert的回答。

3、我发现了好几个版本的代码,但是只有M2Det/utils的nms和pycocotools可以进行编译,所以推荐将你需要调试的代码的nms和pycocotools文件夹中的文件都替换为M2Det/utils中的nms和pycocotools中的文件。

这篇关于【M2Det】编译Cython版本NMS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1112394

相关文章

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

Tomcat版本与Java版本的关系及说明

《Tomcat版本与Java版本的关系及说明》:本文主要介绍Tomcat版本与Java版本的关系及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Tomcat版本与Java版本的关系Tomcat历史版本对应的Java版本Tomcat支持哪些版本的pythonJ

IDEA中Git版本回退的两种实现方案

《IDEA中Git版本回退的两种实现方案》作为开发者,代码版本回退是日常高频操作,IntelliJIDEA集成了强大的Git工具链,但面对reset和revert两种核心回退方案,许多开发者仍存在选择... 目录一、版本回退前置知识二、Reset方案:整体改写历史1、IDEA图形化操作(推荐)1.1、查看提

JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)

《JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)》本文介绍了如何在Windows系统上配置多版本JDK(以JDK8和JDK17为例),并通过图文结合的方式给大家讲解了详细步骤,具有... 目录第一步 下载安装JDK第二步 配置环境变量第三步 切换JDK版本并验证可能遇到的问题前提:公司常

nvm如何切换与管理node版本

《nvm如何切换与管理node版本》:本文主要介绍nvm如何切换与管理node版本问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录nvm切换与管理node版本nvm安装nvm常用命令总结nvm切换与管理node版本nvm适用于多项目同时开发,然后项目适配no

Mybatis从3.4.0版本到3.5.7版本的迭代方法实现

《Mybatis从3.4.0版本到3.5.7版本的迭代方法实现》本文主要介绍了Mybatis从3.4.0版本到3.5.7版本的迭代方法实现,包括主要的功能增强、不兼容的更改和修复的错误,具有一定的参考... 目录一、3.4.01、主要的功能增强2、selectCursor example3、不兼容的更改二、

pytorch+torchvision+python版本对应及环境安装

《pytorch+torchvision+python版本对应及环境安装》本文主要介绍了pytorch+torchvision+python版本对应及环境安装,安装过程中需要注意Numpy版本的降级,... 目录一、版本对应二、安装命令(pip)1. 版本2. 安装全过程3. 命令相关解释参考文章一、版本对