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“l(fā)inear_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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 春 一片生機(jī) 給大地帶來(lái)了溫馨 冰雪融化了 滋潤(rùn)著春的胸懷 冥冥中結(jié)下了 愛(ài)的情絲 夏 匆匆走過(guò) 濃濃熱熱的雨 留...
    天魁詩(shī)詞書(shū)畫(huà)閱讀 314評(píng)論 0 0
  • 對(duì)于天元錳業(yè)的領(lǐng)路人賈天將,社會(huì)和業(yè)界都有這樣一種評(píng)價(jià):自信敦厚、恢弘大氣。還有一個(gè)共識(shí),說(shuō)他像《亮劍》中的李云龍...
    斗志小男閱讀 552評(píng)論 0 0
  • 剽悍晨讀:三個(gè)月就能說(shuō)流利外語(yǔ)?只要做好這3點(diǎn) 很多知識(shí),其實(shí)不是很多知識(shí),而是所有的知識(shí)。我覺(jué)得都應(yīng)該以用為前提...
    小秦哥哥閱讀 348評(píng)論 0 1
  • 由于最近項(xiàng)目需要,在網(wǎng)上找了很多的富文本編輯器,無(wú)意間在gitHub看到這個(gè)demo,感覺(jué)挺不錯(cuò)的,于是借鑒...
    相濡以沫_31閱讀 7,950評(píng)論 50 35

友情鏈接更多精彩內(nèi)容