在没有sudo的情况下,caffe的编译简直是个无比痛苦的事情,在折腾了整整三天之后,终于编了出来,这里把编译的过程记下来
命令执行完后看到如下所示即为成功:
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2 Detecting Python version... 2.6 Detecting Python root... /usr Unicode/ICU support for Boost.Regex?... not found. Generating Boost.Build configuration in project-config.jam... Bootstrapping is done. To build, run: ./b2 To adjust configuration, edit 'project-config.jam'. Further information: - Command line help: ./b2 --help - Getting started guide: http://www.boost.org/more/getting_started/unix-variants.html - Boost.Build documentation: http://www.boost.org/build/doc/html/index.html编译:
./b2 #消耗时间很长 ... ./b2 install --prefix=/home/Lynkzhang/Boost #没有sudo所以只能安装到自己的home下了参考:http://blog.csdn.net/u011641865/article/details/73498533
在一些库编译的时候提示版本太老:
CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.0 or higher is required. You are running version 2.8.12.2cmake版本过旧,自己编译一个cmake 3.0
wget https://cmake.org/files/v3.11/cmake-3.11.0-rc2.tar.gz tar zxvf cmake-3.11.0-rc2.tar.gz cd cmake-3.11.0-rc2 mkdir build cd build ../configure --prefix=/home/Lynkzhang/cmake #同理只能装在自己文件夹 make -j make install之后可以直接使用二进制 /home/Lynkzhang/cmake/bin/cmake 来做cmake
Protobuf不知道为什么没有CMakeList,所以用configure。
git clone https://github.com/google/protobuf.git cd protobuf mkdir build cd build ../configure --prefix=/home/Lynkzhang/Protobuf #指定安装位置 make -j make install下载链接:https://www.hdfgroup.org/downloads/hdf5/source-code/
在编译Dependencies的时候有一个问题,有些编译默认不会出编译出 .so 文件(没有共享库),如果发现没有,就需要添加一个flag:
cmake -DBUILD_SHARED_LIBS=ON接下来的内容应该可以有更优雅的解决方式,我直接暴力改了…
修改 cmake/Dependencies.cmake
set(GFLAGS_INCLUDE_DIR "/home/Lynkzhang/Gflags/include/") set(GFLAGS_LIBRARY "/home/Lynkzhang/Gflags/lib/libgflags.so") set(GLOG_INCLUDE_DIR "/home/Lynkzhang/Glog/include/") set(GLOG_LIBRARY "/home/Lynkzhang/Glog/lib64/libglog.so") set(HDF5_LIBRARIES "/home/Lynkzhang/hdf5/lib/libhdf5.so") set(HDF5_INCLUDE_DIRS "/home/Lynkzhang/hdf5/include/") set(HDF5_HL_LIBRARIES "/home/Lynkzhang/hdf5/lib/libhdf5_hl.so") set(Snappy_INCLUDE_DIR "/home/Lynkzhang/Snappy/include/") set(Snappy_LIBRARIES "/home/Lynkzhang/Snappy/lib64/libsnappy.so") set(OpenCV_INCLUDE_DIRS "/home/Lynkzhang/openCV/include/") set(Boost_INCLUDE_DIRS "/home/Lynkzhang/Boost/include") ... # ---[ Boost #find_package(Boost 1.54 REQUIRED COMPONENTS system thrvead filesystem) list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${Boost_INCLUDE_DIRS}) set(Boost_LIBRARIES "/home/Lynkzhang/Boost/lib/libboost_system.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${Boost_LIBRARIES}) set(Boost_LIBRARIES "/home/Lynkzhang/Boost/lib/libboost_thread.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${Boost_LIBRARIES}) set(Boost_LIBRARIES "/home/Lynkzhang/Boost/lib/libboost_filesystem.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${Boost_LIBRARIES}) ... # ---[ HDF5 #find_package(HDF5 COMPONENTS HL REQUIRED) ... # ---[ OpenCV: #find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) #if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found # find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc) #endif() # list(APPEND Caffe_LINKER_LIBS PUBLIC ${OpenCV_LIBS}) set(OpenCV_LIBS "/home/Lynkzhang/openCV/lib64/libopencv_core.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${OpenCV_LIBS}) set(OpenCV_LIBS "/home/Lynkzhang/openCV/lib64/libopencv_highgui.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${OpenCV_LIBS}) set(OpenCV_LIBS "/home/Lynkzhang/openCV/lib64/libopencv_imgcodecs.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${OpenCV_LIBS}) set(OpenCV_LIBS "/home/Lynkzhang/openCV/lib64/libopencv_imgproc.so") list(APPEND Caffe_LINKER_LIBS PUBLIC ${OpenCV_LIBS}) ... # ---[ BLAS if(NOT APPLE) set(BLAS "MKL") ...一般CUDNN都会在Cuda文件夹下面,找不到的情况下只能自己下载解压,添加路径: 修改cmake/Cuda.cmake
... # cudnn detection if(USE_CUDNN) set(CUDNN_INCLUDE "/home/Lynkzhang/cuda/include") set(CUDNN_LIBRARY "/home/Lynkzhang/cuda/lib64/libcudnn.so") list(APPEND Caffe_DEFINITIONS PUBLIC -DUSE_CUDNN) list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${CUDNN_INCLUDE}) list(APPEND Caffe_LINKER_LIBS PUBLIC ${CUDNN_LIBRARY}) endif() ...简单的编译:
mkdir build cd build cmake .. make -j make runtestruntest测试的时候有一个测试始终过不去,在论坛里有人给出了解决方案:给NVCC加上-G https://github.com/BVLC/caffe/issues/6164
After countless caffe compilations and tests, finally, I find a workaround to this problem: I add a line NVCCFLAGS += -G to Makefile and it changes from
# Debugging ifeq ($(DEBUG), 1) COMMON_FLAGS += -DDEBUG -g -O0 NVCCFLAGS += -G else COMMON_FLAGS += -DNDEBUG -O2 endifto
# Debugging ifeq ($(DEBUG), 1) COMMON_FLAGS += -DDEBUG -g -O0 NVCCFLAGS += -G else COMMON_FLAGS += -DNDEBUG -O2 NVCCFLAGS += -G endifThen, compiling caffe again… and make runtest passes without failure!
但是论坛里面的人用的是Make 和 Makeconfigure 的方法, cmake的方法需要修改CMakeList:
CMakeList.txt
# add definitions to nvcc flags directly set(Caffe_ALL_DEFINITIONS ${Caffe_DEFINITIONS}) list(REMOVE_ITEM Caffe_ALL_DEFINITIONS PRIVATE PUBLIC) list(APPEND CUDA_NVCC_FLAGS ${Caffe_ALL_DEFINITIONS}) list(APPEND CUDA_NVCC_FLAGS "-G") # add here重新从cmake开始编译一次。
最后结果: [==========] 2139 tests from 285 test cases ran. (619216 ms total) [ PASSED ] 2139 tests. [100%] Built target runtest
添加pythonpath:
export PYTHONPATH='/home/Lynkzhang/Caffe/python/':$PYTHONPATHPs. import caffe 时候报错,protobuf 有问题 原因是在编译protobuf的时候少做一步,
cd /home/Lynkzhang/protobuf/python python setup.py install解决