【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

相关文章

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

Redis指南及6.2.x版本安装过程

《Redis指南及6.2.x版本安装过程》Redis是完全开源免费的,遵守BSD协议,是一个高性能(NOSQL)的key-value数据库,Redis是一个开源的使用ANSIC语言编写、支持网络、... 目录概述Redis特点Redis应用场景缓存缓存分布式会话分布式锁社交网络最新列表Redis各版本介绍旧

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

查看MySQL数据库版本的四种方法

《查看MySQL数据库版本的四种方法》查看MySQL数据库的版本信息可以通过多种方法实现,包括使用命令行工具、SQL查询语句和图形化管理工具等,以下是详细的步骤和示例代码,需要的朋友可以参考下... 目录方法一:使用命令行工具1. 使用 mysql 命令示例:方法二:使用 mysqladmin 命令示例:方

Java版本不兼容问题详细解决方案步骤

《Java版本不兼容问题详细解决方案步骤》:本文主要介绍Java版本不兼容问题解决的相关资料,详细分析了问题原因,并提供了解决方案,包括统一JDK版本、修改项目配置和清理旧版本残留等步骤,需要的朋... 目录错误原因分析解决方案步骤第一步:统一 JDK 版本第二步:修改项目配置第三步:清理旧版本残留兼容性对

Linux搭建单机MySQL8.0.26版本的操作方法

《Linux搭建单机MySQL8.0.26版本的操作方法》:本文主要介绍Linux搭建单机MySQL8.0.26版本的操作方法,本文通过图文并茂的形式给大家讲解的非常详细,感兴趣的朋友一起看看吧... 目录概述环境信息数据库服务安装步骤下载前置依赖服务下载方式一:进入官网下载,并上传到宿主机中,适合离线环境

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

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