Search code examples
c++deep-learningcaffecudnn

How to initilize convolution layer in C++ instead of prototext?


I am trying to import the the layer class "CuDNNConvolutionLayer" provided by caffe into my own project (a pure C++ projects). Therefore I cannot use prototxt to specify the layer. Instead, I have to send parameters to the layer manually when I declare it. (I am not sure is this way correct, I am just exploring)

I did this:

int param={20,5,1,0,1,1,???,???}; caffe::CuDNNConvolutionLayer<float> cv1(*param);

The last two parameters in the param are set to initialize "bias_term" (true or false) and "engine" (caffe or CUDNN). As I mentioned I cannot ignore these two values since this is not prototxt, so what value I supposed to fill here, if I want to use CUDNN engine and have no bias term? (I already declare the param as int so i think and expect the answer should be either 0, 1- hope so)

Operating system: ubuntu 18.04
Compiler: gcc
CUDA version (if applicable): 10.0
CUDNN version (if applicable): v7
BLAS:
Python version (if using pycaffe):
MATLAB version (if using matcaffe):

Solution

  • CuDNNConvolutionLayer constructor accepts a reference to LayerParameter protobuf object (not int*). So you have to create an empty LayerParameter object with default constructor, and add the necessary fields to it with its setter methods. As far as I know, {}-style initialization is not supported by ProtoBuf.

    Another way to initialize LayerParameter is by parsing a string literal with its ProtoText representation.

    Reading the documentation of "ProtoBuf C++ generated code" will help.

    Also, check the source of Scale layer for an example of creation of FillerParameter and LayerParameter to construct a layer.