用自己的數(shù)據(jù)集在R-FCN框架下進行檢測

這個教程默認代碼、預訓練模型、測試部分的代碼已經(jīng)下載好,環(huán)境也已經(jīng)編譯好

【訓練】

  • models
    models文件夾下存放的是對應的預訓練的模型在訓練時的網(wǎng)絡參數(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    #初始學習率
lr_policy: "step"    #學習率更新的方式
gamma: 0.1           #學習率衰減的倍率
stepsize: 10000      #迭代一萬次,學習率衰減為原來的0.1倍
display: 20    #每迭代20次在終端打印一次輸出

momentum: 0.9     #動量
weight_decay: 0.0005      #權重衰減率
# 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   #由于是做檢測任務,batch_size值較小
#debug_info: true

根據(jù)以上代碼的第一行我們知道接下來要修改train_agnostic_ohem.prototxt 這里著重講一下,訓練可以選擇兩種方式,一種帶ohem,一種不帶ohem;我在訓練時用的是前者,所以修改的都是文件名里有ohem的

(2)修改train_agnostic_ohem.prototxt
** 兩處數(shù)據(jù)層、兩處卷積層、 對應的兩處cls和bbox參數(shù)層(大家會發(fā)現(xiàn)每個prototxt修改的內容大致相同)
以后需要調整anchor大小來做不同的實驗時,也是在這個文件里改,詳情見下篇博客。

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: 1
        pad: 0
        weight_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: 1
        pad: 0
        weight_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.0625
        output_dim: 2  #cls_num
        group_size: 7
    }
}
layer {
    bottom: "rfcn_bbox"
    bottom: "rois"
    top: "psroipooled_loc_rois"
    name: "psroipooled_loc_rois"
    type: "PSROIPooling"
    psroi_pooling_param {
        spatial_scale: 0.0625
        output_dim: 8 #4*cls_num
        group_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: 1
        pad: 0
        weight_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: 1
        pad: 0
        weight_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.0625
        output_dim: 2  #cls_num
        group_size: 7
    }
}
layer {
    bottom: "rfcn_bbox"
    bottom: "rois"
    top: "psroipooled_loc_rois"
    name: "psroipooled_loc_rois"
    type: "PSROIPooling"
    psroi_pooling_param {
        spatial_scale: 0.0625
        output_dim: 8  #4*cls_num
        group_size: 7
    }
}
layer {
    name: "cls_prob_reshape"
    type: "Reshape"
    bottom: "cls_prob_pre"
    top: "cls_prob"
    reshape_param {
        shape {
            dim: -1
            dim: 2  #cls_num
        }
    }
}
layer {
    name: "bbox_pred_reshape"
    type: "Reshape"
    bottom: "bbox_pred_pre"
    top: "bbox_pred"
    reshape_param {
        shape {
            dim: -1
            dim: 8  #4*cls_num
        }
    }
}

看代碼的內容,沒用到classware/下面的參數(shù)文件,暫不需要修改。

  • tools
    訓練用的train_net.py
    測試用的test_net.py

  • data
    /VOCdevkit 存放數(shù)據(jù)集的信息:包括
    (1)/ImageSets/Main下存放train.txt、val.txt、test.txt,內容為圖片的名稱(不含后綴)
    (2)/fish_image_hz存放所有的圖片
    (3)/Annotations 存放所有圖片的標注信息xml
    (4)/dataset下存放測試要用到的東西
    /dataset/devkit
    /rfcn_models 訓練生成的caffemodel復制粘貼到此文件夾下,為了測試的時候讀取模型
    /imagenet_models 預訓練的模型,譬如ResNet-101-model.caffemodel可從網(wǎng)上下載
    /cache緩存,如若換數(shù)據(jù)集,里面的pkl文件要刪掉

  • output
    output文件夾下存放的是訓練所生成的caffemodel,為了以防程序中斷,我們設置了每迭代一萬次備份一次已訓練好的模型。

  • lib
    用來存放一些python接口文件,如其下的datasets主要負責數(shù)據(jù)庫讀取,config負責cnn一些訓練的配置選項。
    復制已有的datasets/pascal_voc.py,但要修改成自己使用的數(shù)據(jù)集的python文件。譬如datasets/fish4knowledge.py 具體修改的地方:

if __name__ == '__main__':
    from datasets.fish4knowledge import fish4knowledge
    d = 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

image.png
  • caffe
    這里是caffe框架目錄,要事先編譯一遍。

  • experiments
    輸出的日志文件在logs文件夾下
    訓練時運行的腳本在scripts文件夾下,可以用end2end或者alt_opt兩種方式訓練。rfcn_end2end_ohem.sh腳本需要根據(jù)使用的數(shù)據(jù)集做改動。

./expriments/scripts/rfcn_end2end_ohem.sh 0 ResNet-101 fish4knowledge train_net.py

在這個腳本里可以設置迭代次數(shù) "ITERS=***",“0”代表用的GPU_id號,ResNet-101代表使用的預訓練模型,fish4knowledge代表使用的數(shù)據(jù)集名稱,需要在上面的腳本里添加自己的數(shù)據(jù)集case進去,train_net.py是訓練網(wǎng)絡的python代碼,位于/tools文件夾下。

  • results
    測試生成的val_pred.txt在這個文件夾下。接下來我們就講測試部分了。

【測試】

***這些評測算法的代碼可以參考PASCAL_VOC的評估代碼
我就是下載下來再根據(jù)自己的數(shù)據(jù)集作修改的。
1.訓練生成的模型在output文件夾里,復制粘貼到data/rfcn_models/目錄下
2.修改tools/文件夾下的demo_rfcn.py并運行

python demo_rfcn.py --gpu 0

在results/文件夾下會生成val_pred.txt;val_pred.txt文件的每一行對應一個檢測到的目標,格式如下:

<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為算法對于這一預測的置信度,xmin ymin為目標框左上角點坐標,xmax ymax為目標框右下角點坐標。
3.用于評測算法的MATLAB程序位于devkit/evaluation/eval_detection.m。將第二步生成的val_pred.txt復制粘貼到devkit/data/目錄下,運行.m程序之后可得知檢測的mAP值。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容