解决Python ImportError: mmcvext.cpython-38-x86_64-linux-gnu.so未找到问题
背景
使用 mmdet时,遇到了mmcv相关的问题,其中mmcv的安装方法及版本为
pip3 install mmcv-full==1.5.2
具体异常为:
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/root/.local/lib/python3.8/site-packages/debugpy/__main__.py", line 39, in <module>
cli.main()
File "/root/.local/lib/python3.8/site-packages/debugpy/server/cli.py", line 430, in main
run()
File "/root/.local/lib/python3.8/site-packages/debugpy/server/cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "/root/.local/lib/python3.8/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "/root/.local/lib/python3.8/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/root/.local/lib/python3.8/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "tools/train.py", line 20, in <module>
from mmdet3d.apis import init_random_seed, train_model
File "/usr/local/lib/python3.8/dist-packages/mmdet3d/apis/__init__.py", line 2, in <module>
from .inference import (convert_SyncBN, inference_detector,
File "/usr/local/lib/python3.8/dist-packages/mmdet3d/apis/inference.py", line 12, in <module>
from mmdet3d.core import (Box3DMode, CameraInstance3DBoxes, Coord3DMode,
File "/usr/local/lib/python3.8/dist-packages/mmdet3d/core/__init__.py", line 2, in <module>
from .anchor import * # noqa: F401, F403
File "/usr/local/lib/python3.8/dist-packages/mmdet3d/core/anchor/__init__.py", line 2, in <module>
from mmdet.core.anchor import build_prior_generator
File "/root/.local/lib/python3.8/site-packages/mmdet/core/__init__.py", line 3, in <module>
from .bbox import * # noqa: F401, F403
File "/root/.local/lib/python3.8/site-packages/mmdet/core/bbox/__init__.py", line 8, in <module>
from .samplers import (BaseSampler, CombinedSampler,
File "/root/.local/lib/python3.8/site-packages/mmdet/core/bbox/samplers/__init__.py", line 12, in <module>
from .score_hlr_sampler import ScoreHLRSampler
File "/root/.local/lib/python3.8/site-packages/mmdet/core/bbox/samplers/score_hlr_sampler.py", line 3, in <module>
from mmcv.ops import nms_match
File "/root/.local/lib/python3.8/site-packages/mmcv/ops/__init__.py", line 2, in <module>
from .active_rotated_filter import active_rotated_filter
File "/root/.local/lib/python3.8/site-packages/mmcv/ops/active_rotated_filter.py", line 8, in <module>
ext_module = ext_loader.load_ext(
File "/root/.local/lib/python3.8/site-packages/mmcv/utils/ext_loader.py", line 13, in load_ext
ext = importlib.import_module('mmcv.' + name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: /root/.local/lib/python3.8/site-packages/mmcv/_ext.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN3c104impl8GPUTrace13gpuTraceStateE
原因分析
从异常上看,就是mmcv生成的库文件_ext.cpython-38-x86_64-linux-gnu.so有问题,这种情况一般都是mmcv编译的问题,由于直接使用pip3安装时,mmcv-full可能是官方预编译好的,但官方预编译的环境可能和我们自己的环境不太一致,从而导致生成so文件不匹配。
解决方法
- 重新编译mmcv
- 编译方法:由于使用pip3安装时也可以做到重新编译的效果,所以很多时候不需要从下载源码开始,而是使用[官方建议](Installation — mmcv 1.5.2 documentation)的安装方式
pip3 install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.htmlpip3 install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.htmlpip3 install mmcv-full=={mmcv_version} -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.htmlpip3 install mmcv-full==1.3.9 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html
作者:Garfield2005