如何才能將Faster R-CNN訓練起來?
首先進入 Faster RCNN 的官網啦,即:https://github.com/rbgirshick/py-faster-rcnn#installation-sufficient-for-the-demo
先用提供的 model 自己測試一下效果嘛。。。
按照官網安裝教程,安裝基本需求。
Installation (sufficient for the demo)
-
Clone the Faster R-CNN repository
# Make sure to clone with --recursive git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git -
We'll call the directory that you cloned Faster R-CNN into
FRCN_ROOTIgnore notes 1 and 2 if you followed step 1 above.
Note 1: If you didn't clone Faster R-CNN with the
--recursiveflag, then you'll need to manually clone thecaffe-fast-rcnnsubmodule:git submodule update --init --recursiveNote 2: The
caffe-fast-rcnnsubmodule needs to be on thefaster-rcnnbranch (or equivalent detached state). This will happen automatically if you followed step 1 instructions. -
Build the Cython modules
cd $FRCN_ROOT/lib make -
Build Caffe and pycaffe
cd $FRCN_ROOT/caffe-fast-rcnn # Now follow the Caffe installation instructions here: # http://caffe.berkeleyvision.org/installation.html # If you're experienced with Caffe and have all of the requirements installed # and your Makefile.config in place, then simply do: make -j8 && make pycaffe -
Download pre-computed Faster R-CNN detectors
cd $FRCN_ROOT ./data/scripts/fetch_faster_rcnn_models.shThis will populate the
$FRCN_ROOT/datafolder withfaster_rcnn_models. Seedata/README.mdfor details. These models were trained on VOC 2007 trainval.
Demo
After successfully completing basic installation, you'll be ready to run the demo.
To run the demo
cd $FRCN_ROOT ./tools/demo.py
The demo performs detection using a VGG16 network trained for detection on PASCAL VOC 2007.
你自己要下載一個 caffe-master,因為這個檢測方法就是調用 caffe的啦,自己下載了,然后放到(注意,是拷貝內容):
./py-faster-rcnn-master/caffe-fast-rcnn/
然而,情況總是不太順利啊,遇到了如下問題:
執行 demo 的過程中,遇到的首個問題是:
wangxiao@GTX980:~/Desktop/py-faster-rcnn-master$ ./tools/demo.py
Traceback (most recent call last):
File "./tools/demo.py", line 18, in <module>
from fast_rcnn.test import im_detect
File "/home/wangxiao/Desktop/py-faster-rcnn-master/tools/../lib/fast_rcnn/test.py", line 16, in <module>
import caffe
File "/home/wangxiao/Desktop/py-faster-rcnn-master/tools/../caffe-fast-rcnn/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
File "/home/wangxiao/Desktop/py-faster-rcnn-master/tools/../caffe-fast-rcnn/python/caffe/pycaffe.py", line 15, in <module>
import caffe.io
File "/home/wangxiao/Desktop/py-faster-rcnn-master/tools/../caffe-fast-rcnn/python/caffe/io.py", line 2, in <module>
import skimage.io
ImportError: No module named skimage.io
wangxiao@GTX980:~/Desktop/py-faster-rcnn-master$
解決方法是: sudo easy_install -U scikit-image
那么,就這樣完了?? 噢,no,並不是:
wangxiao@GTX980:~/Desktop/py-faster-rcnn-master/tools$ ./demo.py
/usr/local/lib/python2.7/dist-packages/matplotlib-2.0.0b3-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:279: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Traceback (most recent call last):
File "./demo.py", line 127, in <module>
'fetch_faster_rcnn_models.sh?').format(caffemodel))
IOError: /home/wangxiao/Desktop/py-faster-rcnn-master/data/faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel not found.
Did you run ./data/script/fetch_faster_rcnn_models.sh?
因為你測試的時候,沒有提供給他測試的 model啊,ok,執行這句吧,下載一個模型:
./data/script/fetch_faster_rcnn_models.sh
速度還是有點慢的,耐心等候啊,少年!


然后,
wangxiao@GTX980:~/Desktop/py-faster-rcnn-master/tools$ ./demo.py
WARNING: Logging before InitGoogleLogging() is written to STDERR
W0729 10:57:41.107311 7997 _caffe.cpp:122] DEPRECATION WARNING - deprecated use of Python interface
W0729 10:57:41.107381 7997 _caffe.cpp:123] Use this instead (with the named "weights" parameter):
W0729 10:57:41.107398 7997 _caffe.cpp:125] Net('/home/wangxiao/Desktop/py-faster-rcnn-master/models/pascal_voc/VGG16/faster_rcnn_alt_opt/faster_rcnn_test.pt', 1, weights='/home/wangxiao/Desktop/py-faster-rcnn-master/data/faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel')
[libprotobuf ERROR google/protobuf/text_format.cc:245] Error parsing text-format caffe.NetParameter: 350:21: Message type "caffe.LayerParameter" has no field named "roi_pooling_param".
F0729 10:57:41.132531 7997 upgrade_proto.cpp:79] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: /home/wangxiao/Desktop/py-faster-rcnn-master/models/pascal_voc/VGG16/faster_rcnn_alt_opt/faster_rcnn_test.pt
*** Check failure stack trace: ***
Aborted
wangxiao@GTX980:~/Desktop/py-faster-rcnn-master/tools$
我更新了一下系統,但是提示我無法更新啊。。。
wangxiao@GTX980:~$ sudo apt-get update
[sudo] password for wangxiao:
E: Type 'Reading' is not known on line 1 in source list /etc/apt/sources.list.d/bazel.list
E: The list of sources could not be read.
進去這個路徑,將這個文件刪掉,即可,即:sudo rm /etc/apt/sources.list.d/bazel.list
之前出現各種蛋疼的情況主要是因為:不能自己從caffe官網下的那個caffe來用,而應該是從作者自己的 github上來下。
然后加到對應的文件夾進行編譯,就不會出現這些問題了。
至於訓練的話,有兩種方式,一種是: end-to-end,即:
在終端執行:選擇 GPU-0,網絡為: ZF 數據集為: pascal_voc
./experiments/scripts/faster_rcnn_end2end.sh 0 ZF pascal_voc
那么,就可以跑了 。。。
不像 matlab版本那么蛋疼,跑一下,就會自動關閉matlab一次,╮(╯▽╰)╭ 傷不起 。。。
這是最終跑出來的結果:mAP 是:59.09 %
Saving cached annotations to /home/wangxiao/Desktop/py-faster-rcnn-master/data/VOCdevkit2007/annotations_cache/annots.pkl
AP for aeroplane = 0.6043
AP for bicycle = 0.7138
AP for bird = 0.5504
AP for boat = 0.4721
AP for bottle = 0.3404
AP for bus = 0.6633
AP for car = 0.7369
AP for cat = 0.7021
AP for chair = 0.3631
AP for cow = 0.6086
AP for diningtable = 0.6191
AP for dog = 0.6469
AP for horse = 0.7789
AP for motorbike = 0.6782
AP for person = 0.6489
AP for pottedplant = 0.3073
AP for sheep = 0.5715
AP for sofa = 0.5015
AP for train = 0.7026
AP for tvmonitor = 0.6089
Mean AP = 0.5909
~~~~~~~~
Results:
0.604
0.714
0.550
0.472
0.340
0.663
0.737
0.702
0.363
0.609
0.619
0.647
0.779
0.678
0.649
0.307
0.571
0.502
0.703
0.609
0.591
~~~~~~~~
--------------------------------------------------------------
Results computed with the **unofficial** Python eval code.
Results should be very close to the official MATLAB eval code.
Recompute with `./tools/reval.py --matlab ...` for your paper.
-- Thanks, The Management
--------------------------------------------------------------
real 5m25.471s
user 5m11.620s
sys 0m35.848s
Matlab 版本碰到過如下的問題:
1. 找不到 ZF文件夾下面的 test.prototxt 文件:

fast_rcnn startup done
GPU 1: free memory 3786178560
Use GPU 1
Error using CHECK_FILE_EXIST (line 4)
/home/wangxiao/Desktop/matlab-faster_rcnn-master/experiments/models/rpn_prototxts/ZF/test.prototxt does not exist
Error in caffe.get_net (line 21)
CHECK_FILE_EXIST(model_file);
Error in caffe.Net (line 31)
self = caffe.get_net(varargin{:});
Error in proposal_calc_output_size (line 10)
caffe_net = caffe.Net(test_net_def_file, 'test');
Error in script_faster_rcnn_VOC2007_ZF>proposal_prepare_anchors (line 91)
[output_width_map, output_height_map] ...
Error in script_faster_rcnn_VOC2007_ZF (line 41)
[conf_proposal.anchors, conf_proposal.output_width_map, conf_proposal.output_height_map] ...
解決方案是:沒有下載那個文件,囧,下載地址:https://onedrive.live.com/download?resid=36FEC490FBC32F1A!113&authkey=!AIzdm0sD_SmhUQ4&ithint=file%2czip
2. 將上一個問題搞定后,有出現新的,這個有點摸不着頭腦了:

fast_rcnn startup done
GPU 1: free memory 3761143808
Use GPU 1
No appropriate method, property, or field reshape_as_input for class caffe.Net.
Error in proposal_calc_output_size (line 28)
caffe_net.reshape_as_input(net_inputs);
Error in script_faster_rcnn_VOC2007_ZF>proposal_prepare_anchors (line 91)
[output_width_map, output_height_map] ...
Error in script_faster_rcnn_VOC2007_ZF (line 41)
[conf_proposal.anchors, conf_proposal.output_width_map, conf_proposal.output_height_map]
...
解決方法是:可能是你 caffe 版本不對,重新從 faster rcnn 官網上下載,然后編譯 。。。
3. 更蛋疼的是這個問題,表面上看,正在運行,但是 這個時候你看看終端上的顯示:


File not found: ./models/rpn_prototxts/ZF/train_vali.prototxt
但是,這個文件明明就放在那里啊。。。無語。。。
解決方案是:由於下載的model中,關於 prototxt文件中路徑的問題,請改為絕對路徑,如下圖所示,且用”/“作為分隔符,因為下載了的分隔符是不對的,所以,一直找不到這個文件,哎,坑啊。。。。

搞完之后,就可以繼續正常的運行了。。。
fast_rcnn startup done
GPU 1: free memory 3676987392
Use GPU 1
Warning: Specified caffe folder
(/home/wangxiao/Documents/faster_rcnn-master/experiments/external/caffe/matlab/caffe_faster_rcnn) is not exist,
change to default one (/home/wangxiao/Documents/faster_rcnn-master/experiments/external/caffe/matlab)
> In active_caffe_mex at 19
In script_faster_rcnn_VOC2007_ZF at 17
Cleared 0 solvers and 1 stand-alone nets
***************
stage one proposal
***************
conf:
batch_size: 256
bg_thresh_hi: 0.3000
bg_thresh_lo: 0
bg_weight: 1
drop_boxes_runoff_image: 1
feat_stride: 16
fg_fraction: 0.5000
fg_thresh: 0.7000
image_means: [224x224x3 single]
ims_per_batch: 1
max_size: 1000
rng_seed: 6
scales: 600
target_only_gt: 1
test_binary: 0
test_drop_boxes_runoff_image: 0
test_max_size: 1000
test_min_box_size: 16
test_nms: 0.3000
test_scales: 600
use_flipped: 1
use_gpu: 1
anchors: [9x4 double]
output_width_map: [901x1 containers.Map]
output_height_map: [901x1 containers.Map]
opts:
cache_name: 'faster_rcnn_VOC2007_ZF_stage1_rpn'
conf: [1x1 struct]
do_val: 1
imdb_train: {[1x1 struct]}
imdb_val: [1x1 struct]
net_file: '/home/wangxiao/Documents/faster_rcnn-master/experiments/models/pre_trained_models/ZF/ZF...'
roidb_train: {[1x1 struct]}
roidb_val: [1x1 struct]
snapshot_interval: 10000
solver_def_file: '/home/wangxiao/Documents/faster_rcnn-master/experiments/models/rpn_prototxts/ZF/solver_...'
val_interval: 2000
val_iters: 500
Preparing training data...Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
Done.
Preparing validation data...Done.
------------------------- Iteration 2000 -------------------------
Training : err_fg 0.521, err_bg 0.0187, loss (cls 0.242 + reg 0.0218)
Testing : err_fg 0.487, err_bg 0.0131, loss (cls 0.203 + reg 0.0167)
------------------------- Iteration 4000 -------------------------
Training : err_fg 0.495, err_bg 0.0187, loss (cls 0.209 + reg 0.0206)
Testing : err_fg 0.458, err_bg 0.0143, loss (cls 0.182 + reg 0.0158)
------------------------- Iteration 6000 -------------------------
Training : err_fg 0.462, err_bg 0.0205, loss (cls 0.196 + reg 0.0189)
Testing : err_fg 0.409, err_bg 0.0229, loss (cls 0.177 + reg 0.0184)
------------------------- Iteration 8000 -------------------------
Training : err_fg 0.46, err_bg 0.0196, loss (cls 0.19 + reg 0.0194)
Testing : err_fg 0.404, err_bg 0.0163, loss (cls 0.165 + reg 0.0151)
------------------------- Iteration 10000 -------------------------
Training : err_fg 0.437, err_bg 0.0202, loss (cls 0.179 + reg 0.0189)
Testing : err_fg 0.413, err_bg 0.0154, loss (cls 0.168 + reg 0.0158)
Saved as /home/wangxiao/Documents/faster_rcnn-master/experiments/output/rpn_cachedir/faster_rcnn_VOC2007_ZF_stage1_rpn/voc_2007_trainval/iter_10000
4. Faster RCNN 運行出現問題:
fast_rcnn startup done
Error using gpuDevice (line 26)
An unexpected error occurred during CUDA execution. The CUDA error was:
cannot set while device is active in this process
Error in auto_select_gpu (line 15)
g = gpuDevice(i);
Error in script_faster_rcnn_VOC2007_ZF (line 16)
opts.gpu_id = auto_select_gpu;

答:如果之前正常運行,突然出現這個問題,重啟你的 matlab,然后以 sudo 開啟,重新跑一下就沒事了。。。
5. 下面是今天重新跑 faster RCNN 遇到的一個問題:
解決方案是:
數據集的名字或者文件夾不存在,額,將數據集加上就行了...
6. 之前一直很納悶,script_faster_rcnn_VOC0712_VGG16.m 這個腳本的數據集該怎么設置? 是分別單獨設置兩個文件夾,還是將其放到一個文件夾中???
看到這個代碼,我終於恍然大悟 ...

7.
