本文转载自:

http://blog.csdn.net/sinat_30071459/article/details/53202977

说明:

本文假设你已经做好数据集,格式和VOC2007一致,并且Linux系统已经配置好caffe所需环境(博客里教程很多),下面是训练的一些修改。

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。

准备工作:

(1)配置caffe环境(网上找教程)

(2)安装cythonpython-OpenCVeasydict

[plain] view plaincopy
  1. pip install cython
  2. pip install easydict
  3. apt-get install python-opencv

然后,我们就可以开始配置R-FCN了。

1.下载py-R-FCN

[plain] view plaincopy
  1. git clone https://github.com/Orpine/py-R-FCN.git

下面称你的py-R-FCN路径为RFCN_ROOT.

2.下载caffe

[plain] view plaincopy
  1. cd $RFCN_ROOT
  2. git clone https://github.com/Microsoft/caffe.git

如果一切正常的话,python代码会自动添加环境变量 $RFCN_ROOT/caffe/python,否则,你需要自己添加环境变量。

3.Build Cython

[plain] view plaincopy
  1. cd $RFCN_ROOT/lib
  2. make

4.Build caffe和pycaffe

[plain] view plaincopy
  1. cd $RFCN_ROOT/caffe
  2. cp Makefile.config.example Makefile.config

然后修改Makefile.config。caffe必须支持python层,所以WITH_PYTHON_LAYER := 1是必须的。其他配置可参考:Makefile.config

接着:
[plain] view plaincopy
  1. cd $RFCN_ROOT/caffe
  2. make -j8 && make pycaffe

如果没有出错,则:

5.测试Demo

$RFCN_ROOT/data/rfcn_models/resnet50_rfcn_final.caffemodel
$RFCN_ROOT/data/rfcn_models/resnet101_rfcn_final.caffemodel

运行:

[plain] view plaincopy
  1. cd $RFCN_ROOT
  2. ./tools/demo_rfcn.py --net ResNet-50

6.用我们的数据集训练


$VOCdevkit0712/                           # development kit
$VOCdevkit/VOCcode/                   # VOC utility code
$VOCdevkit/VOC0712                    # image sets, annotations, etc.
# ... and several other directories ...

如果你的文件夹名字不是VOCdevkit0712和VOC0712,修改成0712就行了。

(作者是用VOC2007和VOC2012训练的,所以文件夹名字带0712。也可以修改代码,但是那样比较麻烦一些,修改文件夹比较简单)
(2)下载预训练模型
本文以ResNet-50为例,因此下载ResNet-50-model.caffemodel。下载地址:链接:http://pan.baidu.com/s/1slRHD0L 密码:r3ki
然后将caffemodel放在$RFCN_ROOT/data/imagenet_models  (data下没有该文件夹就新建一个)

(3)修改模型网络

[plain] view plaincopy
  1. layer {
  2. name: 'input-data'
  3. type: 'Python'
  4. top: 'data'
  5. top: 'im_info'
  6. top: 'gt_boxes'
  7. python_param {
  8. module: 'roi_data_layer.layer'
  9. layer: 'RoIDataLayer'
  10. param_str: "'num_classes': 16" #cls_num
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. name: 'roi-data'
  3. type: 'Python'
  4. bottom: 'rpn_rois'
  5. bottom: 'gt_boxes'
  6. top: 'rois'
  7. top: 'labels'
  8. top: 'bbox_targets'
  9. top: 'bbox_inside_weights'
  10. top: 'bbox_outside_weights'
  11. python_param {
  12. module: 'rpn.proposal_target_layer'
  13. layer: 'ProposalTargetLayer'
  14. param_str: "'num_classes': 16" #cls_num
  15. }
  16. }
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_cls"
  4. name: "rfcn_cls"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 784 #cls_num*(score_maps_size^2)
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_bbox"
  4. name: "rfcn_bbox"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 3136 #4*cls_num*(score_maps_size^2)
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_cls"
  3. bottom: "rois"
  4. top: "psroipooled_cls_rois"
  5. name: "psroipooled_cls_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 16  #cls_num
  10. group_size: 7
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_bbox"
  3. bottom: "rois"
  4. top: "psroipooled_loc_rois"
  5. name: "psroipooled_loc_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 64 #4*cls_num
  10. group_size: 7
  11. }
  12. }
<2>修改class-aware/test.prototxt
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_cls"
  4. name: "rfcn_cls"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 784 #cls_num*(score_maps_size^2)
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_bbox"
  4. name: "rfcn_bbox"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 3136 #4*cls_num*(score_maps_size^2)
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_cls"
  3. bottom: "rois"
  4. top: "psroipooled_cls_rois"
  5. name: "psroipooled_cls_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 16  #cls_num
  10. group_size: 7
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_bbox"
  3. bottom: "rois"
  4. top: "psroipooled_loc_rois"
  5. name: "psroipooled_loc_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 64  #4*cls_num
  10. group_size: 7
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. name: "cls_prob_reshape"
  3. type: "Reshape"
  4. bottom: "cls_prob_pre"
  5. top: "cls_prob"
  6. reshape_param {
  7. shape {
  8. dim: -1
  9. dim: 16  #cls_num
  10. }
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. name: "bbox_pred_reshape"
  3. type: "Reshape"
  4. bottom: "bbox_pred_pre"
  5. top: "bbox_pred"
  6. reshape_param {
  7. shape {
  8. dim: -1
  9. dim: 64  #4*cls_num
  10. }
  11. }
  12. }
<3>修改train_agnostic.prototxt
[plain] view plaincopy
  1. layer {
  2. name: 'input-data'
  3. type: 'Python'
  4. top: 'data'
  5. top: 'im_info'
  6. top: 'gt_boxes'
  7. python_param {
  8. module: 'roi_data_layer.layer'
  9. layer: 'RoIDataLayer'
  10. param_str: "'num_classes': 16"  #cls_num
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_cls"
  4. name: "rfcn_cls"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 784 #cls_num*(score_maps_size^2)   ###
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_cls"
  3. bottom: "rois"
  4. top: "psroipooled_cls_rois"
  5. name: "psroipooled_cls_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 16 #cls_num   ###
  10. group_size: 7
  11. }
  12. }

<4>修改train_agnostic_ohem.prototxt

[plain] view plaincopy
  1. layer {
  2. name: 'input-data'
  3. type: 'Python'
  4. top: 'data'
  5. top: 'im_info'
  6. top: 'gt_boxes'
  7. python_param {
  8. module: 'roi_data_layer.layer'
  9. layer: 'RoIDataLayer'
  10. param_str: "'num_classes': 16" #cls_num ###
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_cls"
  4. name: "rfcn_cls"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 784 #cls_num*(score_maps_size^2)   ###
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_cls"
  3. bottom: "rois"
  4. top: "psroipooled_cls_rois"
  5. name: "psroipooled_cls_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 16 #cls_num   ###
  10. group_size: 7
  11. }
  12. }

<5>修改test_agnostic.prototxt


[plain] view plaincopy
  1. layer {
  2. bottom: "conv_new_1"
  3. top: "rfcn_cls"
  4. name: "rfcn_cls"
  5. type: "Convolution"
  6. convolution_param {
  7. num_output: 784 #cls_num*(score_maps_size^2) ###
  8. kernel_size: 1
  9. pad: 0
  10. weight_filler {
  11. type: "gaussian"
  12. std: 0.01
  13. }
  14. bias_filler {
  15. type: "constant"
  16. value: 0
  17. }
  18. }
  19. param {
  20. lr_mult: 1.0
  21. }
  22. param {
  23. lr_mult: 2.0
  24. }
  25. }
[plain] view plaincopy
  1. layer {
  2. bottom: "rfcn_cls"
  3. bottom: "rois"
  4. top: "psroipooled_cls_rois"
  5. name: "psroipooled_cls_rois"
  6. type: "PSROIPooling"
  7. psroi_pooling_param {
  8. spatial_scale: 0.0625
  9. output_dim: 16 #cls_num   ###
  10. group_size: 7
  11. }
  12. }
[plain] view plaincopy
  1. layer {
  2. name: "cls_prob_reshape"
  3. type: "Reshape"
  4. bottom: "cls_prob_pre"
  5. top: "cls_prob"
  6. reshape_param {
  7. shape {
  8. dim: -1
  9. dim: 16 #cls_num   ###
  10. }
  11. }
  12. }

(4)修改代码

[plain] view plaincopy
  1. class pascal_voc(imdb):
  2. def __init__(self, image_set, year, devkit_path=None):
  3. imdb.__init__(self, 'voc_' + year + '_' + image_set)
  4. self._year = year
  5. self._image_set = image_set
  6. self._devkit_path = self._get_default_path() if devkit_path is None \
  7. else devkit_path
  8. self._data_path = os.path.join(self._devkit_path, 'VOC' + self._year)
  9. self._classes = ('__background__', # always index 0
  10. '你的标签1','你的标签2',你的标签3','你的标签4'
  11. )

改成你的数据集标签。

<2>$RFCN_ROOT/lib/datasets/imdb.py
主要是assert (boxes[:, 2] >= boxes[:, 0]).all()可能出现AssertionError,具体解决办法参考:
http://blog.csdn.net/xzzppp/article/details/52036794
PS:
上面将有无ohem的prototxt都改了,但是这里训练用的是ohem。
另外,默认的迭代次数很大,可以修改$RFCN\experiments\scripts\rfcn_end2end_ohem.sh:
[plain] view plaincopy
  1. case $DATASET in
  2. pascal_voc)
  3. TRAIN_IMDB="voc_0712_trainval"
  4. TEST_IMDB="voc_0712_test"
  5. PT_DIR="pascal_voc"
  6. ITERS=110000

修改ITERS为你想要的迭代次数即可。

(5)开始训练

[plain] view plaincopy
  1. cd $RFCN_ROOT
  2. ./experiments/scripts/rfcn_end2end_ohem.sh 0 ResNet-50 pascal_voc
正常的话,就开始迭代了:

$RFCN_ROOT/experiments/scripts里还有一些其他的训练方法,也可以测试一下(经过上面的修改,无ohem的end2end训练也改好了,其他训练方法修改的过程差不多)。

(6)结果

[plain] view plaincopy
  1. cd $RFCN_ROOT
  2. ./tools/demo_rfcn.py --net ResNet-50

我将显示的标签改为了中文,修改方法参考:http://blog.csdn.net/sinat_30071459/article/details/51694037

R-FCN+ResNet-50 训练模型相关推荐

  1. MXNet预训练模型下载 ResNet 50 101

    imagenet11k resnet-50-symbol.json resnet-50-0000.params resnet-101-symbol.json resnet-101-0000.param ...

  2. Resnet 50 完整可跑代码 pytorch

    import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision ...

  3. Resnet 50 残差网络

    1.简述 resnet50是何凯明提出,能有效解决深度网络退化问题的一种结构,将输入的多重非线性变化拟合变成了拟合输入与输出的残差,变为恒等映射,50即50层 膜拜巨神:https://github. ...

  4. resnet预训练模型_干货 | NLP中的十个预训练模型

    Word2vec Fasttext ULMFit Glove Cove ELMO GPT1.0 GPT2.0 BERT Flair Embedding 一.Word2vec 1.word2vec种语言 ...

  5. resnet 50 网络分析

  6. Pytorch Resnet预训练模型参数地址

    'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',  'resnet34': 'https://downl ...

  7. 用R做heatmap示例:NBA联盟50位顶级球员的指标表现

    用R做heatmap示例:NBA联盟50位顶级球员的指标表现 虽然 heatmap 只是 R 中一个很普通的图形函数,但这个例子使用了2008-2009赛季 NBA 50个顶级球员数据做了一个极佳的演 ...

  8. ResNet网络学习笔记。

    ResNet网络学习 看b站 霹雳吧啦Wz 的视频总结的学习笔记! 视频的地址 大佬的Github代码 1.ResNet详解 ResNet 网络是在2015年由微软实验室提出,斩获当年 ImageNe ...

  9. 自组网训练生成模型并推理模型完整流程,代码展示LeNet -> AlexNet -> VGGNet -> InceptionNet -> ResNet优化过程

    项目简介 本项目基于20种蝴蝶分类基础上做一个深入浅出的代码理解,及其对数据预处理,自定义数据读取器Reader(Dataset),及其输出final.pdparams,final.pdopt模型,利 ...

  10. 迁移学习:如何为您的机器学习问题选择正确的预训练模型

    https://www.toutiao.com/a6687923187298075144/ 在这篇文章中,我们将简要介绍一下迁移学习是什么,以及如何使用它. 什么是迁移学习? 迁移学习是使用预训练模型 ...

最新文章

  1. java 模板引擎_SpringBoot入门系列(四)如何整合Thymeleaf模板引擎
  2. Eclipse里修改SVN的用户名和密码
  3. [COGS 0065][NOIP 2002] 字串变换
  4. LeetCode 111. Minimum Depth of Binary Tree
  5. 小白都能看懂的干货!大数据这朵“后浪”,能卷起多大的风浪?
  6. linux登陆提示鉴定故障_5个最常见的Linux问题的故障排除提示
  7. JavaScript面向对象编程理解
  8. 基于scikit-learn的SVM实战
  9. LGOJP3959 宝藏
  10. python高级编程教程_python高级编程——入门语法(一)
  11. vray安装显示服务器没有安装,vray3.2安装教程
  12. 音阶频率对照表_各个音阶的对应频率
  13. 2022年系统分析师考试大纲
  14. 银联网关支付,退款java实现
  15. java读取文件之BufferedReader
  16. Go语言之return语句的几种用法
  17. android 设置默认铃声,我的Android进阶之旅------gt;Android系统设置默认来电铃声、闹钟铃声、通知铃声,android进阶...
  18. NCBI推出blastp加速服务(Accelerated protein-protein BLAST)
  19. 前端项目实战95-数组遍历继续处理
  20. element组件树形控件el-tree点击展开节点,节点重影

热门文章

  1. boost::process::on_exit相关的测试程序
  2. boost::mp11::mp_product相关用法的测试程序
  3. boost::math::chi_squared用法的测试程序
  4. GDCM:gdcm::Object的测试程序
  5. boost::core模块实现bit ceil测试
  6. Boost:BOOST_ASSERT_MSG扩展的用法测试程序
  7. 基于Boost::beast模块的无栈协程http服务器
  8. ITK:从文件编写一个转换
  9. ITK:将RGB图像转换为灰度图像
  10. DCMTK:dicom标签的基础类