主要包含:
opencv编译安装;
boost-python3编译安装;
Caffe make及cmake两种安装方式,bug解决等。
1. 安装anaconda 直接到官网下载linux系统最新的anaconda安装shell脚本,在确保联网的情况下,bash AnacondaXXX.sh就行。
2. 创建anaconda的Python2.7及Python3.5环境 1 2 conda create -n caffe_py35 python=3.5 conda create -n caffe_py27 python=2.7
3. 安装NVIDIA GPU驱动、安装CUDA、配置cuDNN 4. 为不同版本的Python,安装支持CUDA的opencv 先到github上,下载opencv项目和opencv-contrib项目(opencv的扩展模块),都解压到目录/home/zhengyuwei/software/opencv
下。
到opencv
解压目录下,进入opencv
,执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 mkdir build cd buildsudo cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX="/home/zhengyuwei/software/opencv/opencv-install/" \ -D INSTALL_C_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH="/home/zhengyuwei/software/opencv/opencv_contrib-master/modules" \ -D BUILD_EXAMPLES=ON \ -D BUILD_opencv_python2=ON \ -D WITH_FFMPEG=1 \ -D WITH_TIFF=ON \ -D WITH_CUDA=ON \ -D CUDA_GENERATION=Pascal \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=ON \ -D WITH_LAPACK=OFF \ -D WITH_GTK=OFF -D WITH_GTK_2_X=OFF -D WITH_MATLAB=OFF -D WITH_QT=ON \ -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D ENABLE_CXX11=1 \ -D WITH_PROTOBUF=ON -D BUILD_PROTOBUF=OFF -D -BUILD_LIBPROTOBUF_FROM_SOURCES=OFF -D PROTOBUF_UPDATE_FILES=OFF \ -D ENABLE_PRECOMPILED_HEADERS=ON \ -D PROTOBUF_INCLUDE_DIR="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/include" \ -D PROTOBUF_INCLUDE_DIRS="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/include" \ -D PROTOBUF_LIBRARIES="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf.so;-lpthread" \ -D PROTOBUF_LIBRARY="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf.so" \ -D PROTOBUF_LIBRARY_DEBUG="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf.so" \ -D PROTOBUF_LITE_LIBRARIES="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf-lite.so" \ -D PROTOBUF_LITE_LIBRARY="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf-lite.so" \ -D PROTOBUF_LITE_LIBRARY_DEBUG="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotobuf-lite.so" \ -D PROTOBUF_PROTOC_EXECUTABLE="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/bin/protoc" \ -D PROTOBUF_PROTOC_LIBRARIES="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotoc.so" \ -D PROTOBUF_PROTOC_LIBRARY="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotoc.so" \ -D PROTOBUF_PROTOC_LIBRARY_DEBUG="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libprotoc.so" \ -D PYTHON2_EXECUTABLE="/home/zhengyuwei/software/anaconda3/envs/caffe_py27/bin/python" \ -D PYTHON2_INCLUDE_DIR="/home/zhengyuwei/software/anaconda3/envs/caffe_py27/include/python2.7" \ -D PYTHON2_LIBRARY="/home/zhengyuwei/software/anaconda3/envs/caffe_py27/lib/libpython2.7.so" \ -D PYTHON2_NUMPY_INCLUDE_DIRS="/home/zhengyuwei/software/anaconda3/envs/caffe_py27/lib/python2.7/site-packages/numpy/core/include" \ -D PYTHON3_EXECUTABLE="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/bin/python" \ -D PYTHON3_INCLUDE_DIR="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/include/python3.5m" \ -D PYTHON3_LIBRARY="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/libpython3.5m.so" \ -D PYTHON3_NUMPY_INCLUDE_DIRS="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib/python3.5/site-packages/numpy/core/include" \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_SKIP_PYTHON_LOADER=ON .. sudo make -j sudo make install
Q1: unsupported/Eigen/…不存在?
A1: 到eigen github下载压缩包,解压后里面有unsupported
目录,移到能找到的include
文件夹下。
Q2: opencv-3.4/modules/dnn/misc/tensorflow/graph.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
protobuf版本问题?
A2: 增加cmake选项-D BUILD_PROTOBUF=OFF -D BUILD_opencv_dnn=OFF
。
安装完成后,到安装路径(CMAKE_INSTALL_PREFIX=/home/zhengyuwei/software/opencv/opencv-install
)的lib
目录下,可以发现python2.7
和python3.5
两个文件夹,里面都有site-packages/cv2.XXX
文件,到对应python环境lib/pythonX.X/site-packages
下,建立so文件的软链接:
1 2 3 cd /home/zhengyuwei/software/anaconda3/envs/caffe_py27/libln -s /home/zhengyuwei/software/opencv/opencv-install/lib/python2.7/site-packages/cv2.so \ /home/zhengyuwei/software/anaconda3/envs/caffe_py27/lib/python2.7/site-packages/cv2.so
最后,检查是否安装成功,也可建立对应的opencv.pc文件,并添加到PATH路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 prefix=/home/zhengyuwei/software/opencv/opencv-install exec_prefix=${prefix} libdir=${exec_prefix} /lib includedir_old=${prefix} /include/opencv4 includedir_new=${prefix} /include Name: OpenCV Description: Openurce Computer Vision Library Version: 4.1.0-pre Libs: -L${libdir} -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_cudaarithm -lopencv_cudabgsegm -lopencv_cudacodec -lopencv_cudafeatures2d -lopencv_cudafilters -lopencv_cudaimgproc -lopencv_cudalegacy -lopencv_cudaobjdetect -lopencv_cudaoptflow -lopencv_cudastereo -lopencv_cudawarping -lopencv_cudev -lopencv_datasets -lopencv_dnn_objdetect -lopencv_dnn -lopencv_dpm -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy -lopencv_gapi -lopencv_hdf -lopencv_hfs -lopencv_highgui -lopencv_imgcodecs -lopencv_img_hash -lopencv_imgproc -lopencv_line_descriptor -lopencv_ml -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching -lopencv_text -lopencv_tracking -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto Libs.private: -L/usr/local /public/anaconda3/lib -lpng -lz -L/usr/lib/x86_64-linux-gnu -ljasper -ljpeg -lImath -lIlmImf -lIex -lHalf -lIlmThread -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lgthread-2.0 -lgstbase-1.0 -lgstreamer-1.0 -lgstvideo-1.0 -lgstapp-1.0 -lgstriff-1.0 -lgstpbutils-1.0 -ldc1394 -lavcodec -lavformat -lavutil -lswscale -ldl -lm -lpthread -lrt -L/usr/lib -lopenblas -L/usr/local /cuda-8.0 -l64 Cflags: -I${includedir_old} -I${includedir_new}
5. 安装caffe 把caffe源码库拷贝下来后,有两种编译caffe的方式:
在caffe目录下直接 make、 make pycaffe 来构建工程caffe的C++库和Python库;
先mkdir build后,在build文件夹下cmake,然后再make、make pycaffe。
对于makefile方法 主要修改 Makefile.config 文件,主要修改点有:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 USE_CUDNN := 1 OPENCV_VERSION := 3 CUDA_DIR := /usr/local /cuda-8.0 PYTHON_INCLUDE := /usr/local /public/anaconda3/envs/caffe_py35/include \ /usr/local /public/anaconda3/envs/caffe_py35/lib/python3.5/site-packages/numpy/core/include PYTHON_LIBRARIES := boost_python3 python3.5m PYTHON_LIB := /usr/local /public/anaconda2/envs/caffe_py3.5/lib /usr/local /lib/ INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local /include /usr/include/hdf5/serial LIBRARY_DIRS := $(PYTHON_LIB) $(CUDA_DIR)/lib64 /usr/local /lib \ /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial USE_NCCL := 1
同时,也可修改下Makefile文件里的:
1 NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
对于cmake方法 修改cmake/Dependencies.cmake:在 if(BUILD_python)
分支中, if(NOT "${python_version}" VERSION_LESS "3.0.0")
分支中,添加:
1 2 3 4 if (NOT Boost_PYTHON_FOUND) find_package (Boost 1.46 COMPONENTS "python3" ) set (Boost_PYTHON_FOUND ${Boost_PYTHON3_FOUND} ) endif ()
然后到build文件加下,执行cmake:
1 2 3 4 5 6 7 8 9 10 11 sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CUDA_TOOLKIT_ROOT_DIR="/usr/local/cuda-8.0" \ -D CUDNN_INCLUDE="/usr/local/cuda-8.0/include" \ -D CUDNN_LIBRARY="/usr/local/cuda-8.0/lib64/libcudnn.so" \ -D PYTHON_LIBRARIES="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/lib;/usr/local/lib" \ -D PYTHON_INCLUDE_DIR="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/include/python3.5m/" \ -D PYTHON_EXECUTABLE="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/bin/python" \ -D python_version=3.5 -D USE_NCCL=1 \ -D NCCL_INCLUDE_DIR="/home/zhengyuwei/software/nccl-master/build/include" \ -D NCCL_LIBRARIES="/home/zhengyuwei/software/nccl-master/build/lib/libnccl.so" ..
Q1: 若opencv报错,没有找到opencv相关的定义。
A1: 确认安装了opencv,在cmake/dependencies.cmake文件下,if(USE_OPENCV)
行后添加一行指定opencv的安装位置:
1 2 if (USE_OPENCV) set (CMAKE_PREFIX_PATH /home/zhengyuwei/software/opencv/opencv-install ${CMAKE_PREFIX_PATH} )
Q2: 如果报compute_image_mean.cpp:(.text.startup+0x16d): undefined reference to google::FlagRegisterer::FlagRe
相关的错误。
A2: gflags的原因,头文件和so版本不一致,导致定义不存在。查看cmake xxx ..
时输出的以下信息,保证路径中头文件和so一直,当然,还有glog等。
1 2 -- Found GFlags: /usr/local /include -- Found gflags (include: /usr/local /include, library: /usr/local /lib/libgflags.so)
Q3: 如果cmake失败,若是protoc报错 ,且确认安装protobuf且版本正确,尝试设置以下环境后,重新安装caffe:
1 2 3 export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH " export LIBRARY_PATH="/usr/local/lib:$LIBRARY_PATH " sudo ldconfig
其实这是在尝试使caffe编译时,能够去获取到指定的版本。另外两个可尝试的方法是:
直接在/etc/sudoers里添加protobuf的bin路径。
修改cmake/ProtoBuf.cmake,最前面加:
1 2 set (CMAKE_PREFIX_PATH /home/zhengyuwei/software/protobuf/protobuf-install -2.6 .1 ${CMAKE_PREFIX_PATH} )find_package ( Protobuf 2.6 .1 )
6. 安装boost-python3 若是boost-python3报错 :libboost_python3.so.1.56.0: undefined symbol: PyClass_Type;则表示有人用python2编译boost产生boost-python3,而boost-python3是不需要PyClass_Type的,此时需要安装新的boost:
官网下载boost代码压缩包,解压;
修改/tools/build/src/tools/
路径下的python.jam
文件中的547行:
1 includes ?= $(prefix)/include/python$(version) ; -> includes ?= $(prefix)/include/python$(version)m ;
1 2 3 4 5 6 7 8 9 10 11 12 13 ./bootstrap.sh --with-python="/home/zhengyuwei/software/anaconda3/envs/caffe_py35/bin/python3" \ --with-python-version=3.5 --with-python-root="/home/zhengyuwei/software/anaconda3/envs/caffe_py35" \ --prefix="/home/zhengyuwei/software/boost_1_69_0/build" ./b2 install -a --with=all ./b2 --with-python --buildid=3 ./b2 --with-python --clean ./bootstrap.sh --with-python="/home/zhengyuwei/software/anaconda3/envs/caffe_py27/bin/python" \ --with-python-version=2.7 --with-python-root="/home/zhengyuwei/software/anaconda3/envs/caffe_py27" \ --prefix="/home/zhengyuwei/software/boost_1_69_0/build" ./b2 --with-python --buildid=2
查看安装目录下lib,有boost-pythonX.so文件,建软连接就行了。