about the weights of the first fully-connected layer after the convolutional layers

269 views
Skip to first unread message

Emre Can Kaya

unread,
Apr 26, 2016, 11:18:47 AM4/26/16
to Caffe Users
dear caffe users;
I was trying to convert a trained caffe network (stored in .caffemodel format) into a Matlab struct which is to be used with Matconvnet. It is a LeNet-5 with: conv1-pool1-conv2-pool2-fc1-relu-fc2-softmax
The feature map (output) of the pool2 layer has size 4x4x80 and this output is to be fully connected to 500 neurons at fc1. when I use  the matlab interface command of caffe :

weights=net.params('fc1', 1).get_data() to store the weights into an array, I obtain a 2 dimensional single array of size 1280x500. However, for Matconvnet, I need to know exactly which weight is to be connected to which filter output ,that is , I need to have a 4-D array having size 4x4x80x500.

Does anyone know how to do this??
 Thank You

Jan

unread,
Apr 27, 2016, 4:44:31 AM4/27/16
to Caffe Users
As caffe simply reshapes the computed features into a single vector (for every data point) when going from a pooling/conv-layer to a fc layer, you should also be able to just reshape the 1280x500 into a 4x4x80x500 matrix. The storage order is different in C (and hence, caffe) than in Matlab, but that is compensated by looking at the transposed matrices, as is already done.

Jan

Emre Can Kaya

unread,
Apr 27, 2016, 5:53:03 AM4/27/16
to Caffe Users
so what should I do?where should I look? where are those transposed matrices ? could you be more specific?




27 Nisan 2016 Çarşamba 11:44:31 UTC+3 tarihinde Jan yazdı:

Emre Can Kaya

unread,
Apr 27, 2016, 5:53:09 AM4/27/16
to Caffe Users
so what should I do?where should I look? where are those transposed matrices ? could you be more specific?




27 Nisan 2016 Çarşamba 11:44:31 UTC+3 tarihinde Jan yazdı:
As caffe simply reshapes the computed features into a single vector (for every data point) when going from a pooling/conv-layer to a fc layer, you should also be able to just reshape the 1280x500 into a 4x4x80x500 matrix. The storage order is different in C (and hence, caffe) than in Matlab, but that is compensated by looking at the transposed matrices, as is already done.

Jan

unread,
Apr 27, 2016, 6:52:33 AM4/27/16
to Caffe Users
weights = net.params('fc1', 1).get_data(); % should have shape [1280, 500]
reshaped_weights
= reshape(weights, [4, 4, 80, 500]);

reshaped_weights is then a matrix that contains the same values as weights, but with the dimensions (aka axes) you want.

Jan
Reply all
Reply to author
Forward
0 new messages