-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThinet.py
321 lines (243 loc) · 10.9 KB
/
Thinet.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
# built-in
from time import time
# 3rd party
import numpy as np
# misc
# Measuring performance via matlab style: tic() and toc()
_tstart_stack = []
def tic():
_tstart_stack.append(time())
def toc():
delta_t = 1000.0 * float(time()-_tstart_stack.pop())
return delta_t
class Thinet:
def __init__(self, input_map, weights, output_map, rate):
"""
:param input_map: input activation map [batch_size x W x H x C1]
:param weights: convolutional filters [K x K x C1 x C2]
:param output_map: output activation map [batch_size x W x H x C2]
:param rate: desired compression rate [0 <= rate <= 1.0]
"""
# Input data
self.weights = weights
self.output = output_map
self.rate = rate
# Calculate padding for input map
if input_map.shape[1] > output_map.shape[1]:
self.input = input_map
else:
pad = (weights.shape[0] - 1) // 2
h = input_map.shape[1] + 2 * pad
w = input_map.shape[2] + 2 * pad
c = input_map.shape[3]
batch_size = input_map.shape[0]
self.input = np.zeros((batch_size, h, w, c), dtype=input_map.dtype)
self.input[:, pad:-pad, pad:-pad, :] = input_map
# Comression results placeholders
self.keep_filters = None
self.remove_filters = None
self.weights_wo_reconst = None
self.weights_reconst = None
@staticmethod
def get_random_coords(width, height, channels, n):
""" Generate random coordinates in the following shape [n x 3]"""
coords = np.zeros((n, 3), dtype=np.int16)
coords[:, 0] = np.random.randint(0, width, size=n)
coords[:, 1] = np.random.randint(0, height, size=n)
coords[:, 2] = np.random.randint(0, channels, size=n)
return coords
def __compute_eq6(self, x_idx, channels):
weights = self.weights
input_map = self.input
assert (weights.shape[0] == weights.shape[1]), "invalid kernel size"
res = 0
offset = weights.shape[0] // 2
for map_in in input_map:
for p in x_idx:
x, y, c = p
T_sum = 0
for j in channels:
kernel = np.squeeze(weights[:, :, j, c])
roi = map_in[y - offset:y + offset + 1, x - offset:x + offset + 1, j]
T_sum += np.sum(roi * kernel)
res += np.power(T_sum, 2)
return res
def __compute_eq6_fast(self, x_idx, channels):
weights = self.weights
input_map = self.input
assert (weights.shape[0] == weights.shape[1]), "invalid kernel size"
res = 0
offset = weights.shape[0] // 2
for p in x_idx:
x, y, c = p
T_sum = np.zeros((input_map.shape[0],))
for j in channels:
kernel = np.expand_dims(np.squeeze(weights[:, :, j, c]), axis=0)
roi = input_map[:, y - offset:y + offset + 1, x - offset:x + offset + 1, j]
T_sum += np.sum(roi * kernel, axis=(1, 2))
res += np.sum(np.power(T_sum, 2))
return res
def __compute_eq6_fast_ultra(self, x_idx, channels):
weights = self.weights
input_map = self.input
assert (weights.shape[0] == weights.shape[1]), "invalid kernel size"
res = 0
offset = weights.shape[0] // 2
for p in x_idx:
x, y, c = p
T_sum = np.zeros((input_map.shape[0],))
kernel = np.expand_dims(weights[:, :, channels, c], axis=0)
roi = input_map[:, y - offset:y + offset + 1, x - offset:x + offset + 1, channels]
T_sum = np.sum(roi * kernel, axis=(1, 2, 3))
res += np.sum(np.power(T_sum, 2))
return res
def __best_filters_scales(self, x_idx, y_idx, filters):
"""
Minimize the reconstruction error by weighting valid channels,
based on: Thinet: equation 7
"""
batch_size1 = self.input.shape[0]
batch_size2 = self.output.shape[0]
assert (batch_size1 == batch_size2), "batch size is not consistent"
batch_size = batch_size1
input_map = self.input
output_map = self.output
channels = len(filters)
sample_pts = y_idx.shape[0]
offset = self.weights.shape[0] // 2
X = np.zeros((sample_pts * batch_size, channels), dtype=np.float32)
Y = np.zeros((sample_pts * batch_size,), dtype=np.float32)
# Compute Y vector [samples*batch_size x 1]
for i, p in enumerate(y_idx):
x, y, c = p
Y[i*batch_size : (i+1)*batch_size] = output_map[:, y, x, c]
# Compute X matrix [samples*batch_size x channels]
for i, p in enumerate(x_idx):
x, y, c = p
for j, f in enumerate(filters):
kernel = np.expand_dims(np.squeeze(self.weights[:, :, f, c]), axis=0)
roi = input_map[:, y - offset:y + offset + 1, x - offset:x + offset + 1, f]
X[i*batch_size : (i+1)*batch_size, j] = np.sum(kernel * roi, axis=(1, 2))
#
# Eq. 7:
#
# w_hat = (X^T * X)^(-1) * X^T * Y
#
var_1 = np.linalg.inv(np.dot(X.T, X))
var_2 = np.dot(X.T, Y)
# Channel filters weights [channels x 1]
w_hat = np.dot(var_1, var_2)
return np.squeeze(w_hat.T)
def __prune_weights(self, filters, scales):
weights = self.weights
K1 = weights.shape[0]
K2 = weights.shape[1]
C1 = len(filters)
C2 = weights.shape[3]
assert (K1 == K2), "invalid kernel size"
weights_wo_reconst = np.zeros((K1, K2, C1, C2), dtype=weights.dtype)
weights_reconst = np.zeros((K1, K2, C1, C2), dtype=weights.dtype)
for i, f in enumerate(filters):
weights_wo_reconst[:, :, i, :] = weights[:, :, f, :]
weights_reconst[:, :, i, :] = weights[:, :, f, :] * scales[i]
return weights_wo_reconst, weights_reconst
def compress(self, sample_points=10):
"""
Compute the most important filters according to compression rate,
based on: ThiNet: Algorithm 1 - a greedy algorithm for minimizing Eq.6
:param sample_points: number of points to use in each activation map
:return I: filters to be preserved in layer i
:return w_reconst: layer i+1 reconstructed filters
"""
H2 = self.output.shape[1]
W2 = self.output.shape[2]
C1 = self.weights.shape[2]
C2 = self.weights.shape[3]
y_idx = Thinet.get_random_coords(W2, H2, C2, sample_points)
x_idx = np.copy(y_idx)
x_idx[:, 0:2] += 2 # corresponding input (x, y) coordinates (assumed kernel size: 5x5)
run_time = 0
T = [] # list of filters to prune
I = list(range(C1)) # list of remaining filters
while len(T) < round(C1 * (1 - self.rate)):
min_value = np.inf
tic()
for i in I:
tmpT = list(T)
tmpT.append(i)
value = self.__compute_eq6_fast_ultra(x_idx, tmpT)
#value_fast = self.__compute_eq6_fast(x_idx, tmpT)
#if np.abs(value - value_fast) > 1:
# raise ValueError("%f != %f" % (value, value_fast))
#print("%f ; %f" % (value, value_fast))
if value < min_value:
min_value = value
min_i = i
I.remove(min_i)
T.append(min_i)
run_time += toc()
print('Filters prunned: %d in %.3f [s]' % (len(T), run_time/1000.0))
# Minimize reconstruction error
W_hat = self.__best_filters_scales(x_idx, y_idx, I)
W_wo_reconst, W_reconst = self.__prune_weights(I, W_hat)
# Update compression results
self.keep_filters = I
self.remove_filters = T
self.weights_wo_reconst = W_wo_reconst
self.weights_reconst = W_reconst
#print("Filters to preserve: ", I)
def save_data_to_file(self, output_file):
data_dict = dict()
data_dict['keep_filters'] = self.keep_filters
data_dict['remove_filters'] = self.remove_filters
data_dict['weights_wo_reconst'] = self.weights_wo_reconst
data_dict['weights_reconst'] = self.weights_reconst
np.save(output_file, data_dict)
def load_data_from_file(self, input_file):
data_dict = np.load(input_file).item()
self.keep_filters = data_dict['keep_filters']
self.remove_filters = data_dict['remove_filters']
self.weights_wo_reconst = data_dict['weights_wo_reconst']
self.weights_reconst = data_dict['weights_reconst']
print('Keep filters: %d : ' % len(self.keep_filters))
print('Remove filters: %d : ' % len(self.remove_filters))
print('Weights wo reconstruction: ', self.weights_wo_reconst.shape)
print('Weights with reconstruction: ', self.weights_reconst.shape)
def compress_callback(self, data_dict, params, with_reconstruction):
"""
:param data_dict: all network variables
:param params: network variables to remove in the following format:
params = { 'remove' : [('conv1', 'weights', 3),
('conv1', 'biases', 0),
'update' : ('conv2', 'weights') }
"""
print('\nRemoved parameters:\n')
for rec in params['remove']:
layer, key, axis = rec
print("\t[%s][%s] %s --> " % (
layer, key, str(data_dict[layer][key].shape)), end="")
data_dict[layer][key] = np.delete(data_dict[layer][key], self.remove_filters, axis=axis)
print("%s" % str(data_dict[layer][key].shape))
print('\nUpdated parameters:\n')
layer, key = params['update']
print("\t[%s][%s] %s --> " % (
layer, key, str(data_dict[layer][key].shape)), end="")
if with_reconstruction:
data_dict[layer][key] = self.weights_reconst
else:
data_dict[layer][key] = self.weights_wo_reconst
print(data_dict[layer][key].shape)
print('')
def update_model(self, model_file_in, model_file_out, with_reconstruction=False):
update_dict = {
'remove' : [('conv1', 'weights', 3),
('conv1', 'biases', 0)],
'update' : ('conv2', 'weights')
}
if self.keep_filters is None:
print("Please run compress() method first")
return
data_dict = np.load(model_file_in, encoding='latin1').item()
# data dict is updated inplace
self.compress_callback(data_dict, update_dict, with_reconstruction)
np.save(model_file_out, data_dict)