经过一天的不懈尝试和查找资料,终于完成了在windows上的caffe编译,也是参考了好几位前辈的经验分享,现分享过程如下:
前言:最好基于caffe-windows(https://www.github.com/happynear/caffe-windows)编译,否则因为一些语言语法的标准问题,要改很多东西,最后即使编译通过了,还不一定能用。
环境:win7 64位,vs2013,boost,cuda7.5,opencv2.4.11,还有caffe依赖的一些第三方库。
步骤:
从github下载caffe-windows后,解压到caffe-windows-master文件夹
1、依赖库下载安装:
boost_1.56_0-msvc-12.0-64(http://sourceforge.net/projects/boost/files/boost-binaries/1.56.0/),安装后目录是C:\local\boost_1_56_0;
opencv2.4.11下载安装,注意针对vs2013 64位;
第三方库安装,(http://download.csdn.net/detail/lyk_ffl/9522629 http://download.csdn.net/detail/lyk_ffl/9522633 )下载后解压到caffe-windows-master根目录下的3rdparty;并将里面的bin目录添加进系统环境变量path里;
生成caffe.pb.cc和caffe.pb.h文件,下载生成批处理工具GeneratePB.bat(http://download.csdn.net/detail/lyk_ffl/9522368),放在caffe-windows-master\scripts文件夹下,直接双击运行GeneratePB.bat。
2、使用vs2013编译caffe:
使用vs2013打开caffe-windows-master\buildVS2013中的MainBuilder.sln,其中的caffelib工程即caffe的基本框架;
添加头文件目录,caffelib工程的Properties\C/C++\General\Additional Include Directories,添加 ../../3rdparty/include;../../src;../../include;C:\local\boost_1_56_0;E:\opencv2411\include;E:\opencv2411\include\opencv;E:\opencv2411\include\opencv2 ,其中boost和opencv的路径根据自己电脑安装路径设置;
设置编译选项,设置caffelib工程的Properties\C/C++\Preprocessor,删除USE_CUDNN(我没有安装cudnn);
依次编译caffelib下面的cpp文件:blob.cpp,common.cpp,data_reader.cpp......solver.cpp,syncedmem.cpp;
依次编译caffelib/layers下面所有的.cpp和.cu文件;
依次编译caffelib/util下面所有的.cpp和.cu文件;
编译caffelib/proto下面的caffe.pb.cc文件;
依次编译caffelib/solvers下面的所有.cpp和.cu文件;
最后Build caffelib工程,生成caffe-windows-master\bin\caffelib.lib文件。
3、生成caffe工具可执行文件:
可执行文件工程主要有caffe工程,compute_image_mean工程,convert_imageset工程等,其他科参考这些工程自己添加,编译方法都是一样的。
添加头文件目录,caffe工程的Properties\C/C++\General\Additional Include Directories,添加 ../../3rdparty/include;../../src;../../include;C:\local\boost_1_56_0;E:\opencv2411\include;E:\opencv2411\include\opencv;E:\opencv2411\include\opencv2 ,其中boost和opencv的路径根据自己电脑安装路径设置;
设置编译选项,设置caffe工程的Properties\C/C++\Preprocessor,删除USE_CUDNN(我编译时没有加cudnn);
添加库文件目录,caffe工程的Properties\Linker\General\Additional Library Directories,添加E:\opencv2411\x64\vc12\lib;../../3rdparty/lib;..\..\bin;C:\local\boost_1_56_0\lib64-msvc-12.0;其中boost和opencv的路径参照自己电脑的安装目录;
添加依赖库文件,caffe工程的Properties\Linker\Input\Additional Dependencies中添加 opencv_calib3d2411.lib;opencv_contrib2411.lib;opencv_core2411.lib;opencv_features2d2411.lib;opencv_flann2411.lib;opencv_gpu2411.lib;opencv_highgui2411.lib;opencv_imgproc2411.lib;opencv_legacy2411.lib;opencv_ml2411.lib;opencv_nonfree2411.lib;opencv_objdetect2411.lib;opencv_ocl2411.lib;opencv_photo2411.lib;opencv_stitching2411.lib;opencv_superres2411.lib;opencv_ts2411.lib;opencv_video2411.lib;opencv_videostab2411.lib;cudart.lib;cuda.lib;nppi.lib;cufft.lib;cublas.lib;curand.lib;gflags.lib;libglog.lib;libopenblas.dll.a;libprotobuf.lib;libprotoc.lib;leveldb.lib;lmdb.lib;libhdf5_D.lib;libhdf5_hl_D.lib;Shlwapi.lib;gflags.lib;libprotobuf.lib;leveldb.lib;lmdb.lib;libhdf5.lib;libhdf5_hl.lib;caffelib.lib;ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib;libprotobuf.lib;hdf5_tools.lib;hdf5_hl_fortran.lib;hdf5_fortran.lib;hdf5_hl_f90cstub.lib;hdf5_f90cstub.lib;hdf5_cpp.lib;hdf5_hl_cpp.lib;hdf5_hl.lib;hdf5.lib;zlib.lib;szip.lib;opencv_world300.lib;shlwapi.lib;leveldb.lib;cublas_device.lib;libglog.lib;lmdb.lib;cudnn.lib;libopenblas.dll.a;gflags.lib;我直接生成的是release版本,如果想生成debug的,有一些库文件需要改成xxd.lib(xx代表文件名)
Build caffe工程,生成caffe-windows-master\bin\caffe.exe文件。
其他工程编译设置一样。
4、测试
下载mnist测试数据集(http://download.csdn.net/detail/lyk_ffl/9522404),解压到caffe-windows-master\data文件夹下;
下载mnist册数数据模型生成和训练批处理工具create_mnist.bat和train_lenet.bat(http://download.csdn.net/detail/lyk_ffl/9522414),解压到caffe-windows-master\examples\mnist 文件夹下,;
修改该文件夹下lenet_solver.prototxt文件里的solver_mode为CPU;
运行create_mnist.bat生成文件夹mnist_test_leveldb和mnist_train_leveldb;
运行train_lenet.bat,即可看到训练精度了,最后可以达到99%以上。