用faster-rcnn训练自己的数据集(VOC2007格式,python版)
用faster-rcnn訓練自己的數據集(VOC2007格式,python版)
一. 配置caffe環境
ubunt16.04下caffe環境安裝
二. 下載,編譯及測試py-faster-rcnn源碼
(一)下載源碼
github鏈接
或者執行 git clone –recursive https://github.com/rbgirshick/py-faster-rcnn.git
注意加上–recursive關鍵字
(二)編譯源碼
編譯過程中可能會出現缺失一些python模塊,按提示安裝
(1)編譯Cython模塊
cd $FRCN_ROOT/lib make(2)修改Markfile配置
參考ubunt16.04下caffe環境安裝
中修改Makefile.config
(3)編譯python接口
cd $FRCN_ROOT/caffe-fast-rcnn make -j8 多核編譯,時間較長 make pycaffe(4)下載訓練好的VGG16和ZF模型
cd $FRCN_ROOT ./data/scripts/fetch_faster_rcnn_models.sh時間太長的話可以考慮找網上別人分享的資源
(三)測試源碼
cd $FRCN_ROOT ./tool/demo.py三. 使用faster-rcnn訓練自己的數據集
(一)下載預訓練參數及模型
cd $FRCN_ROOT ./data/scripts/fetch_imagenet_models.sh ./data/scripts/fetch_selective_search_data.sh(二)制作數據集
制作數據集(VOC2007格式)
將制作好的VOC2007文件夾放置在data/VOCdevkit2007文件夾下,沒有則新建VOCdevkit2007文件夾
(三)修改配置文件
(1)修改py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt和stage2_fast_rcnn_train.pt 兩個文件
備注:3處修改及其附近的代碼
name: "ZF" layer {name: 'data'type: 'Python'top: 'data' top: 'rois' top: 'labels'top: 'bbox_targets'top: 'bbox_inside_weights' top: 'bbox_outside_weights' python_param {module: 'roi_data_layer.layer'layer: 'RoIDataLayer'param_str: "'num_classes': 2" #按訓練集類別改,該值為類別數+1 } }layer {name: "cls_score"type: "InnerProduct"bottom: "fc7"top: "cls_score"param { lr_mult: 1.0 }param { lr_mult: 2.0 } inner_product_param {num_output: 2 #按訓練集類別改,該值為類別數+1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} }layer {name: "bbox_pred"type: "InnerProduct"bottom: "fc7" top: "bbox_pred"param { lr_mult: 1.0 }param { lr_mult: 2.0 }inner_product_param {num_output: 8 #按訓練集類別改,該值為(類別數+1)*4weight_filler {type: "gaussian"std: 0.001}bias_filler {type: "constant"value: 0}} }(2)修改py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt和stage2_rpn_train.pt 兩個文件
備注:1處修改及其附近的代碼
layer {name: 'input-data'type: 'Python'top: 'data'top: 'im_info'top: 'gt_boxes'python_param {module: 'roi_data_layer.layer'layer: 'RoIDataLayer'param_str: "'num_classes': 2" #按訓練集類別改,該值為類別數+1} }(3)修改py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt文件
備注:2處修改及其附近的代碼
layer {name: "cls_score"type: "InnerProduct"bottom: "fc7"top: "cls_score"param { lr_mult: 1.0 }param { lr_mult: 2.0 }inner_product_param {num_output: 2 #按訓練集類別改,該值為類別數+1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} }layer {name: "bbox_pred"type: "InnerProduct"bottom: "fc7"top: "bbox_pred"param { lr_mult: 1.0 }param { lr_mult: 2.0 }inner_product_param {num_output: 8 #按訓練集類別改,該值為(類別數+1)*4weight_filler {type: "gaussian"std: 0.001}bias_filler {type: "constant"value: 0}} }(4)修改py-faster-rcnn/lib/datasets/pascal_voc.py
self._classes = ('__background__', # always index 0'你的標簽1','你的標簽2',你的標簽3','你的標簽4')注:如果只是在原始檢測的20種類別:'aeroplane', 'bicycle', 'bird', 'boat','bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse','motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'中檢測單一類別,可參考修改下面的代碼:def _load_image_set_index(self):"""Load the indexes listed in this dataset's image set file."""# Example path to image set file:# self._devkit_path + /VOCdevkit2007/VOC2007/ImageSets/Main/val.txtimage_set_file = os.path.join(self._data_path, 'ImageSets', 'Main',self._image_set + '.txt')assert os.path.exists(image_set_file), \'Path does not exist: {}'.format(image_set_file)with open(image_set_file) as f:image_index = [x.strip() for x in f.readlines()]注:如果需要在原始的20類別只檢測車輛的話才需要修改這部分代碼.# only load index with cars objnew_image_index = []for index in image_index:filename = os.path.join(self._data_path, 'Annotations', index + '.xml')tree = ET.parse(filename)objs = tree.findall('object')num_objs = 0for ix, obj in enumerate(objs):curr_name = obj.find('name').text.lower().strip()if curr_name == 'car':num_objs += 1breakif num_objs > 0:new_image_index.append(index)return new_image_indexdef _load_pascal_annotation(self, index):"""Load image and bounding boxes info from XML file in the PASCAL VOCformat."""filename = os.path.join(self._data_path, 'Annotations', index + '.xml')tree = ET.parse(filename)objs = tree.findall('object')if not self.config['use_diff']:# Exclude the samples labeled as difficultnon_diff_objs = [obj for obj in objs if int(obj.find('difficult').text) == 0]# if len(non_diff_objs) != len(objs):# print 'Removed {} difficult objects'.format(# len(objs) - len(non_diff_objs))objs = non_diff_objs注:如果需要在原始的20類別只檢測車輛的話才需要修改這部分代碼.# change num objs , only read car# num_objs = len(objs)num_objs = 0for ix, obj in enumerate(objs):curr_name = obj.find('name').text.lower().strip()if curr_name == 'car':num_objs += 1boxes = np.zeros((num_objs, 4), dtype=np.uint16)gt_classes = np.zeros((num_objs), dtype=np.int32)overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)# "Seg" area for pascal is just the box areaseg_areas = np.zeros((num_objs), dtype=np.float32)#注:如果需要在原始的20類別只檢測車輛的話才需要修改這部分代碼 # Load object bounding boxes into a data frame.tmp_ix = 0for ix, obj in enumerate(objs):bbox = obj.find('bndbox')# Make pixel indexes 0-basedx1 = float(bbox.find('xmin').text) - 1y1 = float(bbox.find('ymin').text) - 1x2 = float(bbox.find('xmax').text) - 1y2 = float(bbox.find('ymax').text) - 1curr_name = obj.find('name').text.lower().strip()if curr_name != 'car':continuecls = self._class_to_ind[curr_name]boxes[tmp_ix, :] = [x1, y1, x2, y2]gt_classes[tmp_ix] = clsoverlaps[tmp_ix, cls] = 1.0seg_areas[tmp_ix] = (x2 - x1 + 1) * (y2 - y1 + 1)tmp_ix += 1overlaps = scipy.sparse.csr_matrix(overlaps)return {'boxes' : boxes,'gt_classes': gt_classes,'gt_overlaps' : overlaps,'flipped' : False,'seg_areas' : seg_areas}(4)py-faster-rcnn/lib/datasets/imdb.py修改
def append_flipped_images(self):num_images = self.num_imageswidths = [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 - 1boxes[:, 2] = widths[i] - oldx1 - 1for b in range(len(boxes)):if boxes[b][2] < boxes[b][0]:boxes[b][0] = 0assert (boxes[:, 2] >= boxes[:, 0]).all()(5)py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py修改迭代次數(建議修改)
max_iters=[8000,4000,8000,4000] 建議:第一次訓練使用較低的迭代次數,先確保能正常訓練,如max_iters=[8,4,8,4]訓練分別為4個階段(rpn第1階段,fast rcnn第1階段,rpn第2階段,fast rcnn第2階段)的迭代次數??筛某赡阆M牡螖怠?
如果改了這些數值,最好把py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt里對應的solver文件(有4個)也修改,stepsize小于上面修改的數值,stepsize的意義是經過stepsize次的迭代后降低一次學習率(非必要修改)。
(6)刪除緩存文件(每次修改配置文件后訓練都要做)
刪除py-faster-rcnn文件夾下所有的.pyc文件及data文件夾下的cache文件夾, data/VOCdekit2007下的annotations_cache文件夾(最近一次成功訓練的 annotation和當前annotation一樣的話這部分可以不刪,否則可以正常訓練, 但是最后評價模型會出錯)(四)開始訓練
cd $FRCN_ROOT ./experiments/scripts/faster_rcnn_alt_opt.sh?0?ZF?pascal_voc成功訓練后在py-faster-rcnn/output/faster_rcnn_alt_opt/voc_2007_trainval文件夾下
會有以final.caffemodel結尾的模型文件,一般為ZF_faster_rcnn_final.caffemodel
成功訓練后會有一次模型性能的評估測試,成功的話會有MAP指標和平均MAP指標的輸出,類似下文,
訓練日志文件保存在experiments/logs文件夾下.
(五)測試訓練結果
(1)修改py-faster-rcnn\tools\demo.py
CLASSES = ('__background__','你的標簽1','你的標簽2',你的標簽3','你的標簽4')NETS = {'vgg16': ('VGG16','VGG16_faster_rcnn_final.caffemodel'),'zf': ('ZF','ZF_faster_rcnn_final.caffemodel')}im_names = os.listdir(os.path.join(cfg.DATA_DIR, 'demo'))??(2)放置模型及測試圖片
將訓練得到的py-faster-rcnn\output\faster_rcnn_alt_opt\***_trainval中 ZF的final.caffemodel拷貝至py-faster-rcnn\data\faster_rcnn_models測試圖片放在py-faster-rcnn\data\demo(與上面demo.py設置路徑有關,可修改)(3)進行測試
cd $FRCN_ROOT ./tool/demo.py四. 曾出現過的bug及當時的解決方法
(1) 訓練時出現KeyError:’max_overlaps’ ,解決方法:刪除data文件夾下的cache文件夾
(2) 訓練結束后測試時出現類似
File "/home/hyzhan/py-faster-rcnn/tools/../lib/datasets/voc_eval.py", line 126, in voc_evalR = [obj for obj in recs[imagename] if obj['name'] == classname] KeyError: '000002'解決方法: 刪除data/VOCdekit2007下的annotations_cache文件夾
(3) caffe-fast-rcnn編譯時出現找不到nvcc命令的情況,解決方法:
export PATH=/usr/local/cuda-8.0/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH將cuda安裝路徑添加到環境變量中
(4) caffe-fast-rcnn編譯時出現類似找不到opencv命令的情況,解決方法,添加環境變量:
export LD_LIBRARY_PATH=/home/hyzhan/software/opencv3/lib:$LD_LIBRARY_PATH(5) 訓練的時候執行”./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc”語句進行訓練會出現找不到faster_rcnn_alt_opt.sh文件的情況,解決方法:重新手打命令
(6) 測試之前需要修改tool文件夾下的demo或者mydemo里面的class類別,不然會顯示上次訓練的類別
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的用faster-rcnn训练自己的数据集(VOC2007格式,python版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Faster R-CNN: Toward
- 下一篇: 深度学习中的激活函数与梯度消失