Faster rcnn 安裝、訓練、測試、除錯
先上個檢測效果:
(1)圖片人臉檢測+關鍵點
(2)攝像頭實時人臉+關鍵點
*************************************************************************
安裝************************************************************************
###1
解壓:py-faster-rcnn-master.zip
caffe-faster-rcnn.zip下載 解壓到 caffe-faster-rcnn
替換:
用解壓的 caffe-faster-rcnn 替換 py-faster-rcnn/caffe-faster-rcnn
###2
修改 py-faster-rcnn/caffe-faster-rcnn/Makefile.config下載參考
# USE_CUDNN := 1 (我預設是關閉的)
MATLAB_DIR、PYTHON_INCLUDE、cuda計算能力和路徑
WITH_PYTHON_LAYER := 1
###3
檢查安裝依賴項
pip install cython
sudo apt-get install python-opencv
pip install easydict
###4
編譯Cython modules
cd py-faster-rcnn/lib
make
###5
編譯 Caffe and pycaffe
cd py-faster-rcnn/caffe-fast-rcn
make -j8 && make pycaffe
###6
下載預訓練模型,解壓到 py-faster-rcnn/data
cd py-faster-rcnn/ ./data/scripts/fetch_faster_rcnn_models.sh
This will populate the `py-faster-rcnn/data` folder with `faster_rcnn_models`.
These models were trained on VOC 2007 trainval.
*************************************************************************
訓練
*************************************************************************
###1
製作資料集目錄格式
刪除:
(1)data/VOCdevkit2007/VOC2007下所有檔案
新建
在 ./data/VOCdevkit2007/VOC2007新建 Annotations;ImageSets/Main;JPEGImages說明:
Annotations: 儲存標籤txt轉換的xml檔案JPEGImages: 圖片檔案
ImageSets/Main:檔名列表(不含字尾)
訓練集: train.txt
訓練驗證集: trainval.txt
測試集: test.txt
驗證集: val.txt
#Annotations舉例:
data/VOCdevkit2007/VOC2007/Annotations/0_1_5.xml
內容格式:
<annotation>
<folder>VOC2007</folder>
<filename>0_1_5.jpg</filename>
<source>
<database>My Database</database>
<annotation>VOC2007</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>deeplearning</name>
</owner>
<size>
<width>160</width>
<height>216</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>48</xmin>
<ymin>48</ymin>
<xmax>107</xmax>
<ymax>107</ymax>
</bndbox>
</object>
</annotation>
###2
修改介面
#(1) 修改prototxt配置檔案models/pascal_voc/ZF/faster_rcnn_alt_opt資料夾下的5個檔案,分別為
stage1_rpn_train.pt、stage1_fast_rcnn_train.pt、
stage2_rpn_train.pt、stage2_fast_rcnn_train.pt和fast_rcnn_test.pt
① stage1_fast_rcnn_train.pt、stage2_fast_rcnn_train.pt
修改3個引數
num_class:2(識別1類+背景1類)
cls_score中num_output:2
bbox_pred中num_output:8
② stage1_rpn_train.pt、stage2_rpn_train.pt
修改1個引數
num_class:2(識別1類+背景1類)
③ fast_rcnn_test.pt
修改2個引數:
cls_score中num_output:2
bbox_pred中num_output:8
#(2) 修改lib/datasets/pascal_voc.py
self._classes = ('__background__', # always index 0
'people')(只有這一類)
#(3) 修改lib/datasets/imdb.py
該檔案的append_flipped_images(self)函式
widths = [PIL.Image.open(self.image_path_at(i)).size[0]
for i in xrange(num_images)]
在 boxes[:, 2] = widths[i] - oldx1 - 1下加入程式碼:
for b in range(len(boxes)):
if boxes[b][2]< boxes[b][0]:
boxes[b][0] = 0
#(4) 修改完pascal_voc.py和imdb.py後進入lib/datasets目錄下刪除原來的pascal_voc.pyc和imdb.pyc檔案,重新生成這兩個檔案,因為這兩個檔案是python編譯後的檔案,系統會直接呼叫。
終端進入lib/datasets檔案目錄輸入:
python(此處應出現python的版本)
>>>importpy_compile
>>>py_compile.compile(r'imdb.py')
>>>py_compile.compile(r'pascal_voc.py')
#(5) 刪除快取檔案
① 刪除output/
② 刪除py-faster-rcnn/data/cache中的檔案和
py-faster-rcnn/data/VOCdevkit2007/annotations_cache中的檔案刪除。
#(6) 調參
① 學習率等之類的設定:
py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt中的solve檔案設定
② 迭代次數:py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py中修改
py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt裡對應的solver檔案(有4個)也修改,stepsize小於上面修改的數值。
#(7) 訓練
./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc
*************************************************************************
測試
*************************************************************************
#(1) 訓練完成之後,將output/faster_rcnn_alt_opt/voc_2007_trainval中的最終模型ZF_faster_rcnn_final.caffemodel拷貝到data/faster_rcnn_models中。
#(2) 修改/tools/demo.py:
① CLASSES =('__background__',
'people')
② NETS ={'vgg16': ('VGG16',
'VGG16_faster_rcnn_final.caffemodel'),
'zf': ('ZF',
'ZF_faster_rcnn_final.caffemodel')}
#(3) 在訓練集圖片中找一張出來放入py-faster-rcnn/data/demo資料夾中,命名為000001.jpg。
im_names = ['000001.jpg']
for im_name in im_names:
print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
print 'Demo for data/demo/{}'.format(im_name)
demo(net, im_name)
#(4) 執行demo,即在py-faster-rcnn資料夾下終端輸入:
./tools/demo.py --net zf</span>
#(5) 或者將預設的模型改為zf:
parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
choices=NETS.keys(), default='vgg16')
修改:
default='zf'
執行:
./tools/demo.py
*************************************************************************
錯誤除錯
*************************************************************************
error 1:assert (boxes[:, 2] >= boxes[:, 0]).all()
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(self._args, *self._kwargs)
File "./tools/train_faster_rcnn_alt_opt.py", line 123, in train_rpn
roidb, imdb = get_roidb(imdb_name)
File "./tools/train_faster_rcnn_alt_opt.py", line 68, in get_roidb
roidb = get_training_roidb(imdb)
File "/home/microway/test/pytest/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 121, in get_training_roidb
imdb.append_flipped_images()
File "/home/microway/test/pytest/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 108, in append_flipped_images
assert (boxes[:, 2] >= boxes[:, 0]).all()
AssertionError
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
error1 解決辦法:
將py-faster-rcnn/lib/datasets/imdb.py中的相應程式碼改成如下程式碼即可:
def append_flipped_images(self):
num_images = self.num_images
widths = [PIL.Image.open(self.image_path_at(i)).size[0]
for i in xrange(num_images)]
for i in xrange(num_images):
boxes = self.roidb[i]['boxes'].copy()
oldx1 = boxes[:, 0].copy()
oldx2 = boxes[:, 2].copy()
boxes[:, 0] = widths[i] - oldx2 - 1
boxes[:, 2] = widths[i] - oldx1 - 1
for b in range(len(boxes)):
if boxes[b][2] < boxes[b][0]:
boxes[b][0] = 0
assert (boxes[:, 2] >= boxes[:, 0]).all()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
error 2:IndexError: list index out of range
File "./tools/train_net.py", line 85, in
roidb = get_training_roidb(imdb)
File "/usr/local/fast-rcnn/tools/../lib/fast_rcnn/train.py", line 111, in get_training_roidb
rdl_roidb.prepare_roidb(imdb)
File "/usr/local/fast-rcnn/tools/../lib/roi_data_layer/roidb.py", line 23, in prepare_roidb
roidb[i]['image'] = imdb.image_path_at(i)
IndexError: list index out of range
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
error2 解決辦法:
刪除fast-rcnn-master/data/cache/ 資料夾下的.pkl檔案,或者改名備份,重新訓練即可。