VS2013配置Caffe卷积神经网络工具(64位Windows 7)——准备依赖库

来源:互联网 发布:oracle 去掉重复数据 编辑:程序博客网 时间:2024/05/25 18:12

2014年4月的时候自己在公司就将Caffe移植到Windows系统了,今年自己换了台电脑,想在家里也随便跑跑,本来也装了Ubuntu可以很方便的配置好,无奈在家的风格是“娱乐的时候抽空学习”,所以移植到Windows还是很有必要的。但是,公司禁止将公司内部资料带出,很多地方又都忘记了,周末磨了一天终于移植完,本篇为记录将Caffe移植至Windows7 x64系统下的一些关键步骤。第一步先看看这老些依赖库怎么搞。

在真正开始编译各依赖库之前,需要准备一些必备工具:

        首先当然是VS2013,下载地址:https://www.visualstudio.com/

        其次是CMake工具,下载地址:http://www.cmake.org/download/

1.Boost

下载地址:http://www.boost.org/

编译方法:

1) 运行Visual Studio Tools中的VS2013 x64 Cross Tools Command Prompt终端工具

2) 从终端进入boost库所在目录,如D:\LIBS\boost_1_57_0

3) 运行bootstrap.bat生产64位的bjam.exe

4) 输入命令进行编译,更正一下,msvc-12.0才是vs2013哈,感谢halooooJeffrey http://weibo.com/2211847364

静态库:bjam --build-type=complete toolset=msvc-9.0 toolset=msvc-12.0 threading=multi link=static address-model=64

共享库:bjam --build-type=complete toolset=msvc-9.0 toolset=msvc-12.0 threading=multi link=shared address-model=64

2.OpenCV

下载地址:http://opencv.org/downloads.html 本文中使用的是2.4.10版本

编译方法:

下载后的安装包中有已编译好的库,可直接引用,如D:\LIBS\opencv\build\x64\vc12

3.OpenBlas

下载地址:http://sourceforge.net/projects/openblas/files/

编译方法:

OpenBlas库在windows上编译起来比较复杂,这里给出的下载地址是一个已编译好的压缩包OpenBLAS-v0.2.14-Win32.zip (12.1 MB),直接提供了./bin ./include ./lib路径

4.CUDA

下载地址:https://developer.nvidia.com/cuda-downloads

编译方法:

OpenCV类似,安装好后直接有已编译好的库。如C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\includeC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\lib\x64 

以上是依赖库比较常用,最好将该它们的includelib加入到Windows的环境变量中,方便后续的库的编译

5.GFlags

下载地址:https://github.com/gflags/gflags

编译方法:

1) 启动CMake(cmake-gui)工具

2) 设置source code路径和build路径


3) 单击Configure按钮,并选择并选择Visual Studio 12 2013 Win64编译器编译器


4) 更新完成后,勾选中BUILD_SHARED_LIBSBUILD_STATIC_LIBS


5) 单击Generate按钮,生成VS工程 


6) 打开刚刚生成的VS工程,build其中的ALL_BUILD工程,注意选择x64模式,并分别生成DebugRelease下的库 


7) 编译成功后,在工程路径下会生成binincludelib三个文件夹 


6.GLog

下载地址:https://github.com/google/glog

编译方法:

该工程中包含VS工程google-glog.sln,打开直接编译即可,同样注意Solution Platform选择x64模式,并分别生成DebugRelease下的库

7.LevelDB

下载地址:https://github.com/bureau14/leveldb 

这里没有选择官方的https://github.com/google/leveldb是由于官方版本移除了CMake的依赖项,自己写起来比较复杂

编译方法:

与编译GFlags方法类似,唯一需要注意的地方是将CMakeLists.txt中第82行的-DSNAPPY注释掉,否则需要依赖Snappy库,其实并不绝对需要,为了简单起见将此库去掉。另外Leveldb依赖于boost库,如果没有将boost库添加至环境变量,可能需要手动进行设置。

8.LMDB

下载地址:https://gitorious.org/mdb/mdb/archive/462dc097451834477b597447af69c5acc93182b7.tar.gz

编译方法:

1) 解压压缩包到某路径,例如D:\CODE\CXX\mdb-mdb

2) VS2013中新建工程,FILE --> New --> Project From Existing Code.. 


3) 选取源码所在路径,并给工程起名 


4) 单击next按钮后选择Project typeConsole application project 


5) Solution Platform修改为x64模式

6) 注意将工程的输出改为静态库,右键单击工程 --> property --> Configuration Properties --> General --> Project Default --> Configureation Type --> Static library (.lib)

7) 其中一个.c文件中包含了unistd.h,为了解决这个问题需要准备三个文件 unistd.hgetopt.hgetopt.cunistd.h可以http://stackoverflow.com/questions/341817/is-there-a-replacement-for-unistd-h-for-windows-visual-c解决。另外两个可以从http://ieng6.ucsd.edu/~cs12x/vc08install/getopt9.zip下载

8) 最后编译即可

9.ProtoBuf

下载地址:https://github.com/google/protobuf

编译方法:

压缩包里有一个叫vsprojects的文件夹,其中有现成的VS工程,可以用来直接编译,也需要注意将Solution Platform修改为x64模式

10.HDF5

下载地址:http://www.hdfgroup.org/ftp/HDF5/current/src/CMake/hdf518-CMakeWindows.zip

编译方法:

解压后,在VS2013 x64 Cross Tools Command Prompt终端工具中运行build-VS2013-64.bat即可。

 

整理头文件和库文件

        5-10的头文件和编译后的库统一整理到一个3rdparty文件夹下,其中包含两个文件夹includelib


        include文件夹下包含gflags、glog、google、hdf5、leveldb、lmdb六个文件。gflags的头文件来自于生成的VS工程目录中的include文件夹;glog的头文件来自于VS工程目录中的src\windows文件夹;google中是protobuf的头文件,来自于压缩包中的src\google文件夹;hdf5来自于压缩包中的CMake\hdf5-1.8.14\src文件夹,保留.h文件即可;leveldb的头文件来自于压缩包的include文件夹;lmdb的头文件来自于压缩包中的libraries\liblmdb文件夹,保留.h文件即可

       lib文件夹中的.lib文件直接从编译好的工程目录下拷贝即可,注意debug模式下的.lib文件的文件名修改为xxxd.lib形式

       至此,caffe需要的各项依赖库已经准备完毕,后续会上一些图,看起来直观一些。

 

VS2013配置Caffe卷积神经网络工具(64位Windows 7)——建立工程

将Caffe移植到Windows系统了,今年自己换了台电脑,想在家里也随便跑跑,本来也装了Ubuntu可以很方便的配置好,无奈在家的风格是“娱乐的时候抽空学习”,所以移植到Windows还是很有必要的。但是,公司禁止将公司内部资料带出,很多地方又都忘记了,周末磨了一天终于移植完,本篇为记录将Caffe移植至Windows7 x64系统下的一些关键步骤。下面介绍如何从源码建立VS2013工程。

1. https://github.com/BVLC/caffe下载caffe源码,master或者dev版本都可以,本文中以dev为例

2. 新建一个空的控制台VS2013工程,起名为caffe-dev

File --> New --> Project

选择Win32 Console Application

选择工程路径,比如D:\CODE\CXX

给项目命名caffe-dev

单击OK后选择Empty project 

3. 将工程的Solution Platform改为x64

Build --> Configuration Manager --> Active solution platform --> new --> x64 --> OK 

4. 设置CUDA编译

Project --> Build Customizations... --> CUDA 7.0 --> OK 

5. 设置一些工程下的路径,注意DebugRelease下都要设置

Project --> Properties --> Configuration Properties --> VC++ Directories --> Include Directories中添加openblasopencvboostcuda,例如

D:\LIBS\boost_1_57_0;D:\LIBS\OpenBLAS\include

D:\LIBS\opencv\build\include

D:\LIBS\opencv\build\include\opencv

D:\LIBS\opencv\build\include\opencv2

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\include

Project --> Properties --> Configuration Properties --> VC++ Directories --> Library Directories中添加openblasopencvboostcuda,例如

D:\LIBS\OpenBLAS\lib

D:\LIBS\opencv\build\x64\vc12\lib

D:\LIBS\boost_1_57_0\lib

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\lib\x64

6. 将压缩包内的includesrctools文件夹拷贝到工程根目录下,同时把整理好的3rdparty文件夹也拷贝到工程目录下 


7. 添加caffe的路径和3rdparty中第三方依赖库的路径,同样DebugRelease都要设置

Project --> Properties --> Configuration Properties --> C/C++ --> General --> Additional Include Directories中添加

../include;../src

../3rdparty/include

../3rdparty/include/hdf5

../3rdparty/include/lmdb

Project --> Properties --> Configuration Properties --> Linker --> General --> Additional Library Directories中添加

../3rdparty/lib

Project --> Properties --> Configuration Properties --> Linker --> Input --> Additional Dependencies中添加

opencv_core2410.lib

opencv_highgui2410.lib

opencv_imgproc2410.lib

cudart.lib

cublas.lib

curand.lib

gflags.lib

libglog.lib

libopenblas.dll.a

libprotobuf.lib

libprotoc.lib

leveldb.lib

lmdb.lib

libhdf5.lib

libhdf5_hl.lib

Shlwapi.lib

注意在Debug下应添加xxxd.lib形式的静态库

opencv_core2410d.lib

opencv_highgui2410d.lib

opencv_imgproc2410d.lib

cudart.lib

cublas.lib

curand.lib

gflagsd.lib

libglogd.lib

libopenblas.dll.a

libprotobufd.lib

libprotoc.lib

leveldbd.lib

lmdbd.lib

libhdf5.lib

libhdf5_hl.lib

Shlwapi.lib

8. 利用protoc.execaffe.proto生成caffe.pb.hcaffe.pb.cc文件

在编译protobuf的时候会生成一个可执行文件protoc.exe,在工程的D:\CODE\CXX\caffe-dev\src\caffe\proto路径下有一个文件叫caffe.proto,可以将protoc.exe拷贝到该路径下,在命令行中进入D:\CODE\CXX\caffe-dev\src\caffe\proto路径,输入命令

[cpp] view plain copy
  1. protoc.exe caffe.proto --cpp_output=./  

可以看到在该路径下会生成caffe.pb.hcaffe.pb.ccD:\CODE\CXX\caffe\include\caffe中新建一个文件夹proto,将生成的两个文件移动到D:\CODE\CXX\caffe\include\caffe\proto

9. 准备mkstemp.hmkstemp.cpp文件。

mkstemp.h放在include/caffe/util/路径下,mkstemp.h中的内容为

[cpp] view plain copy
  1. #include <windows.h>  
  2. #include <io.h>  
  3. int mkstemp(char *templates);  

mkstemp.cpp放在src/caffe/util/路径下,mkstemp.cpp中的内容为

[cpp] view plain copy
  1. /* mkstemp extracted from libc/sysdeps/posix/tempname.c.  Copyright 
  2.    (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc. 
  3.  
  4.    The GNU C Library is free software; you can redistribute it and/or 
  5.    modify it under the terms of the GNU Lesser General Public 
  6.    License as published by the Free Software Foundation; either 
  7.    version 2.1 of the License, or (at your option) any later version.  */  
  8.   
  9. #include "caffe/util/mkstemp.hpp"  
  10. #include <errno.h>  
  11. #include <fcntl.h>  
  12. #include <sys/stat.h>  
  13.   
  14. static const char letters[] =  
  15. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  16.   
  17. /* Generate a temporary file name based on TMPL.  TMPL must match the 
  18.    rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed 
  19.    does not exist at the time of the call to mkstemp.  TMPL is 
  20.    overwritten with the result.  */  
  21. int mkstemp (char *tmpl)  
  22. {  
  23.   int len;  
  24.   char *XXXXXX;  
  25.   static unsigned long long value;  
  26.   unsigned long long random_time_bits;  
  27.   unsigned int count;  
  28.   int fd = -1;  
  29.   int save_errno = errno;  
  30.   
  31.   /* A lower bound on the number of temporary files to attempt to 
  32.      generate.  The maximum total number of temporary file names that 
  33.      can exist for a given template is 62**6.  It should never be 
  34.      necessary to try all these combinations.  Instead if a reasonable 
  35.      number of names is tried (we define reasonable as 62**3) fail to 
  36.      give the system administrator the chance to remove the problems.  */  
  37. #define ATTEMPTS_MIN (62 * 62 * 62)  
  38.   
  39.   /* The number of times to attempt to generate a temporary file.  To 
  40.      conform to POSIX, this must be no smaller than TMP_MAX.  */  
  41. #if ATTEMPTS_MIN < TMP_MAX  
  42.   unsigned int attempts = TMP_MAX;  
  43. #else  
  44.   unsigned int attempts = ATTEMPTS_MIN;  
  45. #endif  
  46.   
  47.   len = strlen (tmpl);  
  48.   if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))  
  49.     {  
  50.       errno = EINVAL;  
  51.       return -1;  
  52.     }  
  53.   
  54. /* This is where the Xs start.  */  
  55.   XXXXXX = &tmpl[len - 6];  
  56.   
  57.   /* Get some more or less random data.  */  
  58.   {  
  59.     SYSTEMTIME      stNow;  
  60.     FILETIME ftNow;  
  61.   
  62.     // get system time  
  63.     GetSystemTime(&stNow);  
  64.     stNow.wMilliseconds = 500;  
  65.     if (!SystemTimeToFileTime(&stNow, &ftNow))  
  66.     {  
  67.         errno = -1;  
  68.         return -1;  
  69.     }  
  70.   
  71.     random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32)  
  72.                         | (unsigned long long)ftNow.dwLowDateTime);  
  73.   }  
  74.   value += random_time_bits ^ (unsigned long long)GetCurrentThreadId ();  
  75.   
  76.   for (count = 0; count < attempts; value += 7777, ++count)  
  77.     {  
  78.       unsigned long long v = value;  
  79.   
  80.       /* Fill in the random bits.  */  
  81.       XXXXXX[0] = letters[v % 62];  
  82.       v /= 62;  
  83.       XXXXXX[1] = letters[v % 62];  
  84.       v /= 62;  
  85.       XXXXXX[2] = letters[v % 62];  
  86.       v /= 62;  
  87.       XXXXXX[3] = letters[v % 62];  
  88.       v /= 62;  
  89.       XXXXXX[4] = letters[v % 62];  
  90.       v /= 62;  
  91.       XXXXXX[5] = letters[v % 62];  
  92.   
  93.       fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);  
  94.       if (fd >= 0)  
  95.     {  
  96.       errno = save_errno;  
  97.       return fd;  
  98.     }  
  99.       else if (errno != EEXIST)  
  100.     return -1;  
  101.     }  
  102.   
  103.   /* We got out of the loop because we ran out of combinations to try.  */  
  104.   errno = EEXIST;  
  105.   return -1;  
  106. }  

10. caffe的源文件添加到工程中

D:\CODE\CXX\caffe-dev\src\caffe路径下除了test文件中的所有源文件添加到工程中,如果想要跑一些测试代码,也可以把test中的源文件加进去

D:\CODE\CXX\caffe-dev\include\caffe\proto下的caffe.pb.cc添加到工程中

D:\CODE\CXX\caffe-dev\tools下的caffe.cpp添加到工程中 

11. 修改一些代码

include/caffe/common.hpp中添加#include <direct.h>

include/caffe/util/io.hpp中添加#include "mkstemp.hpp",并将close函数替换为_close,将char* mkdtemp_result = mkdtemp(temp_dirname_cstr);替换为errno_t mkdtemp_result = _mktemp_s(temp_dirname_cstr, sizeof(temp_dirname_cstr)); 

bnll_layer.cu中的const float kBNLL_THRESHOLD = 50.;修改为#define kBNLL_THRESHOLD 50.

util/db.cpp中的mkdir(source.c_str(), 0744)修改为_mkdir(source.c_str())

util/io.cpp中的open函数替换为_openclose函数替换为_close

util/io.cppReadProtoFromBinaryFile函数中的int fd = open(filename, O_RDONLY);替换为int fd = open(filename, O_RDONLY | O_BINARY);

util/math_functions.cpp中添加

[cpp] view plain copy
  1. #define __builtin_popcount __popcnt   
  2. #define __builtin_popcountl __popcnt  
common.cpp中添加#include <process.h>

pid = getpid();替换为pid = _getpid(); 这里本来有个笔误,谢谢dongb5lz的提醒

注释掉::google::InstallFailureSignalHandler();

solver.cpp中的snprintf函数替换为sprintf_s


【caffe-Windows】caffe+VS2013+Windows快速配置教程首先来一波地址: happynear大神的第三方caffe:http://blog.csdn.net/happynear/art

    

【caffe-Windows】caffe+VS2013+Windows高速配置教


首先来一波地址:


happynear大神的第三方caffe:


Neil Z大神的第三方caffe:https://initialneil.wordpress.com/2015/01/11/build-caffe-in-windows-with-visual-studio-2013-cuda-6-5-opencv-2-4-9/


caffe提供Windows工具包(caffe-windows):https://github.com/BVLC/caffe/tree/windows   百度云下载地址:链接: 密码:phf3


微软提供Windows工具包(caffe-master):https://github.com/Microsoft/caffe    百度云下载地址:链接: 密码:ith0





我使用的是微软官方提供的链接,基本上与caffe官网上提供安装包差不多,具体区别没有细看,这个caffe无需配置第三方库的环境变量,直接就可以自动下载所需要的库,包含opencv和boost等。


【注】VS2012的也有人写过,自行参考。此版严格只支持VS2013。此版本无需配置任何第三方环境哦,因为在项目属性里面都已经包含进去了,非常方便。如果出现了一些文件打不开或者其它问题,第一可能是VS自身问题,第二可能你配置过其它caffe,修改过环境变量,导致找不到头文件之类的。


第一步


下载caffe官网提供的工具包,复制Windows下CommonSettings.props.example,后缀改为CommonSettings.props,如下:





第二步


由于我电脑无GPU,所以修改复制过来的CommonSettings.props配置文件(这里还可以根据需要设置MATLAB或者Python的借口,不过我打算学习C++下的caffe编写,所以没有配置这两种接口):








第三步


双击Windows下的caffe.sln,在VS2013中生成解决方案:


【注】里面的caffe.managed和predict 是我自己写的测试项目,其它的才是官网自动生成的哈~~~ :->





然后会弹出一个窗口,Nuget所需要的第三方库,可能会未响应,慢慢等吧。如果出现无法连接的那个问题,说明IP ping不过去,直接把问题复制到网上搜,有解决方法。


第四步


当下载完毕第三方库,你的caffe-Windows并列文件夹下会有一个第三方库的文件夹


【注】caffe-windows和caffe-master对caffe.cpp编译以后下载的第三方库都是十六个文件夹。








最好核对一下文件,免得没下载完就被你关掉了。


这个时候,当下载完毕,VS仍在继续执行生成任务,最终可能失败,原因下面会提示说无法打开libcaffe.lib之类的error。解决方法是直接对libcaffe重新生成,或者直接生成一次





当这个文件生成成功以后,重复一下上一步,对所有文件再重新生成一下,这时候应该不会出现libcaffe.lib无法打开的错误了。


【PS】如果出现windows.h之类的错误,请自行解决,初步断定是VS编译器的环境变量没有配置好,或者版本安装出现过问题,建议重装系统,安装官方版VS2013


第五步


设置一下运行项,解决方案'caffe'->属性





或者下面直接单启动项目是caffe也行,不过最好改一下,原因相信玩过VS的都清楚,如果是单启动项目,不管你运行什么,他都会只运行这个单启动项目设置的源文件


第六步


运行caffe.cpp,直接双击打开caffe.cpp,然后ctrl+f5直接编译,出现如下命令窗口说明编译成功





【PS】这个其实告诉你了如何调用caffe.exe去训练你的模型


在下一篇博客中会介绍cifar的训练实例。

2楼hh1064284609前天 23:34你好,请问NugetPackages第三方库应该放在caffe-Windows下吗,我发现caffe.cpp找不到里面的头文件。而且楼主用的应该是微软提供的工具包而不是caffe官网的,里面少了2个文件夹Re:zb1165048017前天 09:10回复hh1064284609n谢谢指正,我刚才看了一下,的确是微软提供的。不过我电脑上也安装过caffe官方提供的版本链接,生成NugetPackages里面也是总共有十六个文件夹,你的caffe.cpp找不到哪个头文件,要不行就手动添加一下库目录或者附加包含目录1楼Gargan3天前 13:40谢谢!Re: zb1165048017前天 20:06回复Gargann共同学习哈,嘿嘿


0
0 0