How to add a new type of layer in Caffe

Source: Internet
Author: User

How to add a new type of layer in Caffe

Reference: 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, 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.

  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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.