MobileNet

MobileNet

背景介绍

  MobileNet:2017年Google针对手机等嵌入式设备提出的一种轻量级的深层神经网络,故称之为MobileNet,由于其网络结构简单,现在仍然具有很好的适用性。

MobileNet

MobileNet特点

  结构和VGG类似,只是将标准卷积换成SeparableConv深度可分离卷积
  引入ReLU6代替ReLU,使得大于0的数值也具有非线性

Separable Convolution

Xception
  Separable Convolution(深度可分离卷积):是上面两个卷积合二为一的卷积操作。
  第一步:DepthwiseConv,对每一个通道进行卷积
  第二步:PointwiseConv,对第一步得到的结果进行1x1卷积,实现通道融合
  主要作用是
大大降低网络的参数量
,并且可以调整为任意合适的通道数。第一步的目的是减少参数量,第二步是调整通道数,因此将两个卷积操作结合,组成深度可分离卷积。

MobileNet图像分析

MobileNet

TensorFlow2.0实现

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
from functools import reduce
import tensorflow.keras as keras


def compose(*funcs):
if funcs:
return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)
else:
raise ValueError('Composition of empty sequence not supported.')


class Conv_Bn_Relu6(keras.layers.Layer):
def __init__(self, filters, kernel_size, strides, padding, name):
super(Conv_Bn_Relu6, self).__init__()
self._name = name
self.block = keras.Sequential()
if name.find('depthwise') == -1:
self.block.add(keras.layers.Conv2D(filters, kernel_size, strides, padding=padding))
else:
self.block.add(keras.layers.DepthwiseConv2D(kernel_size, strides, padding=padding))
self.block.add(keras.layers.BatchNormalization())
self.block.add(keras.layers.ReLU(6))

def call(self, inputs, **kwargs):

return self.block(inputs)


def block(filters, strides, name):

return compose(Conv_Bn_Relu6(None, (3, 3), strides, 'same', name='{}_depthwiseconv_bn_relu6'.format(name)),
Conv_Bn_Relu6(filters, (1, 1), (1, 1), 'same', name='{}_conv_bn_relu6'.format(name)))


def mobilenet(input_shape):
input_tensor = keras.layers.Input(input_shape, name='input')
x = input_tensor

x = Conv_Bn_Relu6(32, (3, 3), (2, 2), 'same', name='conv_bn_relu6')(x)

filters = [64, 128, 128, 256, 256, 512, 512, 512, 512, 512, 512, 1024, 1024]
strides = [(1, 1), (2, 2), (1, 1), (2, 2), (1, 1), (2, 2), (1, 1), (1, 1), (1, 1), (1, 1), (1, 1), (2, 2), (1, 1)]
for i in range(len(filters)):
x = block(filters[i], strides[i], name='block{}'.format(i + 1))(x)

x = compose(keras.layers.GlobalAveragePooling2D(name='global_averagepool'),
keras.layers.Reshape((1, 1, 1024), name='reshape1'),
keras.layers.Dropout(0.2, name='dropout'),
keras.layers.Conv2D(1000, (1, 1), name='conv'),
keras.layers.Reshape((1000, ), name='reshape2'),
keras.layers.Softmax(name='softmax'))(x)

model = keras.Model(input_tensor, x, name='MobileNet')

return model


if __name__ == '__main__':

model = mobilenet(input_shape=(224, 224, 3))
model.build(input_shape=(None, 224, 224, 3))
model.summary()

MobileNet

MobileNet小结

  MobileNet是一种简单的轻量级深度学习网络,从上图可以看出MobileNet模型的参数量只有4M,在移动设备中有良好表现,有时也可作为替代VGG的特征提取网络

-------------本文结束感谢您的阅读-------------
0%