forked from YonghaoHe/LFD-A-Light-and-Fast-Detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
67 lines (57 loc) · 2.68 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
build external modules, currently including nms and focal loss
after building, the corresponding .so files are copied to the target folders
"""
import os
import shutil
import torch
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CUDAExtension
def make_cuda_ext(name, sources, sources_cuda=[]):
define_macros = []
extra_compile_args = {'cxx': []}
if torch.cuda.is_available():
define_macros += [('WITH_CUDA', None)]
extension = CUDAExtension
extra_compile_args['nvcc'] = [
'-D__CUDA_NO_HALF_OPERATORS__',
'-D__CUDA_NO_HALF_CONVERSIONS__',
'-D__CUDA_NO_HALF2_OPERATORS__',
]
sources += sources_cuda
else:
raise EnvironmentError('external modules must be compiled with CUDA support!!')
return extension(name=name,
sources=sources,
define_macros=define_macros,
extra_compile_args=extra_compile_args)
if __name__ == '__main__':
setup(
name='nms',
ext_modules=[
make_cuda_ext(
name='nms_ext',
sources=['lfd/model/utils/build/nms/src/nms_ext.cpp', 'lfd/model/utils/build/nms/src/cpu/nms_cpu.cpp'],
sources_cuda=['lfd/model/utils/build/nms/src/cuda/nms_cuda.cpp', 'lfd/model/utils/build/nms/src/cuda/nms_kernel.cu']),
make_cuda_ext(
name='sigmoid_focal_loss_ext',
sources=['lfd/model/losses/build/sigmoid_focal_loss/src/sigmoid_focal_loss_ext.cpp'],
sources_cuda=['lfd/model/losses/build/sigmoid_focal_loss/src/cuda/sigmoid_focal_loss_cuda.cu'])
],
cmdclass={'build_ext': BuildExtension}
)
# copy .so files
names_to_target_dirs = {'nms_ext': './lfd/model/utils/libs',
'sigmoid_focal_loss_ext': './lfd/model/losses/libs'}
try:
lib_dir = [dir_name for dir_name in os.listdir('./build') if dir_name.lower().startswith('lib.')][0]
so_file_names = [so_file_name for so_file_name in os.listdir(os.path.join('./build', lib_dir)) if so_file_name.lower().endswith('.so')]
for k, v in names_to_target_dirs.items():
if not os.path.exists(v):
os.makedirs(v)
temp_so_file_name = [so_file_name for so_file_name in so_file_names if so_file_name.lower().startswith(k)][0]
shutil.copyfile(os.path.join('./build', lib_dir, temp_so_file_name), os.path.join(v, temp_so_file_name))
except:
print('Failed to copy .so files to target folders, please check the files are generated successfully!')
print('----> build and copy successfully!')