-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
175 lines (127 loc) · 6.3 KB
/
convert.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
import torch
import numpy as np
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("--input", required=False, default='ShuffleNetV2+.Small.pth.tar', help="pytorch model to convert")
ap.add_argument("--output", required=False, default='ShuffleNetV2_Plus.npy', help="npy file to save the weights")
ap.add_argument("--net_structure", required=False, default='ShuffleNetV2_Plus', help="the model structure")
args = ap.parse_args()
torch_model = args.input
npy_model = args.output
net_structure = args.net_structure
params = torch.load(torch_model, map_location=torch.device('cpu'))
params_dict = params['state_dict']
length = len(params_dict)
dict = {}
def repalce(word, x, y):
word = word.replace(x, y)
return word
def rename(word, x, y):
res = ''
for i, item in enumerate(word):
if item == '.':
res += '/'
else:
res += item
return res
params_dict_tf = {}
cnt = 0
for k, v in params_dict.items():
new_k = k
### process conv
if 'weight' in new_k and \
'fc' not in new_k and \
'classifier' not in new_k and \
len(list(params_dict[k].shape)) > 1:
### conv weights
pre_str, cur_cnt = new_k.rsplit('.', 1)[0].rsplit('.', 1)
cur_cnt = int(cur_cnt)
### conv weight name
if params_dict[k].shape[1] == 1:
weights_name_torch = pre_str + '.' + str(cur_cnt) + '.' + 'weight'
weights_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'depthwise_weights:0'
else:
weights_name_torch = pre_str + '.' + str(cur_cnt) + '.' + 'weight'
weights_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'weights:0'
## gamma =
gamma_name_torch = pre_str + '.' + str(cur_cnt + 1) + '.' + 'weight'
gamma_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'BatchNorm/gamma:0'
##beta
beta_name_torch = pre_str + '.' + str(cur_cnt + 1) + '.' + 'bias'
beta_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'BatchNorm/beta:0'
##movin mean
moving_mean_name_torch = pre_str + '.' + str(cur_cnt + 1) + '.' + 'running_mean'
moving_mean_tf = pre_str + '.' + str(cur_cnt) + '.' + 'BatchNorm/moving_mean:0'
##movin var
moving_var_torch = pre_str + '.' + str(cur_cnt + 1) + '.' + 'running_var'
moving_var_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'BatchNorm/moving_variance:0'
weights_name_tf = rename(weights_name_tf, '.', '/')
gamma_name_tf = rename(gamma_name_tf, '.', '/')
beta_name_tf = rename(beta_name_tf, '.', '/')
moving_mean_tf = rename(moving_mean_tf, '.', '/')
moving_var_name_tf = rename(moving_var_name_tf, '.', '/')
weights_name_tf = repalce(weights_name_tf, 'module', net_structure)
gamma_name_tf = repalce(gamma_name_tf, 'module', net_structure)
beta_name_tf = repalce(beta_name_tf, 'module', net_structure)
moving_mean_tf = repalce(moving_mean_tf, 'module', net_structure)
moving_var_name_tf = repalce(moving_var_name_tf, 'module', net_structure)
try:
'''
pytorch weights is [out,in,ksize[0],ksize[1]]
tensorflow weights is [ksize[0],ksize[1],in,out]
if depthwise:
??
'''
if 'depthwise_weights' in weights_name_tf:
if '5x5' in net_structure:
cur_params = np.array(params_dict[weights_name_torch])
o, i, h, w = cur_params.shape
cur_params_5x5 = np.zeros(shape=[o, i, 5, 5])
cur_params_5x5[:, :, 1:4, 1:4] = cur_params
params_dict_tf[weights_name_tf] = cur_params_5x5.transpose(2, 3, 0, 1)
else:
params_dict_tf[weights_name_tf] = np.array(params_dict[weights_name_torch]).transpose(2, 3, 0, 1)
else:
params_dict_tf[weights_name_tf] = np.array(params_dict[weights_name_torch]).transpose(2, 3, 1, 0)
except:
print(k)
print(params_dict[weights_name_torch].shape)
if gamma_name_torch in params_dict:
params_dict_tf[gamma_name_tf] = np.array(params_dict[gamma_name_torch])
params_dict_tf[beta_name_tf] = np.array(params_dict[beta_name_torch])
params_dict_tf[moving_mean_tf] = np.array(params_dict[moving_mean_name_torch])
params_dict_tf[moving_var_name_tf] = np.array(params_dict[moving_var_torch])
if 'fc' in new_k or 'classifier' in new_k:
pre_str, cur_cnt = new_k.rsplit('.', 1)[0].rsplit('.', 1)
cur_cnt = int(cur_cnt)
if params_dict[k].shape[1] == 1:
weights_name_torch = pre_str + '.' + str(cur_cnt) + '.' + 'weight'
weights_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'depthwise_weights:0'
else:
weights_name_torch = pre_str + '.' + str(cur_cnt) + '.' + 'weight'
weights_name_tf = pre_str + '.' + str(cur_cnt) + '.' + 'weights:0'
weights_name_tf = rename(weights_name_tf, '.', '/')
weights_name_tf = repalce(weights_name_tf, 'module', net_structure)
params_dict[weights_name_torch] = np.array(params_dict[weights_name_torch])
params_dict[weights_name_torch] = np.expand_dims(params_dict[weights_name_torch], -1)
params_dict[weights_name_torch] = np.expand_dims(params_dict[weights_name_torch], -1)
try:
if 'depthwise_weights' in weights_name_tf:
if '5x5' in net_structure:
cur_params = np.array(params_dict[weights_name_torch])
o, i, h, w = cur_params.shape
cur_params_5x5 = np.zeros(shape=[o, i, 5, 5])
cur_params_5x5[:, :, 1:4, 1:4] = cur_params
params_dict_tf[weights_name_tf] = cur_params_5x5.transpose(2, 3, 0, 1)
else:
params_dict_tf[weights_name_tf] = np.array(params_dict[weights_name_torch]).transpose(2, 3, 0, 1)
else:
params_dict_tf[weights_name_tf] = np.array(params_dict[weights_name_torch]).transpose(2, 3, 1, 0)
except:
print(k)
print(params_dict[weights_name_torch].shape)
for k, v in params_dict_tf.items():
if 'first' in k:
print(v)
print(k, v.shape)
np.save(npy_model, params_dict_tf)