caffemodel转matlab格式

来源:互联网 发布:如何提高情商 知乎 编辑:程序博客网 时间:2024/06/04 19:49

1).MATLAB示例程序:

<code class="language-matlab hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">%% Load the Caffe.Net and save in model file.</span>def = fullfile(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'..'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'models'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'VGG_CNN_M_1024'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'test.prototxt'</span>);net = fullfile(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'..'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'output'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'default'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'voc_2007_trainval'</span>, ...<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'vgg_cnn_m_1024_fast_rcnn_iter_40000.caffemodel'</span>);ConvNet = <span class="hljs-transposed_variable" style="box-sizing: border-box;">caffe.</span>Net(def, net, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'test'</span>);save model/ConvNet ConvNet</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

其中,def为网络的test.prototxt文件路径,net为caffe训练得到caffemodel文件路径。调用caffe.Net可以获得MATLAB可以解析的数据形式(矩阵形式),

2)完整示例:将weights和bias转为.mat格式:

 %layers中的parameter(1)表示权重参数,parameter(2)表示偏置参数;
%从网络结构中抽取权重和偏置矩阵,并进行保存;
caffe.reset_all();%清除已经创建的所有solver和独立net;
clear; close all;
%% settings
folder = 'F:/mycode_matlab/文献/caffe/caffe-windows-master/examples/SRCNN_train/';
model = [folder 'SRCNN_mat.prototxt'];
weights = [folder 'SRCNN_iter_500.caffemodel'];
% weights=[folder 'train.h5'];
savepath = [folder 'x3.mat'];
layers = 3;
%% load model using mat_caffe
net = caffe.Net(model,weights,'test');%创建一个网络;

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

for idx = 1 : layers
    conv_filters = net.layers(['conv' num2str(idx)]).params(1).get_data();
    [~,fsize,channel,fnum] = size(conv_filters);


    if channel == 1
        weights = single(ones(fsize^2, fnum));
    else
        weights = single(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};
biases_conv1 = net.layers('conv1').params(2).get_data();
biases_conv2 = net.layers('conv2').params(2).get_data();
biases_conv3 = net.layers('conv3').params(2).get_data();

save(savepath,'weights_conv1','biases_conv1','weights_conv2','biases_conv2','weights_conv3','biases_conv3');

0 0
原创粉丝点击