【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 None


def locate_cuda():
    """Locate the CUDA environment on the system

    Returns 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, everything
    is 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 NVCC
        default_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 cudaconfig


CUDA = 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 dispatch
    to gcc/nvcc works.

    If you subclass UnixCCompiler, it's not trivial to get your subclass
    injected in, and still have the right customizations (i.e.
    distutils.sysconfig.customize_compiler) run on it. So instead of going
    the OO route, I have this. Note, it's kindof like a wierd functional
    subclassing 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.rc
    super = 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 class
            postargs = 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 class
    self.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 trigger
    cmdclass={'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 np

setup(
      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中的文件。

发布了190 篇原创文章 · 获赞 497 · 访问量 206万+

猜你喜欢

转载自blog.csdn.net/u013066730/article/details/103860555
NMS
今日推荐