前前后后花了快一周才搭建好caffe環(huán)境依溯,最開始使用的是anaconda的Python,但是在編譯opencv的時候一直出各種問題,在網上查了很多博客都沒有有效的解決葡粒,所以果斷放棄了使用anaconda份殿。在此記錄一下搭建的整個過程以及出現(xiàn)過的報錯等,同時也感謝參考過的所有博客嗽交。
1.安裝Ubuntu16.04
這里省略了安裝過程,只記錄了配置靜態(tài)ip
(1)編輯/etc/network/interfaces文件
sudo gedit /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
#auto lo
#iface lo inet loopback
auto eno1 #這里需要用ifconfig命令查看一下自己的網卡名稱
iface eno1 inet static
address x.x.x.x #ip地址
netmask x.x.x.x #子網掩碼
gateway x.x.x.x #網關
dns-nameserver x.x.x.x ###dns
(2)重啟網卡
sudo /etc/init.d/networking restart
2.安裝Nvidia
(1)禁用nouveau
sudo gedit /etc/modprobe.d/blacklist.conf
在最后一行添加
backlist vga16fb
blacklist nouveau
blacklist rivafb
blacklist rivatv
blacklist nvidiafb
(2)更新內核
sudo update-initramfs -u
(3)關機重啟
lsmod | grep nouveau
沒有輸出則關閉成功
(4)關閉圖像界面
sudo service lightdm stop
(5)按Ctrl+Alt+F1進入命令行界面
(6)給run文件賦予執(zhí)行權限
sudo chmod a+x NVIDIA-Linux-x86_64-xxx.xx.run
(7)安裝
sudo ./NVIDIA-Linux-x86_64-xxx.xx.run –no-opengl-files
(8)退出命令行界面
sudo service lightdm start
(9)重啟卿嘲,驗證是否安裝成功
nvidia-smi
3.安裝Cuda8.0
(1)下載CUDA
(2)執(zhí)行如下命令(執(zhí)行過程中會讓你選擇是否那種nvidia驅動,需要選否)
sudo chmod 777 cuda_8.0.61_375.26_linux.run
sudo ./cuda_8.0.61_375.26_linux.run
(3)配置環(huán)境變量
- 打開bashrc文件
sudo gedit ~/.bashrc
將以下內容寫入到~/.bashrc尾部
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
- 在/etc/profile文件中添加CUDA環(huán)境變量
sudo gedit /etc/profile
打開文檔都在文檔結尾加上下面兩句
PATH=/usr/local/cuda/bin:$PATH
export PATH
- 使環(huán)境變量立即生效
source /etc/profile
- 添加lib庫路徑轮纫,在 /etc/ld.so.conf.d/新建文件 cuda.conf
sudo gedit /etc/ld.so.conf.d/cuda.conf
- 添加如下路徑
/usr/local/cuda/lib64
- 執(zhí)行下列命令使之立刻生
sudo ldconfig
- 測試CUDA的samples
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
make
sudo ./deviceQuery
顯示為pass則成功
4.安裝cudnn6.0
(1)下載對應的cudnn
(2)執(zhí)行如下命令
sudo tar -xzvf cudnn-8.0-linux-x64-v6.0.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
5.修改默認python版本
本文沒有采用annconda來進行安裝腔寡。因此在編譯之前需要先將ubuntu默認的python版本修改為python3.5。
(1)使用 update-alternatives 來為整個系統(tǒng)更改 Python 版本
# update-alternatives --list python
update-alternatives: error: no alternatives for python
出現(xiàn)以上所示的錯誤信息掌唾,表示 Python 的替代版本尚未被 update-alternatives 命令識別
(2)更新替代列表
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode
(3)再次列出可用的 Python 替代版本
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.5
(4)使用下方的命令隨時在列出的 Python 替代版本中任意切換
# update-alternatives --config python
(5)移除替代版本
# update-alternatives --remove python /usr/bin/python2.7
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode
因為切換了版本放前,所以在使用pip時要注意使用pip3還是pip
6.編譯安裝opencv3.2
(1)下載opencv
手動下載:https://opencv.org/releases.html 下載源碼后解壓
或 wget https://github.com/Itseez/opencv/archive/3.2.0.zip
(2)安裝依賴(還差什么以后再補)
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3.5-dev
sudo apt-get install python3-numpy
(3)創(chuàng)建編譯文件夾
#進入opencv目錄
mkdir build
cd build
(4)編譯準備(修改成自己的路徑忿磅,python2版本只需要保留前兩行)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.5 \
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ ..
(5)編譯安裝
sudo make -j16
sudo make install
(6)添加環(huán)境變量
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
添加環(huán)境變量
gedit ~/.bashrc
把下面這一句加入文檔的最末尾。
export PYTHONPATH=/usr/local/lib/python3.5/dist-packages:$PYTHONPATH
然后
sudo ldconfig
sudo apt-get update
(7)驗證
python
>>>import cv2
>>>cv2.__version__
顯示正確則安裝成功
(8)報錯信息:可能會提示ippicv_linux_20151201下載失敗凭语,這是由于網絡原因葱她,可以自己下載好之后放到相應的文件夾
下載ippicv_linux_20151201
將其放入/home/xxx/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e
路徑下,替換原來的壓縮包似扔,然后重新編譯
(9) 如果想用python調用opencv吨些,安裝python3-opencv
sudo apt-get install python3-opencv
sudo apt-get install python3-numpy
7.安裝caffe
(1)安裝依賴
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev
sudo apt-get install libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
(2)https://github.com/BVLC/caffe 下載caffe后解壓
(3)復制Makefile.config文件
sudo cp Makefile.config.example Makefile.config
(4)修改配置文件Makefile.config
sudo gedit Makefile.config
- 將第5行注釋去掉
USE_CUDNN := 1
- 將第21行注釋去掉
OPENCV_VERSION := 3
- 將第37和38行刪除(cuda8.0后不再支持)
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH :=-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
- 修改第51行
BLAS := open
- 修改python路徑信息
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python3.5 \
/usr/lib/python3/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3/dist-packages/numpy/core/include
- 將# Whatever else you find you need goes here. 下面的
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
替換成
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
- 若要使用python來編寫layer,則去掉下面注釋
#WITH_PYTHON_LAYER := 1 >>>> WITH_PYTHON_LAYER := 1
(5)修改Makefile文件
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替換成
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial
替換成
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
(6)編譯
sudo make all –j16
sudo make test –j16
sudo make runtest –j16
(7)編譯pycaffe
- 在caffe根目錄的python文件夾下炒辉,有一個requirements.txt的清單文件豪墅,上面列出了需要的依賴庫,按照這個清單安裝
cd ./python
for req in $(cat requirements.txt); do pip3 install $req; done
- 安裝完成以后黔寇,再次回到caffe根目錄執(zhí)行
sudo pip3 install -r python/requirements.txt
- 編譯pycaffe接口
sudo make pycaffe -j16
- 配置環(huán)境變量偶器,以便python調用
sudo gedit ~/.bashrc
export PYTHONPATH=/home/caffe/python:$PYTHONPATH
source ~/.bashrc
(8)測試
python
>>> import caffe
沒有報錯則安裝成功
報錯信息:
(1)錯誤1(import caffe時)
undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
/usr/lib/x86_64-linux-gnu/路徑下查找boost_python文件的版本是否與python版本匹配,caffe默認的配置文件為python2.7,因此這里需要修改
將caffe的Makefile的第208行修改為(根據(jù)自己的版本修改)
PYTHON_LIBRARIES := boost_python-py35 python3.5m
然后重新編譯
sudo make clean
sudo ldconfig
sudo make all
sudo make test
sudo make runtest
(2)錯誤2(import caffe時)
File "/usr/local/lib/python3.5/dist-packages/dateutil/rrule.py", line 55
raise ValueError, "Can't create weekday with n == 0"
^
SyntaxError: invalid syntax
升級matplotlib包
(3)錯誤3(import caffe時)
ImportError: No module named 'scipy._lib.decorator'
卸載scipy后重裝,若是在卸載時出現(xiàn)
Cannot uninstall 'ipython'. It is a distutils installed project and thus we cannot accurately det
可以直接在相應的python路徑:lib/site-packages下刪除ipython.xxx.egg-info文件
sudo pip3 install scipy
重新編譯再測試缝裤。
個人主頁
參考鏈接
Ubuntu16.04 + CUDA8.0 + OpenCL + Nvidia 安裝配置和問題總結
Ubuntu16.04 14.04安裝配置Caffe(GPU版)
更改Ubuntu默認python版本的方法
Ubuntu 16.04下安裝Caffe解決 undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
Caffe——Ubuntu 16.04 CPU版本安裝
ImportError: No module named 'scipy._lib.decorator'
Ubuntu18.04LTS下基于 Anaconda3 安裝 Caffe-GPU及 Python3.6 + Pycharm + Mnist例子教程