tf-faster-rcnn實戰(zhàn)篇

準備篇:
TensorFlow:首先查看TensorFlow的版本,代碼支持的是1.2的版本:

import tensorflow as tf
print tf.__version__
1.1.0

因此卸載并安裝指定版本Tensorflow:

pip install -I tensorflow==1.2.1

安裝:
1、下載tf-faster-rcnn代碼:

git clone https://github.com/endernewton/tf-faster-rcnn.git

2、到tf-faster-rcnn/lib下編譯Cython 模塊:
需要注意:首先根據(jù)GPU的型號來修改計算能力(Architecture), 官網(wǎng)提供了5種模型對應(yīng)的計算能力值,我的機子是Tesla K40,所以這里修改sm_52為sm_35,然后執(zhí)行下面代碼進行編譯,否則去重框會出問題

CUDA.jpeg

cd tf-faster-rcnn/lib
make clean
make
cd ..

3、安裝Python COCO API:

cd data
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
make
cd ../../..

4、下載模型

./data/scripts/fetch_faster_rcnn_models.sh

5、使用預(yù)訓(xùn)練模型進行測試

./tools/demo.py

但是GPU上跑測試時用ssh進行遠程解釋器繪圖時出錯:RuntimeError: Invalid DISPLAY variable, 這時需要修改demo.py文件:

import matplotlib.pyplot as plt
plt.switch_backend('agg')

上述是GPU版本測試的方法,如果是基于CPU,還需要對以下幾個.py文件進行改動:
a. tf-faster-rcnn/lib/model/nms_wrapper.py:

from model.config import cfg
#from nms.gpu_nms import gpu_nms
from nms.cpu_nms import cpu_nms

def nms(dets, thresh, force_cpu=False):
  """Dispatch to either CPU or GPU NMS implementations."""

  if dets.shape[0] == 0:
    return []
  return cpu_nms(dets, thresh)
  # if cfg.USE_GPU_NMS and not force_cpu:
  #   return gpu_nms(dets, thresh, device_id=0)
  # else:
  #   return cpu_nms(dets, thresh)

b. tf-faster-rcnn/lib/model/config.py: 注釋以下代碼

__C.USE_GPU_NMS = False

c. tf-faster-rcnn/lib/setup.py: 注釋以下語句

CUDA = locate_cuda()
self.src_extensions.append('.cu')
Extension('nms.gpu_nms',
        ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
        library_dirs=[CUDA['lib64']],
        libraries=['cudart'],
        language='c++',
        runtime_library_dirs=[CUDA['lib64']],
        # this syntax is specific to this build system
        # we're only going to use certain compiler args with nvcc and not with gcc
        # the implementation of this trick is in customize_compiler() below
        extra_compile_args={'gcc': ["-Wno-unused-function"],
                            'nvcc': ['-arch=sm_52',
                                     '--ptxas-options=-v',
                                     '-c',
                                     '--compiler-options',
                                     "'-fPIC'"]},
        include_dirs = [numpy_include, CUDA['include']]

4、運行./tools/demo.py就可以看到結(jié)果啦!
5、當(dāng)然如果想看到自己的圖片的檢測結(jié)果,將圖片放在tf-faster-rcnn/data/demo下,修改源文件demo.py,在im_names下面添加圖片的名稱即可。

im_names = ['000456.jpg', '000542.jpg', '001150.jpg',
                '001763.jpg', '004545.jpg']   

訓(xùn)練:
1、準備訓(xùn)練數(shù)據(jù):數(shù)據(jù)集需要參考VOC2007的數(shù)據(jù)集格式,主要包括三個部分:
JPEGImages:存放用來訓(xùn)練的原始圖像,圖片編號要以6為數(shù)字命名,例如000034.jpg,圖片要是JPEG/JPG格式的,圖片的長寬比(width/height)要在0.462-6.828之間;
Annotations :存放原始圖像中的Object的坐標信息,一個訓(xùn)練圖片對應(yīng)Annotations下的一個同名的XML文件;
ImageSets/Main :指定用來train,trainval,val和test的圖片的編號,因為VOC的數(shù)據(jù)集可以做很多的CV任務(wù),比如Object detection, Semantic segementation, Edge detection等,所以Imageset下有幾個子文件夾(Layout, Main, Segementation),修改下Main下的文件 (train.txt, trainval.txt, val.txt, test.txt),里面寫上想要進行任務(wù)的圖片的編號
將上述你的數(shù)據(jù)集放在tf-faster-rcnn/data/VOCdevkit2007/VOC2007下面,替換原始VOC2007的JPEGIMages,Imagesets,Annotations,這里也可以直接更換文件夾名稱。
2、為訓(xùn)練數(shù)據(jù)創(chuàng)建軟連接
3、修改源代碼:tf-faster-rcnn/experiments/train_faster+rcnn.sh和tf-faster-rcnn/lib/datasets/pascal_voc.py文件
4、運行下面命令開始訓(xùn)練,下次訓(xùn)練之前,需要將data/cache和output(輸出的model存放的位置,不訓(xùn)練此文件夾沒有)兩個文件夾刪除。

ln -s /Users/steven/data/Trainingdata Trainingdata
./experiments/scripts/test_faster_rcnn.sh 0 TrainData res101

tf-faster-rcnn的工程目錄進行簡單介紹:
data: 存放數(shù)據(jù),以及讀取文件的cache;
experiments: 存放配置文件以及運行的log文件,配置文件
lib: python接口
output: 輸出的model存放的位置,不訓(xùn)練此文件夾沒有
tensorboard: 可視化部分
tools: 訓(xùn)練和測試的python文件

下面幾篇是caffe框架下faster-rcnn的相關(guān)博客: http://www.cnblogs.com/dudumiaomiao/p/6556111.html
http://blog.csdn.net/u012841667/article/details/69555074
http://blog.csdn.net/samylee/article/details/51201744
http://blog.csdn.net/Gavin__Zhou/article/details/52052915
http://blog.csdn.net/sinat_30071459/article/details/51332084

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容