Xception

Xception

背景介绍

  Xception:谷歌公司对Inception-V3的改进,被CVPR2017年收录,是一个很好的图像特征提取模型。

Xception

Xception特点

  除了Inception的特点以外,采用了SeparableConv(深度可分离卷积)代替Inception中的(Conv)卷积操作,大大节约了参数量

Separable Convolution

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

Xception图像分析

Xception

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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_Relu(keras.layers.Layer):
def __init__(self, filters, kernel_size, strides, padding, name):
super(Conv_Bn_Relu, self).__init__()
self._name = name
self.block = keras.Sequential()
if name.find('separable') == -1:
self.block.add(keras.layers.Conv2D(filters, kernel_size, strides, padding=padding))
else:
self.block.add(keras.layers.SeparableConv2D(filters, kernel_size, strides, padding=padding))
self.block.add(keras.layers.BatchNormalization())
if name.find('relu') != -1:
self.block.add(keras.layers.ReLU())

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

return self.block(inputs)


def entry_exit_flow(x, filters, relu, name):
shortcut = x
if relu:
x = keras.layers.ReLU(name='{}_relu'.format(name))(x)
x = compose(Conv_Bn_Relu(filters, (3, 3), (1, 1), 'same', name='{}_separableconv_bn_relu1'.format(name)),
Conv_Bn_Relu(filters, (3, 3), (1, 1), 'same', name='{}_separableconv_bn2'.format(name)),
keras.layers.MaxPool2D((3, 3), (2, 2), 'same', name='{}_maxpool'.format(name)))(x)
shortcut = Conv_Bn_Relu(filters, (1, 1), (2, 2), 'same', name='{}_shortcut_conv_bn'.format(name))(shortcut)
x = keras.layers.Add(name='{}_add'.format(name))([x, shortcut])

return x


def middle_flow(x, filters, name):
shortcut = x
x = keras.layers.ReLU(name='{}_relu'.format(name))(x)
x = compose(Conv_Bn_Relu(filters, (3, 3), (1, 1), 'same', name='{}_separableconv_bn_relu1'.format(name)),
Conv_Bn_Relu(filters, (3, 3), (1, 1), 'same', name='{}_separableconv_bn_relu2'.format(name)),
Conv_Bn_Relu(filters, (3, 3), (1, 1), 'same', name='{}_separableconv_bn3'.format(name)),)(x)
x = keras.layers.Add(name='{}_add'.format(name))([x, shortcut])

return x


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

x = compose(Conv_Bn_Relu(32, (3, 3), (2, 2), 'valid', name='conv_bn_relu1'),
Conv_Bn_Relu(64, (3, 3), (1, 1), 'valid', name='conv_bn_relu2'))(x)

entry_filters = [128, 256, 728]
entry_relu = [False, True, True]
for i in range(len(entry_filters)):
x = entry_exit_flow(x, entry_filters[i], entry_relu[i], name='entry_flow{}'.format(i + 1))

for i in range(8):
x = middle_flow(x, 728, name='middle_flow{}'.format(i + 1))

x = entry_exit_flow(x, 1024, relu=True, name='exit_flow1')

x = compose(Conv_Bn_Relu(1536, (3, 3), (1, 1), 'same', name='separableconv_bn_relu1'),
Conv_Bn_Relu(2048, (3, 3), (1, 1), 'same', name='separableconv_bn_relu2'),
keras.layers.GlobalAveragePooling2D(name='global_averagepool'),
keras.layers.Dense(1000, activation='softmax', name='dense'))(x)

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

return model

Xception

Xception小结

  Xception是一种复杂的深度学习网络,从上图可以看出Xception模型的参数量可达23M,因为其优秀的特征提取能力,并且网络结构相比Inception-V3较为简单。因此实际任务经常使用,如语义分割网络DeepLab-V3+使用的特征提取网络就是Xception

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