今天运行faster rcnn demo的时候居然出错了:
guyadong@gyd-u16:~/tmp/py-faster-rcnn$ tools/demo.py
Traceback (most recent call last):
File "tools/demo.py", line 18, in <module>
from fast_rcnn.test import im_detect
File "/home/guyadong/tmp/py-faster-rcnn/tools/../lib/fast_rcnn/test.py", line 16, in <module>
import caffe
File "/home/guyadong/tmp/py-faster-rcnn/tools/../caffe-fast-rcnn/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/home/guyadong/tmp/py-faster-rcnn/tools/../caffe-fast-rcnn/python/caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: /home/guyadong/tmp/py-faster-rcnn/caffe-fast-rcnn/lib/libcaffe.so.1.0.0-rc5: undefined symbol: _ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
guyadong@gyd-u16:~/tmp/py-faster-rcnn$ strings /home/guyadong/tmp/py-faster-rcnn/caffe-fast-rcnn/lib/libcaffe.so.1.0.0-rc5 |grep imencode
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE
根据提示信息,应该是找不到imencode这个函数。
运行caffe的test也同样报错,只是这次是找不到im_read函数。
[----------] 5 tests from ImageDataLayerTest/2, where TypeParam = caffe::GPUDevice<float>
[ RUN ] ImageDataLayerTest/2.TestResize
/home/guyadong/caffe/caffe-master/build/test/test.testbin: symbol lookup error: /home/guyadong/caffe/caffe-master/build/lib/libcaffe.so.1.0.0-rc3: undefined symbol: _ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi
src/caffe/test/CMakeFiles/runtest.dir/build.make:57: recipe for target 'src/caffe/test/CMakeFiles/runtest' failed
make[3]: *** [src/caffe/test/CMakeFiles/runtest] Error 127
CMakeFiles/Makefile2:328: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/all' failed
make[2]: *** [src/caffe/test/CMakeFiles/runtest.dir/all] Error 2
CMakeFiles/Makefile2:335: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/rule' failed
make[1]: *** [src/caffe/test/CMakeFiles/runtest.dir/rule] Error 2
Makefile:240: recipe for target 'runtest' failed
make: *** [runtest] Error 2
imencode,imread都是OpenCV的函数,但是opencv明明已经正确安装了啊,而且前几天faster rcnn已经安装运行成功并且训练了自己的模型,demo程序更是没问题,没道理啊。
用搜索找了半天,尝试了搜索上的各种办法都无效,最后想到用ldd命令列出libcaffe.so.1.0.0-rc5依赖的动态库,发现了问题,如下图:
原来我使用的另一个第三方程序CASSDK中自带了opencv动态库,而且它在LD_LIBRARY_PATH的优先级更高,所以libcaffe.so.1.0.0-rc5在运行时使用了cassdk下的opencv库,而不是我apt-get安装的opencv库。
果断卸载了这个程序,再用ldd命令查看,这下依赖关系正常了。
再运行py-faster-rcnn/tools/demo.py则一切正常。
总结
遇到这种undefined symbol的问题,先别急着重装对应的库,用ldd命令看看动态库依赖关系,更有助于找到问题的根本原因。