Caffe扩展新层

真的是被Caffe玩哭啦!抓狂大哭。先说一下我的情况吧。我是用的Caffe是Windows版本,也许Linux版本就没有我的烦恼了。我想在训练的时候使用 BatchNormail层,由于我原先使用的是大神happynear的老版本了,但是老版本里没有Scale层,所以只能更新新版本。于是我使用官方Caffe-Microsoft版本,训练还算一切正常。但是到了应用阶段出现问题了。在命名空间caffe中找不到各个层了,这就对我使用C++接口产生了影响,怎么办呢?换happynear大神的最新版本,可惜还是这个问题。分析原因是caffe源码的结构发生了变化,在include文件中头文件发生了变化,老版本各个层的头文件基本上定义在vision_layers.hpp,但是新版本在layers文件夹中分别定义的,不知道是不是这个原因,导致了命名空间中找不到各个层了。

说了这么多废话,说下我的解决方法吧,扩展老版本。之所以说是扩展,是因为我并没有写层,只是把新版本中的层添加到老版本中。大家可以参考这个链接:http://blog.csdn.net/kuaitoukid/article/details/41865803

由于我没有写层,所以就简单多了,只需要简单修改一些文件即可,现在就以Scale层为例:

1、首先确定Scale层属于layer类型,是common_layer,data_layer,loss_layer,neuron_layer,vision_layer中哪个?说实话具体分在哪个层不太好确定(个人理解这些只是大致分类,便于管理,放在哪个文件里都可以),一般我们添加的新层都在vision_layer中。首先我们在新版本中.\caffe\include\caffe\layers找到scale_layer.hpp,我们可以把这个文件看成是从老版本vision_layers.hpp中取出来单独建立的一个头文件,我先在要还原回去。打开scale_layer.hpp文件复制代码:

/**
 * @brief Computes a product of two input Blobs, with the shape of the
 *        latter Blob "broadcast" to match the shape of the former.
 *        Equivalent to tiling the latter Blob, then computing the elementwise
 *        product.
 *
 * The second input may be omitted, in which case it's learned as a parameter
 * of the layer.
 */
template <typename Dtype>
class ScaleLayer: public Layer<Dtype> {
 public:
  explicit ScaleLayer(const LayerParameter& param)
      : Layer<Dtype>(param) {}
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "Scale"; }
  // Scale
  virtual inline int MinBottomBlobs() const { return 1; }
  virtual inline int MaxBottomBlobs() const { return 2; }
  virtual inline int ExactNumTopBlobs() const { return 1; }

 protected:
  /**
   * In the below shape specifications, @f$ i @f$ denotes the value of the
   * `axis` field given by `this->layer_param_.scale_param().axis()`, after
   * canonicalization (i.e., conversion from negative to positive index,
   * if applicable).
   *
   * @param bottom input Blob vector (length 2)
   *   -# @f$ (d_0 \times ... \times
   *           d_i \times ... \times d_j \times ... \times d_n) @f$
   *      the first factor @f$ x @f$
   *   -# @f$ (d_i \times ... \times d_j) @f$
   *      the second factor @f$ y @f$
   * @param top output Blob vector (length 1)
   *   -# @f$ (d_0 \times ... \times
   *           d_i \times ... \times d_j \times ... \times d_n) @f$
   *      the product @f$ z = x y @f$ computed after "broadcasting" y.
   *      Equivalent to tiling @f$ y @f$ to have the same shape as @f$ x @f$,
   *      then computing the elementwise product.
   */
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

  shared_ptr<Layer<Dtype> > bias_layer_;
  vector<Blob<Dtype>*> bias_bottom_vec_;
  vector<bool> bias_propagate_down_;
  int bias_param_id_;

  Blob<Dtype> sum_multiplier_;
  Blob<Dtype> sum_result_;
  Blob<Dtype> temp_;
  int axis_;
  int outer_dim_, scale_dim_, inner_dim_;
};
将代码粘贴到老版本中vision_layer.hpp最后即可。

2、将从新版本中.\caffe\src\caffe\layers  中的scale_layer.cpp和scale_layer.cu两个文件复制到老版本中.\caffe\src\caffe\layers.如果不用GPU可以不复制scale_layer.cu

3、修改caffe.proto,主要修改两个地方,增加层类型和增加层参数。如下图:


找到message_ScaleParameter


4、在layer_factory.cpp中添加代码,但是我发现新老版本没有变化,所以我没有修改。

5、修改.\caffe\src\caffe\util\force_link.cpp  这个文件是注册各个层,我们要把新添加的层在这个注册一下,说是注册其实就是添加一句话而已



至于括号里填写什么?可以到相对应的.cpp文件的最后两行找到答案。

给大家一个小技巧,我们可以把新添加的层全都复制到老版本中这样caffe.proto就可以删掉,直接中新版本的caffe.proto代替的,我就是这么干的。

修改完以上的东西我们就可以重新编译就OK!哦!忘了告诉大家这里还需要注意两个问题,1、我们把新层的.cpp文件复制过来后并不能自动添加到工程中,我们需要手动添加。2、修改新层.cpp文件中的一个地方

#include "caffe\layers\scale_layer.hpp"
修改成

#include "vision_layer.hpp"#因为我们把头文件复制到了vision_layer.hpp中

OK!到了这里就差不多了,祝大家好运!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值