forked from zuoqing1988/ZQCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_mtcnn_caffemodel_to_nchw_binary.m
145 lines (132 loc) · 3.68 KB
/
export_mtcnn_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
%export mtcnn caffemodel to nchw binarys
function export_mtcnn_caffemodel_to_nchw_binary()
caffe.set_mode_cpu();
caffe_net = caffe.Net('det1.prototxt', 'det1.caffemodel', 'test');
layers = {
'conv1',2,'none';
'PReLU1',1,'none';
'conv2',2,'none';
'PReLU2',1,'none';
'conv3',2,'none';
'PReLU3',1,'none';
'conv4-1',2,'fc1x1';
'conv4-2',2,'fc1x1'
};
export_to_binary(layers,caffe_net,'det1.nchwbin');
caffe_net = caffe.Net('det2.prototxt', 'det2.caffemodel', 'test');
layers = {
'conv1',2,'none';
'prelu1',1,'none';
'conv2',2,'none';
'prelu2',1,'none';
'conv3',2,'none';
'prelu3',1,'none';
'conv4',2,'fc3x3';
'prelu4',1,'none';
'conv5-1',2,'fc1x1';
'conv5-2',2,'fc1x1'
};
export_to_binary(layers,caffe_net,'det2.nchwbin');
caffe_net = caffe.Net('det3.prototxt', 'det3.caffemodel', 'test');
layers = {
'conv1',2,'none';
'prelu1',1,'none';
'conv2',2,'none';
'prelu2',1,'none';
'conv3',2,'none';
'prelu3',1,'none';
'conv4',2,'none';
'prelu4',1,'none';
'conv5',2,'fc3x3';
'prelu5',1,'none';
'conv6-1',2,'fc1x1';
'conv6-2',2,'fc1x1';
'conv6-3',2,'fc1x1'
};
export_to_binary(layers,caffe_net,'det3.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
end
out = permute(out,[2 1 3 4]);
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