Hi everyone. I'm new to caffe. I want to develop new layers but I don't know to start. I checked the development guide on http://caffe.berkeleyvision.org/development.html. But I still have no clues. Is there anyone who can give a small tutorial step by step on how to develop a new layer in caffe. That will be of great value. Thanks.
Update: see wiki at https://github.com/BVLC/caffe/wiki/Development-Hints
Here's roughly the process I follow.
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.layers/your_layer.cpp.SetUp for initialization: reading parameters, allocating buffers, etc.Forward_cpu for the function your layer computesBackward_cpu for its gradientForward_gpu and Backward_gpu in layers/your_layer.cu.proto/caffe.proto, updating the next available ID. Also declare parameters, if needed, in this file.layer_factory.cpp.test/test_your_layer.cpp. Use test/test_gradient_check_util.hpp to check that your Forward and Backward implementations are in numerical agreement.Since this is a many step process, I thought it worth recording here. I would welcome improvements to Caffe that make the layer adding process less involved. BVLC folk All, if I've made an error or omitted something here, feel free to edit this post the wiki.
@longjon Thank you very much! :-D
I still have a silly question and I really hope you can give me a hint. I want to debug the source code but I don't know how. I see the Windows version of caffe. I can launch my Visual Studio and put a break point in the train_net.cpp and follow the code step by step when I debug. I really have no idea how to do that in linux. I'm not sure if I need an IDE or something else. I've been stuck for days. If you can share your way of doing it, it will help me a lot. Thanks!
Many of the caffe devs use gdb, which is the usual choice for debugging C/C++ on Linux.
@longjon Great! Thank you for all your help! I will try to learn gdb.
@longjon we could put this steps in wiki?
@bhack good idea, I've copied this at https://github.com/BVLC/caffe/wiki/Development-Hints. Edit as you see fit, it's a wiki.
Closing -- documented in the wiki. Thanks Jon!
Is it a must to write a test file ?
@longjon Hi, longion
I have the same question as kuaitoukid, "Is it a must to write a test file"?
Yes you need to write test for new layers.
On Monday, December 29, 2014, Xeraph [email protected] wrote:
@longjon https://github.com/longjon Hi, longion
I have the same question as kuaitoukid, "Is it a must to write a test
file"?—
Reply to this email directly or view it on GitHub
https://github.com/BVLC/caffe/issues/684#issuecomment-68314227.
Sergio
To elaborate a bit, of course you can do whatever you want with your own layers, but if you are doing anything nontrivial, gradient checking is crucial in ensuring a correct implementation.
For PRs to Caffe, code needs to be sufficiently well-tested that we have confidence in its correctness.
It is not a must, but safe to write a test file
@longjon Hi,I followed your step and I have added my new layer,however,when I run caffe,I encountered an error:segmentation fault(core denied),would you please tell me what happend?
@longjon good~
@wshenx You can use QtCreator + Cmake combination that I used in Linux.
If I want to use GPU to train net, and I only write the cpp code, no .cu file, Is it OK?
@artiit Then your Caffe only runs on CPU. By default, Caffe has a safe fall-back mechanism when you do not have GPU CUDA implementation.
For future reference, the wiki page has apparently moved to https://github.com/BVLC/caffe/wiki/Development
@williford I follow the step, but when I train the model, it says unknown layer type.. Do I have to remake caffe after I write the new layer?
It takes so long to test all the layers. Is there a way to only test a specific layer?
Update: I figured that out. First
test/test.testbin --gtest_list_tests
to find out the name of test case for that layer. Then run
test/test.testbin --gtest_filter=<the name of test case>*
Here is the complete command I use to recompile caffe, recompile the tests, and run the tests of the layer I'm working on:
make all && make test && build/test/test_all.testbin --gtest_filter='*LocalLayerTest*'"
use Code::Blocks in linux,just like visual studio in windows,very easy
@cuijianaaa I am trying to use code::block to speed up the testing but cannot manage to start a project with existing files, and I am not confident enough to work directly in the caffe home directory (caffe is finaly working and I would like to keep it that way :) )
could you tell me how to create a project from a copy of the caffe directory?
or if this is absolutely the wrong way to do it please tell me I am new to programming
Most helpful comment
Update: see wiki at https://github.com/BVLC/caffe/wiki/Development-Hints
Here's roughly the process I follow.
common_layers.hpp,data_layers.hpp,loss_layers.hpp,neuron_layers.hpp, orvision_layers.hpp. Include an inline implementation oftypeand the*Blobs()methods to specify blob number requirements. Omit the*_gpudeclarations if you'll only be implementing CPU code.layers/your_layer.cpp.SetUpfor initialization: reading parameters, allocating buffers, etc.Forward_cpufor the function your layer computesBackward_cpufor its gradientForward_gpuandBackward_gpuinlayers/your_layer.cu.proto/caffe.proto, updating the next available ID. Also declare parameters, if needed, in this file.layer_factory.cpp.test/test_your_layer.cpp. Usetest/test_gradient_check_util.hppto check that your Forward and Backward implementations are in numerical agreement.Since this is a many step process, I thought it worth recording here. I would welcome improvements to Caffe that make the layer adding process less involved.
BVLC folkAll, if I've made an error or omitted something here, feel free to editthis postthe wiki.