日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用自己的数据集在R-FCN框架下进行检测

發(fā)布時間:2024/9/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用自己的数据集在R-FCN框架下进行检测 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這個教程默認(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
train_net: "models/fish4knowledge/ResNet-101/rfcn_end2end/train_agnostic_ohem.prototxt" base_lr: 0.001 #初始學(xué)習(xí)率 lr_policy: "step" #學(xué)習(xí)率更新的方式 gamma: 0.1 #學(xué)習(xí)率衰減的倍率 stepsize: 10000 #迭代一萬次,學(xué)習(xí)率衰減為原來的0.1倍 display: 20 #每迭代20次在終端打印一次輸出momentum: 0.9 #動量 weight_decay: 0.0005 #權(quán)重衰減率 # We disable standard caffe solver snapshotting and implement our own snapshot # function snapshot: 0 #選擇迭代多少次保存一次中間模型 # We still use the snapshot prefix, though snapshot_prefix: "resnet101_rfcn_ohem" iter_size: 2 #由于是做檢測任務(wù),batch_size值較小 #debug_info: true

根據(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)時,也是在這個文件里改,詳情見下篇博客。

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" #cls_num} }layer {name: 'roi-data'type: 'Python'bottom: 'rpn_rois'bottom: 'gt_boxes'top: 'rois'top: 'labels'top: 'bbox_targets'top: 'bbox_inside_weights'top: 'bbox_outside_weights'python_param {module: 'rpn.proposal_target_layer'layer: 'ProposalTargetLayer'param_str: "'num_classes': 2" #cls_num} }layer {bottom: "conv_new_1"top: "rfcn_cls"name: "rfcn_cls"type: "Convolution"convolution_param {num_output: 98 # 2*(7^2) 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*2*(7^2)=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} }

(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.py

  • data
    /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 具體修改的地方:
if __name__ == '__main__':from datasets.fish4knowledge import fish4knowledged = fish4knowledge('train', '存放數(shù)據(jù)集的路徑')

除此以外,還有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 knowledge

fast_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> <class_id> <confidence> <xmin> <ymin> <xmax> <ymax>

其中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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。