在caffe中添加新的Layer

参考github上面的答案:https://github.com/BVLC/caffe/issues/684

 

Here's roughly the process I follow.

  1. Add a class declaration for your layer to the appropriate one of common_layers.hpp,data_layers.hpp, loss_layers.hpp, neuron_layers.hpp, or vision_layers.hpp. Include an inline implementation of type and the *Blobs() methods to specify blob number requirements. Omit the*_gpu declarations if you'll only be implementing CPU code.
  2. Implement your layer in layers/your_layer.cpp.
    • SetUp for initialization: reading parameters, allocating buffers, etc.
    • Forward_cpu for the function your layer computes
    • Backward_cpu for its gradient
  3. (Optional) Implement the GPU versions Forward_gpu and Backward_gpu in layers/your_layer.cu.
  4. Add your layer to proto/caffe.proto, updating the next available ID. Also declare parameters, if needed, in this file.
  5. Make your layer createable by adding it to layer_factory.cpp.
  6. Write tests in test/test_your_layer.cpp. Use test/test_gradient_check_util.hpp to check that your Forward and Backward implementations are in numerical agreement.

Since this is a many step process, I thought it worth recording here. I would welcome improvements to Caffe that make the layer adding process less involved. BVLC folk All, if I've made an error or omitted something here, feel free to edit this post the wiki.

 

大致过程翻译:

 

1. 确定要添加的层的类型,是属于 common_layers.hpp,data_layers.hpp, loss_layers.hpp, neuron_layers.hpp, or vision_layers.hpp 中的哪一种,然后在对应的hpp头文件中添加该层的类的定义。

包括内联实现type和*Blob()方法指定blob的数字,如果你只是实现CPU部分的代码,则忽略*_gpu的声明;

2. 实现你要添加的层(应该指的是数据的计算过程,上面头文件中是对层的类的声明和定义),然后放在src/caffe/layers/your_layer.cpp路径下;

  • 设置初始化,读参数,分配缓冲区等
  • Forward_cpu for the function your layer computes
  • Backward_cpu for its gradient

3. 如果有GPU,就实现GPU版本的 Forward_gpu 和 Backward_gpu 在 src/caffe/layers/your_layer.cu路径下;

4. 在 proto/caffe.proto 中找到LayerType对应位置,添加新增加的层,更新一个可用的ID,假如新添加的层有参数,则需要添加对应的参数类;

5. 在layer_factory.cpp 模仿里面的代码,做对应的添加。使得你的层是可创建的;

6. 写一个测试文件,在路径test/test_your_layer.cpp 之下。用test/test_gradient_check_util.hpp  检查你的前向和后向传播是否正确;

 

 

修改示例:https://github.com/luoyetx/OrdinalRegression

 

1. 添加文件到对应路径:
cp layers/ordinal_regression_loss_layer.hpp $CAFFE_HOME/include/caffe/layers/ordinal_regression_loss_layer.hpp 头文件
cp layers/ordinal_regression_loss_layer.cpp $CAFFE_HOME/src/caffe/layers/ordinal_regression_loss_layer.cpp 新加层的cpu版本
cp layers/ordinal_regression_loss_layer.cu $CAFFE_HOME/src/caffe/layers/ordinal_regression_loss_layer.cu 新加层的gpu版本
cp layers/test_ordinal_regression_loss_layer.cpp $CAFFE_HOME/src/caffe/test/test_ordinal_regression_loss_layer.cpp 测试文件

 

2. Modify $CAFFE_HOME/src/caffe/proto/caffe.proto according to layers/caffe.proto 修改文件中的对应位置。

 

3. 重新编译caffe

 

4.运行 make runtest GTEST_FILTER='OrdinalRegressionLossLayerTest/*'   测试

 

 

 

转载于:https://www.cnblogs.com/Allen-rg/p/6102408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值