caffe-ssd中在代码中修改detection_out层的参数

由于caffe-ssd的最后一层detection_out中有用到nms,并且是可以再gpu中实现的,这样就比在cpu上做快很多。为了将ssd工程化,需要设计一个接口,能直接设置detection_out中的nms阈值和confidence阈值。

在caffe中提供了修改每一层参数的代码

//net.hpp 215行
const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name) const;
//通过这个接口可以直接获取到detection_out的指针
//layer.hpp 189行
const LayerParameter& layer_param() const { return layer_param_; }
//通过这个接口可以获取网络层的参数
//caffe.pb.h  19799行
inline ::caffe::DetectionOutputParameter* LayerParameter::mutable_detection_output_param() {
  set_has_detection_output_param();
  if (detection_output_param_ == NULL) detection_output_param_ = new ::caffe::DetectionOutputParameter;
  // @@protoc_insertion_point(field_mutable:caffe.LayerParameter.detection_output_param)
  return detection_output_param_;
}
//通过这个接口可以直接访问detection_output_param的参数
//caffe.pb.h 25308行
inline ::caffe::NonMaximumSuppressionParameter* DetectionOutputParameter::mutable_nms_param() {
  set_has_nms_param();
  if (nms_param_ == NULL) nms_param_ = new ::caffe::NonMaximumSuppressionParameter;
  // @@protoc_insertion_point(field_mutable:caffe.DetectionOutputParameter.nms_param)
  return nms_param_;
}
//通过这个接口可以访问nms的各种参数
//caffe.pb.h 24711行
inline void NonMaximumSuppressionParameter::set_nms_threshold(float value) {
  set_has_nms_threshold();
  nms_threshold_ = value;
  // @@protoc_insertion_point(field_set:caffe.NonMaximumSuppressionParameter.nms_threshold)
}
//通过这个接口可以直接设置nms

之前确实没有怎么仔细去caffe的源码,现在仔细看来,caffe的代码确实写的非常不错。

可以直接通过下面这行代码直接设置nms的阈值了

net_->layer_by_name("detection_out")->layer_param_no_const()->mutable_detection_output_param()->mutable_nms_param()->set_nms_threshold(0.6);

当然原来的源码是不能这样写的,因为有很多变量设置的是const属性,所以不得不将源码修改一下,达到可以访问的目的。

  LayerParameter* layer_param_no_const() {
      LayerParameter* temp = &layer_param_;
      return temp;
  }
  //这个函数是自己加的,名字就是随便取的一个名字。通过这个函数将layer_param_变量的访问权开放出来,后面的函数就可以访问了。

一般在加载模型文件之后,各层的参数就已经确定了。在detection_output_layer.cpp文件中可以看到,LayerSetUp函数在网络初始化之后,nms的阈值和conf的阈值就已经被确定了。两个值分别被保存在 confidence_threshold_ 和 nms_threshold这两个变量中,在forward的过程中,nms函数是直接访问这两个变量,所以影响结果是这两个变量的值,所以需要在forward中nms之前将这两个变量重新赋值,就可以了。

由于涉及到gpu做nms运算,所以在.cu和.cpp文件中都需要修改代码。

const DetectionOutputParameter& detection_output_param = this->layer_param_.detection_output_param();

confidence_threshold_ = detection_output_param.has_confidence_threshold() ?
      detection_output_param.confidence_threshold() : -FLT_MAX;
  // Parameters used in nms.
nms_threshold_ = detection_output_param.nms_param().nms_threshold();
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值