编译后的DCN V2不能import,这是个奇怪的报错,貌似是编译后的so文件(_ext.cpython-37m-x86_64-linux-gnu.so)不能用。
代码:
import _ext as _backend
报错:
Traceback (most recent call last):
File "/tmp-data/user1/code/3d_object_detection/MonoFlex-main/tools/plain_train_net.py", line 25, in <module>
from model.detector import KeypointDetector
File "/tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/detector.py", line 6, in <module>
from .backbone import build_backbone
File "/tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/backbone/__init__.py", line 1, in <module>
from .dla_dcn import build_backbone
File "/tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/backbone/dla_dcn.py", line 16, in <module>
from model.backbone.DCNv2.dcn_v2 import DCN
File "/tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/backbone/DCNv2/dcn_v2.py", line 13, in <module>
import _ext as _backend
ImportError: dynamic module does not define module export function (PyInit__ext)
解决:
- 确认在对应目录(/tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/backbone/DCNv2/)下运行 import _ext 没问题
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
xxx
}
- 2) 编译要正确。怀疑是dcn v2的编译问题,找了参考1(里面也有句 import _ext as backbone),重新编译试试,然后就过了。可能之前的命令不对(权限不对,不该用 sudo . make.sh,那样即使编译通过了也不能使用),正确的命令:
$ cd DCNv2/
$ sudo chmod 777 -R .
$ ./make.sh
$ cd ../../..
$ python setup.py develop
- 3)根据参考4, 先import torch,再import 外部模块
import torch
import _ext
没有先import torch会报错:
ImportError: /tmp-data/user1/code/3d_object_detection/MonoFlex-main/model/backbone/DCNv2/_ext.cpython-37m-x86_64-linux-gnu.so: undefined symbol: THPVariableClass
- 4)尝试手动在终端中import _ext,没有报错则没问题
- 在终端环境(而不是Pycharm环境),进入到要执行的脚本(plain_train_net.py)所在目录(/tmp-data/user1/code/3d_object_detection/MonoFlex-main/tools/),重新运行脚本
$ python plain_train_net.py --batch_size 4 --num_gpus 4 --config runs/monoflex.yaml --output output/exp
参考:
- https://github.com/CharlesShang/DCNv2
- https://github.com/zhangyp15/MonoFlex/tree/main/model/backbone/DCNv2
- https://blog.csdn.net/XCCCCZ/article/details/111089151#comments_18365836
- https://github.com/pytorch/extension-cpp/issues/6
- https://github.com/pytorch/ELF/issues/98
- https://github.com/pybind/pybind11/issues/2145
|