py-rfcn算法caffe配置,训练及应用到自己的数据集

下载程序,
打开py-R-FCN,下载caffe
编译Cython模块
cd lib
make
结果如下图所示:

编译caffe和pycaffe
cd caffe
cp Makefile.config.example MAkefile.config
然后配置Makefile.config文件,可参考我的Makefile.config
[plain] view plain copy
  1. ## Refer to http://caffe.berkeleyvision.org/installation.html  
  2. # Contributions simplifying and improving our build system are welcome!  
  3.   
  4. # cuDNN acceleration switch (uncomment to build with cuDNN).  
  5.  USE_CUDNN := 1     
  6.   
  7. # CPU-only switch (uncomment to build without GPU support).  
  8. # CPU_ONLY := 1  
  9.   
  10. # uncomment to disable IO dependencies and corresponding data layers  
  11. # USE_OPENCV := 0  
  12. # USE_LEVELDB := 0  
  13. # USE_LMDB := 0  
  14.   
  15. # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)  
  16. #   You should not set this flag if you will be reading LMDBs with any  
  17. #   possibility of simultaneous read and write  
  18. # ALLOW_LMDB_NOLOCK := 1  
  19.   
  20. # Uncomment if you're using OpenCV 3  
  21. # OPENCV_VERSION := 3  
  22.   
  23. # To customize your choice of compiler, uncomment and set the following.  
  24. # N.B. the default for Linux is g++ and the default for OSX is clang++  
  25. # CUSTOM_CXX := g++  
  26.   
  27. # CUDA directory contains bin/ and lib/ directories that we need.  
  28. CUDA_DIR := /usr/local/cuda  
  29. # On Ubuntu 14.04, if cuda tools are installed via  
  30. # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:  
  31. # CUDA_DIR := /usr  
  32.   
  33. # CUDA architecture setting: going with all of them.  
  34. # For CUDA < 6.0, comment the *_50 lines for compatibility.  
  35. CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \  
  36.         -gencode arch=compute_35,code=sm_35 \  
  37.         -gencode arch=compute_50,code=sm_50 \  
  38.         -gencode arch=compute_50,code=compute_50 \  
  39.                 -gencode arch=compute_53,code=compute_53 \  
  40.                 -gencode arch=compute_61,code=compute_61   
  41.   
  42. # BLAS choice:  
  43. # atlas for ATLAS (default)  
  44. # mkl for MKL  
  45. # open for OpenBlas  
  46. BLAS := open  
  47. # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.  
  48. # Leave commented to accept the defaults for your choice of BLAS  
  49. # (which should work)!  
  50. # BLAS_INCLUDE := /path/to/your/blas  
  51. # BLAS_LIB := /path/to/your/blas  
  52.   
  53. # Homebrew puts openblas in a directory that is not on the standard search path  
  54. # BLAS_INCLUDE := $(shell brew --prefix openblas)/include  
  55. # BLAS_LIB := $(shell brew --prefix openblas)/lib  
  56.   
  57. # This is required only if you will compile the matlab interface.  
  58. # MATLAB directory should contain the mex binary in /bin.  
  59.  MATLAB_DIR := /usr/local/MATLAB/R2013b  
  60. # MATLAB_DIR := /Applications/MATLAB_R2012b.app  
  61.   
  62. # NOTE: this is required only if you will compile the python interface.  
  63. # We need to be able to find Python.h and numpy/arrayobject.h.  
  64. PYTHON_INCLUDE := /usr/include/python2.7 \  
  65.                 /usr/lib64/python2.7/site-packages/numpy/core/include \  
  66.         /usr/lib/python2.7/dist-packages/numpy/core/include  
  67. # Anaconda Python distribution is quite popular. Include path:  
  68. # Verify anaconda location, sometimes it's in root.  
  69. # ANACONDA_HOME := $(HOME)/anaconda  
  70. # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \  
  71.         # $(ANACONDA_HOME)/include/python2.7 \  
  72.         # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \  
  73.   
  74. # Uncomment to use Python 3 (default is Python 2)  
  75. # PYTHON_LIBRARIES := boost_python3 python3.5m  
  76. # PYTHON_INCLUDE := /usr/include/python3.5m \  
  77. #                 /usr/lib/python3.5/dist-packages/numpy/core/include  
  78.   
  79. # We need to be able to find libpythonX.X.so or .dylib.  
  80. PYTHON_LIB := /usr/lib  
  81. # PYTHON_LIB := $(ANACONDA_HOME)/lib  
  82.   
  83. # Homebrew installs numpy in a non standard path (keg only)  
  84. # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include  
  85. # PYTHON_LIB += $(shell brew --prefix numpy)/lib  
  86.   
  87. # Uncomment to support layers written in Python (will link against Python libs)  
  88. WITH_PYTHON_LAYER := 1  
  89.   
  90. # Whatever else you find you need goes here.  
  91. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include  
  92. LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib  
  93.   
  94. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
  95.  INCLUDE_DIRS += /usr/local/hdf5/include  
  96.  LIBRARY_DIRS += /usr/local/hdf5/lib  
  97.   
  98. # Uncomment to use `pkg-config` to specify OpenCV library paths.  
  99. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
  100. # USE_PKG_CONFIG := 1  
  101.   
  102. # N.B. both build and distribute dirs are cleared on `make clean`  
  103. BUILD_DIR := build  
  104. DISTRIBUTE_DIR := distribute  
  105.   
  106. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171  
  107. # DEBUG := 1  
  108.   
  109. # The ID of the GPU that 'make runtest' will use to run unit tests.  
  110. TEST_GPUID := 0  
  111.   
  112. # enable pretty build (comment to see full commands)  
  113. Q ?= @  
make -j8
结果如下图所示:

make pycaffe
结果如下图所示:

下载预训练模型(https://1drv.ms/u/s!AoN7vygOjLIQqUWHpY67oaC7mopf),放到data数据集下,如图所示(第二个是我自己训练的模型):
运行演示脚本:
./tools/demo_rfcn.py
结果如下图所示:

下载训练,测试,验证数据集:
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
解压到VOCdevkit文件夹中:
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
tar xvf VOCtrainval_11-May-2012.tar
VOCdevkit文件夹的结构如下图所示:

由于py-faster-rcnn不支持多个训练集,我们创造一个新的文件夹叫做VOC0712,把VOC2007和VOC2012里的JPEGImage和Annonation融合到一个单独的文件夹JPEGImage和Annonation里,用下面的程序生成新的ImageSets文件夹:
[plain] view plain copy
  1. %writetxt.m  
  2. file = dir('F:\VOC0712\Annotations\*.xml');  
  3. len = length(file);  
  4.   
  5. num_trainval=sort(randperm(len, floor(9*len/10)));%trainval集占所有数据的9/10,可以根据需要设置  
  6. num_train=sort(num_trainval(randperm(length(num_trainval), floor(5*length(num_trainval)/6))));%train集占trainval集的5/6,可以根据需要设置  
  7. num_val=setdiff(num_trainval,num_train);%trainval集剩下的作为val集  
  8. num_test=setdiff(1:len,num_trainval);%所有数据中剩下的作为test集  
  9. path = 'F:\VOC0712\ImageSets\Main\';  
  10.   
  11. fid=fopen(strcat(path, 'trainval.txt'),'a+');  
  12. for i=1:length(num_trainval)  
  13.     s = sprintf('%s',file(num_trainval(i)).name);  
  14.     fprintf(fid,[s(1:length(s)-4) '\n']);  
  15. end  
  16. fclose(fid);  
  17.   
  18. fid=fopen(strcat(path, 'train.txt'),'a+');  
  19. for i=1:length(num_train)  
  20.     s = sprintf('%s',file(num_train(i)).name);  
  21.     fprintf(fid,[s(1:length(s)-4) '\n']);  
  22. end  
  23. fclose(fid);  
  24.   
  25. fid=fopen(strcat(path, 'val.txt'),'a+');  
  26. for i=1:length(num_val)  
  27.     s = sprintf('%s',file(num_val(i)).name);  
  28.     fprintf(fid,[s(1:length(s)-4) '\n']);  
  29. end  
  30. fclose(fid);  
  31.   
  32. fid=fopen(strcat(path, 'test.txt'),'a+');  
  33. for i=1:length(num_test)  
  34.     s = sprintf('%s',file(num_test(i)).name);  
  35.     if ~isempty(strfind(s,'plain'))  
  36.         fprintf(fid,[s(1:length(s)-4) '\n']);  
  37.     end  
  38. end  
  39. fclose(fid);  
为VOCdevkit创造新的超链接:VOCdevkit0712,如下图所示

下载在ImageNet上预训练好的模型,放到./data/imagenet_models里,如下图所示:

下面开始用VOC0712训练:
experiments/scripts/rfcn_end2end.sh 使用联合近似训练
experiments/scripts/rfcn_end2end_ohem.sh 使用联合近似训练+OHEM
experiments/scripts/rfcn_alt_opt_5stage_ohem.sh 使用分布训练+OHEM
./experiments/scripts/rfcn_end2end[_ohem].sh [GPU_ID] [NET] [DATASET] [--set ...]
下面开始用py-rfcn来训练自己的数据集:(我的数据集是标准pascal voc数据集,名字叫做VOC5000)
首先修改网络模型:
1. 修改/py-R-FCN/models/pascal_voc/ResNet-50/rfcn_end2end/class-aware/train_ohem.prototxt
[plain] view plain copy
  1. name: "ResNet-50"  
  2. layer {  
  3.   name: 'input-data'  
  4.   type: 'Python'  
  5.   top: 'data'  
  6.   top: 'im_info'  
  7.   top: 'gt_boxes'  
  8.   python_param {  
  9.     module: 'roi_data_layer.layer'  
  10.     layer: 'RoIDataLayer'  
  11.     param_str: "'num_classes': 2" #改为你的数据集的类别数+1  
  12.   }  
  13. }  
  14. layer {  
  15.   name: 'roi-data'  
  16.   type: 'Python'  
  17.   bottom: 'rpn_rois'  
  18.   bottom: 'gt_boxes'  
  19.   top: 'rois'  
  20.   top: 'labels'  
  21.   top: 'bbox_targets'  
  22.   top: 'bbox_inside_weights'  
  23.   top: 'bbox_outside_weights'  
  24.   python_param {  
  25.     module: 'rpn.proposal_target_layer'  
  26.     layer: 'ProposalTargetLayer'  
  27.     param_str: "'num_classes': 2"#改为你的数据集的类别数+1  
  28.   }  
  29. }  
  30. layer {  
  31.     bottom: "conv_new_1"  
  32.     top: "rfcn_cls"  
  33.     name: "rfcn_cls"  
  34.     type: "Convolution"  
  35.     convolution_param {  
  36.         num_output: 98 #2*(7^2) cls_num*(score_maps_size^2)(类别数+1)*49  
  37.         kernel_size: 1  
  38.         pad: 0  
  39.         weight_filler {  
  40.             type: "gaussian"  
  41.             std: 0.01  
  42.         }  
  43.         bias_filler {  
  44.             type: "constant"  
  45.             value: 0  
  46.         }  
  47.     }  
  48.     param {  
  49.         lr_mult: 1.0  
  50.     }  
  51.     param {  
  52.         lr_mult: 2.0  
  53.     }  
  54. }  
  55. layer {  
  56.     bottom: "conv_new_1"  
  57.     top: "rfcn_bbox"  
  58.     name: "rfcn_bbox"  
  59.     type: "Convolution"  
  60.     convolution_param {  
  61.         num_output: 392 #8*(7^2) cls_num*(score_maps_size^2)(类别数+1)*49*4  
  62.         kernel_size: 1  
  63.         pad: 0  
  64.         weight_filler {  
  65.             type: "gaussian"  
  66.             std: 0.01  
  67.         }  
  68.         bias_filler {  
  69.             type: "constant"  
  70.             value: 0  
  71.         }  
  72.     }  
  73.     param {  
  74.         lr_mult: 1.0  
  75.     }  
  76.     param {  
  77.         lr_mult: 2.0  
  78.     }  
  79. }  
  80. layer {  
  81.     bottom: "rfcn_cls"  
  82.     bottom: "rois"  
  83.     top: "psroipooled_cls_rois"  
  84.     name: "psroipooled_cls_rois"  
  85.     type: "PSROIPooling"  
  86.     psroi_pooling_param {  
  87.         spatial_scale: 0.0625  
  88.         output_dim: 2  #类别数+1  
  89.         group_size: 7  
  90.     }  
  91. }  
  92. layer {  
  93.     bottom: "rfcn_bbox"  
  94.     bottom: "rois"  
  95.     top: "psroipooled_loc_rois"  
  96.     name: "psroipooled_loc_rois"  
  97.     type: "PSROIPooling"  
  98.     psroi_pooling_param {  
  99.         spatial_scale: 0.0625  
  100.         output_dim: 8#类别数*4  
  101.         group_size: 7  
  102.     }  
  103. }  
2. 修改/py-R-FCN/models/pascal_voc/ResNet-50/rfcn_end2end/class-aware/test.prototxt
[plain] view plain copy
  1. layer {  
  2.     bottom: "conv_new_1"  
  3.     top: "rfcn_cls"  
  4.     name: "rfcn_cls"  
  5.     type: "Convolution"  
  6.     convolution_param {  
  7.         num_output: 98 #21*(7^2) cls_num*(score_maps_size^2)(类别数+1)*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. }  
  26. layer {  
  27.     bottom: "conv_new_1"  
  28.     top: "rfcn_bbox"  
  29.     name: "rfcn_bbox"  
  30.     type: "Convolution"  
  31.     convolution_param {  
  32.         num_output: 392 #8*(7^2) cls_num*(score_maps_size^2)(类别数+1)*49*4  
  33.         kernel_size: 1  
  34.         pad: 0  
  35.         weight_filler {  
  36.             type: "gaussian"  
  37.             std: 0.01  
  38.         }  
  39.         bias_filler {  
  40.             type: "constant"  
  41.             value: 0  
  42.         }  
  43.     }  
  44.     param {  
  45.         lr_mult: 1.0  
  46.     }  
  47.     param {  
  48.         lr_mult: 2.0  
  49.     }  
  50. }  
  51. layer {  
  52.     bottom: "rfcn_cls"  
  53.     bottom: "rois"  
  54.     top: "psroipooled_cls_rois"  
  55.     name: "psroipooled_cls_rois"  
  56.     type: "PSROIPooling"  
  57.     psroi_pooling_param {  
  58.         spatial_scale: 0.0625  
  59.         output_dim: 2 #(类别数+1)  
  60.         group_size: 7  
  61.     }  
  62. }  
  63.   
  64. layer {  
  65.     bottom: "rfcn_bbox"  
  66.     bottom: "rois"  
  67.     top: "psroipooled_loc_rois"  
  68.     name: "psroipooled_loc_rois"  
  69.     type: "PSROIPooling"  
  70.     psroi_pooling_param {  
  71.         spatial_scale: 0.0625  
  72.         output_dim: 8 #(类别数+1)*4  
  73.         group_size: 7  
  74.     }  
  75. }  
  76. layer {  
  77.     name: "cls_prob_reshape"  
  78.     type: "Reshape"  
  79.     bottom: "cls_prob_pre"  
  80.     top: "cls_prob"  
  81.     reshape_param {  
  82.         shape {  
  83.             dim: -1  
  84.             dim: 2 #(类别数+1)  
  85.         }  
  86.     }  
  87. }  
  88. layer {  
  89.     name: "bbox_pred_reshape"  
  90.     type: "Reshape"  
  91.     bottom: "bbox_pred_pre"  
  92.     top: "bbox_pred"  
  93.     reshape_param {  
  94.         shape {  
  95.             dim: -1  
  96.             dim: 8 #(类别数+1)*4  
  97.         }  
  98.     }  
  99. }  
3.修改/py-R-FCN/lib/datasets/pascal_voc.py
[plain] view plain copy
  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.                          'aeroplane')  
  11.         self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))  
  12.         self._image_ext = '.jpg'  
  13. 修改self._classes为你的类别加背景。  
4./py-R-FCN/lib/datasets/factory.py修改
[plain] view plain copy
  1. for year in ['2007', '2012','2001','2002','2006','5000']:  
  2.     for split in ['train', 'val', 'trainval', 'test']:  
  3.         name = 'voc_{}_{}'.format(year, split)  
  4.         __sets[name] = (lambda split=split, year=year: pascal_voc(split, year))  
  5. 我的数据集叫:VOC5000,所以把5000加到年份当中。  
5/py-R-FCN/experiments/scripts/rfcn_end2end_ohem.sh修改
[plain] view plain copy
  1. case $DATASET in  
  2.   pascal_voc)  
  3.     TRAIN_IMDB="voc_5000_trainval"  
  4.     TEST_IMDB="voc_5000_test"  
  5.     PT_DIR="pascal_voc"  
  6.     ITERS=4000  
  7.     ;;  
  8. 把训练数据集和测试数据集改为你的数据集,迭代次数改为4000。  
开始训练: ./experiments/scripts/rfcn_end2end_ohem.sh 0 ResNet-50 pascal_voc

迭代4000次,取得了81.2%的精度。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值