caffe icon indicating copy to clipboard operation
caffe copied to clipboard

weights

Open ghost opened this issue 11 years ago • 13 comments

I am writing a c program for testing a CNN ...can any one suggest procedure to load the weights and biases from the caffe generated .caffemodel

basically i need to read the .caffemodel in C thanks in advance

ghost avatar Feb 20 '15 09:02 ghost

  1. You need to define a ConvNet structure in c program, which has the same layout with the caffemodel you've trained.
  2. Load the ***,caffemodel using source code of caffe, read each layers' weights and bias to meomry.
  3. Assign the weights and bias to the ConvNet structure you defined in (1)
  4. Write the ConvNet structure to a binary file in your own way.
  5. Read the ConvNet structure from the binary file as what you did in (4) to test whether it's OK or not.

chssozxw avatar Feb 20 '15 14:02 chssozxw

@Rventric Thanks you for reply my idea is to test the caffemodel which i have trained in caffe cant i directly use the .caffemodel and write a test case for the same in C and can you be more specific about the Write the ConvNet structure to a binary file in your own way. can you provide me with an example source code of C @Rventric thanks

ghost avatar Feb 20 '15 15:02 ghost

@Rventric can you eloborate about the 2 and 3 points you mentioned .

ghost avatar Feb 20 '15 15:02 ghost

Uhm..., If you just want a test case for your trained caffemodel, why not use the tools/caffe.cpp ?

chssozxw avatar Feb 20 '15 15:02 chssozxw

@Rventric Thankyou Absolutely ..But I am trying to the replicate the framework in C
Can you suggest me something else

ghost avatar Feb 20 '15 15:02 ghost

Ah, I got what you means. I once write some codes, but now they are not at hand. The key is you need a c program that can convert the caffemodel from protobuf file format to your own defined file format I think you can do like this:

(1) Define your own c-struct of the Net and some layers like dataLayer, convLayer, poolLayer and InnerProductLayer and so on:

struct ConvNet { DataLayer data_layer; ConvLayer conv_layer1; PoolLayer pool_layer1; ConvLayer conv_layer2; PoolLayer pool_layer2; ..... InnerProductLayer fc_layer1; SoftmaxLayer softmax_layer;

float* input_data;
float* conv1_data;
float* conv2_data;
....
float* fc1_data;
....

};

struct ConvLayer { ConvLayerParmater paramter; float* weight; float* bias; };

struct ConvLayerParameter { int weight_channels; int weight_height; int weight_width; ..... };

and you also should complete each Layer's defination, which contains a layer's parameters(e.g. channels, height, width) and some data members(e.g. float* ptr_weight, float* ptr_bias). About this, ytou can refer to the implementation of caffe source code.

(2) You can rewrite the toos/caffe.cpp to load the caffemodel to memory, and for each layer, you can use function layer.blobs() to get the weight blob and bias blob pointers and assign them to your own data structures properly. Also, you should get the weight and bias's dimension parameters from caffemodel.

(3) After all the members of your c-struct ConvNet are assigged to proper values, you can write the c-struct to the binary file.

Of course, when test the model, you also should implement the forward propagation of the ConvNet.

chssozxw avatar Feb 20 '15 16:02 chssozxw

@Rventric Great !!!!....thank you

ghost avatar Feb 20 '15 16:02 ghost

@Rventric can you send me the code you tried before so that i can use it as ref when will you get them handy thanks in advance @Rventric

ghost avatar Feb 20 '15 18:02 ghost

@Rventric
connected_layer _parse_connected(list *options, network *net, int count) { int input; float learning_rate, momentum, decay; int output = option_find_int(options, "output",1); char *activation_s = option_find_str(options, "activation", "sigmoid"); ACTIVATION activation = get_activation(activation_s); if(count == 0){ input = option_find_int(options, "input",1); net->batch = option_find_int(options, "batch",1); learning_rate = option_find_float(options, "learning_rate", .001); momentum = option_find_float(options, "momentum", .9); decay = option_find_float(options, "decay", .0001); net->learning_rate = learning_rate; net->momentum = momentum; net->decay = decay; }else{ learning_rate = option_find_float_quiet(options, "learning_rate", net->learning_rate); momentum = option_find_float_quiet(options, "momentum", net->momentum); decay = option_find_float_quiet(options, "decay", net->decay); input = get_network_output_size_layer(_net, count-1); } connected_layer _layer = make_connected_layer(net->batch, input, output, activation,learning_rate,momentum,decay); char *weights = option_find_str(options, "weights", 0); char *biases = option_find_str(options, "biases", 0); parse_data(biases, layer->biases, output); parse_data(weights, layer->weights, input_output); #ifdef GPU if(weights || biases) push_connected_layer(*layer); #endif option_unused(options); return layer; }

ghost avatar Feb 21 '15 08:02 ghost

Thanks for discussing caffe, but please discuss usage questions on caffe-users -- this issues tracker is primarily for Caffe development discussion. Thanks!

rohrbach avatar Feb 22 '15 04:02 rohrbach

@Rventric where can i find weights of the fully connected layer thanks in advance

ghost avatar Feb 26 '15 15:02 ghost

@Rventric feed up writing the code for parsing the .caffemodel to C could you help me with a source code for this in C

ghost avatar Feb 27 '15 05:02 ghost

@Rventric

ghost avatar Feb 28 '15 18:02 ghost