【引言】 最近在用可變卷積的rfcn 模型遷移訓練本身的數據集, MSRA官方使用的MXNet框架html
環境搭建及配置:http://www.cnblogs.com/andre-ma/p/8867031.htmlpython
1.1 ~/Deformable-ConvNets/experiments/rfcn/cfgs/resnet_v1_101_voc0712_rfcn_dcn_end2end_ohem.yaml 文件中修改兩個參數 (yaml文件包含對應訓練腳本的一切配置信息和超參數)git
一個使用GPU個數 : github
gpus: '0,1' 表示用兩塊GPU訓練ubuntu
另外一個是樣本類別數:vim
NUM_CLASSES: 5 樣本類別數需 + 1 由於有背景windows
還有要修改使用的數據集:架構
image_set: 2007_trainval 單單使用VOC2007 如果 image_set: 2007_trainval+2012_trainval 則用VOC2007 和 VOC2012兩個數據集框架
1.2 修改 ~/Deformable-ConvNets/lib/dataset/pascal_voc.py 文件中的樣本標籤類別ide
self.classes = ['__background__', 'tiger_beetle', 'scarab', 'stinkbug','moth'] 和上面對應,加上背景一共爲5類
2.1 獲取數據,這個本身準備,能夠用爬蟲在互聯網上爬取,或用其餘方式收集,推薦幾個圖片爬蟲 : 百度圖片爬蟲 必應圖片爬蟲
2.2 標註圖片信息,在作目標檢測時須要coco數據集或VOC數據集,可用labelImg等圖像標註工具標註圖片:labelImg連接:https://github.com/tzutalin/labelImg
【這裏簡單介紹下VOC數據格式】 參考:http://www.javashuo.com/article/p-ncgqsmya-hz.html
VOC數據集的組織架構以下:
- VOC2007的目錄結構爲:
├── Annotations xml文件
├── ImageSets txt文件
└── JPEGImages 圖片
- ImageSets的目錄結構爲:
├── Layout
├── Main
└── Segmentation
LabelImg可在多個平臺下配置,但要注意各軟件依賴的版本號:一般是python=3.5 pyqt=4 依賴lxml
Ubuntu16.04 配置labelImg教程:https://blog.csdn.net/lightningqw/article/details/78944941 (ubuntu系統下確實是不能用中文,結果發如今windows下以上的方法能夠帶中文)
2.3 排查並刪除全部錯誤jpg格式
經過後綴名來判斷jpg格式文件雖然簡單,可是有時候會出錯,尤爲是從互聯網上獲取的圖片,其質量更是難以保證。
python的PIL工具提供對jpg格式文件的判斷方法:https://blog.csdn.net/qiyuanxiong/article/details/77943578
(ps: 刪除不合格jpg時,記得要對應刪除xml文件信息哦~)
2.3 生成4個txt
train.txt val.txt trainval.txt test.txt 四txt:生成及介紹:https://blog.csdn.net/Bankeey/article/details/76595884
train:val:test = 1: 1: 2 trainval是train和val的並集,即 train:val:trainval = 1:1:2 【訓練、驗證、測試流程詳解】
如何製做本身的VOC2007數據集:https://www.jianshu.com/p/b498a8a5a4f4
熟悉可變卷積Deformable-ConvNets 的老鐵們,可知在experiments文件夾下是不一樣模型的project
如本實驗使用rfcn模型遷移訓練,cd Deformable-ConvNets目錄,執行下面代碼,開始訓練!
python experiments/rfcn/rfcn_end2end_train_test.py --cfg experiments/rfcn/cfgs/resnet_v1_101_voc0712_rfcn_dcn_end2end_ohem.yaml
當且數據和配置文件就緒時,模型可正常訓練;但在測試時,卻報了以下異常:
Traceback (most recent call last): File "experiments/faster_rcnn/rcnn_end2end_train_test.py", line 21, in <module> test.main() File "experiments/faster_rcnn/../../faster_rcnn/test.py", line 52, in main args.vis, args.ignore_cache, args.shuffle, config.TEST.HAS_RPN, config.dataset.proposal, args.thresh, logger=logger, output_path=final_output_path) File "experiments/faster_rcnn/../../faster_rcnn/function/test_rcnn.py", line 72, in test_rcnn pred_eval(predictor, test_data, imdb, cfg, vis=vis, ignore_cache=ignore_cache, thresh=thresh, logger=logger) File "experiments/faster_rcnn/../../faster_rcnn/core/tester.py", line 169, in pred_eval info_str = imdb.evaluate_detections(all_boxes) File "experiments/faster_rcnn/../../faster_rcnn/../lib/dataset/pascal_voc.py", line 258, in evaluate_detections self.write_pascal_results(detections) File "experiments/faster_rcnn/../../faster_rcnn/../lib/dataset/pascal_voc.py", line 403, in write_pascal_results dets = all_boxes[cls_ind][im_ind] IndexError: list index out of range
解決:
(1) 刪除 ./data/cache 全部內容,
cd /root/Deformable-ConvNets/data rm -rf cache/
(2) 刪除對應output中的的臨時結果文件 【假設:faster_rcnn模型出現了該問題,在output文件夾下faster_rcnn模型對應的文件夾是rcnn】
cd /root/Deformable-ConvNets/output rm -rf rcnn/
(3) 數據集的配置要錯開
vim /root/Deformable-ConvNets/experiments/rfcn/cfgs/resnet_v1_101_voc0712_rcnn_dcn_end2end_ohem.yaml
編輯以下代碼,主要是 image_set 和 test_image_set
dataset: dataset: PascalVOC dataset_path: "./data/VOCdevkit" image_set: 2007_trainval root_path: "./data" test_image_set: 2012_test proposal: rpn
使用voc2007作訓練集,voc2012作測試集,其實voc2012是voc2007的拷貝,經測試本解決方案可行
【總結】
1.用本身的數據集遷移訓練可變rfcn模型,主要核心問題是數據集,數據集要有數量和質量
2.使用可變rfcn遷移訓練,關鍵要熟悉流程,以及須要修改的文件和參數
3.遇到問題時不要慌,有時候一着急反而容易把問題複雜化,或是原本快要找到解決問題的正確方法,卻因另外一個小問題掩蓋了真正要解決的問題
anyway: 安息爲王~ :)
本文參考:https://blog.csdn.net/yiweibian/article/details/67634584