zhuwei3014的个人博客分享 http://blog.sciencenet.cn/u/zhuwei3014

博文

如何在caffe中添加新类型的layer

已有 15165 次阅读 2014-12-11 16:47 |个人分类:Caffe|系统分类:科研笔记| caffe

参考:https://github.com/BVLC/caffe/issues/684

 

  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.

以上是github上某大神的解答,步骤很清晰,具体说一下,比如现在要添加一个vision layer,名字叫Aaa_Layer:

1、属于哪个类型的layer,就打开哪个hpp文件,这里就打开vision_layers.hpp,然后自己添加该layer的定义,或者直接复制Convolution_Layer的相关代码来修改类名和构造函数名都改为Aaa_Layer,如果不用GPU,将*_gpu的声明都去掉。

2、实现自己的layer,编写Aaa_Layer.cpp,加入到src/caffe/layers,主要实现Setup、Forward_cpu、Backward_cpu。

3、如果需要GPU实现,那么在Aaa_Layer.cu中实现Forward_gpu和Backward_gpu。

4、修改src/caffe/proto/caffe.proto,好到LayerType,添加Aaa,并更新ID,如果Layer有参数,添加AaaParameter类。

5、在src/caffe/layer_factory.cpp中添加响应代码。

6、在src/caffe/test中写一个test_Aaa_layer.cpp,用include/caffe/test/test_gradient_check_util.hpp来检查前向后向传播是否正确。




https://blog.sciencenet.cn/blog-1583812-850247.html

上一篇:DIY Deep Learning for Vision- a Hands-On Tutorial with Caffe
收藏 IP: 222.190.117.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (1 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-6 00:18

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部