caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Extract filters and bias

Open jensen810814 opened this issue 11 years ago • 6 comments

Hi all, I want to know how can i only extract filters and biases from my own caffemodel. I read the tutorial of " filter visualization example", but i dont know how to do when using my own caffemodel.(I dont know which part i should modify in that reference code) I want to extract filters and bisaes and then save into .mat file for MATLAB. can anyone help me? please and thanks.

jensen810814 avatar Apr 13 '15 11:04 jensen810814

I used matcaffe (the matlab wrapper) for the same issue

  1. open matlab
  2. make sure the path to your matcaffe (caffe_root/matlab/caffe) is in the defpath. If not, add it manually or write: addpath(your path); in matlab
  3. initialize your caffe Net by writing: caffe('init', 'your_net_definition.prototxt','your_model.caffemodel').
  4. do a forward pass by writing: caffe('forward', {});
  5. and then extract the weights: weights = caffe('get_weights'); "weights" will contain the weights and biases of all the layers. You will see it from its structure when you open it
  6. save "weights" into a mat file: save('path_to_folder/filename.mat', 'weights');

userfou avatar Apr 13 '15 22:04 userfou

Thanks for your answer! I did it step by step, but still have error : Error using caffe Expected 3 arguments, got 2

Error in model (line 4) caffe('init','deploy.prototxt','super_resolution_iter_1000.caffemodel');

And the following is my code:

addpath('/home/jensen810814/code/caffe/matlab/caffe/'); addpath('/home/jensen810814/code/caffe/examples/super_resolution/'); addpath('/home/jensen810814/code/caffe/examples/super_resolution/Model/'); caffe('init','deploy.prototxt','super_resolution_iter_1000.caffemodel'); caffe('forward',{}); weights = caffe('get_weights'); save('/home/jensen810814/code/caffe/examples/super_resolution/filter_data.mat','weights');

Can you tell me how to deal with it? thanks a lot. (btw, did the 'your_net_definition.prototxt' mean deploy.prototxt or train_protptxt which i used to train my net?)

jensen810814 avatar Apr 15 '15 04:04 jensen810814

I meant the train.prototxt (or train_test.prototxt if you defined both train and test nets in one prototxt file). Does it work if you use this prototxt file?

userfou avatar Apr 15 '15 13:04 userfou

I tried both files...but still have same problem QAQ.

jensen810814 avatar Apr 15 '15 16:04 jensen810814

I solve this question. Thanks again.

jensen810814 avatar Apr 17 '15 10:04 jensen810814

@jensen810814 how did you solve the question that defined both train and test nets in one prototxt file?

galoiszhang avatar Sep 26 '15 06:09 galoiszhang