您的位置:首页 > 编程语言 > MATLAB

【caffe-MATLAB】自己编译faster-rcnn的MATLAB版本

2017-04-07 12:11 537 查看

今天在自己的电脑上编译MATLAB版本的faster-RCNN代码,将过程在这作一记录。

1.我的环境:Windows10 ,VS2013 , MATLAB2017a ,CUDA7.5

首先下载作者的源码:https://github.com/ShaoqingRen/faster_rcnn
### Preparation for Testing:
0.	Run `fetch_data/fetch_caffe_mex_windows_vs2013_cuda65.m` to download a compiled Caffe mex (for Windows only).
0.	Run `faster_rcnn_build.m`
0.	Run `startup.m`

上面是作者给出的运行方法,但是他的环境是vs2013+cuda6.5,直接下载他的caffe mex在后面的build中会出错。

这里我们需要自己编译caffe,在自己的环境下编译好caffe之后,将build里面的matcaffe拷到G:\faster_rcnn-master\external\caffe\matlab\caffe_faster_rcnn下。

2.接下来就执行2,3步了。

运行第二步的时候经常会出错,原因大多是编译器和cuda路径不对。

错误一般出在G:\faster_rcnn-master\functions\nms下面的nvmex.m文件中。

if ispc % Windows
Host_Compiler_Location = '-ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64"';
CUDA_INC_Location = ['"' getenv('CUDA_PATH')  '\include"'];
CUDA_SAMPLES_Location =['"' getenv('NVCUDASAMPLES7_5_ROOT')  '\common\inc"'];
PIC_Option = '';
if ( strcmp(computer('arch'),'win32') ==1)
machine_str = ' --machine 32 ';
CUDA_LIB_Location = ['"' getenv('CUDA_PATH')  '\lib\Win32"'];
elseif  ( strcmp(computer('arch'),'win64') ==1)
machine_str = ' --machine 64 ';
CUDA_LIB_Location = ['"' getenv('CUDA_PATH')  '\lib\x64"'];
end
NVCC = 'nvcc';


这是Windows下的配置,主要是选择编译器、包含库、cuda库。NVCUDASAMPLES7_5_ROOT本来是....6_5_ROOT,需要改成7.5版本。这些路径可以在MATLAB里单步调试去看。

nvcc --compile -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64" -o nms_gpu_mex.o  --machine 64  ...
-I"E:\MATLAB2017a/extern/include " -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include" ...
-I"C:\ProgramData\NVIDIA Corporation\CUDA Samples\v7.5\common\inc" "functions/nms/nms_gpu_mex.cu"
我的路径是这样的。

运行build.m以后报错:

Error using mex
LIBCMT.lib(crt0dat.obj) : error LNK2005: _amsg_exit 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(crt0dat.obj) : error LNK2005: _initterm_e 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(winapisupp.obj) : error LNK2005: __crtCaptureCurrentContext
已经在 MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(winapisupp.obj) : error LNK2005: __crtCapturePreviousContext
已经在 MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(winapisupp.obj) : error LNK2005: __crtTerminateProcess 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(winapisupp.obj) : error LNK2005: __crtUnhandledException 已经
在 MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)"
(?terminate@@YAXXZ) 已经在 MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(crt0init.obj) : error LNK2005: __xi_a 已经在
MSVCRT.lib(cinitexe.obj) 中定义
LIBCMT.lib(crt0init.obj) : error LNK2005: __xi_z 已经在
MSVCRT.lib(cinitexe.obj) 中定义
LIBCMT.lib(crt0init.obj) : error LNK2005: __xc_a 已经在
MSVCRT.lib(cinitexe.obj) 中定义
LIBCMT.lib(crt0init.obj) : error LNK2005: __xc_z 已经在
MSVCRT.lib(cinitexe.obj) 中定义
LIBCMT.lib(winxfltr.o
4000
bj) : error LNK2005: __CppXcptFilter 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(mlock.obj) : error LNK2005: _lock 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
LIBCMT.lib(mlock.obj) : error LNK2005: _unlock 已经在
MSVCRT.lib(MSVCR120.dll) 中定义
正在创建库 G:\faster_rcnn-master\bin\nms_gpu_mex.lib 和对象
G:\faster_rcnn-master\bin\nms_gpu_mex.exp
LINK : warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用
/NODEFAULTLIB:library
LINK : warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用
/NODEFAULTLIB:library
G:\faster_rcnn-master\bin\nms_gpu_mex.mexw64 : fatal error LNK1169: 找到
一个或多个多重定义的符号

Error in nvmex (line 48)
eval(mexCommandLine);

Error in faster_rcnn_build (line 23)
nvmex('functions/nms/nms_gpu_mex.cu', 'bin');


这个错误是lib库冲突了,解决办法是修改mex命令:


mexCommandLine = ['mex ' '-outdir ' outDir ' ' filename '.o'   ' -L' CUDA_LIB_Location   ' -lcudart' ' LINKFLAGS="$LINKFLAGS /NODEFAULTLIB:LIBCMT.LIB"' ];


意思是忽略LIBCMT.lib库,然后继续编译就可以过了。

3.第三步执行startup.m,

mex -outdir bin nms_gpu_mex.o -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64" -lcudart LINKFLAGS="$LINKFLAGS /NODEFAULTLIB:LIBCMT.LIB"
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
MEX completed successfully.
>> startup
fast_rcnn startup done
至此,基本环境已经配置完成了。

4.运行demo

下载final model,这个最好直接取上面的作者主页下载,给出了百度云的地址。

If the automatic "fetch_data" fails, you may manually download resouces from:

Pre-complied caffe mex:
Windows-based mex complied with VS2013 and Cuda6.5: OneDrive, DropBox, BaiduYun
ImageNet-pretrained networks:
Zeiler & Fergus (ZF) net OneDrive, DropBox, BaiduYun
VGG-16 net OneDrive, DropBox, BaiduYun
Final RPN+FastRCNN models: OneDrive, DropBox, BaiduYun
下载最后一个。

然后需要在工程根目录下面运行 experiments/script_faster_rcnn_demo,刚开始运行总是崩溃,修改模型以后就可以了:

%model_dir                   = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_vgg_16layers'); %% VGG-16
model_dir                   = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_ZF'); %% ZF


原因应该是显存不够造成MATLAB崩溃,ZF模型要小一点。如果这部分出错,可以在代码里检查一下你的模型路径和图片路径是否与真实的一致,然后运行就可以了。可以看到这个例子中作者给出了五张图的测试。

2017.04.10修改:faster_rcnn_VOC0712_vgg_16layers模型在我尝试的时候,将demo里的17行    
opts.test_scales            = 600;

改为  = 300;之后居然神奇地跑通了。可见,之前MATLAB奔溃确实是因为显存不够导致的。

然后运行就可以看到如下结果了:

fast_rcnn startup done
GPU 1: free memory 1686414131
Use GPU 1
001763.jpg (500x375): time 0.230s (resize+conv+proposal: 0.124s, nms+regionwise: 0.105s)
004545.jpg (500x375): time 0.736s (resize+conv+proposal: 0.604s, nms+regionwise: 0.132s)
000542.jpg (500x375): time 0.246s (resize+conv+proposal: 0.154s, nms+regionwise: 0.092s)
000456.jpg (500x375): time 0.222s (resize+conv+proposal: 0.088s, nms+regionwise: 0.134s)
001150.jpg (500x375): time 0.165s (resize+conv+proposal: 0.087s, nms+regionwise: 0.077s)
mean time: 0.320s
Cleared 0 solvers and 2 stand-alone nets









内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: