1. 程式人生 > >Caffe中的Protobuf版本問題

Caffe中的Protobuf版本問題

機器用的人一多,各種環境就容易被搞亂。今天突然有個妹子告訴我caffe跑崩了,可我一年前明明跑的好好的。。。

protobuf版本衝突

報錯的問題如下:
This program requires version 3.6.0 of the Protocol Buffer runtime library, but the installed version is 2.6.1. Please update your library. If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library. (Version verification failed in “google/protobuf/descriptor.pb.cc”.)

大意就是protobuf 2.6.1的版本太低了,要升級到3.6.0(沒想到一年都升級這麼快了?)。

sudo make install
sudo ldconfig
protoc --version

出來版本是3.6.0,protobuf看樣子是安裝成功了,轉去重編caffe。
到了caffe的目錄,還是按原來的cmake方式編譯,記得以前沒報過錯,但這次報錯了,看了一下資訊
error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

說是要C++11支援,於是轉到CMakeLists.txt里加一句

set(CMAKE_CXX_STANDARD 11)

上面那個問題不存在了,但是caffe這個時候編譯出來了一大堆錯誤。。。看的腦殼疼,想了一想原來編caffe的時候CMakeLists裡沒這句也編譯過了,感覺是protobuf 3.6.0這貨用了C++11什麼高階特性帶進來的問題。
感覺這樣下去要踩坑不斷了,回到最初的報錯問題上,它說我的protobuf版本低了要用3.6.0,現在3.6.0出來這麼一堆錯誤,我還是降點版本吧3.3.0。

步入正途

先把依賴裝一下

sudo apt-get install autoconf automake libtool curl make g++ unzip

下載protobuf 3.3.0的程式碼,解壓

wget https://github.com/google/protobuf/archive/v3.3.0.zip
unzip protobuf

進目錄編譯

cd protobuf-3.3.0/
./autogen.sh
./configure
make
make check

最終出來下面這個就是check通過了

===============================================================
Testsuite summary for Protocol Buffers 3.3.0
===============================================================
# TOTAL: 7
# PASS:  7
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
===============================================================

然後安裝(sudo是直接裝在機器的python下的,自己用anaconda的看準了裝在哪再install)

sudo make install
sudo ldconfig
protoc --version

出來的是libprotoc 3.3.0,就說明protobuf已經裝好了。

回到caffe目錄,注掉原來加的C++11那句開始編譯,編譯通過,ldd build/tools/caffe 看protobuf也已經成功連結上去了,感覺這波穩了,滿懷欣喜開始跑一波程式碼!
啪!臉打的啪啪響,又出現一樣的問題,但是這次可以說是精準打臉了,上面那個問題我再貼一遍:
This program requires version 3.6.0 of the Protocol Buffer runtime library, but the installed version is 3.3.0.

caffe一本正經地告訴我,你裝了3.3.0很好,但是我要的是 3.6.0。
想了一下為什麼caffe總問我要3.6.0,然後看了下pip的list,裡面裝了個protobuf 3.6.0,感覺應該是這個造成的。

sudo pip uninstall protobuf

卸掉了pip裡的 protobuf 3.6.0,然後又開始跑程式碼,這次報錯

no module named google.protobuf.internal

很明顯啊,是原來裝在pip裡的protobuf 3.6.0佔用了caffe的python介面,現在只需要進protobuf 3.3.0的python資料夾把3.3.0的python介面編譯安裝下就OK了。

cd protobuf-3.3.0/python/
python setup.py build 
python setup.py install 
python setup.py test

這下import caffe沒問題,跑程式碼也成功了,又獲得了一張好人卡。