-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_model_fix.py
359 lines (273 loc) · 17.6 KB
/
load_model_fix.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import torch
import numpy as np
import os
import config
from models import densenet121, comp_resnet50, comp_dense, densenet_2stage, densenet_3stage, adaptive_densenet
import torch.nn as nn
def load_densenet_model(model, oristate_dict):
cfg = {'dense121': [6, 12, 24, 16]}
state_dict = model.state_dict()
current_cfg = cfg['dense121']
last_select_index = None
last_concat_index = None
all_honey_conv_weight = []
bn_part_name=['.weight','.bias','.running_mean','.running_var']#,'.num_batches_tracked']
prefix = 'rank_adaptive_local3/densenet121_limit3/rank_conv'
subfix = '.npy'
cnt=1
conv_weight_name = 'features.conv0.weight'
all_honey_conv_weight.append(conv_weight_name)
oriweight = oristate_dict[conv_weight_name]
curweight = state_dict[conv_weight_name]
orifilter_num = oriweight.size(0)
start_filternum = orifilter_num
currentfilter_num = curweight.size(0)
if orifilter_num != currentfilter_num:
print('loading rank from: ' + prefix + str(cnt) + subfix)
rank = np.load(prefix + str(cnt) + subfix)
select_index = np.argsort(rank)[orifilter_num - currentfilter_num:] # preserved filter id
select_index.sort()
for index_i, i in enumerate(select_index):
state_dict[conv_weight_name][index_i] = oristate_dict[conv_weight_name][i]
for bn_part in bn_part_name:
state_dict['features.norm0' + bn_part][index_i] = oristate_dict['features.norm0' + bn_part][i]
last_select_index = select_index
else:
state_dict[conv_weight_name] = oriweight
for bn_part in bn_part_name:
state_dict['features.norm0' + bn_part] = oristate_dict['features.norm0'+bn_part]
# last_select_index = np.array(range(int(64*0.65)))
# last_select_index = np.array(range(64))
last_select_index = np.array(range(orifilter_num))
state_dict['features.norm0' + '.num_batches_tracked'] = oristate_dict['features.norm0' + '.num_batches_tracked']
model_block = [model.features.denseblock1, model.features.denseblock2, model.features.denseblock3, model.features.denseblock4]
trainsition_block = [model.features.transition1, model.features.transition2, model.features.transition3]
cnt+=1
print(len(last_select_index))
for layer, num in enumerate(current_cfg):
if last_select_index is not None:
last_concat_index = last_select_index
else :
last_concat_index = np.array(range(start_filternum))
# last_concat_index = np.array(range(int(2**(layer+6))))
# if layer == 0:
# last_concat_index = np.array(range(int(64*0.65)))
# elif layer == 1:
# last_concat_index = np.array(range(80))
# elif layer == 2:
# last_concat_index = np.array(range(160))
# elif layer == 3:
# last_concat_index = np.array(range(320))
block_name = 'features.' + 'denseblock' + str(layer + 1) + '.'
ori_out_num = start_filternum
for k in range(num):
layer_name = block_name + 'denselayer' + str(k+1) + '.'
for num_conv in range(1,3):
if num_conv == 1:
last_select_index = last_concat_index
bn_name = layer_name + 'norm' + str(num_conv)
conv_name = layer_name + 'conv' + str(num_conv)
conv_weight_name = conv_name + '.weight'
all_honey_conv_weight.append(conv_weight_name)
oriweight = oristate_dict[conv_weight_name]
curweight = state_dict[conv_weight_name]
orifilter_num = oriweight.size(0)
currentfilter_num = curweight.size(0)
print(cnt, orifilter_num, currentfilter_num)
# print(conv_weight_name)
# print(orifilter_num)
# print(currentfilter_num)
# print(len(last_select_index))
if orifilter_num != currentfilter_num:
rank = np.load(prefix + str(cnt) + subfix)
select_index = np.argsort(rank)[orifilter_num - currentfilter_num:] # preserved filter id
select_index.sort()
if last_select_index is not None:
print('(input&output)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(last_select_index))
print('output : ', len(select_index))
# print('select_index : ', select_index)
# print('Last_select_index : ', last_select_index)
for index_j, j in enumerate(last_select_index):
for index_i, i in enumerate(select_index):
state_dict[conv_weight_name][index_i][index_j] = oristate_dict[conv_weight_name][i][j]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part][index_j] = oristate_dict[bn_name + bn_part][j]
else:
print('(output)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(oristate_dict[conv_weight_name][0]))
print('output : ', len(select_index))
# print('select_index : ', select_index)
for index_i, i in enumerate(select_index):
state_dict[conv_weight_name][index_i] = oristate_dict[conv_weight_name][i]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part] = oristate_dict[bn_name + bn_part]
last_select_index = select_index
elif last_select_index is not None:
print('(input)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(last_select_index))
print('output : ', orifilter_num)
# print('Last_select_index : ', last_select_index)
for index_j, j in enumerate(last_select_index):
for index_i in range(orifilter_num):
state_dict[conv_weight_name][index_i][index_j] = oristate_dict[conv_weight_name][index_i][j]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part][index_j] = oristate_dict[bn_name + bn_part][j]
last_select_index = None
else:
print('(no_change)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(oristate_dict[conv_weight_name][0]))
print('output : ', orifilter_num)
state_dict[conv_weight_name] = oriweight
for bn_part in bn_part_name:
state_dict[bn_name + bn_part] = oristate_dict[bn_name + bn_part]
last_select_index = None
state_dict[ bn_name + '.num_batches_tracked'] = oristate_dict[bn_name + '.num_batches_tracked']
cnt+=1
if num_conv ==2 :
# if layer == 0:
# ori_out_num = int(64*0.65) + int(32*0.65) *k
# elif layer == 1:
# ori_out_num = 80 + int(32*0.65) *k
# elif layer == 2:
# ori_out_num = 160 + int(32*0.65) *k
# elif layer == 3:
# ori_out_num = 320 + int(32*0.65) *k
# ori_out_num = 2**(layer+6) + 32 * k
# print(ori_out_num)
if last_select_index is not None:
change = last_select_index + ori_out_num
last_concat_index = np.concatenate((last_concat_index, change), axis=0 )
else :
no_change = np.array(range(orifilter_num))
no_change = no_change + ori_out_num
last_concat_index = np.concatenate((last_concat_index, no_change), axis=0 )
ori_out_num += orifilter_num
if layer != 3:
block_name = 'features.' + 'transition' + str(layer + 1) + '.'
bn_name = block_name + 'norm'
conv_name = block_name + 'conv'
conv_weight_name = conv_name + '.weight'
all_honey_conv_weight.append(conv_weight_name)
oriweight = oristate_dict[conv_weight_name]
curweight = state_dict[conv_weight_name]
orifilter_num = oriweight.size(0)
start_filternum = orifilter_num
currentfilter_num = curweight.size(0)
last_select_index = last_concat_index
print(cnt, orifilter_num, currentfilter_num)
if orifilter_num != currentfilter_num:
rank = np.load(prefix + str(cnt) + subfix)
select_index = np.argsort(rank)[orifilter_num - currentfilter_num:] # preserved filter id
select_index.sort()
if last_select_index is not None:
print('(input&output)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(last_select_index))
print('output : ', len(select_index))
# print('select_index : ', select_index)
# print('Last_select_index : ', last_select_index)
for index_j, j in enumerate(last_select_index):
for index_i, i in enumerate(select_index):
state_dict[conv_weight_name][index_i][index_j] = oristate_dict[conv_weight_name][i][j]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part][index_j] = oristate_dict[bn_name + bn_part][j]
else:
print('(output)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(oristate_dict[conv_weight_name][0]))
print('output : ', len(select_index))
# print('select_index : ', select_index)
for index_i, i in enumerate(select_index):
state_dict[conv_weight_name][index_i] = oristate_dict[conv_weight_name][i]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part] = oristate_dict[bn_name + bn_part]
last_select_index = select_index
elif last_select_index is not None:
print('(input)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(last_select_index))
print('output : ', orifilter_num)
# print('Last_select_index : ', last_select_index)
for index_j, j in enumerate(last_select_index):
for index_i in range(orifilter_num):
state_dict[conv_weight_name][index_i][index_j] = oristate_dict[conv_weight_name][index_i][j]
for bn_part in bn_part_name:
state_dict[bn_name + bn_part][index_j] = oristate_dict[bn_name + bn_part][j]
last_select_index = None
else:
print('(no_change)loading rank from: ' + prefix + str(cnt) + subfix)
print('input : ', len(oristate_dict[conv_weight_name][0]))
print('output : ', orifilter_num)
state_dict[conv_weight_name] = oriweight
for bn_part in bn_part_name:
state_dict[bn_name + bn_part] = \
oristate_dict[bn_name + bn_part]
last_select_index = None
state_dict[ bn_name + '.num_batches_tracked'] = oristate_dict[bn_name + '.num_batches_tracked']
cnt+=1
print(len(last_concat_index))
bn_name = 'features.norm5'
last_select_index = last_concat_index
if last_select_index is not None:
print('(input)norm5')
print('norm : ', len(last_select_index))
for index_j, j in enumerate(last_select_index):
for bn_part in bn_part_name:
state_dict[bn_name + bn_part][index_j] = oristate_dict[bn_name + bn_part][j]
else:
print('(no)norm5')
for bn_part in bn_part_name:
state_dict[bn_name + bn_part] = oristate_dict[bn_name + bn_part]
state_dict[ bn_name + '.num_batches_tracked'] = oristate_dict[bn_name + '.num_batches_tracked']
for name, module in model.named_modules():
name = name.replace('module.', '')
if isinstance(module, nn.Conv2d):
conv_name = name + '.weight'
if conv_name not in all_honey_conv_weight:
print('*******************error********************')
state_dict[conv_name] = oristate_dict[conv_name]
if isinstance(module, nn.Linear):
print('fill fc layer ' + str(name))
if name == 'fc1':
if last_select_index is not None :
new_output = state_dict[name + '.weight'].size(0)
orifilter_input = oristate_dict[name + '.weight'][0].size(0)
input_num = orifilter_input-157
print(input_num)
change = np.array(range(157)) + input_num
last_concat_index = np.concatenate((last_select_index, change), axis=0 )
print('input : ', len(last_concat_index))
print('output : ', new_output)
for index_j, j in enumerate(last_concat_index):
for index_i in range(new_output):
state_dict[name + '.weight'][index_i][index_j] = oristate_dict[name + '.weight'][index_i][j]
state_dict[name + '.bias'] = oristate_dict[name + '.bias']
else:
state_dict[name + '.weight'] = oristate_dict[name + '.weight']
state_dict[name + '.bias'] = oristate_dict[name + '.bias']
else:
state_dict[name + '.weight'] = oristate_dict[name + '.weight']
state_dict[name + '.bias'] = oristate_dict[name + '.bias']
model.load_state_dict(state_dict)
checkpoint = {}
checkpoint['model'] = model.state_dict()
torch.save(checkpoint, 'pruned_model/adaptive_local_densenet4' + '.pt' )
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
compress_rate_2 = [[0.25], [0.375, 0.46875, 0.40625, 0.3125, 0.34375, 0.4375], [0.4375, 0.5, 0.375, 0.4375, 0.34375, 0.4375, 0.4375, 0.4375, 0.5, 0.46875, 0.53125, 0.375], [0.40625, 0.5625, 0.5, 0.46875, 0.40625, 0.375, 0.4375, 0.5625, 0.5, 0.65625, 0.46875, 0.5625, 0.65625, 0.46875, 0.5, 0.5, 0.5625, 0.53125, 0.65625, 0.5, 0.375, 0.5625, 0.5625, 0.53125], [0.53125, 0.5, 0.59375, 0.5, 0.53125, 0.5625, 0.59375, 0.375, 0.5, 0.46875, 0.5, 0.59375, 0.5625, 0.53125, 0.5, 0.53125]]
compress_rate2_2 = [[0], [0.7109375, 0.6171875, 0.5234375, 0.53125, 0.4609375, 0.40625], [0.5234375, 0.4609375, 0.4296875, 0.4765625, 0.3984375, 0.4609375, 0.390625, 0.4609375, 0.46875, 0.4375, 0.3203125, 0.4140625], [0.5078125, 0.4375, 0.4609375, 0.5234375, 0.484375, 0.484375, 0.5546875, 0.4921875, 0.546875, 0.5234375, 0.5, 0.4921875, 0.4765625, 0.53125, 0.515625, 0.4921875, 0.546875, 0.53125, 0.5078125, 0.5078125, 0.5078125, 0.484375, 0.4921875, 0.453125], [0.546875, 0.578125, 0.5546875, 0.53125, 0.5078125, 0.5078125, 0.5234375, 0.484375, 0.515625, 0.5546875, 0.546875, 0.453125, 0.5546875, 0.53125, 0.53125, 0.5546875]]
model = adaptive_densenet(config.SMPL_MEAN_PARAMS, compress_rate_2, compress_rate2_2).to(device)
compress_rate_2 = [[0.203125], [0.28125, 0.4375, 0.34375, 0.25, 0.25, 0.3125], [0.375, 0.40625, 0.3125, 0.375, 0.28125, 0.375, 0.34375, 0.375, 0.46875, 0.4375, 0.5, 0.3125], [0.3125, 0.5, 0.4375, 0.4375, 0.375, 0.3125, 0.375, 0.5, 0.4375, 0.59375, 0.4375, 0.53125, 0.59375, 0.4375, 0.46875, 0.46875, 0.53125, 0.46875, 0.625, 0.4375, 0.34375, 0.53125, 0.5, 0.5], [0.5, 0.4375, 0.5625, 0.4375, 0.5, 0.5, 0.5, 0.34375, 0.5, 0.4375, 0.5, 0.5625, 0.53125, 0.53125, 0.5, 0.53125]]
compress_rate2_2 = [[0], [0.65625, 0.5859375, 0.4453125, 0.46875, 0.359375, 0.3515625], [0.46875, 0.3984375, 0.359375, 0.4140625, 0.328125, 0.3984375, 0.3203125, 0.3984375, 0.40625, 0.390625, 0.28125, 0.3828125], [0.453125, 0.40625, 0.3984375, 0.4609375, 0.421875, 0.4296875, 0.484375, 0.4296875, 0.4765625, 0.4609375, 0.453125, 0.4296875, 0.421875, 0.484375, 0.453125, 0.4375, 0.484375, 0.4765625, 0.4375, 0.4453125, 0.46875, 0.3984375, 0.421875, 0.3984375], [0.5390625, 0.546875, 0.53125, 0.5, 0.5078125, 0.5078125, 0.515625, 0.484375, 0.515625, 0.5390625, 0.53125, 0.453125, 0.546875, 0.5078125, 0.53125, 0.515625]]
origin_model = adaptive_densenet(config.SMPL_MEAN_PARAMS, compress_rate_2, compress_rate2_2).to(device)
checkpoint = torch.load('/home/urp10/SPIN/models_trained/adaptive_local_3.pt')
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if 'module' not in k:
continue
# k = 'module.'+k
else:
k = k.replace('module.', '')
new_state_dict[k]=v
origin_model.load_state_dict(new_state_dict)
oristate_dict = origin_model.state_dict()
load_densenet_model(model, oristate_dict)