forked from amolchanov86/matcaffe_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caffe_save_net.m
172 lines (148 loc) · 6.66 KB
/
caffe_save_net.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function [ out ] = caffe_save_net( filename, header, net_descr, data_block )
%% Description
% Function generates *.prototxt file from structure provided
% Net desctiption is organized as following:
% - header structure (containing the network's name: Ex: header.name = 'MyNet'; )
% - net_descr structure is a cell array. Every cell is interpreted as
% 'layer' structure of the proto file. Every structure of the proto file
% is represented as matlab structure. Every value field as variable of
% the structure. If a proto structure contains several variables with the
% same name then one must create a cell array with the name corresponding
% to the name of the variable (for example it is often the case for 'bottom'
% variable since some layers can accept inputs from several different
% layers.
%
% --- INPUTS:
% [ out ] = caffe_save_net( filename, header, net_descr )
% filename = file name
% net_descr = net description is a cell array with structures describing
% particular layers.
% header = network header structure. It must contain 'name' parameter, the
% rest (such as inputs for the deployment network) is optional
%% Parameters
extension = '.prototxt';
indent = ' ';
%% Execution
% Correct if extension already present
if length(filename ) >= 10
if strcmp( filename(end-8:end), extension)
filename = filename(1:end-9);
end
end
% Open file to write
fileID = fopen( [filename extension], 'w');
indent_cur = '';
% Do the job by calling functions recursively
function var_type = caffe_var_type(var)
if isstruct( var )
var_type = 1;
else
if iscell( var )
var_type = 2;
else
var_type = 0;
end
end
end
function caffe_save_net_head( file_id, str_in, indent, indent_cur, data_blob)
%Saving the name as a first parameter (some minor fool-proof)
caffe_save_net_param( file_id, str_in.name, 'name', '', indent_cur);
str_in = rmfield( str_in, 'name');
if exist('data_blob', 'var') && ~isempty(data_blob)
fprintf(file_id, data_blob);
end
%Saving the rest of parameters
parameter_names = fieldnames(str_in);
for var_i=1:length(parameter_names)
% fprintf('Field name = %s \n', parameter_names{var_i} );
var_type = caffe_var_type( str_in.(parameter_names{var_i}) );
switch var_type
case 0
caffe_save_net_param(file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, '', indent_cur );
case 1
caffe_save_net_str (file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, indent, indent_cur );
case 2
caffe_save_net_cell (file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, '', indent, indent_cur);
end
end
end
function caffe_save_net_str( file_id, str_in, str_name, indent, indent_cur)
parameter_names = fieldnames(str_in);
fprintf(file_id, '%s%s {\n', indent_cur, str_name);
for var_i=1:length(parameter_names)
var_type = caffe_var_type( str_in.(parameter_names{var_i}) );
switch var_type
case 0
caffe_save_net_param(file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, str_name, [indent_cur indent] );
case 1
caffe_save_net_str (file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, indent, [indent_cur indent]);
case 2
caffe_save_net_cell (file_id, str_in.(parameter_names{var_i}), parameter_names{var_i}, str_name, indent, [indent_cur indent]);
end
end
fprintf(file_id, '%s}\n', indent_cur);
end
function caffe_save_net_cell(file_id, cell_in, cell_name, parent_name, indent, indent_cur)
for cell_i=1:length(cell_in)
var_type = caffe_var_type( cell_in{cell_i} );
switch var_type
case 0
caffe_save_net_param(file_id, cell_in{cell_i}, cell_name, parent_name, indent_cur );
case 1
caffe_save_net_str (file_id, cell_in{cell_i}, cell_name, indent, indent_cur);
end
end
end
function caffe_save_net_param(file_id, var_in, var_name, parent_name, indent_cur)
if isinteger(var_in)
var_str_in = sprintf('%d', var_in);
num_stat = 1;
else
if isnumeric(var_in)
var_str_in = sprintf('%.10g', var_in);
num_stat = 1;
else
var_str_in = var_in;
[num, num_stat] = str2num( var_in );
end
end
no_quot = 0;
no_quot = no_quot || strcmp( 'pool', var_name ) && strcmp( 'pooling_param', parent_name );
no_quot = no_quot || strcmp( 'phase', var_name ) && strcmp( 'include', parent_name );
no_quot = no_quot || strcmp( 'mirror', var_name ) && strcmp( 'transform_param', parent_name );
no_quot = no_quot || strcmp( 'backend', var_name ) && strcmp( 'data_param', parent_name );
if num_stat || no_quot
fprintf(file_id, '%s%s: %s\n', indent_cur, var_name, var_str_in );
else
fprintf(file_id, '%s%s: \"%s\"\n', indent_cur, var_name, var_str_in );
end
end
% Filling in the header
fprintf('%s : saving header ... \n', mfilename);
if exist('data_block', 'var')
caffe_save_net_head( fileID, header, indent, indent_cur, data_block);
else
caffe_save_net_head( fileID, header, indent, indent_cur);
end
% Iterate through layers writing them into the file
fprintf('%s : saving body and loss ... \n', mfilename);
% some cells are cell array themself, so we will flatten them
net_descr_ = {};
for i_layer = 1:length(net_descr)
net_descr_ = [net_descr_ net_descr{i_layer}];
end
for layer_i=1:length(net_descr_)
if isfield(net_descr_{layer_i}, 'name')
fprintf('Layer %d: name = %s : ', layer_i, net_descr_{layer_i}.name );
else
fprintf('Layer %d ... ', layer_i);
end
bottom_str = net_descr_{layer_i}.bottom;
if iscell(bottom_str), bottom_str = strjoin(bottom_str, ','); end
top_str = net_descr_{layer_i}.top;
if iscell(top_str), top_str = strjoin(top_str, ','); end
fprintf('%s --> %s\n', bottom_str, top_str);
caffe_save_net_str( fileID, net_descr_{layer_i}, 'layer', indent, indent_cur);
end
fclose(fileID);
end