【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

相关文章

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Ubuntu如何升级Python版本

《Ubuntu如何升级Python版本》Ubuntu22.04Docker中,安装Python3.11后,使用update-alternatives设置为默认版本,最后用python3-V验证... 目China编程录问题描述前提环境解决方法总结问题描述Ubuntu22.04系统自带python3.10,想升级

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Linux升级或者切换python版本实现方式

《Linux升级或者切换python版本实现方式》本文介绍在Ubuntu/Debian系统升级Python至3.11或更高版本的方法,通过查看版本列表并选择新版本进行全局修改,需注意自动与手动模式的选... 目录升级系统python版本 (适用于全局修改)对于Ubuntu/Debian系统安装后,验证Pyt

MySQL 升级到8.4版本的完整流程及操作方法

《MySQL升级到8.4版本的完整流程及操作方法》本文详细说明了MySQL升级至8.4的完整流程,涵盖升级前准备(备份、兼容性检查)、支持路径(原地、逻辑导出、复制)、关键变更(空间索引、保留关键字... 目录一、升级前准备 (3.1 Before You Begin)二、升级路径 (3.2 Upgrade

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性: