# 安裝第三方依賴庫 sudo apt-get update && apt-get install -y --no-install-recommends \ build-essential \ cmake \ vim \ git \ wget \ curl \ zip \ liblapack-dev \ liblapack3 \ libatlas-base-dev \ libopenblas-dev \ libboost-all-dev \ libgflags-dev \ libgoogle-glog-dev \ libhdf5-serial-dev \ libleveldb-dev \ liblmdb-dev \ libprotobuf-dev \ libsnappy-dev \ protobuf-compiler \ python-dev \ python-numpy \ python-opencv \ python-scipy # 刪除軟件安裝包 sudo rm -rf /var/lib/apt/lists/*
注意:若是出現如下提示「E: 沒法得到鎖 /var/cache/apt/archives/lock - open (11: 資源暫時> 不可用) E: 沒法對目錄 /var/cache/apt/archives/ 加鎖」,請刪除對應目錄 後再執行apt-get命令python
sudo rm -rf /var/cache/apt/archives/lock
# 須要將pip升級到10.0.1版本(使用pip install --upgrade或者pip install -U pip會致使pip沒法使用),不然沒法安裝setuptools,升級到10.0.1版本後會自動成功安裝setuptools # 卸載pip(沒有安裝則該如下命令不作任何處理) sudo apt-get remove -y python-pip # 官方下載pip安裝文件(代替sudo apt-get install -y python-pip) sudo -i curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python get-pip.py sudo rm -rf get-pip.py # 刪除pip安裝文件 # 若是pip有問題 sudo apt autoremove -y python-pip # pip方式安裝第三方庫 sudo -H pip install Cython \ numpy \ scipy \ scikit-image \ matplotlib \ ipython \ h5py \ leveldb \ networkx \ nose \ pandas \ python-dateutil \ protobuf \ python-gflags \ pyyaml \ Pillow \ six --user # 也能夠按照$CAFFE_ROOT/python/requirements.txt中指定具體版本安裝 pip install Cython==0.19.2 \ numpy==1.7.1 \ scipy==0.13.2 \ scikit-image==0.9.3 \ matplotlib==1.3.1 \ ipython==3.0.0 \ h5py==2.2.0 \ leveldb==0.191 \ networkx==1.8.1 \ nose==1.3.0 \ pandas==0.12.0 \ python-dateutil==2.6.0 \ protobuf==2.5.0 \ python-gflags==2.0 \ pyyaml==3.10 \ Pillow==2.3.0 \ six==1.1.0 --user
sudo -i #切換到root用戶 # Ubuntu16.04默認安裝OpenCV3.0+,須要手動安裝OpenCV2.0 wget -O /opt/opencv2.4.13.6.zip https://github.com/opencv/opencv/archive/2.4.13.6.zip unzip opencv2.4.13.6.zip cd opencv-2.4.13.6/ && mkdir release/ && cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. make && make install
注意:FATAL: In-source builds are not allowed. You should create separate directory for build files. ---Configuring incomplete, errors occurred! 則應該是在代碼根目錄下直接執行過 cmake,致使根目錄下生成了 CMakeCache.txt,CMakefile,須要刪除 CMakeCache.txt,CMakeFile再次執行編譯便可。linux
# 設置使用vim編輯文件時設置行號 sudo vim /etc/vim/vimrc # 再最後一行添加 set number # 保存退出
su guxiaotu # 切換回用戶guxiaotu # 切換到當前用戶主目錄下 cd $id git clone https://github.com/weiliu89/caffe.git caffe-ssd # 進入caffe-ssd源代碼目錄 cd caffe-ssd # checkout出ssd算法源碼 git checkout ssd
注意:出現如下提示說明分支切換成功「分支 ssd 設置爲跟蹤來自 origin 的遠程分支 ssd。切換到一個新分支 'ssd'」git
# 在最後添加,caffe源碼默認存放在當前用戶hxr的主目錄下 echo 'export CAFFE_ROOT=/home/guxiaotu/caffe-ssd' >> ~/.bashrc # 配置$CAFFE_ROOT # 配置$CAFFE_ROOT # 將/usr/lib/python2.7/dist-packages和$CAFFE_ROOT/python追加到$PYTHONPATH. echo 'export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:$CAFFE_ROOT/python'>>~/.bashrc # 將$CAFFE_ROOT/build/tool命令工具追加到$PATH中 echo 'export PATH=$PATH:$CAFFE_ROOT/build/tool' >> ~/.bashrc # 使環境變量生效 source ~/.bashrc
cd $CAFFE_ROOT # 複製Makefile.confit cp Makefile.config.example Makefile.config # 編輯Makfile.config sudo vim Makefile.config # 修改如下內容 CPU_ONLY := 1 # 第8行,將前面#取消,啓用只使用CPU模式 WITH_PYTHON_LAYER := 1 # 第89行,取消註釋表示使用Python編寫layer
注意:配置OpenCV3(須要修改Makefile等文件,因此我直接使用OpenCV2,而不配置OpenCV3)github
# 終端下查看OpenCV版本(如下兩種方法均可以) pkg-config --modversion opencv # 顯示爲3.1版本 apt show libopencv-dev # 取消註釋啓用OPENCV_VERNSION OPENCV_VERSION := 3 # 第21行,因爲ubuntu17.10安裝的依賴opencv爲3.0版本,因此也啓用
注意:第65~67行代表了makefile文件從哪一個文件路徑尋找python的numpy庫,因此在/etc/profile中環境變量PYTHONPATH須要手動追加/usr/lib/python2.7/dist-packages(Python2.7),另外還提供了Anaconda方式配置Python,以及Matlab環境的配置web
65 # We need to be able to find Python.h and numpy/arrayobject.h. 66 PYTHON_INCLUDE := /usr/include/python2.7 \ 67 /usr/lib/python2.7/dist-packages/numpy/core/include
注意:第75~78行代表默認Python環境爲Python2.7算法
75 # Uncomment to use Python 3 (default is Python 2) 76 # PYTHON_LIBRARIES := boost_python3 python3.5m 77 # PYTHON_INCLUDE := /usr/include/python3.5m \ 78 # /usr/lib/python3.5/dist-packages/numpy/core/include
91 # Whatever else you find you need goes here. 92 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 93 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib # 後面追加配置hdf5路徑 92 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial 93 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
su hxr #切換用戶hxr # 保證在$CAFFE_ROOT目錄中 cd $CAFFE_ROOT make -j8 # 編譯,-j8:加速CPU便宜速度,其餘參數-j4,-j16 # 確保$CAFFE_ROOT/python添加到環境變量PYTHONPATH中(詳細請看4. 設置環境變量) make pycaffe # 編譯pycaffe make test -j8 # (可選) make runtest -j8
清除make編譯命令 make cleanbootstrap
$CAFFE_ROOT/models/VGGNet/
# 若是使用做者已經訓練好的模型數據,請下載到$CAFFE_ROOT/model sudo wget -P $CAFFE_ROOT/model http://www.cs.unc.edu/%7Ewliu/projects/SSD/models_VGGNet_VOC0712_SSD_300x300.tar.gz # 解壓到制定目錄 tar -zxvf $CAFFE_ROOT/model/models_VGGNet_VOC0712_SSD_300x300.tar.gz -C $CAFFE_ROOT/model
$HOME/data/
中# 用戶主目錄下建立data目錄後進入 mkdir $HOME/data # 下載數據集 sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar # 解壓到指定目錄(必須按照如下順序解壓,不能顛倒) tar -xvf $HOME/data/VOCtrainval_11-May-2012.tar -C $HOME/data tar -xvf $HOME/data/VOCtrainval_06-Nov-2007.tar -C $HOME/data tar -xvf $HOME/data/VOCtest_06-Nov-2007.tar -C $HOME/data
注意:三個壓縮文件解壓順序必定不能打亂ubuntu
cd $CAFFE_ROOT # 必須保證在$CAFFE_ROOT中執行 # 在data/VOC0712/中建立trainval.txt, test.txt, and test_name_size.txt ./data/VOC0712/create_list.sh # You can modify the parameters in create_data.sh if needed. # It will create lmdb files for trainval and test with encoded original image: # - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb # - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb # and make soft links at examples/VOC0712/ ./data/VOC0712/create_data.sh
注意:若是提示缺乏某個model,說明缺乏對應Python第三方庫,使用pip install安裝vim
# It will create model definition files and save snapshot models in: # - $CAFFE_ROOT/models/VGGNet/VOC0712/SSD_300x300/ # and job file, log file, and the python script in: # - $CAFFE_ROOT/jobs/VGGNet/VOC0712/SSD_300x300/ # and save temporary evaluation results in: # - $HOME/data/VOCdevkit/results/VOC2007/SSD_300x300/ # It should reach 77.* mAP at 120k iterations. python examples/ssd/ssd_pascal.py
# If you would like to test a model you trained, you can do: python examples/ssd/score_ssd_pascal.py
$CAFFE_ROOT/examples/videos
cd $CAFFE_ROOT # 測試示例視頻 sudo vim $CAFFE_ROOT/examples/ssd/ssd_pascal_video.py # 第99~100行修改模式爲CPU,P.Solver.GPU修改成P.Solver.CPU 99 # Use GPU or CPU 100 solver_mode = P.Solver.CPU # 第77~76行修改視頻文件路徑$CAFFE_ROOT/examples/videos 75 # The video file path 76 video_file = "examples/videos/ILSVRC2015_train_00755001.mp4"
# 攝像頭測試 sudo vim $CAFFE_ROOT/examples/ssd/ssd_pascal_webcam.py # 第100~101行修改模式爲CPU,P.Solver.GPU修改成P.Solver.CPU 101 # Use GPU or CPU 102 solver_mode = P.Solver.CPU # If you would like to attach a webcam to a model you trained, you can do: python examples/ssd/ssd_pascal_webcam.py