setup.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # --------------------------------------------------------
  2. # Fast R-CNN
  3. # Copyright (c) 2015 Microsoft
  4. # Licensed under The MIT License [see LICENSE for details]
  5. # Written by Ross Girshick
  6. # --------------------------------------------------------
  7. import os
  8. from os.path import join as pjoin
  9. import numpy as np
  10. from distutils.core import setup
  11. from distutils.extension import Extension
  12. from Cython.Distutils import build_ext
  13. def find_in_path(name, path):
  14. "Find a file in a search path"
  15. # adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/
  16. for dir in path.split(os.pathsep):
  17. binpath = pjoin(dir, name)
  18. if os.path.exists(binpath):
  19. return os.path.abspath(binpath)
  20. return None
  21. def locate_cuda():
  22. """Locate the CUDA environment on the system
  23. Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
  24. and values giving the absolute path to each directory.
  25. Starts by looking for the CUDAHOME env variable. If not found, everything
  26. is based on finding 'nvcc' in the PATH.
  27. """
  28. # first check if the CUDAHOME env variable is in use
  29. if 'CUDAHOME' in os.environ:
  30. home = os.environ['CUDAHOME']
  31. nvcc = pjoin(home, 'bin', 'nvcc')
  32. else:
  33. # otherwise, search the PATH for NVCC
  34. default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')
  35. nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)
  36. if nvcc is None:
  37. raise EnvironmentError('The nvcc binary could not be '
  38. 'located in your $PATH. Either add it to your path, or set $CUDAHOME')
  39. home = os.path.dirname(os.path.dirname(nvcc))
  40. cudaconfig = {'home': home, 'nvcc': nvcc,
  41. 'include': pjoin(home, 'include'),
  42. 'lib64': pjoin(home, 'lib64')}
  43. for k, v in cudaconfig.items():
  44. if not os.path.exists(v):
  45. raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
  46. return cudaconfig
  47. # CUDA = locate_cuda()
  48. # Obtain the numpy include directory. This logic works across numpy versions.
  49. try:
  50. numpy_include = np.get_include()
  51. except AttributeError:
  52. numpy_include = np.get_numpy_include()
  53. def customize_compiler_for_nvcc(self):
  54. """inject deep into distutils to customize how the dispatch
  55. to gcc/nvcc works.
  56. If you subclass UnixCCompiler, it's not trivial to get your subclass
  57. injected in, and still have the right customizations (i.e.
  58. distutils.sysconfig.customize_compiler) run on it. So instead of going
  59. the OO route, I have this. Note, it's kindof like a wierd functional
  60. subclassing going on."""
  61. # tell the compiler it can processes .cu
  62. # self.src_extensions.append('.cu')
  63. # save references to the default compiler_so and _comple methods
  64. default_compiler_so = self.compiler_so
  65. super = self._compile
  66. # now redefine the _compile method. This gets executed for each
  67. # object but distutils doesn't have the ability to change compilers
  68. # based on source extension: we add it.
  69. def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
  70. print(extra_postargs)
  71. if os.path.splitext(src)[1] == '.cu':
  72. # use the cuda for .cu files
  73. self.set_executable('compiler_so', CUDA['nvcc'])
  74. # use only a subset of the extra_postargs, which are 1-1 translated
  75. # from the extra_compile_args in the Extension class
  76. postargs = extra_postargs['nvcc']
  77. else:
  78. postargs = extra_postargs['gcc']
  79. super(obj, src, ext, cc_args, postargs, pp_opts)
  80. # reset the default compiler_so, which we might have changed for cuda
  81. self.compiler_so = default_compiler_so
  82. # inject our redefined _compile method into the class
  83. self._compile = _compile
  84. # run the customize_compiler
  85. class custom_build_ext(build_ext):
  86. def build_extensions(self):
  87. customize_compiler_for_nvcc(self.compiler)
  88. build_ext.build_extensions(self)
  89. ext_modules = [
  90. Extension(
  91. "tools.cython_bbox",
  92. ["tools/bbox.pyx"],
  93. extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
  94. include_dirs=[numpy_include]
  95. ),
  96. Extension(
  97. "nms.cpu_nms",
  98. ["nms/cpu_nms.pyx"],
  99. extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
  100. include_dirs=[numpy_include]
  101. )
  102. # Extension('nms.gpu_nms',
  103. # ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
  104. # library_dirs=[CUDA['lib64']],
  105. # libraries=['cudart'],
  106. # language='c++',
  107. # runtime_library_dirs=[CUDA['lib64']],
  108. # # this syntax is specific to this build system
  109. # # we're only going to use certain compiler args with nvcc and not with gcc
  110. # # the implementation of this trick is in customize_compiler() below
  111. # extra_compile_args={'gcc': ["-Wno-unused-function"],
  112. # 'nvcc': ['-arch=sm_52',
  113. # '--ptxas-options=-v',
  114. # '-c',
  115. # '--compiler-options',
  116. # "'-fPIC'"]},
  117. # include_dirs = [numpy_include, CUDA['include']]
  118. # )
  119. ]
  120. setup(
  121. name='tf_faster_rcnn',
  122. ext_modules=ext_modules,
  123. # inject our custom trigger
  124. cmdclass={'build_ext': custom_build_ext}, requires=['tensorflow', 'numpy', 'cv2', 'matplotlib', 'requests']
  125. )