Faster R-CNN教程
轉載自:Faster R-CNN教程 - CarryPotMan的博客 - 博客頻道 - CSDN.NET http://blog.csdn.net/u012891472/article/details/51282809
本教程主要基于Python版本的faster R-CNN,因為python layer的使用,這個版本會比matlab的版本速度慢10%,但是準確率應該是差不多的。
目前已經實現的有兩種方式:
推薦使用第二種,因為第二種使用的顯存更小,而且訓練會更快,同時準確率差不多甚至略高一點。
Contents
配置環境
1配置python layers
#In your Makefile.config, make sure to have this line uncommented WITH_PYTHON_LAYER := 1 # Unrelatedly, it's also recommended that you use CUDNN USE_CUDNN := 1- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
2安裝幾個依賴cython, python-OpenCV, easydict
sudo apt-get install python-opencv sudo pip install cython easydict- 1
- 2
- 1
- 2
安裝步驟
1克隆工程
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git- 1
- 1
2編譯Cython模塊
cd $FRCN_ROOT/lib make- 1
- 2
- 1
- 2
3編譯caffe和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- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Demo
安裝步驟完成后,就可以運行一下demo了。
cd $FRCN_ROOT ./tools/demo.py- 1
- 2
- 1
- 2
訓練自己的訓練集
工程目錄簡介
首先工程的根目錄簡單的稱為 FRCN_ROOT,可以看到根目錄下有以下幾個文件夾
- caffe-fast-rcnn
這里是caffe框架目錄
- data
用來存放pretrained模型,比如imagenet上的,以及讀取文件的cache緩存
- experiments
存放配置文件以及運行的log文件,另外這個目錄下有scripts可以用end2end或者alt_opt兩種方式訓練。
- lib
用來存放一些python接口文件,如其下的datasets主要負責數據庫讀取,我們接下來的文件都是放在這個目錄下的;config負責cnn一些訓練的配置選項,建議根據自己的需要在experiment/cfgs/faster_rcnn_end2end.yml文件中進行設置覆蓋config.py中的設置。
- models
里面存放了三個模型文件,小型網絡的ZF,大型網絡VGG16,中型網絡VGG_CNN_M_1024。推薦使用VGG16,如果使用端到端的approximate joint training方法,開啟CuDNN,只需要3G的顯存即可。
- output
這里存放的是訓練完成后的輸出目錄,默認會在faster_rcnn_end2end文件夾下
- tools
里面存放的是訓練和測試的Python文件。
創建數據集
接下來我們就要創建自己的數據集了,這部分主要在lib目錄里操作。這里下面存在3個目錄:
- datasets
在這里修改讀寫數據的接口主要是datasets目錄下
- fast_rcnn
主要存放的是python的訓練和測試腳本,以及訓練的配置文件config.py
- nms
做非極大抑制的部分,有gpu和cpu兩種實現方式
- roi_data_layer
主要是一些ROI處理操作
- rpn
這就是RPN的核心代碼部分,有生成proposals和anchor的方法
-
transform
-
utils
1構建自己的IMDB子類
1.1文件概述?
可有看到datasets目錄下主要有三個文件,分別是
- factory.py
- imdb.py
- pascal_voc.py
factory.py 是個工廠類,用類生成imdb類并且返回數據庫共網絡訓練和測試使用;imdb.py 這里是數據庫讀寫類的基類,封裝了許多db的操作,但是具體的一些文件讀寫需要繼承繼續讀寫;pascal_voc.py Ross在這里用pascal_voc.py這個類來操作。
1.2讀取文件函數分析?
接下來我來介紹一下pasca_voc.py這個文件,我們主要是基于這個文件進行修改,里面有幾個重要的函數需要修改
- def?init(self, image_set, year, devkit_path=None)?
這個是初始化函數,它對應著的是pascal_voc的數據集訪問格式,其實我們將其接口修改的更簡單一點。 - def image_path_at(self, i)?
根據第i個圖像樣本返回其對應的path,其調用了image_path_from_index(self, index)作為其具體實現 - def image_path_from_index(self, index)?
實現了 image_path的具體功能 - def _load_image_set_index(self)?
加載了樣本的list文件 - def _get_default_path(self)?
獲得數據集地址 - def gt_roidb(self)?
讀取并返回ground_truth的db - def selective_search_roidb?
讀取并返回ROI的db,這個是fast rcnn用的,faster版本的不用管這個函數。 - def _load_selective_search_roidb(self, gt_roidb)?
加載預選框的文件 - def selective_search_IJCV_roidb(self)?
在這里調用讀取Ground_truth和ROI db并將db合并 - def _load_selective_search_IJCV_roidb(self, gt_roidb)?
這里是專門讀取作者在IJCV上用的dataset - def?_load_pascal_annotation(self, index)?
這個函數是讀取gt的具體實現 - def _write_voc_results_file(self, all_boxes)?
voc的檢測結果寫入到文件 - def _do_matlab_eval(self, comp_id, output_dir=’output’)?
根據matlab的evluation接口來做結果的分析 - def evaluate_detections?
其調用了_do_matlab_eval - def competition_mode?
設置competitoin_mode,加了一些噪點
1.3訓練數據格式
在我的檢測任務里,我主要是在SED數據集上做行人檢測,因此我這里只有background 和person 兩類物體,為了操作方便,我像pascal_voc數據集里面一樣每個圖像用一個xml來標注。如果大家不知道怎么生成xml文件,可以用這個工具?labelImg?
這里我要特別提醒一下大家,一定要注意坐標格式,一定要注意坐標格式,一定要注意坐標格式,重要的事情說三遍!!!要不然你會犯很多錯誤都會是因為坐標不一致引起的報錯。
1.4修改讀取接口?
這里是原始的pascal_voc的init函數,在這里,由于我們自己的數據集往往比voc的數據集要更簡單的一些,在作者代碼里面用了很多的路徑拼接,我們不用去迎合他的格式,將這些操作簡單化即可,在這里我會一一列舉每個我修改過的函數。這里按照文件中的順序排列。
修改后的初始化函數:
class hs(imdb):def __init__(self, image_set, devkit_path=None): # modifiedimdb.__init__(self, image_set)self._image_set = image_setself._devkit_path = devkit_path #datasets路徑self._data_path = os.path.join(self._devkit_path,image_set) #圖片文件夾路徑self._classes = ('__background__', # always index 0'person') #two classesself._class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) # form the dict{'__background__':'0','person':'1'}self._image_ext = '.jpg'self._image_index = self._load_image_set_index('ImageList.txt')# Default to roidb handlerself._roidb_handler = self.selective_search_roidbself._salt = str(uuid.uuid4())self._comp_id = 'comp4'# PASCAL specific config optionsself.config = {'cleanup' : True,'use_salt' : True,'use_diff' : False,'matlab_eval' : False,'rpn_file' : None,'min_size' : 16} #小于16個像素的框扔掉assert os.path.exists(self._devkit_path), \'VOCdevkit path does not exist: {}'.format(self._devkit_path)assert os.path.exists(self._data_path), \'Path does not exist: {}'.format(self._data_path)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
修改后的image_path_from_index:
def image_path_from_index(self, index): #modified"""Construct an image path from the image's "index" identifier."""image_path = os.path.join(self._data_path,index +'.jpg')assert os.path.exists(image_path), \'Path does not exist: {}'.format(image_path)return image_path- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
修改后的_load_image_set_index:
def _load_image_set_index(self,imagelist): # modified"""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._devkit_path, imagelist)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()]return image_index- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
gt_roidb(self):
這個函數里有個生成ground truth的文件,我需要特別說明一下,如果你再次訓練的時候修改了數據庫,比如添加或者刪除了一些樣本,但是你的數據庫名字函數原來那個,必須要在data/cache/目錄下把數據庫的緩存文件.pkl給刪除掉,否則其不會重新讀取相應的數據庫,而是直接從之前讀入然后緩存的pkl文件中讀取進來,這樣修改的數據庫并沒有進入網絡,而是加載了老版本的數據。
修改的_load_pascal_annotation(self, index):
def _load_pascal_annotation(self, index): #modified"""Load image and bounding boxes info from XML file in the PASCAL VOCformat."""filename = os.path.join(self._devkit_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_objsnum_objs = len(objs)boxes = 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)# Load object bounding boxes into a data frame.for ix, obj in enumerate(objs):bbox = obj.find('bndbox')# Make pixel indexes 0-basedx1 = float(bbox.find('xmin').text)y1 = float(bbox.find('ymin').text)x2 = float(bbox.find('xmax').text)y2 = float(bbox.find('ymax').text)cls = self._class_to_ind[obj.find('name').text.lower().strip()]boxes[ix, :] = [x1, y1, x2, y2]gt_classes[ix] = clsoverlaps[ix, cls] = 1.0seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)overlaps = scipy.sparse.csr_matrix(overlaps)return {'boxes' : boxes,'gt_classes': gt_classes,'gt_overlaps' : overlaps,'flipped' : False,'seg_areas' : seg_areas}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
因為我和Pascal用了一樣的xml格式,所以這個函數我的改動不多。如果你想用txt文件保存ground truth,做出相應的修改即可。?
想采用txt方式存儲的童鞋,可以參考文末博客的寫法。?
坐標的順序強調一下,要左上右下,并且x1必須要小于x2,這個是基本,反了會在坐標水平變換的時候會出錯,坐標從0開始,如果已經是0,則不需要再-1。如果怕出錯,可以直接把出界的的直接置0.
記得在最后的main下面也修改相應的路徑
from datasets.hs import hs d = hs('hs', '/home/zyy/workspace/wangml/py-faster-rcnn/lib/datasets/') res = d.roidb from IPython import embed; embed()- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
OK,在這里我們已經完成了整個的讀取接口的改寫。
2修改factory.py?
當網絡訓練時會調用factory里面的get方法獲得相應的imdb,?
首先在文件頭import 把pascal_voc改成hs
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
訓練和檢測
1.預訓練模型介紹?
首先在data目錄下,有兩個目錄
-
faster_rcnn_models/
-
imagenet_models/
faster_rcnn_model文件夾下面是作者用faster rcnn訓練好的三個網絡,分別對應著小、中、大型網絡,大家可以試用一下這幾個網絡,看一些檢測效果,他們訓練都迭代了80000次,數據集都是pascal_voc的數據集。
imagenet_model文件夾下面是在Imagenet上訓練好的通用模型,在這里用來初始化網絡的參數.
在這里我比較推薦大型網絡,訓練也挺快的,差不多25小時(titan black 6G)。?
還有一個比較奇怪的現象,開啟CuDNN一般情況是可以加速的,但是在訓練ZF模型的時候,開啟CuDNN反而會特別慢,所以大家如果訓練特別慢,可以嘗試關掉CuDNN。
2.修改模型文件配置?
模型文件在models下面對應的網絡文件夾下,在這里我用大型網絡的配置文件修改為例子?
比如:我的檢測目標物是person ,那么我的類別就有兩個類別即 background 和 person?
因此,首先打開網絡的模型文件夾,打開train.prototxt?
修改的地方重要有三個?
分別是這幾個地方
測試的時候,test.prototxt也需要做相應的修改。
OK,如果你要進一步修改網絡訓練中的學習速率,步長,gamma值,以及輸出模型的名字,需要在同目錄下的solver.prototxt中修改。
3.啟動Fast RCNN網絡訓練
python ./tools/train_net.py --gpu 1 --solver models/hs/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/VGG16.v2.caffemodel --imdb hs --iters 80000 --cfg experiments/cfgs/faster_rcnn_end2end.yml- 1
- 1
參數講解:
-
這里的–是兩個-,不要輸錯
-
train_net.py是網絡的訓練文件,之后的參數都是附帶的輸入參數
-
–gpu 代表機器上的GPU編號,如果是nvidia系列的tesla顯卡,可以在終端中輸入nvidia-smi來查看當前的顯卡負荷,選擇合適的顯卡
-
–solver 代表模型的配置文件,train.prototxt的文件路徑已經包含在這個文件之中
-
–weights 代表初始化的權重文件,這里用的是Imagenet上預訓練好的模型,大型的網絡我們選擇用VGG16.v2.caffemodel
-
–imdb 這里給出的訓練的數據庫名字需要在factory.py的_sets中,我在文件里面有_sets[‘hs’],train_net.py這個文件會調用factory.py再生成hs這個類,來讀取數據
4.啟動Fast RCNN網絡檢測?
可以參考tools下面的demo.py 文件,來做檢測,并且將檢測的坐標結果輸出到相應的txt文件中。
總結
以上是生活随笔為你收集整理的Faster R-CNN教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: faster rcnn可视化(修改dem
- 下一篇: Caffe的Solver参数设置