Extracting filters from caffemodel using matlab for super-resolution

166 views
Skip to first unread message

Antonio Paes

unread,
May 19, 2016, 4:06:50 PM5/19/16
to Caffe Users
Hi guys, 

I'm trying extract filters from caffemodel for applying on an image and enhance the resolution. For conv layers works ok, but, i'm using reshape and concat layers, and this layers are crucial for performance of my method. I'm using this code: 

caffe.reset_all();
clear; close all;
%% settings
folder = '/home/antonio/face_hallucination/matlab_codes/';
model = [folder 'fh_4x_net.prototxt'];
weights = [folder 'caffenet_fh_gln_decout1_iter_140000.caffemodel'];
savepath = [folder 'xn_gln_dcou1.mat'];
layers = 5;

%% load model using mat_caffe
net = caffe.Net(model,weights,'test');

%% reshap parameters
weights_conv = cell(layers,1);

for idx = 1 : layers
    
    if (idx == 1)
        idx = idx -1;
    end
    conv_filters = net.layers(['conv' num2str(idx)]).params(1).get_data();
    if (idx == 0)
        idx = idx+1;
    end
    [~,fsize,channel,fnum] = size(conv_filters);
   
    if channel == 1
        weights = double(ones(fsize^2, fsize));
    else
        weights = double(ones(channel, fsize^2, fnum));
    end
    
    for i = 1 : channel
        for j = 1 : fnum
             temp = conv_filters(:,:,i,j);
             if channel == 1
                weights(:,j) = temp(:);
             else
                weights(i,:,j) = temp(:);
             end
        end
    end

    weights_conv{idx} = weights;
end

%% save parameters
weights_conv1 = weights_conv{1};
weights_conv2 = weights_conv{2};
weights_conv3 = weights_conv{3};
weights_conv4 = weights_conv{4};
weights_conv5 = weights_conv{5};
biases_conv2 = net.layers('conv2').params(2).get_data();
biases_conv3 = net.layers('conv3').params(2).get_data();
biases_conv4 = net.layers('conv4').params(2).get_data();
biases_conv5 = net.layers('conv5').params(2).get_data();

save(savepath,'weights_conv1','weights_conv2','biases_conv2','weights_conv3','biases_conv3', 'weights_conv4', 'biases_conv4', 'weights_conv5', 'biases_conv5');


Someone can help me?
Reply all
Reply to author
Forward
0 new messages