windows下部署caffe版的py-faster-rcnn(GPU版本)

折腾了三天整,终于搞明白了windows下py-faster-rcnn安装。一度想要从入门到放弃。

本人在ubuntu上复现了FAIR的 detectron ,以及RBG的py-faster-rcnn,按照网上或者我博客里的教程可以比较轻松地实现。可是唯独windows版的py-faster-rcnn复现部署是个坑,原因有以下三点:1. RBG不再更新py-faster-rcnn,导致fast rcnn caffe不能支持cudnn5及其以上版本,限制了硬件性能。 2.官方py-faster-rcnn的caffe和lib不支持windows,而且RGB貌似没有支持windows平台的开发欲望。3. 在windows下安装的python第三方依赖库不方便,众多依赖此起彼伏,安装困难。

检索了十几篇windows下py-faster-rcnn安装的技术文章,总结下安装思路主要分以下两种:

思路1. 下载R-FCN的caffe-rfcn,并修改编译,替换py-faster-rcnn目录下caffe-fast-rcnn中的python接口;下载py-faster-rcnn-windows的lib库,并修改编译,替换py-faster-rcnn目录下的lib。代表文章:

https://www.jianshu.com/p/53a2f02064f6

思路2. 下载微软的Caffe,git的地址https://github.com/BVLC/caffe/tree/windows增加相应的roi pooling和smooth L1层,修改caffe.proto文件等,替换py-faster-rcnn目录下caffe-fast-rcnn中的python接口;下载py-faster-rcnn-windows的lib库,并修改编译,替换py-faster-rcnn目录下的lib。代表文章:https://blog.csdn.net/zhangzm0128/article/details/71698880

思路1和思路2的不同点就是caffe基础库不同。这里不做评判哪种安装思路更快更好,只介绍思路2的安装,参考了这篇文章,并删减了驱动,cuda,cudnn安装步骤,增加了令人困扰的python第三方库的安装列表(摸索了好久出来的列表)。其实思路1是我首先尝试的,但是没有成功。。。

安装环境

64位 windows7或windows10 (本人windows7)

vs2013

cuda8.0

cudnn5.1

python2.7.14 或 anaconda2 (本人python2.7成功;anaconda2也尝试过,败在依赖上,可能是我步骤不对)

cmake-3.11.0

git

opencv2.49

以上环境缺少啥就装啥,安装很简单,不再赘述。

先重点声明几个目录的名称,安装过程很容易搞混

文件目录1. caffe:微软官方caffe目录

文件目录2. caffe-fast-rcnn: py-faster-rcnn官方caffe目录

文件目录3. caffe-frcnn : caffe增加层后的目录文件夹,后文会讲。

1. 安装python2.7.14以及第三方库

官网下载python,默认安装目录:C:\Python27。增加系统环境变量path:C:\Python27;C:\Python27\Scripts

打开cmd,依次输入以下命令(注意顺序最好不要随意改变)安装第三方库和工具:

cd C:\Python27\Scripts
easy_install pip
pip install numpy
pip install scipy
pip install matplotlib
pip install sklearn
pip install scikit-image
pip install h5py
pip install protobuf
pip install leveldb
pip install nose
pip install pandas
pip install tornado
pip install pyzmq
pip install pygments
pip install flask
pip install graphviz
pip install hypothesis
pip install pyyaml
pip install requests
pip install easydict
pip install cython

2. 编译caffe的python接口

(1)下载微软官方的caffe:
git clone https://github.com/BVLC/caffe.git  
cd caffe  
git checkout windows 

(2)修改C:\caffe\scripts下的build_win.cmd。

第7行的VERSION是你VS的版本,VS2015对应的是14,VS2013对应的是12;第8行改为0;第9行改为0(如果你不用GPU,那就还是1);13行的python_version是你的python版本,2.x就是2,3.x就是3;24,28行是你的python的安装目录,改为C:\Python27。如果你是anaconda就改你的anaconda的目录。

同样69-95行同样修改。以下是我修改后的build_win.cmd,可供参考

@echo off
@setlocal EnableDelayedExpansion

:: Default values
if DEFINED APPVEYOR (
    echo Setting Appveyor defaults
    if NOT DEFINED MSVC_VERSION set MSVC_VERSION=12
    if NOT DEFINED WITH_NINJA set WITH_NINJA=0
    if NOT DEFINED CPU_ONLY set CPU_ONLY=0
    if NOT DEFINED CUDA_ARCH_NAME set CUDA_ARCH_NAME=Auto
    if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
    if NOT DEFINED USE_NCCL set USE_NCCL=0
    if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
    if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=2
    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 NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
    if NOT DEFINED RUN_TESTS set RUN_TESTS=1
    if NOT DEFINED RUN_LINT set RUN_LINT=1
    if NOT DEFINED RUN_INSTALL set RUN_INSTALL=1

    :: Set python 2.7 with conda as the default python
    if !PYTHON_VERSION! EQU 2 (
        set CONDA_ROOT=C:\Python27
    )
    :: Set python 3.5 with conda as the default python
    if !PYTHON_VERSION! EQU 3 (
        set CONDA_ROOT=C:\Miniconda35-x64
    )
    set PATH=!CONDA_ROOT!;!CONDA_ROOT!\Scripts;!CONDA_ROOT!\Library\bin;!PATH!

    :: Check that we have the right python version
    !PYTHON_EXE! --version
    :: Add the required channels
    conda config --add channels conda-forge
    conda config --add channels willyd
    :: Update conda
    conda update conda -y
    :: Download other required packages
    conda install --yes cmake ninja numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz

    if ERRORLEVEL 1  (
      echo ERROR: Conda update or install failed
      exit /b 1
    )

    :: Install cuda and disable tests if needed
    if !WITH_CUDA! == 1 (
        call %~dp0\appveyor\appveyor_install_cuda.cmd
        set CPU_ONLY=0
        set RUN_TESTS=0
        set USE_NCCL=1
    ) else (
        set CPU_ONLY=1
    )

    :: Disable the tests in debug config
    if "%CMAKE_CONFIG%" == "Debug" (
        echo Disabling tests on appveyor with config == %CMAKE_CONFIG%
        set RUN_TESTS=0
    )

    :: Disable linting with python 3 until we find why the script fails
    if !PYTHON_VERSION! EQU 3 (
        set RUN_LINT=0
    )

) 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=12
    :: Change to 1 to use Ninja generator (builds much faster)
    if NOT DEFINED WITH_NINJA set WITH_NINJA=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=2
    :: 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
)

:: Set the appropriate CMake generator
:: Use the exclamation mark ! below to delay the
:: expansion of CMAKE_GENERATOR
if %WITH_NINJA% EQU 0 (
    if "%MSVC_VERSION%"=="14" (
        set CMAKE_GENERATOR=Visual Studio 14 2015 Win64
    )
    if "%MSVC_VERSION%"=="12" (
        set CMAKE_GENERATOR=Visual Studio 12 2013 Win64
    )
    if "!CMAKE_GENERATOR!"=="" (
        echo ERROR: Unsupported MSVC version
        exit /B 1
    )
) else (
    set CMAKE_GENERATOR=Ninja
)

echo INFO: ============================================================
echo INFO: Summary:
echo INFO: ============================================================
echo INFO: MSVC_VERSION               = !MSVC_VERSION!
echo INFO: WITH_NINJA                 = !WITH_NINJA!
echo INFO: CMAKE_GENERATOR            = "!CMAKE_GENERATOR!"
echo INFO: CPU_ONLY                   = !CPU_ONLY!
echo INFO: CUDA_ARCH_NAME             = !CUDA_ARCH_NAME!
echo INFO: CMAKE_CONFIG               = !CMAKE_CONFIG!
echo INFO: USE_NCCL                   = !USE_NCCL!
echo INFO: CMAKE_BUILD_SHARED_LIBS    = !CMAKE_BUILD_SHARED_LIBS!
echo INFO: PYTHON_VERSION             = !PYTHON_VERSION!
echo INFO: BUILD_PYTHON               = !BUILD_PYTHON!
echo INFO: BUILD_PYTHON_LAYER         = !BUILD_PYTHON_LAYER!
echo INFO: BUILD_MATLAB               = !BUILD_MATLAB!
echo INFO: PYTHON_EXE                 = "!PYTHON_EXE!"
echo INFO: RUN_TESTS                  = !RUN_TESTS!
echo INFO: RUN_LINT                   = !RUN_LINT!
echo INFO: RUN_INSTALL                = !RUN_INSTALL!
echo INFO: ============================================================

:: Build and exectute the tests
:: Do not run the tests with shared library
if !RUN_TESTS! EQU 1 (
    if %CMAKE_BUILD_SHARED_LIBS% EQU 1 (
        echo WARNING: Disabling tests with shared library build
        set RUN_TESTS=0
    )
)

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

:: 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% ^
      -DCOPY_PREREQUISITES:BOOL=1 ^
      -DINSTALL_PREREQUISITES:BOOL=1 ^
      -DUSE_NCCL:BOOL=!USE_NCCL! ^
      -DCUDA_ARCH_NAME:STRING=%CUDA_ARCH_NAME% ^
	  -DCUDNN_ROOT=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA ^
      "%~dp0\.."

if ERRORLEVEL 1 (
  echo ERROR: Configure failed
  exit /b 1
)

:: Lint
if %RUN_LINT% EQU 1 (
    cmake --build . --target lint  --config %CMAKE_CONFIG%
)

if ERRORLEVEL 1 (
  echo ERROR: Lint failed
  exit /b 1
)

:: Build the library and tools
cmake --build . --config %CMAKE_CONFIG%

if ERRORLEVEL 1 (
  echo ERROR: Build failed
  exit /b 1
)

:: Build and exectute the tests
if !RUN_TESTS! EQU 1 (
    cmake --build . --target runtest --config %CMAKE_CONFIG%

    if ERRORLEVEL 1 (
        echo ERROR: Tests failed
        exit /b 1
    )

    if %BUILD_PYTHON% EQU 1 (
        if %BUILD_PYTHON_LAYER% EQU 1 (
            :: Run python tests only in Release build since
            :: the _caffe module is _caffe-d is debug
            if "%CMAKE_CONFIG%"=="Release" (
                :: Run the python tests
                cmake --build . --target pytest

                if ERRORLEVEL 1 (
                    echo ERROR: Python tests failed
                    exit /b 1
                )
            )
        )
    )
)

if %RUN_INSTALL% EQU 1 (
    cmake --build . --target install --config %CMAKE_CONFIG%
)

popd
@endlocal
之后进入caffe\scripts,打开cmd,直接执行build_win.cmd。cmake会编译caffe。编译过程会自动下载需要的库,因为服务器呢都不在国内,需要挂个VPN

(3)编译结束后。把caffe\python下的caffe的文件夹以及.py文件等一并拷贝到python的第三方包的文件夹C:\Python27\Lib\site-packages下就ok。

    

现在可以在cmd输入以下命令验证caffe是否安装成功

python
import caffe

3. 修改caffe,增加layer

(1)这一步需要重新git clone一个微软的caffe,并命名为caffe-frcnn

git clone https://github.com/BVLC/caffe.git  
cd caffe  
git checkout windows 

把步骤2中修改后的 C:\caffe\scripts下的build_win.cmd文件复制到C:\caffe-frcnn\scripts下替换同名文件。

(2)git clone一个py-faster-rcnn文件

git clone https://github.com/rbgirshick/py-faster-rcnn
clone到本地后的文件夹为py-faster-rcnn

(3)添加层和文件

 打开py-faster-rcnn\caffe-fast-rcnn\src\caffe\layers文件夹,找到4个文件分别为:


然后copy到caffe-frcnn\src\caffe\layers里。接着添加头文件,打开py-faster-rcnn\caffe-fast-rcnn\include\caffe,把fast_rcnn_layers.hpp这文件copy到caffe-frcnn\include\caffe下。

(4)然后需要修改C:\caffe-frcnn\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];  
}  
(5)编译caffe-frcnn的python接口

    之后打开cmd,进入caffe-frcnn\scripts,再次执行build_win.cmd。编译好之后,只需把caffe-frcnn根目录下的python文件夹替换py-faster-rcnn\caffe-fast-rcnn的python文件夹。

4. 修改编译windows版的faster rcnn依赖库lib

git clone faster rcnn在windows下的lib https://github.com/MrGF/py-faster-rcnn-windows,clone后lib文件夹如下:


需要修改的文件:

lib/rpn下的proposal_layer.py,proposal_target_layer.py,Anchor_target_layer.py

lib/roi_data_layer下的layer.py

依次打开以上需要修改的py文件,把param_str_改为param_str。同时proposal_layer.py还需另一个修改:

cfg_key = str(self.phase) # either 'TRAIN' or 'TEST'   改为
cfg_key = str('TRAIN' if self.phase == 0 else 'TEST')

否则在demo时候会报错。

打开cmd,进入到lib路径下,输入:

SET VS90COMNTOOLS=%VS120COMNTOOLS%

这是vs2013的临时路径命令,若vs版本是2015那么应该把12改成14

然后输入

python setup.py install
python setup_cuda.py install

提示Finished processing dependencies for py-faster-rcnn-gpu=0.0.0 

说明安装成功。

删掉整个py-faster-rcnn\lib文件夹,然后将刚才编译好的lib文件复制到py-faster-rcnn\下。

5. demo

把训练好的模型放在py-faster-rcnn\data\faster_rcnn_models下,

执行python demo.py。

cmd显示推理信息如下。

由于是项目的图像,我就不post了。800*400分辨率的图像,GTX1080TI的推理速度为0.078s,满意地笑了



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值