参考: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.
大概意思是说:
新添一个自己设计的layer, 比如Your_Layer,然后作用跟Convolution_Layer一模一样。注意这里的命名方式,Your第一个字母大写,剩下的小写。
1 首先确定要添加的layer的类型,是common_layer 还是 data_layer 还是loss_layer, neuron_layer, vision_layer ,这里的Your_Layer肯定是属vision_layer了,所以打开vision_layers.hpp 然后复制convolution_layer的相关代码,把类名还有构造函数的名字改为YourLayer,如果没有用到GPU运算,那么把里面的带GPU的函数都删掉。
2、将Your_layer.cpp 添加到src/caffe/layers文件夹中,代码内容复制convolution_layer.cpp 把对应的类名修改(可以搜一下conv关键字,然后改为Your)
3、(可选)假如有gpu的代码就添加相应的Your_layer.cu。
4、修改proto/caffe.proto文件,找到LayerType,添加Your,并更新ID(新的ID应该是34)。假如说Your_Layer有参数,比如Convolution肯定是有参数的,那么添加YourParameter类。
5、在layer_factory.cpp中添加相应的代码,就是一堆if … else的那片代码。
6、这个可以不做,但是为了结果还是做一个,就是写一个测试文件,检查前向后向传播的数据是否正确。在 /test下写一个测试代码,比如test_your_layer.cpp。
未完待续。。。写一个实例,设计自己的新的Layer.