本機Geforce gtx1060 驅動版本384.130, 已安裝cuda-9.0,cudnn7.0.5,Anaconda3
常用命令:
find /|grep filename #尋找某個文件
ls -l # 列出該文件夾下的所有文件信息
sudo gedit file
sudo chmod 777 filename
sudo cp filename1 filename2
sudo ln -s filename1 filename2 # 把filename1建立軟鏈接為filename2
ldconfig -v # 在默認搜尋目錄(/lib和/usr/lib)以及動態庫配置文件/etc/ld.so.conf內所列的目錄下,搜索出可共享的動態鏈接庫(格式如,lib*.so*),
進而創建出動態裝入程序(ld.so), 所以往/lib和/usr/lib里面加東西,是不用修改/etc/ld.so.conf的,但是添加之后之后要運行一下 ldconfig
sudo gedit ~/.bashrc # 添加環境變量需要,每個用戶都有一個.bashrc文件,在用戶目錄下.
# /etc/bashrc或/etc/bash.bashrc:為每一個運行bash shell的用戶執行此文件
source ~/.bashrc #重新執行剛修改的初始化文件,使之立即生效,而不必注銷並重新登錄
第一步 安裝caffe環境
按照https://www.jianshu.com/p/8d309dcc8b99的步驟一步一步走
安裝caffe for python的虛擬環境--->安裝基礎依賴庫--->克隆Caffe代碼--->修改Makefile.config、Makefile
--->編譯pycaffe--->拷貝編譯后的libcaffe.so至caffe的虛擬環境。
我的最終版Makefile.config內容如下:
注意: 在沒有安裝opencv之前,我沒有去取消23和108行的注釋,是為了一步一步來,防止不知道問題出在caffe還是opencv。
對於該文件的修改,我還結合了這篇https://www.cnblogs.com/whb-20160329/p/10256766.html,哪里該注釋掉還是取消注釋,自己最好搞清楚,會有所收獲。
1 ## Refer to http://caffe.berkeleyvision.org/installation.html 2 # Contributions simplifying and improving our build system are welcome! 3 4 # cuDNN acceleration switch (uncomment to build with cuDNN). 5 USE_CUDNN := 1 6 7 # CPU-only switch (uncomment to build without GPU support). 8 # CPU_ONLY := 1 9 10 # uncomment to disable IO dependencies and corresponding data layers 11 # USE_OPENCV := 0 12 # USE_LEVELDB := 0 13 # USE_LMDB := 0 14 # This code is taken from https://github.com/sh1r0/caffe-android-lib 15 # USE_HDF5 := 0 16 17 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) 18 # You should not set this flag if you will be reading LMDBs with any 19 # possibility of simultaneous read and write 20 # ALLOW_LMDB_NOLOCK := 1 21 22 # Uncomment if you're using OpenCV 3 23 OPENCV_VERSION := 3 24 25 # To customize your choice of compiler, uncomment and set the following. 26 # N.B. the default for Linux is g++ and the default for OSX is clang++ 27 # CUSTOM_CXX := g++ 28 29 # CUDA directory contains bin/ and lib/ directories that we need. 30 CUDA_DIR := /usr/local/cuda-9.0 31 # On Ubuntu 14.04, if cuda tools are installed via 32 # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: 33 # CUDA_DIR := /usr 34 35 # CUDA architecture setting: going with all of them. 36 # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. 37 # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. 38 # For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility. 39 CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \ 40 -gencode arch=compute_35,code=sm_35 \ 41 -gencode arch=compute_50,code=sm_50 \ 42 -gencode arch=compute_52,code=sm_52 \ 43 -gencode arch=compute_60,code=sm_60 \ 44 -gencode arch=compute_61,code=sm_61 \ 45 -gencode arch=compute_61,code=compute_61 46 47 # BLAS choice: 48 # atlas for ATLAS (default) 49 # mkl for MKL 50 # open for OpenBlas 51 BLAS := atlas 52 # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. 53 # Leave commented to accept the defaults for your choice of BLAS 54 # (which should work)! 55 # BLAS_INCLUDE := /path/to/your/blas 56 # BLAS_LIB := /path/to/your/blas 57 58 # Homebrew puts openblas in a directory that is not on the standard search path 59 # BLAS_INCLUDE := $(shell brew --prefix openblas)/include 60 # BLAS_LIB := $(shell brew --prefix openblas)/lib 61 62 # This is required only if you will compile the matlab interface. 63 # MATLAB directory should contain the mex binary in /bin. 64 # MATLAB_DIR := /usr/local 65 # MATLAB_DIR := /Applications/MATLAB_R2012b.app 66 67 # NOTE: this is required only if you will compile the python interface. 68 # We need to be able to find Python.h and numpy/arrayobject.h. 69 # PYTHON_INCLUDE := /usr/include/python2.7 \ 70 /usr/lib/python2.7/dist-packages/numpy/core/include 71 # Anaconda Python distribution is quite popular. Include path: 72 # Verify anaconda location, sometimes it's in root. 73 ANACONDA_HOME := $(HOME)/anaconda3/envs/caffe 74 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 75 $(ANACONDA_HOME)/include/python3.5m \ 76 $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include 77 78 # Uncomment to use Python 3 (default is Python 2) 79 # PYTHON_LIBRARIES := boost_python3 python3.5m 80 # PYTHON_INCLUDE := /usr/include/python3.5m \ 81 # /usr/lib/python3.5/dist-packages/numpy/core/include 82 83 # We need to be able to find libpythonX.X.so or .dylib. 84 # PYTHON_LIB := /usr/lib 85 PYTHON_LIB := $(ANACONDA_HOME)/lib 86 87 # Homebrew installs numpy in a non standard path (keg only) 88 # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include 89 # PYTHON_LIB += $(shell brew --prefix numpy)/lib 90 91 # Uncomment to support layers written in Python (will link against Python libs) 92 WITH_PYTHON_LAYER := 1 93 94 # Whatever else you find you need goes here. 95 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/ /usr/local/cuda-9.0/include/ 96 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/cuda-9.0/lib64/ 97 98 # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies 99 # INCLUDE_DIRS += $(shell brew --prefix)/include 100 # LIBRARY_DIRS += $(shell brew --prefix)/lib 101 102 # NCCL acceleration switch (uncomment to build with NCCL) 103 # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) 104 # USE_NCCL := 1 105 106 # Uncomment to use `pkg-config` to specify OpenCV library paths. 107 # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) 108 USE_PKG_CONFIG := 1 109 110 # N.B. both build and distribute dirs are cleared on `make clean` 111 BUILD_DIR := build 112 DISTRIBUTE_DIR := distribute 113 114 # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 115 # DEBUG := 1 116 117 # The ID of the GPU that 'make runtest' will use to run unit tests. 118 TEST_GPUID := 0 119 120 # enable pretty build (comment to see full commands) 121 Q ?= @
編譯caffe,要在你git下載的caffe目錄下編譯
make -j8 # make clean是清除之前編譯產生的內容
這里你可能會遇到一些問題,仔細檢擦自己的Makefile.config先(要搞明白這個文件),再去百度搜索問題。
第二步 安裝opencv3.4.2
同時參考https://blog.csdn.net/dl_chenbo/article/details/78687461/和https://www.jianshu.com/p/c42fc8702f2b,下面只說與他們的不同之處:
1. 本博客所述整個過程只要是安裝東西,我都是在激活虛擬環境下安裝的,因為本身用虛擬環境就是為了不影響別的用戶。
2. 我下載的是3.4.2版本,解壓之后如下
3. CMake 配置如下:(根據自己的目錄修改幾個小地方)
cmake -D CMAKE_BUILD_TYPE=Release -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/fly/opencv/opencv_contrib-3.4.2/modules -D CMAKE_LIBRARY_PATH=/usr/local/cuda-9.0/lib64/stubs -D WITH_CUDA=ON -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_opencv_dnn=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES --expt-relaxed-constexpr" -DCMAKE_C_COMPILER=/usr/bin/cc -DCMAKE_CXX_COMPILER=/usr/bin/c++ -DBUILD_LIBPROTOBUF_FROM_SOURCES=ON -DBUILD_PERF_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -DPYTHON3_NUMPY_INCLUDE_DIRS=$(python -c "import numpy; import os; print(os.path.join(numpy.__path__[0], 'core', 'include'))") -DPYTHON_LIBRARY=/home/fly/anaconda3/envs/caffe/lib/libpython3.5m.so ..
4. 編譯遇到許多文件下載不下來
參考https://blog.csdn.net/CSDN330/article/details/86747867
打開這個文件,ctrl+f 查找failed,自然會發現它在哪些個網址下載哪些包(安裝不同的版本的opencv對應不同版本的包)的時候失敗,以及這些包應該下載的位置,我利用一個插件(類似於vpn)下載了這些包,並且根據參考鏈接里的方法將下載地址修改為本地,才成功。
還要(重要!)
進入目錄:/etc/ld.so.conf.d
創建:opencv.conf # sudo gedit opencv.conf
添加內容:/home/fly/opencv/opencv-3.4.2/build/lib
執行:ldconfig
最后make install別忘了。
補充說明:提供的鏈接目的是為了更好理解自己的安裝。遇到問題,先看鏈接里面有木有,檢查自己的,再百度。
第三步 再次編譯caffe(此時你已經安裝了opencv)
make clean
make -j8
make test -j8
make runtest -j8
make pycaffe -j8
我遇到問題之后就做了下面的事情
進入home/fly/ , sudo gedit ~/.bashrc,再最后加上
export PYTHONPATH=/home/fly/CAFFE_ROOT/caffe/python:$PYTHONPATH #將caffe的python路徑加入到環境變量中,為了使得import caffe成功
export PKG_CONFIG_PATH=/home/fly/anaconda3/envs/caffe/lib/pkgconfig:$PKG_CONFIG_PATH # 將opencv庫路徑加入環境變量
#查看linux下的opencv安裝版本
pkg-config opencv --modversion
#這里你可以使用pkg-config opencv --libs
看你的opencv安裝在哪里,也與makefile.config里面的第108行相呼應。
再次參考https://www.cnblogs.com/whb-20160329/p/10256766.html和https://www.jianshu.com/p/8d309dcc8b99和https://www.jianshu.com/p/c42fc8702f2b后面的部分(根據自己的目錄修改),其中這個pip install scikit-image protobuf ,caffe依賴的庫一定要裝,不然會import不成功。
最終
說明:第一次配置caffe,歷時五天,踩了許多坑,參考了很多博客和網站,也收獲了不少,希望正在路上安裝的不要放棄,搞清楚每一步做了什么,先后順序。這個博客也是記錄一下,有許多不足之處,只能靠各位同仁的努力,會成功的。