ruotianluo / pytorch-faster-rcnn

pytorch1.0 updated. Support cpu test and demo. (Use detectron2, it's a masterpiece)
MIT License
1.82k stars 475 forks source link

How to adapt /lib/nms/build.py to support pytorch1.0? #146

Open xiangyangli-cn opened 5 years ago

xiangyangli-cn commented 5 years ago

I changed the lib/nms/build.py as:

import os import torch

from torch.utils.ffi import create_extension

from torch.utils.cpp_extension import BuildExtension

sources = ['src/nms.c'] headers = ['src/nms.h'] defines = [] with_cuda = False

if torch.cuda.is_available(): print('Including CUDA code.') sources += ['src/nms_cuda.c'] headers += ['src/nms_cuda.h'] defines += [('WITH_CUDA', None)] with_cuda = True

this_file = os.path.dirname(os.path.realpath(file)) print(this_file) extra_objects = ['src/cuda/nms_kernel.cu.o'] extra_objects = [os.path.join(this_file, fname) for fname in extra_objects]

BuildExtension

create_extension

ffi = BuildExtension( '_ext.nms', headers=headers, sources=sources, define_macros=defines, relative_to=file, with_cuda=with_cuda, extra_objects=extra_objects )

if name == 'main': ffi.build()

But the Errors are:

cd nms/src/cuda; \ pwd;\ echo "Compiling nms kernels by nvcc..."; \ nvcc -c -o nms_kernel.cu.o nms_kernel.cu -x cu -Xcompiler -fPIC -arch=sm_52; \ cd ../..;\ python build.py;\ cd ..;\

/home/isia/d1/000-REC/mask-faster-rcnn-lxy/lib/nms/src/cuda Compiling nms kernels by nvcc... Including CUDA code. /home/isia/d1/000-REC/mask-faster-rcnn-lxy/lib/nms Traceback (most recent call last): File "build.py", line 33, in extra_objects=extra_objects File "/usr/local/lib/python2.7/dist-packages/torch/utils/cpp_extension.py", line 228, in init super(BuildExtension, self).init(*args, **kwargs) File "build/bdist.linux-x86_64/egg/setuptools/init.py", line 163, in init

File "/usr/lib/python2.7/distutils/cmd.py", line 59, in init raise TypeError, "dist must be a Distribution instance" TypeError: dist must be a Distribution instance cd layer_utils/roi_pooling/src/cuda; \ pwd; \ echo "Compiling roi_pooling kernels by nvcc..."; \ nvcc -c -o roi_pooling_kernel.cu.o roi_pooling_kernel.cu -x cu -Xcompiler -fPIC -arch=sm_52; \ cd ../..;\ python build.py;\ cd ../../.. /home/isia/d1/000-REC/mask-faster-rcnn-lxy/lib/layer_utils/roi_pooling/src/cuda Compiling roi_pooling kernels by nvcc... Including CUDA code. /home/isia/d1/000-REC/mask-faster-rcnn-lxy/lib/layer_utils/roi_pooling Traceback (most recent call last): File "build.py", line 33, in extra_objects=extra_objects File "/usr/local/lib/python2.7/dist-packages/torch/utils/cpp_extension.py", line 228, in init super(BuildExtension, self).init(*args, **kwargs) File "build/bdist.linux-x86_64/egg/setuptools/init.py", line 163, in init

File "/usr/lib/python2.7/distutils/cmd.py", line 59, in init raise TypeError, "dist must be a Distribution instance" TypeError: dist must be a Distribution instance

Spark001 commented 5 years ago

torch.utils.cpp_extension is for C++ to create extension. However, nms module is written in C code here.

So it doesn't work that you just replace create_extension to BuildExtension when import packages.

You need rewrite the nms.c to C++ code if you want use the cpp_extension and you can refer the whole procedure that rewrote C code with C++ extensions like pytorch/audio/commit/18c01be.

Or just use pytorch 0.4.x instead : )