Write in front:
Caffe has many network layers, the latest version of the code has covered many types of network layer, however, sometimes for a variety of reasons, its given network layer can not meet our requirements, it is necessary to change it to meet their own needs, thanks to the author of Open source code and many code maintainers.
Since the network layer in Caffe is given the layer base class directly or indirectly, when we need to add a new type, we need to choose our own base class, so that we can better take advantage of some of the existing methods of the base class. Our new class can be based on the
1. directly inherit from layer
2. Inherit from DataLayer
3. Inherit from Neuronlayer
4. Inherit from Losslayer
5. Or if you want to improve a layer of code directly, you can directly inherit the class, and make the corresponding changes.
Specific steps:
The first step is to add the header file. h, you can create a new header file yourself, or append to an existing header file, such as INCLUDE/DATALAYER.HPP, to define some class variables inside.
The second step, add the implementation file, create the class implementation function, put in the src/xxx.cpp, if there is cuda implementation, you need to add the corresponding. cu file implementation, the main need to write Layersetup, reshape, forward, backup function.
The third step is to register the class and add the corresponding class to the src/caffe/layer_factory.cpp, adding
REGISTER_LAYER_CLASS(MYLAYER, MyLayer);
Fourth step, in the Src/caffe/proto/caffe.proto file, add the corresponding class and class parameters.
Add a row in enum Layertype {}
enumLayerType {MYLAYER = ID }//其中MYLAYER 是新添加的类名,ID是类编号,注意不能更之前的重复。
Add Class Parameters again
optional MylayerParameter mylayer_param = ID2;//其中MylayerParameter 是网络层参数,ID2 是ID
Finally define class parameters
message MylayerParameter { uint321; bool2 [defaulttrue]; }
The fifth step, write the test file, this is optional, but in order to merge into GitHub, need to test pass.
Add your own network layer in the Caffe