FasterRCNN增删类别及训练需要修改地方

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lingyunxianhe/article/details/82901206

       FasterRCNN增删类别及训练需要修改地方,有些零碎,必须每一个地方都要修改到位才能训练,在此过程中查了好些资料,结合自己的经历特此完整全面的整理了一下,以备后续使用。

       在此过程中有个blog写的还不错在此:https://www.cnblogs.com/hansjorn/p/7724852.html

一、配置文件的修改
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
py-faster-rcnn/models/fab/ZF/faster_rcnn_alt_opt文件夹下需要修改的文件如下:
(注:lineNum表示第几行)
##############################################################
1、stage1_fast_rcnn_train_defect.pt
**********1 lineNum=14 param_str: "'num_classes': 4"**********
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': 4" #按训练集类别改,该值为类别数+1  
  }  
}  

**********2 lineNum=247 num_output: 4**********
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: 4 #按训练集类别改,该值为类别数+1 
    weight_filler { 
      type: "gaussian" 
      std: 0.01 
    } 
    bias_filler { 
      type: "constant" 
      value: 0 
    } 
  } 

**********3 lineNum=266 num_output: 16**********
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: 16 #按训练集类别改,该值为(类别数+1)*4 
    weight_filler { 
      type: "gaussian" 
      std: 0.001 
    } 
    bias_filler { 
      type: "constant" 
      value: 0 
    } 
  } 

##############################################################
2、stage1_rpn_train_defect.pt
**********1 lineNum=11 param_str: "'num_classes': 4"**********
name: "ZF"
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': 4"
  }
}

##############################################################
3、stage2_fast_rcnn_train_defect.pt
**********1 lineNum=14 param_str: "'num_classes': 4"**********
name: "ZF"
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': 4"
  }
}

**********2 lineNum=247 num_output: 4**********
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: 4
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

**********3 lineNum=266 num_output: 16**********
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: 16
    weight_filler {
      type: "gaussian"
      std: 0.001
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

##############################################################
4、stage2_rpn_train_defect.pt
**********1 lineNum=11 param_str: "'num_classes': 4"**********
name: "ZF"
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': 4"
  }
}

##############################################################
5、faster_rcnn_test_defect.pt
**********1 lineNum=306  num_output: 4**********
layer {
  name: "cls_score"
  type: "InnerProduct"
  bottom: "fc7"
  top: "cls_score"
  inner_product_param {
    num_output: 4 #按训练集类别改,该值为类别数+1 
  }
}

**********2 lineNum=315 num_output: 16**********
layer {
  name: "bbox_pred"
  type: "InnerProduct"
  bottom: "fc7"
  top: "bbox_pred"
  inner_product_param {
    num_output: 16 #按训练集类别改,该值为(类别数+1)*4
  }
}


二、×.py文件的修改:
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
py-faster-rcnn/lib/datasets 文件夹下需要修改的文件如下:
##############################################################
1、fab_defect.py (原始文件为:pascal_voc.py) 
**********1 lineNum=30 self._classes =类别1,类别2,.....**********
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
                         'aeroplane', 'bicycle', 'bird', 'boat',
                         'bottle', 'bus', 'car', 'cat', 'chair',
                         'cow', 'diningtable', 'dog', 'horse',
                         'motorbike', 'person', 'pottedplant',
                         'sheep', 'sofa', 'train', 'tvmonitor')

××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
文件路径:py-faster-rcnn/tools 文件夹下需要修改的文件如下:
##############################################################
1、demo_defect.py (原始文件为:demo.py.py 约第79行) 
CLASSES = ('__background__',
          'bl',
          'nak',
          'break')


三、×.xml文件的修改
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
如果你原来标注文件中含有A,B,C,D四个类别,但现在需要去掉D类,那么标注文件中需要删掉D标注,否则会发生:
fab_defect.py (原始文件为:pascal_voc.py约第238行) 
cls = self._class_to_ind[obj.find('name').text.lower().strip()]这条语句会报错

四、删除以前的训练预处理×.pkl和*.txt文件
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
1、删除py-faster-rcnn/data/cache 下的*.pkl文件
2、删除py-faster-rcnn//data/FABdevkit2017/annotations_cache 下的*.txt文件


 

猜你喜欢

转载自blog.csdn.net/lingyunxianhe/article/details/82901206
今日推荐