Faster R-CNN教程
轉(zhuǎn)載自:Faster R-CNN教程 - CarryPotMan的博客 - 博客頻道 - CSDN.NET http://blog.csdn.net/u012891472/article/details/51282809
本教程主要基于Python版本的faster R-CNN,因為python layer的使用,這個版本會比matlab的版本速度慢10%,但是準確率應(yīng)該是差不多的。
目前已經(jīng)實現(xiàn)的有兩種方式:
推薦使用第二種,因為第二種使用的顯存更小,而且訓(xùn)練會更快,同時準確率差不多甚至略高一點。
Contents
配置環(huán)境
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
訓(xùn)練自己的訓(xùn)練集
工程目錄簡介
首先工程的根目錄簡單的稱為 FRCN_ROOT,可以看到根目錄下有以下幾個文件夾
- caffe-fast-rcnn
這里是caffe框架目錄
- data
用來存放pretrained模型,比如imagenet上的,以及讀取文件的cache緩存
- experiments
存放配置文件以及運行的log文件,另外這個目錄下有scripts可以用end2end或者alt_opt兩種方式訓(xùn)練。
- lib
用來存放一些python接口文件,如其下的datasets主要負責(zé)數(shù)據(jù)庫讀取,我們接下來的文件都是放在這個目錄下的;config負責(zé)cnn一些訓(xùn)練的配置選項,建議根據(jù)自己的需要在experiment/cfgs/faster_rcnn_end2end.yml文件中進行設(shè)置覆蓋config.py中的設(shè)置。
- models
里面存放了三個模型文件,小型網(wǎng)絡(luò)的ZF,大型網(wǎng)絡(luò)VGG16,中型網(wǎng)絡(luò)VGG_CNN_M_1024。推薦使用VGG16,如果使用端到端的approximate joint training方法,開啟CuDNN,只需要3G的顯存即可。
- output
這里存放的是訓(xùn)練完成后的輸出目錄,默認會在faster_rcnn_end2end文件夾下
- tools
里面存放的是訓(xùn)練和測試的Python文件。
創(chuàng)建數(shù)據(jù)集
接下來我們就要創(chuàng)建自己的數(shù)據(jù)集了,這部分主要在lib目錄里操作。這里下面存在3個目錄:
- datasets
在這里修改讀寫數(shù)據(jù)的接口主要是datasets目錄下
- fast_rcnn
主要存放的是python的訓(xùn)練和測試腳本,以及訓(xùn)練的配置文件config.py
- nms
做非極大抑制的部分,有g(shù)pu和cpu兩種實現(xiàn)方式
- roi_data_layer
主要是一些ROI處理操作
- rpn
這就是RPN的核心代碼部分,有生成proposals和anchor的方法
-
transform
-
utils
1構(gòu)建自己的IMDB子類
1.1文件概述?
可有看到datasets目錄下主要有三個文件,分別是
- factory.py
- imdb.py
- pascal_voc.py
factory.py 是個工廠類,用類生成imdb類并且返回數(shù)據(jù)庫共網(wǎng)絡(luò)訓(xùn)練和測試使用;imdb.py 這里是數(shù)據(jù)庫讀寫類的基類,封裝了許多db的操作,但是具體的一些文件讀寫需要繼承繼續(xù)讀寫;pascal_voc.py Ross在這里用pascal_voc.py這個類來操作。
1.2讀取文件函數(shù)分析?
接下來我來介紹一下pasca_voc.py這個文件,我們主要是基于這個文件進行修改,里面有幾個重要的函數(shù)需要修改
- def?init(self, image_set, year, devkit_path=None)?
這個是初始化函數(shù),它對應(yīng)著的是pascal_voc的數(shù)據(jù)集訪問格式,其實我們將其接口修改的更簡單一點。 - def image_path_at(self, i)?
根據(jù)第i個圖像樣本返回其對應(yīng)的path,其調(diào)用了image_path_from_index(self, index)作為其具體實現(xiàn) - def image_path_from_index(self, index)?
實現(xiàn)了 image_path的具體功能 - def _load_image_set_index(self)?
加載了樣本的list文件 - def _get_default_path(self)?
獲得數(shù)據(jù)集地址 - def gt_roidb(self)?
讀取并返回ground_truth的db - def selective_search_roidb?
讀取并返回ROI的db,這個是fast rcnn用的,faster版本的不用管這個函數(shù)。 - def _load_selective_search_roidb(self, gt_roidb)?
加載預(yù)選框的文件 - def selective_search_IJCV_roidb(self)?
在這里調(diào)用讀取Ground_truth和ROI db并將db合并 - def _load_selective_search_IJCV_roidb(self, gt_roidb)?
這里是專門讀取作者在IJCV上用的dataset - def?_load_pascal_annotation(self, index)?
這個函數(shù)是讀取gt的具體實現(xiàn) - def _write_voc_results_file(self, all_boxes)?
voc的檢測結(jié)果寫入到文件 - def _do_matlab_eval(self, comp_id, output_dir=’output’)?
根據(jù)matlab的evluation接口來做結(jié)果的分析 - def evaluate_detections?
其調(diào)用了_do_matlab_eval - def competition_mode?
設(shè)置competitoin_mode,加了一些噪點
1.3訓(xùn)練數(shù)據(jù)格式
在我的檢測任務(wù)里,我主要是在SED數(shù)據(jù)集上做行人檢測,因此我這里只有background 和person 兩類物體,為了操作方便,我像pascal_voc數(shù)據(jù)集里面一樣每個圖像用一個xml來標注。如果大家不知道怎么生成xml文件,可以用這個工具?labelImg?
這里我要特別提醒一下大家,一定要注意坐標格式,一定要注意坐標格式,一定要注意坐標格式,重要的事情說三遍!!!要不然你會犯很多錯誤都會是因為坐標不一致引起的報錯。
1.4修改讀取接口?
這里是原始的pascal_voc的init函數(shù),在這里,由于我們自己的數(shù)據(jù)集往往比voc的數(shù)據(jù)集要更簡單的一些,在作者代碼里面用了很多的路徑拼接,我們不用去迎合他的格式,將這些操作簡單化即可,在這里我會一一列舉每個我修改過的函數(shù)。這里按照文件中的順序排列。
修改后的初始化函數(shù):
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):
這個函數(shù)里有個生成ground truth的文件,我需要特別說明一下,如果你再次訓(xùn)練的時候修改了數(shù)據(jù)庫,比如添加或者刪除了一些樣本,但是你的數(shù)據(jù)庫名字函數(shù)原來那個,必須要在data/cache/目錄下把數(shù)據(jù)庫的緩存文件.pkl給刪除掉,否則其不會重新讀取相應(yīng)的數(shù)據(jù)庫,而是直接從之前讀入然后緩存的pkl文件中讀取進來,這樣修改的數(shù)據(jù)庫并沒有進入網(wǎng)絡(luò),而是加載了老版本的數(shù)據(jù)。
修改的_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格式,所以這個函數(shù)我的改動不多。如果你想用txt文件保存ground truth,做出相應(yīng)的修改即可。?
想采用txt方式存儲的童鞋,可以參考文末博客的寫法。?
坐標的順序強調(diào)一下,要左上右下,并且x1必須要小于x2,這個是基本,反了會在坐標水平變換的時候會出錯,坐標從0開始,如果已經(jīng)是0,則不需要再-1。如果怕出錯,可以直接把出界的的直接置0.
記得在最后的main下面也修改相應(yīng)的路徑
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,在這里我們已經(jīng)完成了整個的讀取接口的改寫。
2修改factory.py?
當網(wǎng)絡(luò)訓(xùn)練時會調(diào)用factory里面的get方法獲得相應(yīng)的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
訓(xùn)練和檢測
1.預(yù)訓(xùn)練模型介紹?
首先在data目錄下,有兩個目錄
-
faster_rcnn_models/
-
imagenet_models/
faster_rcnn_model文件夾下面是作者用faster rcnn訓(xùn)練好的三個網(wǎng)絡(luò),分別對應(yīng)著小、中、大型網(wǎng)絡(luò),大家可以試用一下這幾個網(wǎng)絡(luò),看一些檢測效果,他們訓(xùn)練都迭代了80000次,數(shù)據(jù)集都是pascal_voc的數(shù)據(jù)集。
imagenet_model文件夾下面是在Imagenet上訓(xùn)練好的通用模型,在這里用來初始化網(wǎng)絡(luò)的參數(shù).
在這里我比較推薦大型網(wǎng)絡(luò),訓(xùn)練也挺快的,差不多25小時(titan black 6G)。?
還有一個比較奇怪的現(xiàn)象,開啟CuDNN一般情況是可以加速的,但是在訓(xùn)練ZF模型的時候,開啟CuDNN反而會特別慢,所以大家如果訓(xùn)練特別慢,可以嘗試關(guān)掉CuDNN。
2.修改模型文件配置?
模型文件在models下面對應(yīng)的網(wǎng)絡(luò)文件夾下,在這里我用大型網(wǎng)絡(luò)的配置文件修改為例子?
比如:我的檢測目標物是person ,那么我的類別就有兩個類別即 background 和 person?
因此,首先打開網(wǎng)絡(luò)的模型文件夾,打開train.prototxt?
修改的地方重要有三個?
分別是這幾個地方
測試的時候,test.prototxt也需要做相應(yīng)的修改。
OK,如果你要進一步修改網(wǎng)絡(luò)訓(xùn)練中的學(xué)習(xí)速率,步長,gamma值,以及輸出模型的名字,需要在同目錄下的solver.prototxt中修改。
3.啟動Fast RCNN網(wǎng)絡(luò)訓(xùn)練
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
參數(shù)講解:
-
這里的–是兩個-,不要輸錯
-
train_net.py是網(wǎng)絡(luò)的訓(xùn)練文件,之后的參數(shù)都是附帶的輸入?yún)?shù)
-
–gpu 代表機器上的GPU編號,如果是nvidia系列的tesla顯卡,可以在終端中輸入nvidia-smi來查看當前的顯卡負荷,選擇合適的顯卡
-
–solver 代表模型的配置文件,train.prototxt的文件路徑已經(jīng)包含在這個文件之中
-
–weights 代表初始化的權(quán)重文件,這里用的是Imagenet上預(yù)訓(xùn)練好的模型,大型的網(wǎng)絡(luò)我們選擇用VGG16.v2.caffemodel
-
–imdb 這里給出的訓(xùn)練的數(shù)據(jù)庫名字需要在factory.py的_sets中,我在文件里面有_sets[‘hs’],train_net.py這個文件會調(diào)用factory.py再生成hs這個類,來讀取數(shù)據(jù)
4.啟動Fast RCNN網(wǎng)絡(luò)檢測?
可以參考tools下面的demo.py 文件,來做檢測,并且將檢測的坐標結(jié)果輸出到相應(yīng)的txt文件中。
總結(jié)
以上是生活随笔為你收集整理的Faster R-CNN教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: faster rcnn可视化(修改dem
- 下一篇: Caffe的Solver参数设置