首页 > 程序开发 > 综合编程 > 其他综合 >

使用libcaffe为工程添加深度学习功能

2017-02-13

很多时候需要在自己的解决方案里添加caffe的功能,基本思路是在工程(x64)里添加编译好的libcaffe lib(使用windows版本)。

使用libcaffe为工程添加深度学习功能:很多时候需要在自己的解决方案里添加caffe的功能,基本思路是在工程(x64)里添加编译好的libcaffe.lib(使用windows版本)。
以下是配置方法


1)首先将原始caffe工程里 Build/x64/Debug 和 Build/x64/Release 的所有 DLL 和编译好的 libcaffe.lib 复制到独立的目录(如 CAFFE_LIB/dlls/Debug 和 CAFFE_LIB/dlls/Release)方便后期引用(注意,Debug和Release的libcaffe.lib是不同的,需要区分)

2)为工程添加 INCLUDE
将caffe原工程的include目录复制到 CAFFE_LIB/include

// Debug & Rlease
F:\Projects\CAFFE\CAFFE_LIB\include
F:\Projects\CAFFE\NugetPackages\boost.1.59.0.0\lib\native\include
F:\Projects\CAFFE\NugetPackages\glog.0.3.3.0\build\native\include
F:\Projects\CAFFE\NugetPackages\gflags.2.1.2.1\build\native\include
F:\Projects\CAFFE\NugetPackages\protobuf-v120.2.6.1\build\native\include
F:\Projects\CAFFE\NugetPackages\OpenBLAS.0.2.14.1\lib\native\include
F:\Projects\CAFFE\NugetPackages\OpenCV.2.4.10\build\native\include
E:\CUDA\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include
E:\CUDA\cuda\include

3)为工程添加 LIB

// Debug
F:\Projects\CAFFE\NugetPackages\lmdb-v120-clean.0.9.14.0\lib\native\lib\x64  
F:\Projects\CAFFE\NugetPackages\LevelDB-vc120.1.2.0.0\build\native\lib\x64\v120\Debug 
F:\Projects\CAFFE\NugetPackages\OpenCV.2.4.10\build\native\lib\x64\v120\Debug                        
F:\Projects\CAFFE\NugetPackages\boost_date_time-vc120.1.59.0.0\lib\native\address-model-64\lib 
F:\Projects\CAFFE\NugetPackages\boost_filesystem-vc120.1.59.0.0\lib\native\address-model-64\lib 
F:\Projects\CAFFE\NugetPackages\boost_system-vc120.1.59.0.0\lib\native\address-model-64\lib      
F:\Projects\CAFFE\NugetPackages\glog.0.3.3.0\build\native\lib\x64\v120\Debug\dynamic              
F:\Projects\CAFFE\NugetPackages\boost_thread-vc120.1.59.0.0\lib\native\address-model-64\lib     
F:\Projects\CAFFE\NugetPackages\boost_chrono-vc120.1.59.0.0\lib\native\address-model-64\lib     
F:\Projects\CAFFE\NugetPackages\boost_python2.7-vc120.1.59.0.0\lib\native\address-model-64\lib 
F:\Projects\CAFFE\NugetPackages\gflags.2.1.2.1\build\native\x64\v120\static\Lib   
F:\Projects\CAFFE\NugetPackages\hdf5-v120-complete.1.8.15.2\lib\native\lib\x64  
F:\Projects\CAFFE\NugetPackages\protobuf-v120.2.6.1\build\native\lib\x64\v120\Debug 
F:\Projects\CAFFE\NugetPackages\OpenBLAS.0.2.14.1\lib\native\lib\x64 
E:\CUDA\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64  
F:\Projects\CAFFE\CAFFE_LIB\dlls\Debug  
E:\CUDA\cuda\lib\x64  
\$(PythonDir)\libs

// Release
F:\Projects\CAFFE\NugetPackages\lmdb-v120-clean.0.9.14.0\lib\native\lib\x64
F:\Projects\CAFFE\NugetPackages\LevelDB-vc120.1.2.0.0\build\native\lib\x64\v120\Release
F:\Projects\CAFFE\NugetPackages\OpenCV.2.4.10\build\native\lib\x64\v120\Release
F:\Projects\CAFFE\NugetPackages\boost_date_time-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\boost_filesystem-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\boost_system-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\boost_thread-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\boost_chrono-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\boost_python2.7-vc120.1.59.0.0\lib\native\address-model-64\lib
F:\Projects\CAFFE\NugetPackages\glog.0.3.3.0\build\native\lib\x64\v120\Release\dynamic
F:\Projects\CAFFE\NugetPackages\gflags.2.1.2.1\build\native\x64\v120\static\Lib
F:\Projects\CAFFE\NugetPackages\hdf5-v120-complete.1.8.15.2\lib\native\lib\x64
F:\Projects\CAFFE\NugetPackages\protobuf-v120.2.6.1\build\native\lib\x64\v120\Release
F:\Projects\CAFFE\NugetPackages\OpenBLAS.0.2.14.1\lib\native\lib\x64
E:\CUDA\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64
F:\Projects\CAFFE\CAFFE_LIB\dlls\Release
E:\CUDA\cuda\lib\x64
\$(PythonDir)\libs  

3)添加库依赖

// Debug
libglog.lib 
libcaffe.lib         
gflagsd.lib   
gflags_nothreadsd.lib 
hdf5.lib         
hdf5_hl.lib     
libprotobuf.lib     
libopenblas.dll.a  
cublas.lib 
cuda.lib   
curand.lib 
cudart.lib 
cudnn.lib 
Shlwapi.lib
LevelDb.lib 
lmdbD.lib 
opencv_core2410d.lib       
opencv_highgui2410d.lib    
opencv_imgproc2410d.lib   
opencv_video2410d.lib      
opencv_objdetect2410d.lib

// Release
libglog.lib
libcaffe.lib
gflags.lib
gflags_nothreads.lib
hdf5.lib
hdf5_hl.lib
libprotobuf.lib
libopenblas.dll.a
cublas.lib
cuda.lib
curand.lib
cudart.lib
cudnn.lib
Shlwapi.lib
LevelDb.lib
lmdb.lib
opencv_core2410.lib
opencv_highgui2410.lib
opencv_imgproc2410.lib
opencv_video2410.lib
opencv_objdetect2410.lib

4)宏定义

_SCL_SECURE_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
USE_OPENCV
USE_LEVELDB
USE_LMDB
USE_CUDNN

5)添加DLL依赖
配置属性 -> 调试 -> 环境
path=F:\Projects\CAFFE\CAFFE_LIB\dlls\Debug;


理论上通过上面的配置,就可以在工程里调用caffe了(比如把classification.cpp内容添加进来就可以实现分类)
但此时运行还会报错 “Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Input (known types: Input )”
参考这里,需要再对caffe的layer做一个声明。于是添加一个头文件:

#ifndef LAYER_H_
#define LAYER_H_

#include "caffe/common.hpp"
#include "caffe/layers/input_layer.hpp"
#include "caffe/layers/inner_product_layer.hpp"

namespace caffe
{
    extern INSTANTIATE_CLASS(InputLayer);
    extern INSTANTIATE_CLASS(InnerProductLayer);
}

#endif

我这里只出现这两个层的错误,对于其他应用,可能需要添加其他层。

相关文章
最新文章
热点推荐