用自己的数据集在R-FCN框架下进行检测
這個教程默認(rèn)代碼、預(yù)訓(xùn)練模型、測試部分的代碼已經(jīng)下載好,環(huán)境也已經(jīng)編譯好
【訓(xùn)練】
- models
models文件夾下存放的是對應(yīng)的預(yù)訓(xùn)練的模型在訓(xùn)練時的網(wǎng)絡(luò)參數(shù)。
以ResNet-101為例:
注意:下面的cls_num指的是數(shù)據(jù)集的類別數(shù)+1(背景)。比如我想檢測魚這一個類別,cls_num=2.
(1)修改其中的solver_ohem.protoxt
根據(jù)以上代碼的第一行我們知道接下來要修改train_agnostic_ohem.prototxt 這里著重講一下,訓(xùn)練可以選擇兩種方式,一種帶ohem,一種不帶ohem;我在訓(xùn)練時用的是前者,所以修改的都是文件名里有ohem的
(2)修改train_agnostic_ohem.prototxt
** 兩處數(shù)據(jù)層、兩處卷積層、 對應(yīng)的兩處cls和bbox參數(shù)層(大家會發(fā)現(xiàn)每個prototxt修改的內(nèi)容大致相同)
以后需要調(diào)整anchor大小來做不同的實(shí)驗(yàn)時,也是在這個文件里改,詳情見下篇博客。
(3)修改test_agnostic.prototxt
layer {bottom: "conv_new_1"top: "rfcn_cls"name: "rfcn_cls"type: "Convolution"convolution_param {num_output: 98 #cls_num*(score_maps_size^2)kernel_size: 1pad: 0weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}}param {lr_mult: 1.0}param {lr_mult: 2.0} }layer {bottom: "conv_new_1"top: "rfcn_bbox"name: "rfcn_bbox"type: "Convolution"convolution_param {num_output: 392 #4*cls_num*(score_maps_size^2)kernel_size: 1pad: 0weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}}param {lr_mult: 1.0}param {lr_mult: 2.0} }layer {bottom: "rfcn_cls"bottom: "rois"top: "psroipooled_cls_rois"name: "psroipooled_cls_rois"type: "PSROIPooling"psroi_pooling_param {spatial_scale: 0.0625output_dim: 2 #cls_numgroup_size: 7} }layer {bottom: "rfcn_bbox"bottom: "rois"top: "psroipooled_loc_rois"name: "psroipooled_loc_rois"type: "PSROIPooling"psroi_pooling_param {spatial_scale: 0.0625output_dim: 8 #4*cls_numgroup_size: 7} }layer {name: "cls_prob_reshape"type: "Reshape"bottom: "cls_prob_pre"top: "cls_prob"reshape_param {shape {dim: -1dim: 2 #cls_num}} }layer {name: "bbox_pred_reshape"type: "Reshape"bottom: "bbox_pred_pre"top: "bbox_pred"reshape_param {shape {dim: -1dim: 8 #4*cls_num}} }看代碼的內(nèi)容,沒用到classware/下面的參數(shù)文件,暫不需要修改。
tools
訓(xùn)練用的train_net.py
測試用的test_net.pydata
/VOCdevkit 存放數(shù)據(jù)集的信息:包括
(1)/ImageSets/Main下存放train.txt、val.txt、test.txt,內(nèi)容為圖片的名稱(不含后綴)
(2)/fish_image_hz存放所有的圖片
(3)/Annotations 存放所有圖片的標(biāo)注信息xml
(4)/dataset下存放測試要用到的東西
/dataset/devkit
/rfcn_models 訓(xùn)練生成的caffemodel復(fù)制粘貼到此文件夾下,為了測試的時候讀取模型
/imagenet_models 預(yù)訓(xùn)練的模型,譬如ResNet-101-model.caffemodel可從網(wǎng)上下載
/cache緩存,如若換數(shù)據(jù)集,里面的pkl文件要刪掉output
output文件夾下存放的是訓(xùn)練所生成的caffemodel,為了以防程序中斷,我們設(shè)置了每迭代一萬次備份一次已訓(xùn)練好的模型。- lib
用來存放一些python接口文件,如其下的datasets主要負(fù)責(zé)數(shù)據(jù)庫讀取,config負(fù)責(zé)cnn一些訓(xùn)練的配置選項(xiàng)。
復(fù)制已有的datasets/pascal_voc.py,但要修改成自己使用的數(shù)據(jù)集的python文件。譬如datasets/fish4knowledge.py 具體修改的地方:
除此以外,還有data_path,存放圖片名稱的路徑等等,自己看代碼來修改。
datasets/factory.py 修改的地方有:
from datasets.fish4knowledge import fish4knowledge devkit = '存放數(shù)據(jù)集的路徑' for split in ['train', 'val']:name = 'fish4knowledge_{}'.format(split)__sets[name] = (lambda imageset=split, devkit=devkit: fish4knowledge(imageset, devkit))datasets/_ init _.py修改成:
from .fish4knowledge import knowledgefast_rcnn/config.py
caffe
這里是caffe框架目錄,要事先編譯一遍。experiments
輸出的日志文件在logs文件夾下
訓(xùn)練時運(yùn)行的腳本在scripts文件夾下,可以用end2end或者alt_opt兩種方式訓(xùn)練。rfcn_end2end_ohem.sh腳本需要根據(jù)使用的數(shù)據(jù)集做改動。./expriments/scripts/rfcn_end2end_ohem.sh 0 ResNet-101 fish4knowledge train_net.py
在這個腳本里可以設(shè)置迭代次數(shù) “ITERS=*“,“0”代表用的GPU_id號,ResNet-101代表使用的預(yù)訓(xùn)練模型,fish4knowledge代表使用的數(shù)據(jù)集名稱,需要在上面的腳本里添加自己的數(shù)據(jù)集case進(jìn)去,train_net.py是訓(xùn)練網(wǎng)絡(luò)的python代碼,位于/tools文件夾下。
- results
測試生成的val_pred.txt在這個文件夾下。接下來我們就講測試部分了。
【測試】
*這些評測算法的代碼可以參考PASCAL_VOC的評估代碼
我就是下載下來再根據(jù)自己的數(shù)據(jù)集作修改的。
1.訓(xùn)練生成的模型在output文件夾里,復(fù)制粘貼到data/rfcn_models/目錄下
2.修改tools/文件夾下的demo_rfcn.py并運(yùn)行
“`
python demo_rfcn.py –gpu 0
““
在results/文件夾下會生成val_pred.txt;val_pred.txt文件的每一行對應(yīng)一個檢測到的目標(biāo),格式如下:
其中image_id為測試圖片的id號即幀數(shù)(列于devkit/data/val.txt文件中,這個txt文件區(qū)別于上面的ImageSets/Main/下的txt,每行除了包括圖片文件名(不含后綴),還有幀數(shù)), class_id 為物體的種類(參見devkit/data/meta_data.mat(需要修改)), confidence為算法對于這一預(yù)測的置信度,xmin ymin為目標(biāo)框左上角點(diǎn)坐標(biāo),xmax ymax為目標(biāo)框右下角點(diǎn)坐標(biāo)。
3.用于評測算法的MATLAB程序位于devkit/evaluation/eval_detection.m。將第二步生成的val_pred.txt復(fù)制粘貼到devkit/data/目錄下,運(yùn)行.m程序之后可得知檢測的mAP值。
總結(jié)
以上是生活随笔為你收集整理的用自己的数据集在R-FCN框架下进行检测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于深度学习的图像语义分割技术概述之4常
- 下一篇: OpenCV —数据持久化: FileS