Skip to content

Instantly share code, notes, and snippets.

@bearpaw
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearpaw/5c38196a3f0c755621b8 to your computer and use it in GitHub Desktop.
Save bearpaw/5c38196a3f0c755621b8 to your computer and use it in GitHub Desktop.
convert-matcaffe3-model-to-matcaffe
deploy_file = '/home/wyang/code/pose/chen-nips14-pose/external/my_models/lsp/lsp_deploy_conv_grabcut.prototxt';
weights_file = '/home/wyang/code/pose/chen-nips14-pose/cache/lsp/fully_conv_net_grabcut.caffemodel';
save_model_file = 'fully_conv_net_grabcut_matcaffe1.caffemodel';
% try
% % ------- Set params for old caffe model
% load('trans_params.mat');
% addpath('/home/wyang/code/caffe-xianjiec/matlab/caffe');
% set_weights(deploy_file, weights_file, trans_params, save_model_file);
% rmpath('/home/wyang/code/caffe-xianjiec/matlab/caffe');
% catch
% ------- Read params from new caffe model
addpath('~/code/caffe-bearpaw/matlab');
trans_params = get_weights(deploy_file, weights_file);
rmpath('~/code/caffe-bearpaw/matlab');
save('trans_params.mat', 'trans_params');
% end
% addpath('/home/wyang/code/caffe-xianjiec/matlab/caffe');
% caffe('reset');
% caffe('init', deploy_file, save_model_file);
% w = caffe('get_weights');
% caffe('reset');
% rmpath('/home/wyang/code/caffe-xianjiec/matlab/caffe');
function trans_params = get_weights(deploy_file, weights_file)
caffe.set_mode_cpu();
phase = 'test';
% Initialize a network
net = caffe.Net(deploy_file, weights_file, phase);
nlayers = length(net.layer_names);
% get params of each layer who has parameters
trans_params = struct('weights', 'layer_names');
trans_params_cnt = 0;
for i = 1:nlayers
weights = cell(2, 1);
try
weights{1} = net.params(net.layer_names{i}, 1).get_data();
catch
% do nothing
end
try
weights{2} = net.params(net.layer_names{i}, 2).get_data();
catch
% do nothing
end
if ~isempty(weights{1}) || ~isempty(weights{2})
fprintf('%s has params\n', net.layer_names{i});
trans_params_cnt = trans_params_cnt + 1;
trans_params(trans_params_cnt).weights = weights;
trans_params(trans_params_cnt).layer_names = net.layer_names{i};
end
end
% clear network
caffe.reset_all();
function set_weights(deploy_file, weights_file, trans_params, save_model_file)
caffe('reset');
caffe('init', deploy_file, weights_file);
caffe('set_weights', trans_params);
caffe('save', save_model_file);
caffe('reset');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment