今天以caffe-yolo(https://github.com/yeahkun/caffe-yolo.git)为例:
首先,下载代码库:git clone https://github.com/yeahkun/caffe-yolo.git
修改以下几个文件:
src/caffe/util/bbox_util.cpp
顶部添加:
#if _MSC_VER
#define snprintf _snprintf
#endif
src/caffe/layers/detection_loss_layer.cpp
注释掉:
#ifdef CPU_ONLY
//STUB_GPU(DetectionLossLayer);
#endif
src/caffe/util/db_lmdb.cpp
顶部添加:
#if defined(_MSC_VER)
#include <direct.h>
#define mkdir(X, Y) _mkdir(X)
#endif
src/caffe/util/hdf5.cpp
主要是VC不能在case中声明变量:
// Verifies format of data stored in HDF5 file and reshapes blob accordingly.
template <typename Dtype>
void hdf5_load_nd_dataset_helper(
hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
Blob<Dtype>* blob) {
// Verify that the dataset exists.
CHECK(H5LTfind_dataset(file_id, dataset_name_))
<< "Failed to find HDF5 dataset " << dataset_name_;
// Verify that the number of dimensions is in the accepted range.
herr_t status;
int ndims;
status = H5LTget_dataset_ndims(file_id, dataset_name_, &ndims);
CHECK_GE(status, 0) << "Failed to get dataset ndims for " << dataset_name_;
CHECK_GE(ndims, min_dim);
CHECK_LE(ndims, max_dim);
// Verify that the data format is what we expect: float or double.
std::vector<hsize_t> dims(ndims);
H5T_class_t class_;
status = H5LTget_dataset_info(
file_id, dataset_name_, dims.data(), &class_, NULL);
CHECK_GE(status, 0) << "Failed to get dataset info for " << dataset_name_;
switch (class_) {
case H5T_FLOAT:
// In VC++ declaring and initializing variables in case statement without
// curly braces (new scope), cause compiler error C2360
// https://msdn.microsoft.com/en-us/library/61af7cx3.aspx
{
LOG_FIRST_N(INFO, 1) << "Datatype class: H5T_FLOAT";
break;
}
case H5T_INTEGER:
{
LOG_FIRST_N(INFO, 1) << "Datatype class: H5T_INTEGER";
break;
}
case H5T_TIME:
{
LOG(FATAL) << "Unsupported datatype class: H5T_TIME";
}
case H5T_STRING:
{
LOG(FATAL) << "Unsupported datatype class: H5T_STRING";
}
case H5T_BITFIELD:
{
LOG(FATAL) << "Unsupported datatype class: H5T_BITFIELD";
}
case H5T_OPAQUE:
{
LOG(FATAL) << "Unsupported datatype class: H5T_OPAQUE";
}
case H5T_COMPOUND:
{
LOG(FATAL) << "Unsupported datatype class: H5T_COMPOUND";
}
case H5T_REFERENCE:
{
LOG(FATAL) << "Unsupported datatype class: H5T_REFERENCE";
}
case H5T_ENUM:
{
LOG(FATAL) << "Unsupported datatype class: H5T_ENUM";
}
case H5T_VLEN:
{
LOG(FATAL) << "Unsupported datatype class: H5T_VLEN";
}
case H5T_ARRAY:
{
LOG(FATAL) << "Unsupported datatype class: H5T_ARRAY";
}
default:
{
LOG(FATAL) << "Datatype class unknown"; }
}
vector<int> blob_dims(dims.size());
for (int i = 0; i < dims.size(); ++i) {
blob_dims[i] = dims[i];
}
blob->Reshape(blob_dims);
}
src/caffe/util/io.cpp
顶部添加:
#if defined(_MSC_VER)
#include <io.h>
#endif
src/caffe/util/signal_handler.cpp
主要是宏定义问题:
void handle_signal(int signal) {
switch (signal) {
#ifdef _MSC_VER
case SIGBREAK: // there is no SIGHUP in windows, take SIGBREAK instead.
got_sighup = true;
break;
#else
case SIGHUP:
got_sighup = true;
break;
#endif
case SIGINT:
got_sigint = true;
break;
}
}
void HookupHandler() {
if (already_hooked_up) {
LOG(FATAL) << "Tried to hookup signal handlers more than once.";
}
already_hooked_up = true;
#ifdef _MSC_VER
if (signal(SIGBREAK, handle_signal) == SIG_ERR) {
LOG(FATAL) << "Cannot install SIGBREAK handler.";
}
if (signal(SIGINT, handle_signal) == SIG_ERR) {
LOG(FATAL) << "Cannot install SIGINT handler.";
}
#else
struct sigaction sa;
// Setup the handler
sa.sa_handler = &handle_signal;
// Restart the system call, if at all possible
sa.sa_flags = SA_RESTART;
// Block every signal during the handler
sigfillset(&sa.sa_mask);
// Intercept SIGHUP and SIGINT
if (sigaction(SIGHUP, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot install SIGHUP handler.";
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot install SIGINT handler.";
}
#endif
}
// Set the signal handlers to the default.
void UnhookHandler() {
if (already_hooked_up) {
#ifdef _MSC_VER
if (signal(SIGBREAK, SIG_DFL) == SIG_ERR) {
LOG(FATAL) << "Cannot uninstall SIGBREAK handler.";
}
if (signal(SIGINT, SIG_DFL) == SIG_ERR) {
LOG(FATAL) << "Cannot uninstall SIGINT handler.";
}
#else
struct sigaction sa;
// Setup the sighub handler
sa.sa_handler = SIG_DFL;
// Restart the system call, if at all possible
sa.sa_flags = SA_RESTART;
// Block every signal during the handler
sigfillset(&sa.sa_mask);
// Intercept SIGHUP and SIGINT
if (sigaction(SIGHUP, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot uninstall SIGHUP handler.";
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot uninstall SIGINT handler.";
}
#endif
already_hooked_up = false;
}
}
最后是:
src/caffe/common.cpp
头部添加:
#if defined(_MSC_VER)
#include <process.h>
#define getpid() _getpid()
#endif
添加修改:
void GlobalInit(int* pargc, char*** pargv) {
// Google flags.
::gflags::ParseCommandLineFlags(pargc, pargv, true);
// Google logging.
::google::InitGoogleLogging(*(pargv)[0]);
// Provide a backtrace on segfault.
// Windows port of glogs doesn't have this function built
#if !defined(_MSC_VER)
::google::InstallFailureSignalHandler();
#endif
}
我贴出我的配置文件:
CommonSettings.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<BuildDir>$(SolutionDir)..\Build</BuildDir>
<!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.-->
<CpuOnlyBuild>true</CpuOnlyBuild>
<UseCuDNN>false</UseCuDNN>
<CudaVersion>7.5</CudaVersion>
<!-- NOTE: If Python support is enabled, PythonDir (below) needs to be
set to the root of your Python installation. If your Python installation
does not contain debug libraries, debug build will not work. -->
<PythonSupport>true</PythonSupport>
<!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
set to the root of your Matlab installation. -->
<MatlabSupport>false</MatlabSupport>
<CudaDependencies></CudaDependencies>
<!-- Set CUDA architecture suitable for your GPU.
Setting proper architecture is important to mimize your run and compile time. -->
<CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture>
<!-- CuDNN 3 and 4 are supported -->
<CuDnnPath></CuDnnPath>
<ScriptsDir>$(SolutionDir)\scripts</ScriptsDir>
</PropertyGroup>
<!-- temp here-->
<PropertyGroup>
<OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<!-- 3rdparty here-->
<PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'">
<CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies>
</PropertyGroup>
<PropertyGroup Condition="'$(UseCuDNN)'=='true'">
<CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies>
</PropertyGroup>
<PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''">
<LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>
<IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup>
<LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup>
<the3rdpartyDir>$(SolutionDir)..\..\caffe-3rdparty</the3rdpartyDir>
<LibraryPath>$(the3rdpartyDir)\lib;$(the3rdpartyDir)\lib\$(Configuration);$(LibraryPath)</LibraryPath>
<IncludePath>$(the3rdpartyDir)\include;$(the3rdpartyDir)\include\OpenBLAS;$(the3rdpartyDir)\include\hdf5;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(PythonSupport)'=='true'">
<PythonDir>D:\AI\Anaconda3</PythonDir>
<LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
<IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(MatlabSupport)'=='true'">
<MatlabDir>E:\MATLAB\R2015b</MatlabDir>
<LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
<IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<!-- Define here-->
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BOOST_ALL_NO_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">
<ClCompile>
<PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(UseCuDNN)'=='true'">
<ClCompile>
<PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<CudaCompile>
<Defines>USE_CUDNN</Defines>
</CudaCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">
<ClCompile>
<PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">
<ClCompile>
<PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<Optimization>Full</Optimization>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<GenerateDebugInformation>true</GenerateDebugInformation>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<OptimizeReferences>true</OptimizeReferences>
<OpenCVLib>opencv_core2411.lib;opencv_highgui2411.lib;opencv_imgproc2411.lib</OpenCVLib>
<BoostLib>libboost_chrono-vc120-mt-1_59.lib;libboost_date_time-vc120-mt-1_59.lib;libboost_filesystem-vc120-mt-1_59.lib;libboost_python-vc120-mt-1_59.lib;libboost_regex-vc120-mt-1_59.lib;libboost_system-vc120-mt-1_59.lib;libboost_thread-vc120-mt-1_59.lib</BoostLib>
<HDF5Lib>hdf5.lib;hdf5_hl.lib</HDF5Lib>
<AdditionalDependencies>libcaffe.lib;libprotobuf.lib;libglog.lib;gflags.lib;LevelDB.lib;LMDB.lib;libopenblas.dll.a;%(HDF5Lib);%(BoostLib);%(OpenCVLib);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<OpenCVLib>opencv_core2411d.lib;opencv_highgui2411d.lib;opencv_imgproc2411d.lib</OpenCVLib>
<BoostLib>libboost_chrono-vc120-mt-gd-1_59.lib;libboost_date_time-vc120-mt-gd-1_59.lib;libboost_filesystem-vc120-mt-gd-1_59.lib;libboost_python-vc120-mt-gd-1_59.lib;libboost_regex-vc120-mt-gd-1_59.lib;libboost_system-vc120-mt-gd-1_59.lib;libboost_thread-vc120-mt-gd-1_59.lib</BoostLib>
<HDF5Lib>hdf5.lib;hdf5_hl.lib</HDF5Lib>
<AdditionalDependencies>libcaffe.lib;libprotobuf.lib;libglog.lib;gflags.lib;LevelDB.lib;LMDB.lib;libopenblas.dll.a;%(HDF5Lib);%(BoostLib);%(OpenCVLib);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>
最后是目录结构:
只要包含一下CommonSettings.props,很轻松就能编译通过得~~~~