VS2013(Win10X64)-配置编译Caffe
???????? 主要看這篇文章,有點小瑕疵,瑕不掩瑜。參考鏈接:http://www.bubuko.com/infodetail-902302.html
???????? 文中紅色標記為文章小瑕疵的地方,在此文中標記出來,做為修改對上一文的補充。
???????? 參考原文鏈接:http://blog.csdn.net/joshua_1988/article/details/45048871
???????? 2014年4月的時候自己在公司就將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編譯
property--> Build dependencies -->?Build?Customizations...?-->?CUDA 6.5 -->?OK
5.? 設置一些工程下的路徑,注意Debug和Release下都要設置
在Project?-->?Properties?-->?Configuration?Properties?-->?VC++?Directories?-->?Include?Directories中添加openblas、opencv、boost和cuda,例如
C:\Tools\boost_1_57\include;
C:\Tools\OpenBLAS_MinGW\include;
C:\Tools\opencv\build\include;
C:\Tools\opencv\build\include\opencv;
C:\Tools\opencv\build\include\opencv2;
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include;
在Project?-->?Properties?-->?Configuration?Properties?-->?VC++?Directories?-->?Library?Directories中添加openblas、opencv、boost和cuda,例如
C:\Tools\OpenBLAS_MinGW\lib;
C:\Tools\opencv\build\x64\vc12\lib;
C:\Tools\boost_1_56\lib64-msvc-12.0;
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\lib\x64;
6.?將壓縮包內的include、src和tools文件夾拷貝到工程根目錄下,同時把整理好的3rdparty文件夾也拷貝到工程目錄下?
??????????????????????????
7.? 添加caffe的路徑和3rdparty中第三方依賴庫的路徑,同樣Debug和Release都要設置
在Project?-->?Properties?-->?Configuration?Properties?-->?C/C++?-->?General?-->?Additional?Include?Directories中添加
../include;../src
../3rdparty/include
../3rdparty/include/hdf5
../3rdparty/include/lmdb
??????????? C:\Tools\Caffe-3rd\include;
在Project?-->?Properties?-->?Configuration?Properties?-->?Linker?-->?General?-->?Additional?Library?Directories中添加
../3rdparty/lib
C:\Tools\Caffe-3rd\lib\LevelDB;
C:\Tools\Caffe-3rd\lib\lmdb;
C:\Tools\Caffe-3rd\lib\openblas;
C:\Tools\Caffe-3rd\lib\protobuf;
C:\Tools\Caffe-3rd\lib\snappy;
在Project?-->?Properties?-->?Configuration?Properties?-->?Linker?-->?Input?-->?Additional?Dependencies中添加
opencv_core2410.lib
opencv_highgui2410.lib
opencv_imgproc2410.lib
opencv_ts300.lib
opencv_world300.lib
cudart.lib
cublas.lib
curand.lib
gflags.lib
libglog.lib
libopenblas.dll.a
libopenblas.lib
libprotobuf.lib
libprotoc.lib
leveldb.lib
lmdb.lib
libhdf5.lib
libhdf5_hl.lib
Shlwapi.lib
注意在Debug下應添加xxxd.lib形式的自己確認一下debug命名 靜態庫
opencv_core2410d.lib
opencv_highgui2410d.lib
opencv_imgproc2410d.lib
opencv_ts300d.lib
opencv_world300d.libcudart.lib
cublas.lib
curand.lib
gflagsd.lib
libglogd.lib
libopenblas.dll.a
libopenblas.lib
libprotobuf-gd.lib
libprotoc.lib
leveldbd.lib
lmdbd.lib
libhdf5.lib
libhdf5_hl.lib
Shlwapi.lib
8.?利用protoc.exe和caffe.proto生成caffe.pb.h和caffe.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路徑,輸入命令
編譯blob.cpp
直接編譯時會報錯,缺少文件”caffe\proto\caffe.pb.h”
這個時候需要將proto.exe放到../3rdparty/bin文件夾
將 GernaratePB.bat 放在../scripts文件夾,點擊運行,可以生成文件
可以看到在該路徑下會生成caffe.pb.h和caffe.pb.cc,在D:\CODE\CXX\caffe\include\caffe中新建一個文件夾proto,
將生成的兩個文件移動到D:\CODE\CXX\caffe\include\caffe\proto中
<del>cd C:\Tools\caffe-master\src\caffe\proto protoc --cpp_out="." caffe.proto</del>9.?準備mkstemp.h和mkstemp.cpp文件。
將mkstemp.h放在include/caffe/util/路徑下,mkstemp.h中的內容為
#include <windows.h> #include <io.h> int mkstemp(char *templates);將mkstemp.cpp放在src/caffe/util/路徑下,mkstemp.cpp中的內容為
/* mkstemp extracted from libc/sysdeps/posix/tempname.c. Copyright (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. */ #include "caffe/util/mkstemp.hpp" #include <errno.h> #include <fcntl.h> #include <sys/stat.h> static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; /* Generate a temporary file name based on TMPL. TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed does not exist at the time of the call to mkstemp. TMPL is overwritten with the result. */ int mkstemp (char *tmpl) { int len; char *XXXXXX; static unsigned long long value; unsigned long long random_time_bits; unsigned int count; int fd = -1; int save_errno = errno; /* A lower bound on the number of temporary files to attempt to generate. The maximum total number of temporary file names that can exist for a given template is 62**6. It should never be necessary to try all these combinations. Instead if a reasonable number of names is tried (we define reasonable as 62**3) fail to give the system administrator the chance to remove the problems. */ #define ATTEMPTS_MIN (62 * 62 * 62) /* The number of times to attempt to generate a temporary file. To conform to POSIX, this must be no smaller than TMP_MAX. */ #if ATTEMPTS_MIN < TMP_MAX unsigned int attempts = TMP_MAX; #else unsigned int attempts = ATTEMPTS_MIN; #endif len = strlen (tmpl); if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) { errno = EINVAL; return -1; } /* This is where the Xs start. */ XXXXXX = &tmpl[len - 6]; /* Get some more or less random data. */ { SYSTEMTIME stNow; FILETIME ftNow; // get system time GetSystemTime(&stNow); stNow.wMilliseconds = 500; if (!SystemTimeToFileTime(&stNow, &ftNow)) { errno = -1; return -1; } random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32) | (unsigned long long)ftNow.dwLowDateTime); } value += random_time_bits ^ (unsigned long long)GetCurrentThreadId (); for (count = 0; count < attempts; value += 7777, ++count) { unsigned long long v = value; /* Fill in the random bits. */ XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE); if (fd >= 0) { errno = save_errno; return fd; } else if (errno != EEXIST) return -1; } /* We got out of the loop because we ran out of combinations to try. */ errno = EEXIST; return -1; }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函數替換為_open,close函數替換為_close
將util/io.cpp中ReadProtoFromBinaryFile函數中的int?fd?=?open(filename,?O_RDONLY);替換為int?fd?=?open(filename,?O_RDONLY?|?O_BINARY);
在util/math_functions.cpp中添加
#define __builtin_popcount __popcnt #define __builtin_popcountl __popcnt在common.cpp中添加#include?<process.h>
將pid?=?getpid();替換為pid?= _getpid(); (這里本來有個筆誤,謝謝dongb5lz的提醒)
注釋掉::google::InstallFailureSignalHandler();
將solver.cpp中的snprintf函數替換為sprintf_s
?編譯吧!!!!
.....................
???????? ?? 按照文章http://www.bubuko.com/infodetail-902302.html里面的順序編譯.....,排錯逐個排除....
總結
以上是生活随笔為你收集整理的VS2013(Win10X64)-配置编译Caffe的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动物-昆虫:蚂蚱
- 下一篇: 宝可梦剑盾伊布在哪抓 盘点十大人气最高的