forked from ben-hawks/RN0X_Pokemon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resnet_v1_eembc.py
380 lines (342 loc) · 16 KB
/
resnet_v1_eembc.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import qkeras
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Activation, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, AveragePooling2D, MaxPooling2D, Add
from tensorflow.keras.regularizers import l1_l2
from qkeras.qlayers import QDense, QActivation
from qkeras.qconvolutional import QConv2D
from qkeras.qconv2d_batchnorm import QConv2DBatchnorm
from qkeras.qpooling import QAveragePooling2D
# define model
def resnet_v1_eembc(input_shape=[32, 32, 3], num_classes=10, l1p=0, l2p=1e-4,
num_filters=[16, 16, # block 1
32, 32, # block 2
64, 64 # block 3
],
kernel_sizes=[3, 3, 3, # block 1
3, 3, 1, # block 2
3, 3, 1 # block 3
],
strides=['111', # block 1
'212', # block 2
'212', # block 3
],
skip=True,
avg_pooling=False):
# Input layer, change kernel size to 7x7 and strides to 2 for an official resnet
inputs = Input(shape=input_shape)
x = Conv2D(num_filters[0],
kernel_size=kernel_sizes[0],
strides=int(strides[0][0]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# First stack
# Weight layers
y = Conv2D(num_filters[1],
kernel_size=kernel_sizes[1],
strides=int(strides[0][1]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(num_filters[0],
kernel_size=kernel_sizes[2],
strides=int(strides[0][2]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
y = BatchNormalization()(y)
# Overall residual, connect weight layer and identity paths
if skip:
x = Add()([x, y])
else:
x = y
x = Activation('relu')(x)
if len(num_filters) > 2 and num_filters[2] > 0 and strides[1] != '' and kernel_sizes[3] > 0:
# Second stack
# Weight layers
y = Conv2D(num_filters[2],
kernel_size=kernel_sizes[3],
strides=int(strides[1][0]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(num_filters[3],
kernel_size=kernel_sizes[4],
strides=int(strides[1][1]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
y = BatchNormalization()(y)
# Adjust for change in dimension due to stride in identity
x = Conv2D(num_filters[3],
kernel_size=kernel_sizes[5],
strides=int(strides[1][2]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
# Overall residual, connect weight layer and identity paths
if skip:
x = Add()([x, y])
else:
x = y
x = Activation('relu')(x)
if len(num_filters) > 4 and num_filters[4] > 0 and strides[2] != '' and kernel_sizes[6] > 0:
# Third stack
# Weight layers
y = Conv2D(num_filters[4],
kernel_size=kernel_sizes[6],
strides=int(strides[2][0]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(num_filters[5],
kernel_size=kernel_sizes[7],
strides=int(strides[2][1]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
y = BatchNormalization()(y)
# Adjust for change in dimension due to stride in identity
x = Conv2D(num_filters[5],
kernel_size=kernel_sizes[8],
strides=int(strides[2][2]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
# Overall residual, connect weight layer and identity paths
if skip:
x = Add()([x, y])
else:
x = y
x = Activation('relu')(x)
if len(num_filters) > 6 and num_filters[6] > 0 and strides[3] != '' and kernel_sizes[9] > 0:
# Fourth stack (not complete stack)
# Weight layers
y = Conv2D(num_filters[6],
kernel_size=kernel_sizes[9],
strides=int(strides[3][0]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = BatchNormalization()(y)
x = Activation('relu')(y)
if len(num_filters) > 7 and num_filters[7] > 0 and strides[3] != '' and kernel_sizes[10] > 0:
y = x
y = Conv2D(num_filters[7],
kernel_size=kernel_sizes[10],
strides=int(strides[3][1]),
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
y = BatchNormalization()(y)
x = Activation('relu')(y)
# Overall residual, connect weight layer and identity paths
if skip:
y = QActivation(activation=logit_quantizer)(y)
x = Add()([x, y])
else:
x = y
x = QActivation(activation=activation_quantizer)(x)
# Final classification layer.
pool_size = int(np.amin(x.shape[1:3]))
if pool_size > 1 and avg_pooling:
x = AveragePooling2D(pool_size=pool_size)(x)
y = Flatten()(x)
y = Dense(num_classes,
kernel_initializer='he_normal')(y)
outputs = Activation('softmax', name='softmax')(y)
# Instantiate model.
model = Model(inputs=inputs, outputs=outputs)
return model
# quantized model
def resnet_v1_eembc_quantized(input_shape=[32, 32, 3], num_classes=10, l1p=0, l2p=1e-4,
num_filters=[16, 16, # block 1
32, 32, # block 2
64, 64 # block 3
],
kernel_sizes=[3, 3, 3, # block 1
3, 3, 1, # block 2
3, 3, 1 # block 3
],
strides=['111', # block 1
'212', # block 2
'212', # block 3
],
logit_total_bits=7, logit_int_bits=2, activation_total_bits=7, activation_int_bits=2,
alpha=1, use_stochastic_rounding=False,
logit_quantizer='quantized_bits', activation_quantizer='quantized_relu',
skip=True,
avg_pooling=False,
final_activation=True):
logit_quantizer = getattr(qkeras.quantizers, logit_quantizer)(logit_total_bits, logit_int_bits, alpha=alpha, use_stochastic_rounding=use_stochastic_rounding)
if activation_quantizer == 'binary_tanh':
activation_quantizer = qkeras.quantizers.binary_tanh
else:
activation_quantizer = getattr(qkeras.quantizers, activation_quantizer)(activation_total_bits, activation_int_bits, use_stochastic_rounding=use_stochastic_rounding)
# Input layer, change kernel size to 7x7 and strides to 2 for an official resnet
inputs = Input(shape=input_shape)
x = QConv2DBatchnorm(num_filters[0],
kernel_size=kernel_sizes[0],
strides=int(strides[0][0]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(inputs)
x = QActivation(activation=activation_quantizer)(x)
# First stack
# Weight layers
y = QConv2DBatchnorm(num_filters[1],
kernel_size=kernel_sizes[1],
strides=int(strides[0][1]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = QActivation(activation=activation_quantizer)(y)
y = QConv2DBatchnorm(num_filters[0],
kernel_size=kernel_sizes[2],
strides=int(strides[0][2]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
# Overall residual, connect weight layer and identity paths
if skip:
y = QActivation(activation=logit_quantizer)(y)
x = Add()([x, y])
else:
x = y
x = QActivation(activation=activation_quantizer)(x)
if len(num_filters) > 2 and num_filters[2] > 0 and strides[1] != '' and kernel_sizes[3] > 0:
# Second stack
# Weight layers
y = QConv2DBatchnorm(num_filters[2],
kernel_size=kernel_sizes[3],
strides=int(strides[1][0]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = QActivation(activation=activation_quantizer)(y)
y = QConv2DBatchnorm(num_filters[3],
kernel_size=kernel_sizes[4],
strides=int(strides[1][1]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
# Adjust for change in dimension due to stride in identity
x = QConv2D(num_filters[3],
kernel_size=kernel_sizes[5],
strides=int(strides[1][2]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
x = QActivation(activation=logit_quantizer)(x)
# Overall residual, connect weight layer and identity paths
if skip:
y = QActivation(activation=logit_quantizer)(y)
x = Add()([x, y])
else:
x = y
x = QActivation(activation=activation_quantizer)(x)
if len(num_filters) > 4 and num_filters[4] > 0 and strides[2] != '' and kernel_sizes[6] > 0:
# Third stack
# Weight layers
y = QConv2DBatchnorm(num_filters[4],
kernel_size=kernel_sizes[6],
strides=int(strides[2][0]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
y = QActivation(activation=activation_quantizer)(y)
y = QConv2DBatchnorm(num_filters[5],
kernel_size=kernel_sizes[7],
strides=int(strides[2][1]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
# Adjust for change in dimension due to stride in identity
x = QConv2D(num_filters[5],
kernel_size=kernel_sizes[8],
strides=int(strides[2][2]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
x = QActivation(activation=logit_quantizer)(x)
# Overall residual, connect weight layer and identity paths
if skip:
y = QActivation(activation=logit_quantizer)(y)
x = Add()([x, y])
else:
x = y
x = QActivation(activation=activation_quantizer)(x)
if len(num_filters) > 6 and num_filters[6] > 0 and strides[3] != '' and kernel_sizes[9] > 0:
# Fourth stack (not complete stack)
# Weight layers
y = QConv2DBatchnorm(num_filters[6],
kernel_size=kernel_sizes[9],
strides=int(strides[3][0]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
x = QActivation(activation=activation_quantizer)(y)
if len(num_filters) > 7 and num_filters[7] > 0 and strides[3] != '' and kernel_sizes[10] > 0:
y = x
y = QConv2DBatchnorm(num_filters[7],
kernel_size=kernel_sizes[10],
strides=int(strides[3][1]),
padding='same',
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal',
kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
x = QActivation(activation=logit_quantizer)(x)
# Overall residual, connect weight layer and identity paths
if skip:
y = QActivation(activation=logit_quantizer)(y)
x = Add()([x, y])
else:
x = y
x = QActivation(activation=activation_quantizer)(x) # TODO FIXME
# Final classification layer.
pool_size = int(np.amin(x.shape[1:3]))
if pool_size > 1 and avg_pooling:
x = QAveragePooling2D(pool_size=pool_size, quantizer=logit_quantizer)(x)
y = Flatten()(x)
# Changed output to separate QDense but did not quantize softmax as specified
outputs = QDense(num_classes,
kernel_quantizer=logit_quantizer,
bias_quantizer=logit_quantizer,
kernel_initializer='he_normal')(y)
if final_activation:
outputs = Activation('softmax', name='softmax')(outputs)
# Instantiate model.
model = Model(inputs=inputs, outputs=outputs)
return model