(四)用faster r-cnn训练自己的数据集(添加一个新的层:centerLoss层用于监督训练)

一,简介

想做目标检测和大分类,在py-faster-rcnn的基础上使用ps-ROI操作,并使用了可变性卷积(deformable Convlution);原先对于分类结果cls_score的损失用的是softmax loss, 看了center loss的论文:论文;

(四)用faster r-cnn训练自己的数据集(添加一个新的层:centerLoss层用于监督训练)_第1张图片
在分类的最后全连接那一层中,分别添加一个softmax loss层和一个center loss层,用于监督网络的学习。

(1)添加center loss 的相关文件并编译
原本caffe中没有center loss,需要手动添加关于center loss 的hpp,cpp以及cu等文件;具体操作参考:caffe添加centerloss

(2) 修改网络结构,使用center loss训练
添加好了center Loss 的相关文件并重新编译caffe后,便可以在网络中使用caffe了:修改网络结构文件(protototxt)---->在原先网络输出的最后一层(也就是输出类别得分那一层)后面再添加一层和这一层数量一样的全连接层---->分别在这两层后面添加softmax loss层和center loss 层。
注意: center loss 最后记得添加 propagate_down: true ,propagate_down: false 这两句,否则计算的center loss 无法进行法相传播而报错!这两句分别表示这一层的两个bottom是否需要back_propagate,即反向传播,第一个是需要的,labels这个输入是不需要反向传播的。

#========= RCNN ============

layer {
  name: "roi_pool5"
  type: "ROIPooling"
  bottom: "conv4_blk"
#  bottom: "conv_new_1"
#  bottom: "rfcn_cls"
  bottom: "rois"
  top: "pool5"
#  top: "cls_score"
  roi_pooling_param {
    pooled_w: 7
    pooled_h: 7
    spatial_scale: 0.0625 # 1/16
  }
}

layer {
  name: "cls_score1"
  type: "InnerProduct"
#  bottom: "conv4_blk"
  bottom: "pool5"
  top: "cls_score1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 184      #yuesongtian
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

#-----added by ard ---------
layer {
  name: "cls_score2"
  type: "InnerProduct"
#  bottom: "conv4_blk"
  bottom: "cls_score1"
  top: "cls_score2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 184      #yuesongtian
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

layer {
  name: "ps_bbox_roi_pool5"
  type: "PSROIPooling"
  bottom: "rfcn_bbox"
  bottom: "rois"
  top: "bbox_pool5"
  psroi_pooling_param {
    output_dim: 8      #4 * (bg + fg)
    group_size: 7
    spatial_scale: 0.0625 # 1/16
  }
}
layer {
  name: "global_bbox"
  type: "Pooling"
  bottom: "bbox_pool5"
  top: "bbox_pred"
  pooling_param {
    kernel_size: 7
        stride: 7
        pool: AVE
  }
}
############# online hard example mining #####################
layer {
   name: "per_roi_loss_cls"
   type: "SoftmaxWithLossOHEM"
   bottom: "cls_score2"
   bottom: "labels"
   top: "temp_loss_cls"
   top: "temp_prob_cls"
   top: "per_roi_loss_cls"
   loss_weight: 0
   loss_weight: 0
   loss_weight: 0
   propagate_down: false
   propagate_down: false
}

layer {
  name: "reshape_per_roi_loss_cls"
  bottom: "per_roi_loss_cls"
  top: "per_roi_loss_cls_reshape"
  type: "Reshape"
  reshape_param { shape { dim: 0 dim: 1 } }
}

layer {
   name: "per_roi_loss_bbox"
   type: "SmoothL1LossOHEM"
   bottom: "bbox_pred"
   bottom: "bbox_targets"
   bottom: "bbox_inside_weights"
   top: "temp_loss_bbox"
   top: "per_roi_loss_bbox"
   loss_weight: 0
   loss_weight: 0
   propagate_down: false
   propagate_down: false
   propagate_down: false
}

layer {
  name: "reshape_per_roi_loss_bbox"
  bottom: "per_roi_loss_bbox"
  top: "per_roi_loss_bbox_reshape"
  type: "Reshape"
  reshape_param { shape { dim: 0 dim: 1 } }
}

layer {
   name: "per_roi_loss"
   type: "Eltwise"
   bottom: "per_roi_loss_cls_reshape"
   bottom: "per_roi_loss_bbox_reshape"
   top: "per_roi_loss"
   propagate_down: false
   propagate_down: false
}

layer {
   bottom: "rois"
   bottom: "per_roi_loss"
   bottom: "labels"
   bottom: "bbox_inside_weights"
   top: "labels_ohem"
   top: "bbox_loss_weights_ohem"
   name: "annotator_detector"
   type: "BoxAnnotatorOHEM"
   box_annotator_ohem_param {
        roi_per_img: 128
        ignore_label: -1
   }
   propagate_down: false
   propagate_down: false
   propagate_down: false
   propagate_down: false
}
layer {
   name: "silence"
   type: "Silence"
   bottom: "bbox_outside_weights"
   bottom: "temp_loss_cls"
   bottom: "temp_prob_cls"
   bottom: "temp_loss_bbox"
}

#-----------------------output------------------------
#------center loss----------
layer {
  name: "center_loss"
  type: "CenterLoss"
  bottom: "cls_score1"
  bottom: "labels_ohem"
  top: "center_loss"
  param {
    lr_mult: 1
    decay_mult: 0
  }
  center_loss_param {
    num_output: 10
    center_filler {
      type: "xavier"
    }    
  }
  loss_weight: 0.01 
  propagate_down: true 
  propagate_down: false
} 
#------softmax loss---------
layer {
   name: "loss_fuji"
   type: "SoftmaxWithLoss"
   bottom: "cls_score2"
   bottom: "labels_ohem"
#   bottom: "labels"
   top: "loss_cls"
   loss_weight: 1
   loss_param {
        ignore_label: -1
   }
   propagate_down: true
   propagate_down: false
}
#--------------accuracy---------------------
layer {
   name: "accuarcy"
   type: "Accuracy"
   bottom: "cls_score2"
   bottom: "labels_ohem"
#   bottom: "labels"
   top: "accuarcy"
   #include: { phase: TEST }
   accuracy_param {
        ignore_label: -1
   }
   propagate_down: false
   propagate_down: false
}

#--------------loss_bbox---------------------
layer {
   name: "loss_bbox"
   type: "SmoothL1LossOHEM"
#   type: "SmoothL1Loss"
   bottom: "bbox_pred"
   bottom: "bbox_targets"
   bottom: "bbox_loss_weights_ohem"
   top: "loss_bbox"
   loss_weight: 1
   loss_param {
        normalization: PRE_FIXED
        pre_fixed_normalizer: 128
   }
   propagate_down: true
   propagate_down: false
   propagate_down: false
}

你可能感兴趣的:(caffe深度学习)