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
-
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
-
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
-
Step 4: Instantiate and register your layer in your cpp file with the macro provided inlayer_factory.hpp.
-
Step 5: add newlayer to train_test_prototxt,compile and run!