forked from huawei-noah/Efficient-AI-Backbones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost_net.py
executable file
·381 lines (332 loc) · 17 KB
/
ghost_net.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
381
# Changed for building GhostNet
#
# modified from the code: https://github.com/balancap/tf-imagenet/blob/master/models/mobilenet/mobilenet_v2.py
# =============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import namedtuple
import functools
import numpy as np
import tensorflow as tf
from tensorpack.models import (
MaxPooling, GlobalAvgPooling, BatchNorm, Dropout, BNReLU, FullyConnected)
from tensorpack.tfutils import argscope
from tensorpack.models.common import layer_register
from tensorpack.utils.argtools import shape2d
from imagenet_utils import ImageNetModel
import utils
from myconv2d import MyConv2D as Conv2D
from myconv2d import BNNoReLU, SELayer
from myconv2d import GhostModule as MyConv
kernel_initializer = tf.contrib.layers.variance_scaling_initializer(2.0)
slim = tf.contrib.slim
# =========================================================================== #
# GhostNet model.
# =========================================================================== #
class GhostNet(ImageNetModel):
"""GhostNet model.
"""
def __init__(self, num_classes=1000, dw_code=None, ratio_code=None, se=1, data_format='NHWC',
width=1.0, depth=1.0, lr=0.2, weight_decay = 0.00004, dropout_keep_prob=0.8,
label_smoothing=0.0):
self.scope = 'MobileNetV2'
self.num_classes = num_classes
self.dw_code = dw_code
self.ratio_code = ratio_code
self.se = se
self.depth = depth
self.depth_multiplier = width
self.data_format = data_format
self.lr = lr
self.weight_decay = weight_decay
self.dropout_keep_prob = dropout_keep_prob
self.label_smoothing = label_smoothing
self.image_shape = 224
def get_logits(self, inputs):
sc = ghostnet_arg_scope(
data_format=self.data_format,
weight_decay=self.weight_decay,
use_batch_norm=True,
batch_norm_decay=0.9997,
batch_norm_epsilon=0.001,
regularize_depthwise=False)
with slim.arg_scope(sc):
with argscope(Conv2D,
kernel_initializer=kernel_initializer):
with argscope([Conv2D, BatchNorm], data_format=self.data_format):
logits, end_points = mobilenet_v2(
inputs,
dw_code=self.dw_code,
ratio_code=self.ratio_code,
se=self.se,
num_classes=self.num_classes,
dropout_keep_prob=self.dropout_keep_prob,
min_depth=8,
depth_multiplier=self.depth_multiplier,
depth=self.depth,
conv_defs=None,
prediction_fn=tf.contrib.layers.softmax,
spatial_squeeze=True,
reuse=None,
scope=self.scope,
global_pool=False)
return logits
# =========================================================================== #
# Functional definition.
# =========================================================================== #
# Conv and Bottleneck namedtuple define layers of the GhostNet architecture
# Conv defines 3x3 convolution layers
# stride is the stride of the convolution
# depth is the number of channels or filters in a layer
Conv = namedtuple('Conv', ['kernel', 'stride', 'depth', 'factor', 'se'])
Bottleneck = namedtuple('Bottleneck', ['kernel', 'stride', 'depth', 'factor', 'se'])
# _CONV_DEFS specifies the GhostNet body
_CONV_DEFS_0 = [
Conv(kernel=[3, 3], stride=2, depth=16, factor=1, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=16, factor=1, se=0),
Bottleneck(kernel=[3, 3], stride=2, depth=24, factor=48/16, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=24, factor=72/24, se=0),
Bottleneck(kernel=[5, 5], stride=2, depth=40, factor=72/24, se=1),
Bottleneck(kernel=[5, 5], stride=1, depth=40, factor=120/40, se=1),
Bottleneck(kernel=[3, 3], stride=2, depth=80, factor=240/40, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=200/80, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=184/80, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=184/80, se=0),
Bottleneck(kernel=[3, 3], stride=1, depth=112, factor=480/80, se=1),
Bottleneck(kernel=[3, 3], stride=1, depth=112, factor=672/112, se=1),
Bottleneck(kernel=[5, 5], stride=2, depth=160, factor=672/112, se=1),
Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960/160, se=0),
Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960/160, se=1),
Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960/160, se=0),
Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960/160, se=1),
Conv(kernel=[1, 1], stride=1, depth=960, factor=1, se=0),
Conv(kernel=[1, 1], stride=1, depth=1280, factor=1, se=0)
]
@layer_register(log_shape=True)
def DepthConv(x, kernel_shape, padding='SAME', stride=1, data_format='NHWC',
W_init=None, activation=tf.identity):
in_shape = x.get_shape().as_list()
if data_format=='NHWC':
in_channel = in_shape[3]
stride_shape = [1, stride, stride, 1]
elif data_format=='NCHW':
in_channel = in_shape[1]
stride_shape = [1, 1, stride, stride]
out_channel = in_channel
channel_mult = out_channel // in_channel
if W_init is None:
W_init = kernel_initializer
kernel_shape = shape2d(kernel_shape) #[kernel_shape, kernel_shape]
filter_shape = kernel_shape + [in_channel, channel_mult]
W = tf.get_variable('W', filter_shape, initializer=W_init)
conv = tf.nn.depthwise_conv2d(x, W, stride_shape, padding=padding, data_format=data_format)
return activation(conv, name='output')
def ghostnet_base(inputs,
final_endpoint=None,
min_depth=8,
depth_multiplier=1.0,
depth=1.0,
conv_defs=None,
output_stride=None,
dw_code=None,
ratio_code=None,
se=1,
scope=None):
def depth(d):
d = max(int(d * depth_multiplier), min_depth)
d = round(d / 4) * 4
return d
end_points = {}
# Used to find thinned depths for each layer.
if depth_multiplier <= 0:
raise ValueError('depth_multiplier is not greater than zero.')
if conv_defs is None:
conv_defs = _CONV_DEFS_0
if dw_code is None or len(dw_code) < len(conv_defs):
dw_code = [3] * len(conv_defs)
print('dw_code', dw_code)
if ratio_code is None or len(ratio_code) < len(conv_defs):
ratio_code = [2] * len(conv_defs)
print('ratio_code', ratio_code)
se_code = [x.se for x in conv_defs]
print('se_code', se_code)
if final_endpoint is None:
final_endpoint = 'Conv2d_%d'%(len(conv_defs)-1)
if output_stride is not None and output_stride not in [8, 16, 32]:
raise ValueError('Only allowed output_stride values are 8, 16, 32.')
with tf.variable_scope(scope, 'MobilenetV2', [inputs]):
with slim.arg_scope([slim.conv2d, slim.separable_conv2d], padding='SAME'):
# The current_stride variable keeps track of the output stride of the
# activations, i.e., the running product of convolution strides up to the
# current network layer. This allows us to invoke atrous convolution
# whenever applying the next convolution would result in the activations
# having output stride larger than the target output_stride.
current_stride = 1
# The atrous convolution rate parameter.
rate = 1
net = inputs
in_depth = 3
gi = 0
for i, conv_def in enumerate(conv_defs):
print('---')
end_point_base = 'Conv2d_%d' % i
if output_stride is not None and current_stride == output_stride:
# If we have reached the target output_stride, then we need to employ
# atrous convolution with stride=1 and multiply the atrous rate by the
# current unit's stride for use in subsequent layers.
layer_stride = 1
layer_rate = rate
rate *= conv_def.stride
else:
layer_stride = conv_def.stride
layer_rate = 1
current_stride *= conv_def.stride
# change last bottleneck
if i+2 == len(conv_defs):
end_point = end_point_base
net = Conv2D(end_point, net, depth(conv_def.depth), [1, 1], stride=1,
data_format='NHWC', activation=BNReLU, use_bias=False)
ksize = utils.ksize_for_squeezing(net, 1024)
net = slim.avg_pool2d(net, ksize, padding='VALID',
scope='AvgPool_7')
end_points[end_point] = net
# Normal conv2d.
elif i+1 == len(conv_defs):
end_point = end_point_base
net = Conv2D(end_point, net, 1280, conv_def.kernel, stride=conv_def.stride,
data_format='NHWC', activation=BNReLU, use_bias=False)
end_points[end_point] = net
elif isinstance(conv_def, Conv):
end_point = end_point_base
net = Conv2D(end_point, net, depth(conv_def.depth), conv_def.kernel, stride=conv_def.stride,
data_format='NHWC', activation=BNReLU, use_bias=False)
end_points[end_point] = net
# Bottleneck block.
elif isinstance(conv_def, Bottleneck):
# Stride > 1 or different depth: no residual part.
if layer_stride == 1 and in_depth == conv_def.depth:
res = net
else:
end_point = end_point_base + '_shortcut_dw'
res = DepthConv(end_point, net, conv_def.kernel, stride=layer_stride,
data_format='NHWC', activation=BNNoReLU)
end_point = end_point_base + '_shortcut_1x1'
res = Conv2D(end_point, res, depth(conv_def.depth), [1, 1], strides=1, data_format='NHWC',
activation=BNNoReLU, use_bias=False)
# Increase depth with 1x1 conv.
end_point = end_point_base + '_up_pointwise'
net = MyConv(end_point, net, depth(in_depth * conv_def.factor), [1, 1], dw_code[gi], ratio_code[gi],
strides=1, data_format='NHWC', activation=BNReLU, use_bias=False)
end_points[end_point] = net
# Depthwise conv2d.
if layer_stride > 1:
end_point = end_point_base + '_depthwise'
net = DepthConv(end_point, net, conv_def.kernel, stride=layer_stride,
data_format='NHWC', activation=BNNoReLU)
end_points[end_point] = net
# SE
if se_code[i] > 0 and se > 0:
end_point = end_point_base + '_se'
net = SELayer(end_point, net, depth(in_depth * conv_def.factor), 4)
end_points[end_point] = net
# Downscale 1x1 conv.
end_point = end_point_base + '_down_pointwise'
net = MyConv(end_point, net, depth(conv_def.depth), [1, 1], dw_code[gi], ratio_code[gi], strides=1,
data_format='NHWC', activation=BNNoReLU if res is None else BNNoReLU, use_bias=False)
gi += 1
# Residual connection?
end_point = end_point_base + '_residual'
net = tf.add(res, net, name=end_point) if res is not None else net
end_points[end_point] = net
# Unknown...
else:
raise ValueError('Unknown convolution type %s for layer %d'
% (conv_def.ltype, i))
in_depth = conv_def.depth
# Final end point?
if final_endpoint in end_points:
return end_points[final_endpoint], end_points
raise ValueError('Unknown final endpoint %s' % final_endpoint)
def mobilenet_v2(inputs,
num_classes=1000,
dropout_keep_prob=0.999,
is_training=True,
min_depth=8,
depth_multiplier=1.0,
depth=1.0,
conv_defs=None,
prediction_fn=tf.contrib.layers.softmax,
spatial_squeeze=True,
reuse=None,
scope='MobilenetV2',
global_pool=False,
dw_code=None,
ratio_code=None,
se=1,
):
input_shape = inputs.get_shape().as_list()
if len(input_shape) != 4:
raise ValueError('Invalid input tensor rank, expected 4, was: %d' %
len(input_shape))
with tf.variable_scope(scope, 'MobilenetV2', [inputs], reuse=reuse) as scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
net, end_points = ghostnet_base(inputs, scope=scope, dw_code=dw_code, ratio_code=ratio_code,
se=se, min_depth=min_depth, depth=depth,
depth_multiplier=depth_multiplier,
conv_defs=conv_defs)
with tf.variable_scope('Logits'):
if not num_classes:
return net, end_points
# 1 x 1 x 1280
net = Dropout('Dropout_1b', net, keep_prob=dropout_keep_prob)
logits = Conv2D('Conv2d_1c_1x1', net, num_classes, 1, strides=1,
data_format='NHWC', activation=None)
if spatial_squeeze:
logits = utils.spatial_squeeze(logits, scope='SpatialSqueeze')
end_points['Logits'] = logits
if prediction_fn:
end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
return logits, end_points
def ghostnet_arg_scope(is_training=True,
data_format='NHWC',
weight_decay=0.00004,
use_batch_norm=True,
batch_norm_decay=0.99,
batch_norm_epsilon=0.001,
regularize_depthwise=False):
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
'updates_collections': tf.GraphKeys.UPDATE_OPS,
'fused': True,
'scale': True,
'data_format': data_format,
'is_training': is_training,
}
if use_batch_norm:
normalizer_fn = slim.batch_norm
normalizer_params = batch_norm_params
else:
normalizer_fn = None
normalizer_params = {}
weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay)
weights_initializer = kernel_initializer
if regularize_depthwise:
depthwise_regularizer = weights_regularizer
else:
depthwise_regularizer = None
with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
weights_initializer=weights_initializer,
activation_fn=tf.nn.relu,
normalizer_fn=normalizer_fn,
normalizer_params=normalizer_params):
with slim.arg_scope([slim.batch_norm], **batch_norm_params):
with slim.arg_scope([slim.conv2d], weights_regularizer=weights_regularizer):
with slim.arg_scope([slim.separable_conv2d],
weights_regularizer=depthwise_regularizer):
# Data format scope...
data_sc = utils.data_format_scope(data_format)
with slim.arg_scope(data_sc) as sc:
return sc