forked from zuoqing1988/ZQCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_spherefacenet20_caffemodel_to_nchw_binary.m
157 lines (145 loc) · 4.01 KB
/
export_spherefacenet20_caffemodel_to_nchw_binary.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
function export_spherefacenet20_caffemodel_to_nchw_binary()
caffe.set_mode_cpu();
caffe_net = caffe.Net('sphereface_deploy.prototxt', 'sphereface_model_iter_28000.caffemodel', 'test');
layers = {
'conv1_1',2,'none';
'relu1_1',1,'none';
'conv1_2',2,'none';
'relu1_2',1,'none';
'conv1_3',2,'none';
'relu1_3',1,'none';
'res1_3',0,'none';
'conv2_1',2,'none';
'relu2_1',1,'none';
'conv2_2',2,'none';
'relu2_2',1,'none';
'conv2_3',2,'none';
'relu2_3',1,'none';
'res2_3',0,'none';
'conv2_4',2,'none';
'relu2_4',1,'none';
'conv2_5',2,'none';
'relu2_5',1,'none';
'res2_5',0,'none';
'conv3_1',2,'none';
'relu3_1',1,'none';
'conv3_2',2,'none';
'relu3_2',1,'none';
'conv3_3',2,'none';
'relu3_3',1,'none';
'res3_3',0,'none';
'conv3_4',2,'none';
'relu3_4',1,'none';
'conv3_5',2,'none';
'relu3_5',1,'none';
'res3_5',0,'none';
'conv3_6',2,'none';
'relu3_6',1,'none';
'conv3_7',2,'none';
'relu3_7',1,'none';
'res3_7',0,'none';
'conv3_8',2,'none';
'relu3_8',1,'none';
'conv3_9',2,'none';
'relu3_9',1,'none';
'res3_9',0,'none';
'conv4_1',2,'none';
'relu4_1',1,'none';
'conv4_2',2,'none';
'relu4_2',1,'none';
'conv4_3',2,'none';
'relu4_3',1,'none';
'fc5',2,'fc7x6';
};
export_to_binary(layers,caffe_net,'sphereface20.nchwbin');
end
function [] = export_to_binary(layers, caffe_net, out_file)
n = size(layers,1);
all = [];
for i = 1:n
layer_name = layers{i,1};
flag = layers{i,3};
for j = 1:uint32((layers{i,2}))
tmp = caffe_net.params(layer_name,j).get_data();
if j == 1
tmp = auto_permute(tmp,flag);
end
all =[all;tmp(:)];
end
end
fid = fopen(out_file,'wb');
fwrite(fid,all,'float');
fclose(fid);
end
function [out] = auto_permute(in,flag)
out = in;
if strcmp(flag,'fc3x3')==1
if ndims(in) == 2
[m,n]=size(in);
out = reshape(in, [3 3 uint32(m/9) n]);
end
elseif strcmp(flag,'fc1x1') == 1
if ndims(in) == 2
[m,n]=size(in);
out = reshape(in, [1 1 m n]);
end
elseif strcmp(flag,'fc7x6') == 1
if ndims(in) == 2
[m,n]=size(in);
out = reshape(in, [7 6 uint32(m/42) n]);
end
end
out = permute(out,[1 2 3 4]);%not permuted
end
function [out] = auto_swap_N(in, flag)
out = in;
if strcmp(flag, 'none') == 1
return ;
elseif strcmp(flag, 'swapN_pair') == 1
if ndims(in) == 4
out(:,:,:,1:2:end) = in(:,:,:,2:2:end);
out(:,:,:,2:2:end) = in(:,:,:,1:2:end);
else
out(:,1:2:end) = in(:,2:2:end);
out(:,2:2:end) = in(:,1:2:end);
end
elseif strcmp(flag, 'swapN_half') == 1
if ndims(in) == 4
n = size(in,4);
half_n = uint32(n/2);
out(:,:,:,1:half_n) = in(:,:,:,half_n+1:end);
out(:,:,:,half_n+1:end) = in(:,:,:,1:half_n);
else
n = size(in,2);
half_n = uint32(n/2);
out(:,1:half_n) = in(:,half_n+1:end);
out(:,half_n+1:end) = in(:,1:half_n);
end
end
end
function [out] = auto_swap_C(in, flag)
out = in;
if strcmp(flag, 'none') == 1
return ;
elseif strcmp(flag, 'swapN_pair') == 1
if ndims(in) == 4
out(:,:,1:2:end,1) = in(:,:,2:2:end,1);
out(:,:,2:2:end,1) = in(:,:,1:2:end,1);
else
out(1:2:end,1) = in(2:2:end,1);
out(2:2:end,1) = in(1:2:end,1);
end
elseif strcmp(flag, 'swapN_half') == 1
if ndims(in) == 4
n = size(in,4);
half_n = uint32(n/2);
out(:,:,1:half_n,1) = in(:,:,half_n+1:end,1);
out(:,:,half_n+1:end,1) = in(:,:,1:half_n,1);
else
n = size(in,1);
half_n = uint32(n/2);
out(1:half_n,1) = in(half_n+1:end,1);
out(half_n+1:end,1) = in(1:half_n,1);
end
end
end