部分借鑑http://blog.csdn.net/u011635764/article/details/52831167
根據之前發的文章,之前在TX1上已經安裝好caffe了
基本環境是,Ubuntu16.04系統,cuda8.0, cudnn, opencv3.1.0等caffe必需的依賴
首先安裝pip用於安裝之後的Python依賴
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip
安裝Python依賴包
sudo pip install Cython
sudo pip install easydict
從github上下載程序源碼
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
修改並編譯Cython,打開py-faster-rcnn/lib找到setup.py
根據自己的顯卡計算能力修改第135行,查詢網址爲:https://developer.nvidia.com/cuda-gpus
TX1計算能力爲5.3,則修改爲
'nvcc': ['-arch=sm_53',
之後執行make進行編譯
之後打開caffe-faster-rcnn將安裝好的的caffe目錄下的Makefile.config和Makefile.config.example複製到該目錄下
然後對Makefile.config文件進行修改
USE_CUDNN := 1
OPENCV_VERSION := 3
CUSTOM_CXX := g++
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/lib/x86_64-Linux-gnu/hdf5/serial /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
USE_PKG_CONFIG := 1
以上是在網上查閱各種資料彙總而得,可能有多餘的修改,但是能順利跑通
因爲作者的caffe版本不支持新的cudnn加速,我們需要將
py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp
py-faster-rcnn/caffe-fast-rcnn/src/caffe/util/cudnn.cpp
py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers/ 目錄下16個cudnn_開頭的文件
py-faster-rcnn/caffe-fast-rcnn/include/caffe/layers/目錄下8個cudnn_開頭的文件
用已安裝好的caffe相應目錄下的對應文件進行替換
修改一個include錯誤,打開
py-faster-rcnn/caffe-fast-rcnn/src/caffe/test/test_smooth_L1_loss_layer.cpp
刪除第十一行
#include "caffe/vision_layers.hpp"
如果直接通過網上很多教程說的方法直接用make編譯,會報錯如下
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
/usr/bin/ld: cannot find -lhdf5_hl
/usr/bin/ld: cannot find -lhdf5
collect2: error: ld returned 1 exit status
Makefile:554: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1
此處必須通過cmake進行編譯
在caffe-fast-rcnn路徑下,在終端執行
make clean
mkdir build
cd build
cmake ..
make all -j4
就能通過編譯,原因貌似是make只能在當前目錄下尋找動態鏈接庫,具體原因還希望有大神進行解答之後在caffe-fast-rcnn路徑下編譯
sudo make -j8 test
立刻完成
之後在caffe-fast-rcnn路徑下執行
make runtest
發現問題
F0511 14:58:00.916214 7960 db_lmdb.hpp:14] Check failed: mdb_status == 0 (12 vs. 0) Cannot allocate memory
*** Check failure stack trace: ***
@ 0x7f8401e718 google::LogMessage::Fail()
@ 0x7f84020614 google::LogMessage::SendToLog()
@ 0x7f8401e290 google::LogMessage::Flush()
@ 0x7f84020eb4 google::LogMessageFatal::~LogMessageFatal()
@ 0x7f843b37ec caffe::db::LMDB::Open()
@ 0x93b264 caffe::DBTest<>::SetUp()
@ 0xcb8f04 testing::internal::HandleExceptionsInMethodIfSupported<>()
@ 0xcb1dd8 testing::Test::Run()
@ 0xcb1f98 testing::TestInfo::Run()
@ 0xcb20a4 testing::TestCase::Run()
@ 0xcb3b08 testing::internal::UnitTestImpl::RunAllTests()
@ 0xcb3e1c testing::UnitTest::Run()
@ 0x84f570 main
@ 0x7f7fc008a0 __libc_start_main
Aborted
src/caffe/test/CMakeFiles/runtest.dir/build.make:57: recipe for target 'src/caffe/test/CMakeFiles/runtest' failed
make[3]: *** [src/caffe/test/CMakeFiles/runtest] Error 134
CMakeFiles/Makefile2:328: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/all' failed
make[2]: *** [src/caffe/test/CMakeFiles/runtest.dir/all] Error 2
CMakeFiles/Makefile2:335: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/rule' failed
make[1]: *** [src/caffe/test/CMakeFiles/runtest.dir/rule] Error 2
Makefile:240: recipe for target 'runtest' failed
make: *** [runtest] Error 2
此處是因爲分配內存不夠,解決方法爲:
cd examples/mnist
將convert_mnist_data.cpp中
CHECK_EQ(mdb_env_set_mapsize(mdb_env, 1099511627776), MDB_SUCCESS) // 1TB
修改爲
CHECK_EQ(mdb_env_set_mapsize(mdb_env, 1073741824), MDB_SUCCESS) // 1GB
同樣的,在/src/caffe/util/db_lmdb.cpp中將LMDB_MAP_SIZE修改爲4294967296
之後在caffe-fast-rcnn路徑下執行
sudo make pycaffe
就可以完成編譯
之後下載已經訓練好的模型,網上大多數方法爲
./data/scripts/fetch_faster_rcnn_models.sh
我發現網站無響應,大家可以直接用瀏覽器打開https://dl.dropboxusercontent.com/s/o6ii098bu51d139/faster_rcnn_models.tgz進行下載
下載完成後,解壓文件夾至py-faster-rcnn\data目錄下
最後,執行demo程序
python /tools/demo.py
發現報錯
ImportError: No module named skimage.io
解決方法爲,執行
sudo apt-get install python-skimage
再次執行demo又出現問題
ImportError: No module named google.protobuf.internal
解決方法爲,執行
sudo pip install protobuf
呼,這樣就可以執行demo了,希望這篇文章能幫助到大家但是,執行demo時又出現了問題,由於這是在嵌入式環境下進行,默認載入的模型是VGG16網絡,VGG16網絡比較大,可能會出現內存不足,導致killed
Loaded network /mnt/sd1/faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/000456.jpg
Detection took 1.879s for 300 object proposals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/000542.jpg
Killed
解決方法是打開tools/demo.py文件將
parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
choices=NETS.keys(), default='vgg16')
修改爲
parser.add_argument('--net', dest='demo_net', help='Network to use [zf]',
choices=NETS.keys(), default='zf')
即可跑通demo
Loaded network /mnt/sd1/faster_rcnn_models/ZF_faster_rcnn_final.caffemodel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/000456.jpg
Detection took 0.510s for 300 object proposals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/000542.jpg
Detection took 0.552s for 135 object proposals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/001150.jpg
Detection took 1.014s for 231 object proposals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/001763.jpg
Detection took 0.714s for 200 object proposals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo for data/demo/004545.jpg
Detection took 0.577s for 300 object proposals
上文可以看到,faster-Rcnn在TX1運行比較快,但跟PC端還是有相當大差距的,得到的結果是