Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to develop new layers #684

Closed
wshenx opened this issue Jul 14, 2014 · 23 comments
Closed

how to develop new layers #684

wshenx opened this issue Jul 14, 2014 · 23 comments
Labels

Comments

@wshenx
Copy link

wshenx commented Jul 14, 2014

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.

@longjon
Copy link
Contributor

longjon commented Jul 19, 2014

Update: see wiki at https://github.com/BVLC/caffe/wiki/Development-Hints

Here's roughly the process I follow.

  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.

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.

@wshenx
Copy link
Author

wshenx commented Jul 22, 2014

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

@longjon
Copy link
Contributor

longjon commented Jul 23, 2014

Many of the caffe devs use gdb, which is the usual choice for debugging C/C++ on Linux.

@wshenx
Copy link
Author

wshenx commented Jul 23, 2014

@longjon Great! Thank you for all your help! I will try to learn gdb.

@bhack
Copy link
Contributor

bhack commented Jul 23, 2014

@longjon we could put this steps in wiki?

@longjon
Copy link
Contributor

longjon commented Jul 23, 2014

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

@shelhamer
Copy link
Member

Closing -- documented in the wiki. Thanks Jon!

@kuaitoukid
Copy link

Is it a must to write a test file ?

@wangdelp
Copy link

@longjon Hi, longion
I have the same question as kuaitoukid, "Is it a must to write a test file"?

@sguada
Copy link
Contributor

sguada commented Dec 29, 2014

Yes you need to write test for new layers.

On Monday, December 29, 2014, Xeraph notifications@github.com 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
#684 (comment).

Sergio

@longjon
Copy link
Contributor

longjon commented Dec 29, 2014

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.

@kuaitoukid
Copy link

It is not a must, but safe to write a test file

@ghost
Copy link

ghost commented Dec 7, 2015

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

@gucasdongzi
Copy link

@longjon good~

@antran89
Copy link
Contributor

@wshenx You can use QtCreator + Cmake combination that I used in Linux.

@artiit
Copy link

artiit commented Apr 11, 2016

If I want to use GPU to train net, and I only write the cpp code, no .cu file, Is it OK?

@antran89
Copy link
Contributor

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

@williford
Copy link
Contributor

For future reference, the wiki page has apparently moved to https://github.com/BVLC/caffe/wiki/Development

@LinHungShi
Copy link

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

@misaka-10032
Copy link

misaka-10032 commented Jun 23, 2016

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

@matthieudelaro
Copy link

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*'"

@cuijianaaa
Copy link

use Code::Blocks in linux,just like visual studio in windows,very easy

@PhilippeCb
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

17 participants