vlfeat / matconvnet

MatConvNet: CNNs for MATLAB
Other
1.4k stars 753 forks source link

How to convert the trained model in MatConvNet to the model file that Caffe can easily use ? #225

Open CASIAHYH opened 9 years ago

CASIAHYH commented 9 years ago

Hi, guys, I want to use the trained model (in the form of mat file with variable {layers} ) produced by MatConvNet in Caffe. If anyone has such experience ? An existed convertion matlab code is encouraged!! Thanks a lot.

rahman-mdatiqur commented 9 years ago

following..

layumi commented 9 years ago

I am a green hand in deeplearning and I met the same problem. I solve it as follow: Install matcaffe first and write a prototxt for your own net. Then just insert values from matconvnet model. I have written a simple example to convert matconvnet model to caffemodel by using matcaffe, which may include some errors. https://github.com/layumi/Batch-Normal-For-Caffe/tree/master/Tool Hope it helps.

zoharby commented 8 years ago

see PR #298, I wrote a function to export the models to Caffe (inspired by layumi's code): https://github.com/zoharby/matconvnet/blob/master/utils/convert_matconvnet_caffe.m

avishayzanbar commented 8 years ago

hi, i am new user in caffe.. when trying your script it fails due to 'Undefined variable "caffe" or class "caffe.set_mode_cpu"'. I am using ubuntu with caffe configured with matlab. thank you

zoharby commented 8 years ago

It means you have not installed the Caffe MATLAB interface properly. Please ensure you are using the latest Caffe master version, and that you have compiled the MATLAB interface of Caffe correctly. Lastly, ensure that the Caffe's "matlab" folder is in your MATLAB path.

avishayzanbar commented 8 years ago

Thank you very much Zohar, after the installation of caffe with matlab, do i have to do anything before running the convert script in matlab?

zoharby commented 8 years ago

Nothing special, just make sure the caffe/matlab folder is in the MATLAB path.

avishayzanbar commented 8 years ago

It works, thanks a lot

DongXingshuai commented 8 years ago

@zoharby Hi, zoharby. I am new user in MatConvNet, when I trying your function it fails and report : The number of filter groups does not divide the number of filters. Error in vl_simplenn (line 193) res(i+1).x = vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, ...

Error in convert_matconvnet_caffe (line 253) res = vl_simplenn(layer, dummy_data) The MatConvNet I use was download from:https://github.com/zoharby/matconvnet Thanks a lot!

zoharby commented 8 years ago

@DongXingshuai - can you send me a link to download the inputs you are using (your network and the test image/data you are using)? I will have a look

DongXingshuai commented 8 years ago

@zoharby Hi, zoharby ! Thank you for your kindly reply. The first link is the model , and the second link is the test image. This image is the raw version, in the code I convert the image to 256x256x3. Thank you! https://www.dropbox.com/s/ug78wqclo7bspum/model_without_order_info_256.mat?dl=0 https://www.dropbox.com/s/wj73mzryuzl38xy/84.png?dl=0

zoharby commented 8 years ago

@DongXingshuai you are feeding the network with a 256x256x3 image, while your network seems your network expects an input with a single channel (1st convolution is layer 15x15x1, not 15x15x3).

Also, please use this updated version of my script (which contains a fix for single channel inputs): https://github.com/zoharby/matconvnet/blob/devel/utils/export_caffe_model.m

khurram-amin commented 8 years ago

@layumi I am trying to convert a cnn written in matconvnet into caffe version by following your technique. One problem that i am facing is that the network contains a fully connected layer and these are implemented in matconvnet as convolution-layer. Now when I try to copy weights from this layer (from matconvnet) to caffe's FC layer it gives a shape error.

input data/diff size: [ 6 6 256 6144 ] vs target blob shape: [ 9216 6144 ]

This is expected. The thing I cannot figure out is how to properly reshape this matrix. Should I just reshape it as [6x6x256, 6144] matrix? Will this be correct or do i need to apply some dimension flips before doing this?

zoharby commented 8 years ago

@khurram-amin Try my script, it handles this case.

My original script: https://github.com/zoharby/matconvnet/blob/devel/utils/export_caffe_model.m

The version adapted by MatConvNet's team: https://github.com/vlfeat/matconvnet/blob/simplenn-caffe-deploy/utils/simplenn_caffe_deploy.m

khurram-amin commented 8 years ago

@zoharby There is some issue in vl_simplenn file of matconvnet and I am unable to run network and it seems that your script is also calling this function.

Can you please explain this reshaping issue here. It 'd be really helpful.

khurram-amin commented 8 years ago

% Convert from HxWxCxN to WxHxCxN per Caffe's convention filters = permute(filters, [2 1 3 4]);

% Fully connected layer, squeeze to 2 dims filters = reshape(filters, [], size(filters, 4));

% RGB 2 BGR conversion filters = filters(:,:, [3 2 1], :); % Do this only for first convolution;

I guess these are the relevant lines in matconvnet's script.

zoharby commented 8 years ago

% Convert from HxWxCxN to WxHxCxN per Caffe's convention filters = permute(filters, [2 1 3 4]); This is needed because MATLAB's memory layout is different than Caffe.


% Fully connected layer, squeeze to 2 dims filters = reshape(filters, [], size(filters, 4)); This is the trick needed for the fully connected layers


% RGB 2 BGR conversion filters = filters(:,:, [3 2 1], :); % Do this only for first convolution; MATLAB works with RGB images, while Caffe usually assumes BGR images (of course depends how you load them, but this is the convention)

khurram-amin commented 8 years ago

@zoharby Thank you. It really helped.

avishayzanbar commented 8 years ago

hello, i am trying to convert a certain network created in MatConvNet to Caffe using @zoharby script added above. in this network, the 2 last layers are FC. from some reason, the set_data command of the first FC layer is being failed due to:

input data/diff size does not match target blob shape, input data/diff size: [ 7680 512 ] vs target blob shape: [ 12288 512 ]

the thing is, the weights sizes are 3x5x512x512 which should be reshaped to 7680x512 as it does.

does anyone knows what might be the problem in this case or why it expects to size 12288?

thank you

wadesunyang commented 7 years ago

@zoharby , @layumi thank you for this solution. but i find that MatConvNet treats pooling operators is a little different from caffe. for the output size, one use the function "ceil", and the other use "floor" ,and I want to know how to solve this problem. Especially i want to change it to the "fullconnected net",so the input size is not sure, but your code didn't mentioned this problem. thank you very much.

avishayzanbar commented 7 years ago

hi, i am trying to convert a trained MatConvNet DagNN to Caffe, does anyone have an example for such net in Caffe?

is it possible? thanks a lot

hunterlew commented 7 years ago

@zoharby Thanks for your contribution. But why my conversion reports this error at the beginning?

load('net-epoch-100.mat') export_caffe_model(net,'test') Index exceeds matrix dimensions.

Error in export_caffe_model (line 36) net.layers{idx}.weights{1} = gather(net.layers{idx}.weights{1});

zoharby commented 7 years ago

@hunterlew A cleaner version of my code is now merged to the master branch: https://github.com/vlfeat/matconvnet/blob/master/utils/simplenn_caffe_deploy.m

Please try using it instead, hopefully it is a bit more robust

gperezs commented 7 years ago

Hello @zoharby! your function looks great, does it support batch normalization layers from matconvnet?

h612 commented 7 years ago

Hello I am trying to use Deep Learning in Matlab- I do not have a GPU in my laptop. I just came across one model which I want to use. However it is with extension model = 'deploy.prototxt'; How can I convert .prototxt to .mat?

ZiangYan commented 7 years ago

Thanks @layumi ! Your method works for me. Matconvnet and caffe have similar storage format.