caffe SSD 代码编译运行流程及问题解决

caffe SSD 代码编译运行流程及问题解决

该文基于以下代码:

https://github.com/weiliu89/caffe/tree/ssd

down下来后,进入目录

-rw-rw-r--  1 ~ ~  23986 813 15:29 Makefile
-rw-rw-r--  1 ~ ~   4402 810 17:05 Makefile.config.example

按照机器配置和环境的情况修改这两个文件,首先复制Makefile.config.example到Makefile.config

cp Makefile.config.example Makefile.config

在config文件里进行修改,如uncomment以下几项:

USE_CUDNN :=1,因为我们需要调用CUDNN运行caffe代码

OPENCV_VERSION :=3,因为机器上的opencv版本号是3.X

CUDA_ARCH := -gencode arch=compute_50,code=sm_50 
             -gencode arch=compute_52,code=sm_52 
             -gencode arch=compute_61,code=sm_61

CUDA_ARCH只保留这几项就行,这是和gpu的算力性能有关的项目,本机器用的是TITAN Xp,因此如果不把较小的注释掉就会报warning。

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial

加上hdf5的路径到include中。

这样Makefile的配置文件基本完成。下面对Makefile修改。

常见问题一般都是库找不到,因此主要关注这一段代码:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem boost_regex m hdf5_serial_hl hdf5_serial 

# handle IO dependencies
USE_LEVELDB ?= 1
USE_LMDB ?= 1
USE_OPENCV ?= 1

ifeq ($(USE_LEVELDB), 1)
    LIBRARIES += leveldb snappy
endif
ifeq ($(USE_LMDB), 1)
    LIBRARIES += lmdb
endif
ifeq ($(USE_OPENCV), 1)
    LIBRARIES += opencv_core opencv_highgui opencv_imgproc

    ifeq ($(OPENCV_VERSION), 3)
        LIBRARIES += opencv_imgcodecs opencv_videoio
    endif

endif
PYTHON_LIBRARIES ?= boost_python27 python2.7
WARNINGS := -Wall -Wno-sign-compare

第一行中的LIBRARIES += 。。。,如果报错某个库没有找到,可以加入到这一行,如boost_regex m hdf5_serial_hl hdf5_serial 都是添加上的。因为曾经报错boost_regex::XXX没有找到。

报错与解决

~/about-caffe/caffe-ssd$ make all
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
CXX/LD -o .build_release/tools/finetune_net.bin
CXX/LD -o .build_release/tools/convert_imageset.bin
.build_release/lib/libcaffe.so: undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&)'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
.build_release/lib/libcaffe.so: undefined reference to `boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::put_mem_block(void*)'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::get_mem_block()'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
.build_release/lib/libcaffe.so: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const'
.build_release/lib/libcaffe.so: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const'
collect2: error: ld returned 1 exit status
Makefile:619: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1

问题类型:【undefined reference to `boost::re_detail::】

解决方案:看到一个说法是由于boost的版本过低,应该大于1.60,而本机的boost为1.58,所以手动下载一个boost进行安装,这里用的是1.68版本号的boost库,安装好之后问题解决。(安装boost参考本文:https://blog.csdn.net/this_capslock/article/details/47170313

另一个问题是在make pycaffe时候,出现:

~/about-caffe/caffe-ssd$ make py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
/usr/bin/ld: cannot find -lboost_python
collect2: error: ld returned 1 exit status
Makefile:501: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1

问题类型:【cannot find -lboost_python】,但是实际上在makefile中已经把boost_python 添加上了:

PYTHON_LIBRARIES ?= boost_python python2.7

这里的解决方案是吧上面一句改为:

PYTHON_LIBRARIES ?= boost_python27 python2.7

即将boost_python改为boost_python。

最后caffe和pycaffe都编译成功,运行ssd的python脚步,报错:

~/about-caffe/caffe-ssd$ python examples/ssd/ssd_pascal.py
Traceback (most recent call last):
  File "examples/ssd/ssd_pascal.py", line 2, in <module>
    import caffe
ImportError: No module named caffe

错误类型:【No module named XXX】,这种情况有两种可能的原因,一是没有make pycaffe;另一种是没有把pycaffe的路径加入环境变量。这里我们make过了pycaffe,因此需要手动用加入pycaffe到环境变量。

caffe_root = '~/about-caffe/caffe-ssd/'
if caffe_root + 'python' not in sys.path:
    sys.path.append(caffe_root + 'python')
# sys.path.insert(0, caffe_root + 'python')

import caffe

这样就可以运行了。

接下来就是讲lmdb数据换成需要训练的自己的数据集,对ssd的caffe模型进行训练了~

2018年8月13日16:14:04

你以为钟情于天使,其实是中了邪魔。 —— 剧作家,塞万提斯 【堂吉诃德】

原文地址:https://www.cnblogs.com/morikokyuro/p/13256711.html