How to add a new type of layer in Caffe
Reference: https://github.com/BVLC/caffe/issues/684
ADD a class declaration for your layer to the appropriate one of COMMON_LAYERS.HPP,DATA_LAYERS.HPP, LOSS_LAYERS.HPP, neuro N_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 is implementing CPU code.
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
(Optional) Implement the GPU versions Forward_gpu and Backward_gpu in Layers/your_layer.cu.
Add your layer to Proto/caffe.proto, updating the next available ID. Also declare parameters, if needed, in this file.
Make your layer createable by adding it to layer_factory.cpp.
Write tests in Test/test_your_layer.cpp. Use TEST/TEST_GRADIENT_CHECK_UTIL.HPP to check for that your Forward and backward implementations is in numerical agreement.
The above is a big God on GitHub's answer, the steps are clear, specifically, for example, now to add a vision layer, named Aaa_layer:
1, belong to which type of layer, open which HPP file, here Open vision_layers.hpp, and then add the definition of the layer itself, or directly copy the Convolution_layer of the relevant code to modify the class name and constructor names to the AAA _layer, if the GPU is not used, remove the *_GPU declaration.
2, to achieve their own Layer, write aaa_layer.cpp, add to Src/caffe/layers, the main implementation of Setup, FORWARD_CPU, backward_cpu.
3, if GPU implementation is required, then implement FORWARD_GPU and Backward_gpu in Aaa_layer.cu.
4, modify Src/caffe/proto/caffe.proto, good to Layertype, add AAA, and update ID, if layer has parameters, add Aaaparameter class.
5. Add the response code in Src/caffe/layer_factory.cpp.
6, write a test_aaa_layer.cpp in the src/caffe/test, use the INCLUDE/CAFFE/TEST/TEST_GRADIENT_CHECK_UTIL.HPP to check the forward and backward propagation is correct.
How to add a new type of layer in Caffe