This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
/
caffe2npz.py
185 lines (148 loc) · 7.54 KB
/
caffe2npz.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import argparse
import numpy as np
import os
import chainer
from chainer.links.caffe.caffe_function import CaffeFunction
from chainercv.links import SEResNet101
from chainercv.links import SEResNet152
from chainercv.links import SEResNet50
from chainercv.links import SEResNeXt101
from chainercv.links import SEResNeXt50
def _transfer_components(src, dst_conv, dst_bn, bname, cname):
src_conv = getattr(src, 'conv{}_{}'.format(bname, cname))
src_bn = getattr(src, 'conv{}_{}/bn'.format(bname, cname))
src_scale = getattr(src, 'conv{}_{}/bn/scale'.format(bname, cname))
if dst_conv.groups == 1:
dst_conv.W.data[:] = src_conv.W.data
else:
group_size = src_conv.W.data.shape[1] // dst_conv.groups
for group in range(dst_conv.groups):
from_idx = group_size * group
to_idx = group_size * (group + 1)
dst_conv.W.data[from_idx: to_idx, :, :, :] = \
src_conv.W.data[from_idx: to_idx, from_idx: to_idx, :, :]
dst_bn.avg_mean[:] = src_bn.avg_mean
dst_bn.avg_var[:] = src_bn.avg_var
dst_bn.gamma.data[:] = src_scale.W.data
dst_bn.beta.data[:] = src_scale.bias.b.data
def _transfer_se_components(src, dst_se, bname, cname):
src_se_down = getattr(src, 'conv{}_{}_down'.format(bname, cname))
src_se_up = getattr(src, 'conv{}_{}_up'.format(bname, cname))
hidden_size, in_size = dst_se.down.W.shape
dst_se.down.W.data[:] = src_se_down.W.data.reshape((hidden_size, in_size))
dst_se.down.b.data[:] = src_se_down.b.data
dst_se.up.W.data[:] = src_se_up.W.data.reshape((in_size, hidden_size))
dst_se.up.b.data[:] = src_se_up.b.data
def _transfer_bottleneckA(src, dst, name):
_transfer_components(
src, dst.conv1.conv, dst.conv1.bn, name, '1x1_reduce')
_transfer_components(
src, dst.conv2.conv, dst.conv2.bn, name, '3x3')
_transfer_components(
src, dst.conv3.conv, dst.conv3.bn, name, '1x1_increase')
_transfer_components(
src, dst.residual_conv.conv, dst.residual_conv.bn, name, '1x1_proj')
_transfer_se_components(src, dst.se, name, '1x1')
def _transfer_bottleneckB(src, dst, name):
_transfer_components(
src, dst.conv1.conv, dst.conv1.bn, name, '1x1_reduce')
_transfer_components(
src, dst.conv2.conv, dst.conv2.bn, name, '3x3')
_transfer_components(
src, dst.conv3.conv, dst.conv3.bn, name, '1x1_increase')
_transfer_se_components(src, dst.se, name, '1x1')
def _transfer_block(src, dst, names):
_transfer_bottleneckA(src, dst.a, names[0])
for i, name in enumerate(names[1:]):
dst_bottleneckB = getattr(dst, 'b{}'.format(i + 1))
_transfer_bottleneckB(src, dst_bottleneckB, name)
def _transfer_resnet50(src, dst, class_indices):
# Reorder weights to work on RGB and not on BGR
dst.conv1.conv.W.data[:] = src['conv1/7x7_s2'].W.data[:, ::-1]
# No bias setting for conv1, which is different from ResNet50.
dst.conv1.bn.avg_mean[:] = src['conv1/7x7_s2/bn'].avg_mean
dst.conv1.bn.avg_var[:] = src['conv1/7x7_s2/bn'].avg_var
dst.conv1.bn.gamma.data[:] = src['conv1/7x7_s2/bn/scale'].W.data
dst.conv1.bn.beta.data[:] = src['conv1/7x7_s2/bn/scale'].bias.b.data
_transfer_block(src, dst.res2, ['2_1', '2_2', '2_3'])
_transfer_block(src, dst.res3, ['3_1', '3_2', '3_3', '3_4'])
_transfer_block(src, dst.res4, ['4_1', '4_2', '4_3', '4_4', '4_5', '4_6'])
_transfer_block(src, dst.res5, ['5_1', '5_2', '5_3'])
dst.fc6.W.data[:] = src.classifier.W.data[class_indices, :]
dst.fc6.b.data[:] = src.classifier.b.data[class_indices]
def _transfer_resnet101(src, dst, class_indices):
# Reorder weights to work on RGB and not on BGR
dst.conv1.conv.W.data[:] = src['conv1/7x7_s2'].W.data[:, ::-1]
dst.conv1.bn.avg_mean[:] = src['conv1/7x7_s2/bn'].avg_mean
dst.conv1.bn.avg_var[:] = src['conv1/7x7_s2/bn'].avg_var
dst.conv1.bn.gamma.data[:] = src['conv1/7x7_s2/bn/scale'].W.data
dst.conv1.bn.beta.data[:] = src['conv1/7x7_s2/bn/scale'].bias.b.data
_transfer_block(src, dst.res2, ['2_{}'.format(i) for i in range(1, 4)])
_transfer_block(src, dst.res3, ['3_{}'.format(i) for i in range(1, 5)])
_transfer_block(src, dst.res4, ['4_{}'.format(i) for i in range(1, 24)])
_transfer_block(src, dst.res5, ['5_{}'.format(i) for i in range(1, 4)])
dst.fc6.W.data[:] = src.classifier.W.data[class_indices, :]
dst.fc6.b.data[:] = src.classifier.b.data[class_indices]
def _transfer_resnet152(src, dst, class_indices):
# Reorder weights to work on RGB and not on BGR
dst.conv1.conv.W.data[:] = src['conv1/7x7_s2'].W.data[:, ::-1]
dst.conv1.bn.avg_mean[:] = src['conv1/7x7_s2/bn'].avg_mean
dst.conv1.bn.avg_var[:] = src['conv1/7x7_s2/bn'].avg_var
dst.conv1.bn.gamma.data[:] = src['conv1/7x7_s2/bn/scale'].W.data
dst.conv1.bn.beta.data[:] = src['conv1/7x7_s2/bn/scale'].bias.b.data
_transfer_block(src, dst.res2, ['2_{}'.format(i) for i in range(1, 4)])
_transfer_block(src, dst.res3, ['3_{}'.format(i) for i in range(1, 9)])
_transfer_block(src, dst.res4, ['4_{}'.format(i) for i in range(1, 37)])
_transfer_block(src, dst.res5, ['5_{}'.format(i) for i in range(1, 4)])
dst.fc6.W.data[:] = src.classifier.W.data[class_indices, :]
dst.fc6.b.data[:] = src.classifier.b.data[class_indices]
def _load_class_indices():
# The caffemodel weights in the original repository
# (https://github.com/hujie-frank/SENet) have been trained with a modified
# order of class indices.
indices = np.zeros(1000, dtype=np.int32)
file = os.path.join(os.path.dirname(__file__), 'label_map.csv')
with open(file, 'r') as fr:
lines = fr.readlines()
for line in lines:
index_modified, index_origin = map(int, line.strip().split(','))
indices[index_origin] = index_modified
return indices
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'model_name', choices=(
'se-resnet50', 'se-resnet101', 'se-resnet152',
'se-resnext50', 'se-resnext101',
))
parser.add_argument('caffemodel')
parser.add_argument('output', nargs='?', default=None)
args = parser.parse_args()
caffemodel = CaffeFunction(args.caffemodel)
if args.model_name == 'se-resnet50':
model = SEResNet50(pretrained_model=None, n_class=1000)
model(np.zeros((1, 3, 224, 224), dtype=np.float32))
_transfer_resnet50(caffemodel, model, _load_class_indices())
elif args.model_name == 'se-resnet101':
model = SEResNet101(pretrained_model=None, n_class=1000)
model(np.zeros((1, 3, 224, 224), dtype=np.float32))
_transfer_resnet101(caffemodel, model, _load_class_indices())
elif args.model_name == 'se-resnet152':
model = SEResNet152(pretrained_model=None, n_class=1000)
model(np.zeros((1, 3, 224, 224), dtype=np.float32))
_transfer_resnet152(caffemodel, model, _load_class_indices())
elif args.model_name == 'se-resnext50':
model = SEResNeXt50(pretrained_model=None, n_class=1000)
model(np.zeros((1, 3, 224, 224), dtype=np.float32))
_transfer_resnet50(caffemodel, model, _load_class_indices())
elif args.model_name == 'se-resnext101':
model = SEResNeXt101(pretrained_model=None, n_class=1000)
model(np.zeros((1, 3, 224, 224), dtype=np.float32))
_transfer_resnet101(caffemodel, model, _load_class_indices())
if args.output is None:
output = '{}_imagenet_convert.npz'.format(args.model_name)
else:
output = args.output
chainer.serializers.save_npz(output, model)
if __name__ == '__main__':
main()