Add a new layer to Caffe

How to add a new layer on caffe network?
------this is original work, please indicate the source if reproduced!------
The official but outdated document can be found: https://github.com/BVLC/caffe/wiki/Development
and useful links: https://yunmingzhang.wordpress.com/2015/01/19/how-to-create-your-own-layer-in-deep-learning-framework-caffe/
In this work, we try to add a very simple layer: a linear function f(x)=0.5(x)+0.1 as activation layer (just similar to sigma and tahn) to illustrate the way to add new layer to Caffe.

  • Step 1: Create a new class
    you should create a newclass in caffe_root/inclide/caffexx.hppfile.
    Similar to sigma, it should be addedto neuron_layers.hpp
    If you want to use GPU version,uncomment gpu functions and implement it in linear_layer.cu


    a.png
    a.png
  • Step2: Assign new ID to your layer
    In caffe_root/src/caffe/proto/caffe.proto, we define our layer as LINEAR and assign ID=38 (can be other number), meanwhile, we assign layer parameter ID


    b.png
    b.png
  • Setp 3: Implement linear layer incaffe_root/src/caffe/layers.
    Create a new .cpp file as“linear_layer.cpp”,there are fourmethods might be implemented
    LayerSetUp(optional): for one-time initialization: reading parameters, fixed-size allocations, etc.
    Reshape: for computing the sizes of top blobs,allocating buffers, and any other work that depends on the shapes of bottomblobs
    Forward_cpu: for the function your layer computes
    Backward_cpu: for its gradient (Optional -- a layer can be forward-only)
    Here, we implement CPU version ofForward and Backward


    d.png
    d.png
  • Step 4: Instantiate and register your layer in your cpp file with the macro provided inlayer_factory.hpp.


    c.png
    c.png
  • Step 5: add newlayer to train_test_prototxt,compile and run!


    e.png
    e.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 春 一片生机 给大地带来了温馨 冰雪融化了 滋润着春的胸怀 冥冥中结下了 爱的情丝 夏 匆匆走过 浓浓热热的雨 留...
    天魁诗词书画阅读 295评论 0 0
  • 对于天元锰业的领路人贾天将,社会和业界都有这样一种评价:自信敦厚、恢弘大气。还有一个共识,说他像《亮剑》中的李云龙...
    斗志小男阅读 500评论 0 0
  • 剽悍晨读:三个月就能说流利外语?只要做好这3点 很多知识,其实不是很多知识,而是所有的知识。我觉得都应该以用为前提...
    小秦哥哥阅读 315评论 0 1
  • 由于最近项目需要,在网上找了很多的富文本编辑器,无意间在gitHub看到这个demo,感觉挺不错的,于是借鉴...
    相濡以沫_31阅读 7,921评论 50 35