基于Faster RCNN的医学图像检测(肺结节检测)

来源:互联网 发布:java数组怎么添加元素 编辑:程序博客网 时间:2024/03/29 03:27

Faster-R-CNN算法由两大模块组成:1.PRN候选框提取模块 2.Fast R-CNN检测模块。其中,RPN是全卷积神经网络,用于提取候选框;Fast R-CNN基于RPN提取的proposal检测并识别proposal中的目标。


在 学习Faster-RCNN (py-faster-rcnn demo)的基础上 用自己的数据训练 这里选择的是CT肺数据,关于数据处理方面的问题参照我博客 : caffe finetune predict and detect the lung nodule

以及做成Faster RCNN格式的数据 参照我的博客:将自己的数据做成Faster RCNN的格式(VOC2007格式)


修改参数

这里主要介绍一下怎么修改Faster RCNN中的一些参数。 
1.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt修改

layer {  name: 'data'  type: 'Python'  top: 'data'  top: 'rois'  top: 'labels'  top: 'bbox_targets'  top: 'bbox_inside_weights'  top: 'bbox_outside_weights'  python_param {    module: 'roi_data_layer.layer'    layer: 'RoIDataLayer'    param_str: "'num_classes': 2" #按训练集类别改,该值为类别数+1  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
layer {  name: "cls_score"  type: "InnerProduct"  bottom: "fc7"  top: "cls_score"  param { lr_mult: 1.0 }  param { lr_mult: 2.0 }  inner_product_param {    num_output: 2 #按训练集类别改,该值为类别数+1    weight_filler {      type: "gaussian"      std: 0.01    }    bias_filler {      type: "constant"      value: 0    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
layer {  name: "bbox_pred"  type: "InnerProduct"  bottom: "fc7"  top: "bbox_pred"  param { lr_mult: 1.0 }  param { lr_mult: 2.0 }  inner_product_param {    num_output: 8 #按训练集类别改,该值为(类别数+1)*4    weight_filler {      type: "gaussian"      std: 0.001    }    bias_filler {      type: "constant"      value: 0    }  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt修改

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" #按训练集类别改,该值为类别数+1  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt修改

ayer {    name: 'data'    type: 'Python'    top: 'data'    top: 'rois'    top: 'labels'    top: 'bbox_targets'    top: 'bbox_inside_weights'    top: 'bbox_outside_weights'    python_param {      module: 'roi_data_layer.layer'      layer: 'RoIDataLayer'      param_str: "'num_classes': 2" #按训练集类别改,该值为类别数+1    }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
layer {    name: "cls_score"    type: "InnerProduct"    bottom: "fc7"    top: "cls_score"    param { lr_mult: 1.0 }    param { lr_mult: 2.0 }    inner_product_param {      num_output: 2 #按训练集类别改,该值为类别数+1      weight_filler {        type: "gaussian"        std: 0.01      }      bias_filler {        type: "constant"        value: 0      }    }  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
layer {    name: "bbox_pred"    type: "InnerProduct"    bottom: "fc7"    top: "bbox_pred"    param { lr_mult: 1.0 }    param { lr_mult: 2.0 }    inner_product_param {      num_output: 8 #按训练集类别改,该值为(类别数+1)*4      weight_filler {        type: "gaussian"        std: 0.001      }      bias_filler {        type: "constant"        value: 0      }    }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_rpn_train.pt修改

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" #按训练集类别改,该值为类别数+1    }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

5.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt修改 
这个是在测试阶段需要

layer {    name: "cls_score"    type: "InnerProduct"    bottom: "fc7"    top: "cls_score"    inner_product_param {      num_output: 2#按训练集类别改,该值为类别数+1    }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
layer {    name: "bbox_pred"    type: "InnerProduct"    bottom: "fc7"    top: "bbox_pred"    inner_product_param {      num_output: 8 #按训练集类别改,该值为(类别数+1)*4    }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6.py-faster-rcnn/lib/datasets/pascal_voc.py修改

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__','nodule') # bg always index 0  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

用你生成的数据集直接替换原来VOC2007内的Annotations,ImageSets和JPEGImages即可,以免出现各种错误。


!!!为防止与之前的模型搞混,训练前把output文件夹删除(或改个其他名),还要把py-faster-rcnn/data/cache中的文件和py-faster-rcnn/data/VOCdevkit2007/annotations_cache中的文件删除(如果有的话)。


至于学习率等之类的设置,可在py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt中的solve文件设置,迭代次数可在py-faster-rcnn\tools的train_faster_rcnn_alt_opt.py中修改:

max_iters = [80000, 40000, 80000, 40000]  
  • 1

分别为4个阶段(rpn第1阶段,fast rcnn第1阶段,rpn第2阶段,fast rcnn第2阶段)的迭代次数。可改成你希望的迭代次数。


如果改了这些数值,最好把py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt里对应的solver文件(有4个)也修改,stepsize小于上面修改的数值。

开始训练

进入py-faster-rcnn,执行:

./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc  
  • 1

这样,就开始训练了。

测试

将训练得到的py-faster-rcnn\output\faster_rcnn_alt_opt***_trainval中ZF的caffemodel(ZF_faster_rcnn_final.caffemodel)拷贝至py-faster-rcnn\data\faster_rcnn_models(如果没有这个文件夹,就新建一个),然后,修改: 
py-faster-rcnn\tools\demo.py,主要修改:

CLASSES = ('__background__','nodule')#           'aeroplane', 'bicycle', 'bird', 'boat',#           'bottle', 'bus', 'car', 'cat', 'chair',#           'cow', 'diningtable', 'dog', 'horse',#           'motorbike', 'person', 'pottedplant',#           'sheep', 'sofa', 'train', 'tvmonitor')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

改成你的数据集标签;

NETS = {'vgg16': ('VGG16',                    'VGG16_faster_rcnn_final.caffemodel'),          'zf': ('ZF',                    'ZF_faster_rcnn_final.caffemodel')}  
  • 1
  • 2
  • 3
  • 4

上面ZF的caffemodel改成你的caffemodel,如果名字一致则无需更改~

im_names = ['1559.jpg','1564.jpg']  
  • 1

改成你的测试图片。(测试图片放在py-faster-rcnn\data\demo中)

结果

在py-faster-rcnn下, 
执行:

./tools/demo.py --net zf  
  • 1

结果如下: 
这里写图片描述
这里写图片描述

总结

这只是跑通了,还没有达到要求 因此还需要进一步学习其中的机制~

原创粉丝点击