Faster R-CNN在Window環境的目標檢測
阿新 • • 發佈:2018-12-30
Faster R-CNN在Linux環境下面的訓練和檢測相信很多感興趣的人都可以根據下面
獲得python版本的環境搭建指導。
但是Faster R-CNN工作在Windows卻很少看到有人能夠成功搭建訓練和檢測環境。
本文,主要介紹使用Linux環境下自定義訓練模型在Windows環境下Faster R-CNN的python版本的目標檢測移植。
採用pascal_voc格式製作自己的資料集,模型使用中等網路模型,使用的端到端的訓練方法,測試的使用CPU進行測試。
Faster R-CNN移植需要分兩部分:
1、Faster R-CNN Caffe移植:
第一步:安裝caffe的python依賴環境,可以依據faster rcnn caffe目錄下面python的requirements.txt檔案下載對應的python模組:
Cython>=0.19.2 numpy>=1.7.1 scipy>=0.13.2 scikit-image>=0.9.3 matplotlib>=1.3.1 ipython>=3.0.0 h5py>=2.2.0 leveldb>=0.191 networkx>=1.8.1 nose>=1.3.0 pandas>=0.12.0 python-dateutil>=1.4,<2 protobuf>=2.5.0 python-gflags>=2.0 pyyaml>=3.10 Pillow>=2.3.0 six>=1.1.0
也可以安裝Anaconda 來更快速獲得相應的科學計算工具庫。由於該網站下載速度極慢,我採用 了python2.7的版本安裝。
一般通過pip和wheel兩種方式即可完成所有包的安裝;
下面的faster rcnn caffe本身不支援Windows的,需要基於該原始碼進行移植。
可以根據官方caffe-window版本進行對比移植。請使用VS2013工程進行編譯。
基本上移植難度很小。
第三步:生成pycaffe:
編譯libcaffe,pycaffe。
檢查Build\x64\Release\pycaffe\caffe目錄下是否拷貝了pycaffe依賴庫。若沒有,則需要採用下面bat指令碼生成:
進入當前caffe-windows\windows\scripts\\目錄下執行下面命令:
ProtoCompile.cmd D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\
PythonPreBuild D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\
PythonPostBuild D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\Build\\x64\\Release\\
2、Faster R-CNN目標檢測移植:
第一步:
主要工作是移植nms,bbox兩個模組,具體主要修改setup.py指令碼和nms包裝檔案: nms包裝檔案,全部採用CPU方式:#from nms.gpu_nms import gpu_nms
def nms(dets, thresh, force_cpu=True):
setup.py修改:只留cpu的方式
ext_modules = [
# unix _compile: obj, src, ext, cc_args, extra_postargs, pp_opts
Extension(
"utils.cython_bbox",
sources=["utils\\bbox.pyx"],
extra_compile_args={'gcc': []},
include_dirs = [numpy_include]
),
Extension(
"nms.cpu_nms",
sources=["nms\\cpu_nms.pyx"],
extra_compile_args={'gcc': []},
include_dirs = [numpy_include],
)
]
註釋掉cuda的依賴:
def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
postfix=os.path.splitext(sources[0])[1]
if postfix == '.cu':
# use the cuda for .cu files
#self.set_executable('compiler_so', CUDA['nvcc'])
# use only a subset of the extra_postargs, which are 1-1 translated
# from the extra_compile_args in the Extension class
postargs = extra_postargs['nvcc']
else:
postargs = extra_postargs['gcc']
return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)
由於本人機器不支援GPU,所以註釋掉相關GPU模式的介面和模組的編譯; 並下載gcc 在cmd中輸入python setup.py build_ext --inplace編譯。 我在編譯nms報模型資料型別和nms定義的型別不匹配,需要修改cpu_nms.pyx,如下:
cdef np.ndarray[np.int64_t, ndim=1] order = scores.argsort()[::-1]
或參考官方問題解決:
將編譯輸出的caffe目錄拷貝到caffe-fast-rcnn\python目錄下面。
可以嘗試通過執行caffe程式,以確保caffe依賴庫都已經拷貝進來。
第二步:修改demo
修改tool\demo.py中物種:CLASSES = ('__background__',
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
為自己的物種: 如
CLASSES = ('__background__',
'flags')
訓練模型修改:
NETS = {'vgg16': ('VGG16',
'vgg16_fast_rcnn_iter_40000.caffemodel'),
'vgg_cnn_m_1024': ('VGG_CNN_M_1024',
'vgg_cnn_m_1024_faster_rcnn_iter_40000.caffemodel'),
'zf': ('ZF',
'ZF_faster_rcnn_final.caffemodel')}
第三步:修改測試模型
和訓練模型修改是一樣的,按照官方要求進行修改: It looks like you have 3 classes.In the train.prototxt and test.prototxt files that you're using,
you'll need to change num_output from 21 to 3 in the cls_score layer and from 84 to 12 in the bbox_pred layer.
You'll also need to change num_classes from 21 to 3 in the Python layer that provides data to the net (the very first layer). 將訓練好的模型拷貝到指定位置, 執行python tools\demo.py --cpu --net vgg_cnn_m_1024即可輸出檢測的結果:
在使用訓練的模型測試的時候沒有任何輸出時就需要考慮Linux環境和Windows環境的差別了,此處我遇到過這種情況之一是由於模型載入不完整導致的該問題,具體修改可以參考如下:
修改內容:在io.cpp中增加windows的支援:
#ifdef _MSC_VER
int fd = open(filename, O_RDONLY|O_BINARY);
#else
int fd = open(filename, O_RDONLY);
#endif