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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

R-FCN+ResNet-50用自己的数据集训练模型(python版本)

發(fā)布時(shí)間:2024/9/21 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 R-FCN+ResNet-50用自己的数据集训练模型(python版本) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

說明:

本文假設(shè)你已經(jīng)做好數(shù)據(jù)集,格式和VOC2007一致,并且Linux系統(tǒng)已經(jīng)配置好caffe所需環(huán)境(博客里教程很多),下面是訓(xùn)練的一些修改。


py-R-FCN源碼下載地址:

https://github.com/Orpine/py-R-FCN

也有Matlab版本:

https://github.com/daijifeng001/R-FCN

本文用到的是python版本。


本文主要參考https://github.com/Orpine/py-R-FCN。


準(zhǔn)備工作:

(1)配置caffe環(huán)境(網(wǎng)上找教程)

(2)安裝cython,?python-opencv,?easydict

[plain] view plaincopy
  • pip?install?cython??
  • pip?install?easydict??
  • apt-get?install?python-opencv??

  • 然后,我們就可以開始配置R-FCN了。

    1.下載py-R-FCN

    [plain] view plaincopy
  • git?clone?https://github.com/Orpine/py-R-FCN.git??
  • 下面稱你的py-R-FCN路徑為RFCN_ROOT.


    2.下載caffe

    注意,該caffe版本是微軟版本 [plain] view plaincopy
  • cd?$RFCN_ROOT??
  • git?clone?https://github.com/Microsoft/caffe.git??
  • 如果一切正常的話,python代碼會(huì)自動(dòng)添加環(huán)境變量 $RFCN_ROOT/caffe/python,否則,你需要自己添加環(huán)境變量。

    3.Build Cython

    [plain] view plaincopy
  • cd?$RFCN_ROOT/lib??
  • make??

  • 4.Build caffe和pycaffe

    [plain] view plaincopy
  • cd?$RFCN_ROOT/caffe??
  • cp?Makefile.config.example?Makefile.config??
  • 然后修改Makefile.config。caffe必須支持python層,所以WITH_PYTHON_LAYER := 1是必須的。其他配置可參考:Makefile.config
    接著: [plain] view plaincopy
  • cd?$RFCN_ROOT/caffe??
  • make?-j8?&&?make?pycaffe??
  • 如果沒有出錯(cuò),則:

    5.測試Demo

    經(jīng)過上面的工作,我們可以測試一下是否可以正常運(yùn)行。 我們需要下載作者訓(xùn)練好的模型,地址:鏈接:http://pan.baidu.com/s/1kVGy8DL 密碼:pwwg 然后將模型放在$RFCN_ROOT/data。看起來是這樣的: $RFCN_ROOT/data/rfcn_models/resnet50_rfcn_final.caffemodel $RFCN_ROOT/data/rfcn_models/resnet101_rfcn_final.caffemodel運(yùn)行: [plain] view plaincopy
  • cd?$RFCN_ROOT??
  • ./tools/demo_rfcn.py?--net?ResNet-50??



  • 6.用我們的數(shù)據(jù)集訓(xùn)練

    (1)拷貝數(shù)據(jù)集
    假設(shè)我們已經(jīng)做好數(shù)據(jù)集了,格式是和VOC2007一致,將你的數(shù)據(jù)集 拷貝到$RFCN_ROOT/data下??雌饋硎沁@樣的: $VOCdevkit0712/ # development kit $VOCdevkit/VOCcode/ # VOC utility code $VOCdevkit/VOC0712 # image sets, annotations, etc. # ... and several other directories ...如果你的文件夾名字不是VOCdevkit0712和VOC0712,修改成0712就行了。 (作者是用VOC2007和VOC2012訓(xùn)練的,所以文件夾名字帶0712。也可以修改代碼,但是那樣比較麻煩一些,修改文件夾比較簡單)
    (2)下載預(yù)訓(xùn)練模型 本文以ResNet-50為例,因此下載ResNet-50-model.caffemodel。下載地址:鏈接:http://pan.baidu.com/s/1slRHD0L 密碼:r3ki 然后將caffemodel放在$RFCN_ROOT/data/imagenet_models ?(data下沒有該文件夾就新建一個(gè))

    (3)修改模型網(wǎng)絡(luò)

    打開$RFCN_ROOT/models/pascal_voc/ResNet-50/rfcn_end2end ?(以end2end為例)
    注意:下面的cls_num指的是你數(shù)據(jù)集的類別數(shù)+1(背景)。比如我有15類,+1類背景,cls_num=16.
    <1>修改class-aware/train_ohem.prototxt [plain] view plaincopy
  • 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':?16"?#cls_num??
  • ??}??
  • }??

  • [plain] view plaincopy
  • 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':?16"?#cls_num??
  • ??}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_cls"??
  • ????name:?"rfcn_cls"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?784?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_bbox"??
  • ????name:?"rfcn_bbox"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?3136?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?16??#cls_num??
  • ????????group_size:?7??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?64?#4*cls_num??
  • ????????group_size:?7??
  • ????}??
  • }??


  • <2>修改class-aware/test.prototxt

    [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_cls"??
  • ????name:?"rfcn_cls"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?784?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_bbox"??
  • ????name:?"rfcn_bbox"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?3136?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?16??#cls_num??
  • ????????group_size:?7??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?64??#4*cls_num??
  • ????????group_size:?7??
  • ????}??
  • }??
  • [plain] view plaincopy
  • layer?{??
  • ????name:?"cls_prob_reshape"??
  • ????type:?"Reshape"??
  • ????bottom:?"cls_prob_pre"??
  • ????top:?"cls_prob"??
  • ????reshape_param?{??
  • ????????shape?{??
  • ????????????dim:?-1??
  • ????????????dim:?16??#cls_num??
  • ????????}??
  • ????}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????name:?"bbox_pred_reshape"??
  • ????type:?"Reshape"??
  • ????bottom:?"bbox_pred_pre"??
  • ????top:?"bbox_pred"??
  • ????reshape_param?{??
  • ????????shape?{??
  • ????????????dim:?-1??
  • ????????????dim:?64??#4*cls_num??
  • ????????}??
  • ????}??
  • }??

  • <3>修改train_agnostic.prototxt
    [plain] view plaincopy
  • 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':?16"??#cls_num??
  • ??}??
  • }??
  • [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_cls"??
  • ????name:?"rfcn_cls"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?784?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?16?#cls_num???###??
  • ????????group_size:?7??
  • ????}??
  • }??

  • <4>修改train_agnostic_ohem.prototxt

    [plain] view plaincopy
  • 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':?16"?#cls_num?###??
  • ??}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_cls"??
  • ????name:?"rfcn_cls"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?784?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?16?#cls_num???###??
  • ????????group_size:?7??
  • ????}??
  • }??

  • <5>修改test_agnostic.prototxt
    [plain] view plaincopy
  • layer?{??
  • ????bottom:?"conv_new_1"??
  • ????top:?"rfcn_cls"??
  • ????name:?"rfcn_cls"??
  • ????type:?"Convolution"??
  • ????convolution_param?{??
  • ????????num_output:?784?#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??
  • ????}??
  • }??

  • [plain] view plaincopy
  • 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:?16?#cls_num???###??
  • ????????group_size:?7??
  • ????}??
  • }??

  • [plain] view plaincopy
  • layer?{??
  • ????name:?"cls_prob_reshape"??
  • ????type:?"Reshape"??
  • ????bottom:?"cls_prob_pre"??
  • ????top:?"cls_prob"??
  • ????reshape_param?{??
  • ????????shape?{??
  • ????????????dim:?-1??
  • ????????????dim:?16?#cls_num???###??
  • ????????}??
  • ????}??
  • }??

  • (4)修改代碼

    <1>$RFCN/lib/datasets/pascal_voc.py
    [plain] view plaincopy
  • class?pascal_voc(imdb):??
  • ????def?__init__(self,?image_set,?year,?devkit_path=None):??
  • ????????imdb.__init__(self,?'voc_'?+?year?+?'_'?+?image_set)??
  • ????????self._year?=?year??
  • ????????self._image_set?=?image_set??
  • ????????self._devkit_path?=?self._get_default_path()?if?devkit_path?is?None?\??
  • ????????????????????????????else?devkit_path??
  • ????????self._data_path?=?os.path.join(self._devkit_path,?'VOC'?+?self._year)??
  • ????????self._classes?=?('__background__',?#?always?index?0??
  • ?????????????????????????'你的標(biāo)簽1','你的標(biāo)簽2',你的標(biāo)簽3','你的標(biāo)簽4'??
  • ??????????????????????)??
  • 改成你的數(shù)據(jù)集標(biāo)簽。
    <2>$RFCN_ROOT/lib/datasets/imdb.py
    主要是assert (boxes[:, 2] >= boxes[:, 0]).all()可能出現(xiàn)AssertionError,具體解決辦法參考: http://blog.csdn.net/xzzppp/article/details/52036794


    PS: 上面將有無ohem的prototxt都改了,但是這里訓(xùn)練用的是ohem。 另外,默認(rèn)的迭代次數(shù)很大,可以修改$RFCN\experiments\scripts\rfcn_end2end_ohem.sh: [plain] view plaincopy
  • case?$DATASET?in??
  • ??pascal_voc)??
  • ????TRAIN_IMDB="voc_0712_trainval"??
  • ????TEST_IMDB="voc_0712_test"??
  • ????PT_DIR="pascal_voc"??
  • ????ITERS=110000??

  • 修改ITERS為你想要的迭代次數(shù)即可。

    (5)開始訓(xùn)練

    [plain] view plaincopy
  • cd?$RFCN_ROOT??
  • ./experiments/scripts/rfcn_end2end_ohem.sh?0?ResNet-50?pascal_voc??

  • 正常的話,就開始迭代了:


    $RFCN_ROOT/experiments/scripts里還有一些其他的訓(xùn)練方法,也可以測試一下(經(jīng)過上面的修改,無ohem的end2end訓(xùn)練也改好了,其他訓(xùn)練方法修改的過程差不多)。

    (6)結(jié)果

    將訓(xùn)練得到的模型($RFCN_ROOT/output/rfcn_end2end_ohem/voc_0712_trainval里最后的caffemodel)拷貝到$RFCN_ROOT/data/rfcn_models下,然后打開$RFCN_ROOT/tools/demo_rfcn.py,將CLASSES修改成你的標(biāo)簽,NETS修改成你的model,im_names修改成你的測試圖片(放在data/demo下),最后: [plain] view plaincopy
  • cd?$RFCN_ROOT??
  • ./tools/demo_rfcn.py?--net?ResNet-50??

  • 我將顯示的標(biāo)簽改為了中文,修改方法參考: http://blog.csdn.net/sinat_30071459/article/details/51694037

    總結(jié)

    以上是生活随笔為你收集整理的R-FCN+ResNet-50用自己的数据集训练模型(python版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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