Caffe环境配置+fast-rcnn demo.py 运行(主要是记录添加额外层的过程遇到的问题)

1.配置环境:【采用的是BVLC官方版本:https://github.com/BVLC/caffe.git 

caffe+VS2015+python3.5+CUDA8.0(x64)+cuDNN v5.1(x64)

首先除了caffe,先安装好后面的软件,环境变量都要记得添加。

python3.5我选择的是安装:Anaconda3-4.2.0-Windows-x86_64

2.caffe编译

a.在cmd命令行下运行

C:\Projects> git clone https://github.com/BVLC/caffe.git  
C:\Projects> cd caffe  
C:\Projects\caffe> git checkout windows  
:: Edit any of the options inside build_win.cmd to suit your needs  

   C:\Projects表示的是想要安装的位置

b.下载需要的文件:

https://github.com/willyd/caffe-builder/releases/download/v1.0.1/libraries_v140_x64_py27_1.0.1.tar.bz2

 

下完依赖包,然后在caffe目录下,新建一个名为“build”的文件夹,然后再把我们下好的依赖包解压到build文件夹里面。

解压完后,发现它是个libraries文件夹,然后把\libraries\bin,\libraries\lib,\libraries\x64\vc14\bin三个的绝对路径添加到环境变量里面(添加完后记得重启)。

c.进入caffe文件夹:C:\Projects\caffe\scripts\build_win.cmd

用Notepad++打开build_win.cmd

c-1:删除:

if NOT EXIST build mkdir build  
pushd build  
  
:: Setup the environement for VS x64  
set batch_file=!VS%MSVC_VERSION%0COMNTOOLS!..\..\VC\vcvarsall.bat  
call "%batch_file%" amd64  

c-2:如果需要cuDNN库,那么在(没删之前的)143行-155行那一块:

:: Configure using cmake and using the caffe-builder dependencies  
:: Add -DCUDNN_ROOT=C:/Projects/caffe/cudnn-8.0-windows10-x64-v5.1/cuda ^  
:: below to use cuDNN  
cmake -G"!CMAKE_GENERATOR!" ^  
      -DBLAS=Open ^  
      -DCMAKE_BUILD_TYPE:STRING=%CMAKE_CONFIG% ^  
      -DBUILD_SHARED_LIBS:BOOL=%CMAKE_BUILD_SHARED_LIBS% ^  
      -DBUILD_python:BOOL=%BUILD_PYTHON% ^  
      -DBUILD_python_layer:BOOL=%BUILD_PYTHON_LAYER% ^  
      -DBUILD_matlab:BOOL=%BUILD_MATLAB% ^  
      -DCPU_ONLY:BOOL=%CPU_ONLY% ^  
      -DCUDNN_ROOT=C:\Projects\cuda ^   
      ::要求是反斜杠  
      -C %cd%\libraries\caffe-builder-config.cmake ^  
      %~dp0\..  

c-3:然后按照配置更改如下内容:

) else (
    :: Change the settings here to match your setup
    :: Change MSVC_VERSION to 12 to use VS 2013
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14  //这里是VS版本,2015-14,2013-12
    :: Change to 1 to use Ninja generator (builds much faster)
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0  //这里一定要改为0
    :: Change to 1 to build caffe without CUDA support
    if NOT DEFINED CPU_ONLY set CPU_ONLY=0
    :: Change to generate CUDA code for one of the following GPU architectures
    :: [Fermi  Kepler  Maxwell  Pascal  All]
    if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
    :: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
    if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
    :: Set to 1 to use NCCL
    if NOT DEFINED USE_NCCL set USE_NCCL=0
    :: Change to 1 to build a caffe.dll
    if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
    :: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
    if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=3  //python 版本:我的是Python3.5
    :: Change these options for your needs.
    if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
    if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
    if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=0
    :: If python is on your path leave this alone
    if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
    :: Run the tests
    if NOT DEFINED RUN_TESTS set RUN_TESTS=0
    :: Run lint
    if NOT DEFINED RUN_LINT set RUN_LINT=0
    :: Build the install target
    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
)
//这里是VS版本,2015-14,2013-12
    :: Change to 1 to use Ninja generator (builds much faster)
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0  //这里一定要改为0
    :: Change to 1 to build caffe without CUDA support
    if NOT DEFINED CPU_ONLY set CPU_ONLY=0
    :: Change to generate CUDA code for one of the following GPU architectures
    :: [Fermi  Kepler  Maxwell  Pascal  All]
    if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
    :: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
    if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
    :: Set to 1 to use NCCL
    if NOT DEFINED USE_NCCL set USE_NCCL=0
    :: Change to 1 to build a caffe.dll
    if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
    :: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
    if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=3  //python 版本:我的是Python3.5
    :: Change these options for your needs.
    if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
    if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
    if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=0
    :: If python is on your path leave this alone
    if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
    :: Run the tests
    if NOT DEFINED RUN_TESTS set RUN_TESTS=0
    :: Run lint
    if NOT DEFINED RUN_LINT set RUN_LINT=0
    :: Build the install target
    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
)

c-4:到scripts目录下打开命令窗口执行:

     C:\Projects\caffe\scripts> build_win.cmd  

等待一段时间就编译结束,如果想要在Python中import caffe,那么将C:\projects\caffe\python\caffe这一整个文件复制到

C:\Users\Admin\Anaconda3\Lib\site-packages文件中(我anaconda安装在C盘,没有另改位置)

 

二、接下来是想要运行FAST-RCNN/FASTER-RCNN

参考:https://blog.csdn.net/zhangzm0128/article/details/71698880

这两个框架都有对应的caffe版本,直接下载相应的caffe版本编译更好,然而我喜欢BVLC版本的caffe,所以就需要手动添加缺失的layer-roi_pooling_layer+smooth_L1_loss_layer:

1.需要四个文件,可以在py-faster-rcnn\caffe-fast-rcnn\src\caffe\layers中找到,

roi_pooling_layer.cpp/roi_pooling_layer.cu

smooth_L1_loss_layer.cpp/smooth_L1_loss_layer.cu

将这四个文件复制到C:\Projects\caffe\src\caffe\layers

2.添加头文件:

打开py-faster-rcnn\caffe-fast-rcnn\include\caffefast_rcnn_layers.hpp

复制到C:\Projects\caffe\include\caffe。

3.配置新层

打开C:\Projects\caffe\src\caffe\proto下的caffe.proto

添加的信息如下:

 optional ROIPoolingParameter roi_pooling_param = 8266711;  
  optional SmoothL1LossParameter smooth_l1_loss_param = 8266712;  
message ROIPoolingParameter {  
  // Pad, kernel size, and stride are all given as a single value for equal  
  // dimensions in height and width or as Y, X pairs.  
  optional uint32 pooled_h = 1 [default = 0]; // The pooled output height  
  optional uint32 pooled_w = 2 [default = 0]; // The pooled output width  
  // Multiplicative spatial scale factor to translate ROI coords from their  
  // input scale to the scale used when pooling  
  optional float spatial_scale = 3 [default = 1];  
}  
message SmoothL1LossParameter {  
  // SmoothL1Loss(x) =  
  //   0.5 * (sigma * x) ** 2    -- if x < 1.0 / sigma / sigma  
  //   |x| - 0.5 / sigma / sigma -- otherwise  
  optional float sigma = 1 [default = 1];  

三、运行fast-rcnn:demo.py

主要是注意一下,因为rcnn里的文件是用Python2 写的,所以多多少少会有语法问题,只要按照Python3的语法改好就行了

https://blog.csdn.net/happynear/article/details/46822109

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值