前言
caffe 的配置之路真的是步履维艰,每前进一部都要付出惨痛的代价,会遇到各种问题。并且caffe 的配置并不是一劳永逸的,每次跑带代码的时候都需要重新配置,每次都会遇到问题,真的是不堪其苦,所以请关爱单身狗,关爱程序员,更要关爱单身的程序员。相信有很多小伙伴会遇到和我一样的问题,我将遇到过的问题的解决方法记录下来,为不辞辛苦前赴后继的程序员们,铺上一块砖,填上一些坑。
环境
不谈环境的解决方法都是耍流氓。所以,正派的我还是先说一下我电脑的基本环境。操作系统是Ubuntu14.4, python使用的是anaconda3。使用anaconda的好处是可以建立多个独立的不同版本不同作用的虚拟环境,例如我可以为tensorflow建立两个独立的环境tfpy27和tfpy36, 为caffe使用python建立连个独立的环境,caffepy27和caffe36。不让他们共享环境,可以避免很多问题,因为有时候tensorflow和caffe依赖的包的版本会不同,很是头疼。所以建立多个环境,根据实际情况使用 source activate 环境名
选择不同的环境,很是方便。至于怎样在anaconda中创建环境,资料很多,大家可以自行百度。
第一坑:google.protobuf相关的问题
我在配置caffe的时候遇到的最多的,最棘手的就是google.protobuf的问题,真的是处处都有她的婀娜多姿身影。
第一砖:#error This file was generated by an older version of protoc which is
这个问题可能还有另一种形式 :#error This file was generated by an newer version of protoc which is
。通过older和newer我们可以判断,这是protobuf包的版本不匹配的问题。通过google发现这篇文章,解决了我的问题。在你为在百度找不到答案而的绝望时候,别忘了去google,相信你一定会体会到什么叫山重水复疑无路,柳暗花明又一村的。
具体的解决方法是,首先是使用protoc --version
查看你的系统中libprotobuf-dev的版本,我的输出 protobuf 2.6.1
。然后使用source activate 环境名
激活你caffe中Makefile.config文件中配置的python环境,我的是source activate caffepy27
。然后使用 protoc –version 查看你的python环境中的libprotobuf-dev的版本。我的输出是protobuf 3.5.1
。很显然,编译caffe的时候找到了两个版本的protobuf。我的建议是无论两个版本的protobuf是不是相同版本,都卸载掉。卸载的命令是:
conda uninstall libprotobuf
conda uninstall protobuf
注意,一定要libprotobuf和protobuf都卸载掉。
编译成功后,在运行代码的时候python可能会提示找不到protobuf模块,这时候我们再使用
将protobuf模块安装上就可以了。以后再编译caffe的时候如果冲突,再卸,再装。。。一把老泪…
conda install protobuf
* 总之,出现该问题的解决方法是,卸载python中冲突的protobuf和libprotobuf。*
第二砖:.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::Message::DebugString() const’
完整的问题很长,大概是这样的:
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::Message::DebugString() const'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::OnShutdownDestroyString(std::string const*)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(int, std::string const&, google::protobuf::io::CodedOutputStream*)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::MessageLite::ParseFromString(std::string const&)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::NameOfEnum(google::protobuf::EnumDescriptor const*, int)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::WireFormatLite::WriteString(int, std::string const&, google::protobuf::io::CodedOutputStream*)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::WireFormatLite::ReadBytes(google::protobuf::io::CodedInputStream*, std::string*)'
.build_release/lib/libcaffe.so: undefined reference to `google::protobuf::Message::InitializationErrorString() const'
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/extract_features.bin] Error 1
其实导致这个问题的因素有很多,但是本质也是protubuf版本或多个版本冲突的问题。首先参照第一砖的解决犯法进行解决。我是先遇到第一砖中的问题的,但是我在解决的时候只卸载了protobuf,没有卸载libprotobuf,就出现了这个问题,当我把libprotobuf也卸载掉,问题就消失了。
更新中。。。