-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
357 lines (322 loc) · 22.3 KB
/
model.py
File metadata and controls
357 lines (322 loc) · 22.3 KB
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
import tensorflow as tf
from layers import depthwise_separable_conv2d, conv2d, avg_pool_2d, dense, flatten, dropout
import os
from utils import load_obj, save_obj
import numpy as np
import functools
class MobileNet:
"""
MobileNet Class
"""
def __init__(self,
args,
activation=tf.nn.relu6,
custom_getter=None):
# init parameters and input
self.X = None
self.y = None
self.logits = None
self.is_training = None
self.loss = None
self.regularization_loss = None
self.cross_entropy_loss = None
self.train_op = None
self.accuracy = None
self.y_out_argmax = None
self.summaries_merged = None
self.args = args
self.mean_img = None
self.nodes = dict()
self.activation = activation
self.custom_getter = custom_getter
self.pretrained_path = os.path.realpath(self.args.pretrained_path)
self.__build()
def __init_input(self):
with tf.variable_scope('input'):
# Input images
self.X = tf.placeholder(tf.float32,
[self.args.batch_size, self.args.img_height, self.args.img_width,
self.args.num_channels])
# Classification supervision, it's an argmax. Feel free to change it to one-hot,
# but don't forget to change the loss from sparse as well
self.y = tf.placeholder(tf.int32, [self.args.batch_size])
# is_training is for batch normalization and dropout, if they exist
self.is_training = tf.placeholder(tf.bool)
def __init_mean(self):
# Preparing the mean image.
img_mean = np.ones((1, 224, 224, 3))
img_mean[:, :, :, 0] *= 103.939
img_mean[:, :, :, 1] *= 116.779
img_mean[:, :, :, 2] *= 123.68
self.mean_img = tf.constant(img_mean, dtype=tf.float32)
def __build(self):
self.__init_global_epoch()
self.__init_global_step()
self.__init_mean()
self.__init_input()
self.__init_network()
self.__init_output()
def __init_network(self):
with tf.variable_scope('mobilenet_encoder'):
# Preprocessing as done in the paper
with tf.name_scope('pre_processing'):
preprocessed_input = (self.X - self.mean_img) / 255.0
# Model is here!
conv1_1 = conv2d('conv_1', preprocessed_input, num_filters=int(round(32 * self.args.width_multiplier)),
kernel_size=(3, 3),
padding='SAME', stride=(2, 2), activation=self.activation,
batchnorm_enabled=self.args.batchnorm_enabled,
is_training=self.is_training, l2_strength=self.args.l2_strength, bias=self.args.bias)
self.__add_to_nodes([conv1_1])
############################################################################################
conv2_1_dw, conv2_1_pw = depthwise_separable_conv2d('conv_ds_2', conv1_1,
width_multiplier=self.args.width_multiplier,
num_filters=64, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv2_1_dw, conv2_1_pw])
conv2_2_dw, conv2_2_pw = depthwise_separable_conv2d('conv_ds_3', conv2_1_pw,
width_multiplier=self.args.width_multiplier,
num_filters=128, kernel_size=(3, 3), padding='SAME',
stride=(2, 2),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv2_2_dw, conv2_2_pw])
############################################################################################
conv3_1_dw, conv3_1_pw = depthwise_separable_conv2d('conv_ds_4', conv2_2_pw,
width_multiplier=self.args.width_multiplier,
num_filters=128, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv3_1_dw, conv3_1_pw])
conv3_2_dw, conv3_2_pw = depthwise_separable_conv2d('conv_ds_5', conv3_1_pw,
width_multiplier=self.args.width_multiplier,
num_filters=256, kernel_size=(3, 3), padding='SAME',
stride=(2, 2),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv3_2_dw, conv3_2_pw])
############################################################################################
conv4_1_dw, conv4_1_pw = depthwise_separable_conv2d('conv_ds_6', conv3_2_pw,
width_multiplier=self.args.width_multiplier,
num_filters=256, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv4_1_dw, conv4_1_pw])
conv4_2_dw, conv4_2_pw = depthwise_separable_conv2d('conv_ds_7', conv4_1_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(2, 2),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv4_2_dw, conv4_2_pw])
############################################################################################
conv5_1_dw, conv5_1_pw = depthwise_separable_conv2d('conv_ds_8', conv4_2_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_1_dw, conv5_1_pw])
conv5_2_dw, conv5_2_pw = depthwise_separable_conv2d('conv_ds_9', conv5_1_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_2_dw, conv5_2_pw])
conv5_3_dw, conv5_3_pw = depthwise_separable_conv2d('conv_ds_10', conv5_2_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_3_dw, conv5_3_pw])
conv5_4_dw, conv5_4_pw = depthwise_separable_conv2d('conv_ds_11', conv5_3_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_4_dw, conv5_4_pw])
conv5_5_dw, conv5_5_pw = depthwise_separable_conv2d('conv_ds_12', conv5_4_pw,
width_multiplier=self.args.width_multiplier,
num_filters=512, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_5_dw, conv5_5_pw])
conv5_6_dw, conv5_6_pw = depthwise_separable_conv2d('conv_ds_13', conv5_5_pw,
width_multiplier=self.args.width_multiplier,
num_filters=1024, kernel_size=(3, 3), padding='SAME',
stride=(2, 2),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv5_6_dw, conv5_6_pw])
############################################################################################
conv6_1_dw, conv6_1_pw = depthwise_separable_conv2d('conv_ds_14', conv5_6_pw,
width_multiplier=self.args.width_multiplier,
num_filters=1024, kernel_size=(3, 3), padding='SAME',
stride=(1, 1),
batchnorm_enabled=self.args.batchnorm_enabled,
activation=self.activation,
is_training=self.is_training,
l2_strength=self.args.l2_strength,
biases=(self.args.bias, self.args.bias),
custom_getter=self.custom_getter)
self.__add_to_nodes([conv6_1_dw, conv6_1_pw])
############################################################################################
avg_pool = avg_pool_2d(conv6_1_pw, size=(7, 7), stride=(1, 1))
dropped = dropout(avg_pool, self.args.dropout_keep_prob, self.is_training)
self.logits = flatten(conv2d('fc', dropped, kernel_size=(1, 1), num_filters=self.args.num_classes,
l2_strength=self.args.l2_strength,
bias=self.args.bias))
self.__add_to_nodes([avg_pool, dropped, self.logits])
def __init_output(self):
with tf.variable_scope('output'):
self.regularization_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
self.cross_entropy_loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=self.y, name='loss'))
self.loss = self.regularization_loss + self.cross_entropy_loss
# Important for Batch Normalization
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
self.train_op = tf.train.AdamOptimizer(learning_rate=self.args.learning_rate).minimize(self.loss)
self.y_out_argmax = tf.argmax(tf.nn.softmax(self.logits), axis=-1, output_type=tf.int32)
self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.y, self.y_out_argmax), tf.float32))
# Summaries needed for TensorBoard
with tf.name_scope('train-summary-per-iteration'):
tf.summary.scalar('loss', self.loss)
tf.summary.scalar('acc', self.accuracy)
self.summaries_merged = tf.summary.merge_all()
def __restore(self, file_name, sess):
try:
print("Loading ImageNet pretrained weights...")
variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='mobilenet_encoder')
dict = load_obj(file_name)
run_list = []
for variable in variables:
for key, value in dict.items():
if key in variable.name:
run_list.append(tf.assign(variable, value))
sess.run(run_list)
print("ImageNet Pretrained Weights Loaded Initially\n\n")
except:
print("No pretrained ImageNet weights exist. Skipping...\n\n")
def load_pretrained_weights(self, sess):
self.__restore(self.pretrained_path, sess)
def __add_to_nodes(self, nodes):
for node in nodes:
self.nodes[node.name] = node
def __init_global_epoch(self):
"""
Create a global epoch tensor to totally save the process of the training
:return:
"""
with tf.variable_scope('global_epoch'):
self.global_epoch_tensor = tf.Variable(-1, trainable=False, name='global_epoch')
self.global_epoch_input = tf.placeholder('int32', None, name='global_epoch_input')
self.global_epoch_assign_op = self.global_epoch_tensor.assign(self.global_epoch_input)
def __init_global_step(self):
"""
Create a global step variable to be a reference to the number of iterations
:return:
"""
with tf.variable_scope('global_step'):
self.global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
self.global_step_input = tf.placeholder('int32', None, name='global_step_input')
self.global_step_assign_op = self.global_step_tensor.assign(self.global_step_input)
class MobileNetQuantize(MobileNet):
"""
MobileNetQuantize Class
"""
def __init__(self,
args,
activation_quantizer=None,
activation_quantizer_kwargs=None,
weight_quantizer=None,
weight_quantizer_kwargs=None):
activation_quantizer_kwargs = activation_quantizer_kwargs if activation_quantizer_kwargs is not None else {}
weight_quantizer_kwargs = weight_quantizer_kwargs if weight_quantizer_kwargs is not None else {}
activation = activation_quantizer(**activation_quantizer_kwargs)
weight_quantization = weight_quantizer(**weight_quantizer_kwargs)
custom_getter = functools.partial(self._quantized_variable_getter,
weight_quantization=weight_quantization)
assert weight_quantizer
assert activation_quantizer
super().__init__(args,
activation=activation,
custom_getter=custom_getter)
@staticmethod
def _quantized_variable_getter(getter, name, weight_quantization=None, *args, **kwargs):
"""Get the quantized variables.
Use if to choose or skip the target should be quantized.
Args:
getter: Default from tensorflow.
name: Default from tensorflow.
weight_quantization: Callable object which quantize variable.
args: Args.
kwargs: Kwargs.
"""
assert callable(weight_quantization)
var = getter(name, *args, **kwargs)
with tf.variable_scope(name):
# print("all op", var.op.name)
# Apply weight quantize to variable whose last word of name is "kernel".
if "weights" == var.op.name.split("/")[-1]:
# print("quantized op", var.op.name)
quantized_var = weight_quantization(var)
# tf.summary.histogram(quantized_var)
return quantized_var
return var