在做科研的过程中会遇到各种深度学习框架,包括tensorlow,theano,pytorch,mxnet,caffe等等,其中caffe算是最难装的那一个,但是绕不开的一个,特别是在学术领域,今天就分享一下安装cpu搬caffe的过程(本人GTX720,故使用CPU版本)
官方推荐的编译工具是vs2013,当然vs2015可能也是可以的哦!
网址为 https://github.com/Microsoft/caffe (注意最好下载微软这个源,可以帮我们少些步骤)
进入到项目的windows目录下,将CommonSettings.props.example
文件复制一份,并改名为CommonSettings.props
,修改里面的useCpuOnly为True,useCudnn为false,matlab和python也先设为false,编译完成这一步之后再编译这两个接口。
windows目录下现在是一个vs项目,用vs2013打开vs项目,首先选择libcaffe为启动项目,并右键设置改项目将警告视为错位选项是“否”。右键开始编译项目,操作没问题的化这一步会编译成功。
选中解决方案,右键,启动项目为整个解决方案,然后生成解决方案,几分钟之后就完成了,再进入caffe-master\Build\x64\Debug目录,可以阿奎那到编译完成的caffe.exe和classification.exe。
一定要注意2点:
1)修改配置文件,设置matlab支持未true,(使用微软的源的一定要将matcaffe项目下
caffe_.cpp文件16行中的 #include "gpu\mxGPUArray.h"
改为 "#include
,同时,还要在设置文件的matlab设置处的includepath中加入$(MatlabDir) ,切记切记!!!
2) 编译matcafe需要用到三个项目,caffe, libcaffe和matcaffe,分别在这三个项目上右键“将警告视为错误”开关,选择否,这一点也很重要 ,切记切记!!
然后你就可以编译了,不论是release还是debug都可以,选择matcaffe为启动项目即可。
编译完成之后会生成matcaffe文件夹,将其加入matlab的工具箱路径即可。
如果提示如下错误,请将Release(你的可能是Debug)加入你电脑的的环境变量中
我安装的是anacoda2,所以我用anacoda演示
3.1 和前面一样,首先设置Pycaffe,matcaffe和caffe三个项目的“将警告视为错误”选择否。
3.2 修改anancoda的include中pyconfig.h文件部分如下
/* For an MSVC DLL, we can nominate the .lib files used by extensions */
#ifdef MS_COREDLL
# ifndef Py_BUILD_CORE /* not building the core - must be an ext */
# if defined(_MSC_VER)
/* So MSVC users need not specify the .lib file in
their Makefile (other compilers are generally
taken care of by distutils.) */
# ifdef _DEBUG
# pragma comment(lib,"python27.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */
# endif /* _MSC_VER */
# endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */
...
#ifdef _DEBUG
//# define Py_DEBUG
#endif
直接编译pycaffe,完成之后会生成pycaffe文件夹,将caffe文件夹拷贝到anacoda的Lib/site-pachages文件夹下,打开python测试
λ python
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
Traceback (most recent call last):
File "" , line 1, in
File "D:\Program Files\Anaconda2\lib\site-packages\caffe\__init__.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
File "D:\Program Files\Anaconda2\lib\site-packages\caffe\pycaffe.py", line 15, in
import caffe.io
File "D:\Program Files\Anaconda2\lib\site-packages\caffe\io.py", line 8, in
from caffe.proto import caffe_pb2
File "D:\Program Files\Anaconda2\lib\site-packages\caffe\proto\caffe_pb2.py", line 6, in
from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal
出现如上错误是因为缺少protobuf包,安装protobuf即可
λ pip install protobuf
Collecting protobuf
Downloading protobuf-3.5.1-py2.py3-none-any.whl (388kB)
100% |████████████████████████████████| 389kB 344kB/s
Requirement already satisfied: six>=1.9 in d:\program files\anaconda2\lib\site-packages (from protobuf)
Requirement already satisfied: setuptools in d:\program files\anaconda2\lib\site-packages\setuptools-27.2.0-py2.7.egg (from protobuf)
Installing collected packages: protobuf
Successfully installed protobuf-3.5.1
λ python
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
>>>
Python2和Python3的接口编译有一点区别,因为编译python接口的时候会寻找Python27.lib文件,我承认这的确是个很大的坑。
办法也很简单,在Pycaffe项目上右键,加入Python2的库文件即可,我的路径在anacoda2/libs下,如下图:
参考:
http://blog.csdn.net/anshiquanshu/article/details/53748023?locationNum=16&fps=1
http://blog.csdn.net/u013498583/article/details/72778923