# CNN模型之MobileNet

However，在某些真实的应用场景如移动或者嵌入式设备，如此大而复杂的模型是难以被应用的。

Depthwise separable convolution：

MobileNet的基本单元是深度级可分离卷积（depthwise separable convolution---DSC），其实这种结构之前已经被使用在Inception模型中。

depthwise convolution和pointwise convolution

D(K)、D(K)、M、D(F)、D(F) + M、N、D(F)、D(F)

MobileNet的一般结构：

MobileNet的网络结构如表1所示。

MobileNet 瘦身:

MobileNet 的TensorFlow实现:

TensorFlow的nn库有depthwise convolution算子tf.nn.depthwise_conv2d，所以MobileNet很容易在TensorFlow上实现：

```class MobileNet(object):
def __init__(self, inputs, num_classes=1000, is_training=True,
width_multiplier=1, scope="MobileNet"):
"""
The implement of MobileNet(ref:https://arxiv.org/abs/1704.04861)
:param inputs: 4-D Tensor of [batch_size, height, width, channels]
:param num_classes: number of classes
:param is_training: Boolean, whether or not the model is training
:param width_multiplier: float, controls the size of model
:param scope: Optional scope for variables
"""
self.inputs = inputs
self.num_classes = num_classes
self.is_training = is_training
self.width_multiplier = width_multiplier

# construct model
with tf.variable_scope(scope):
# conv1
net = conv2d(inputs, "conv_1", round(32 * width_multiplier), filter_size=3,
strides=2)  # ->[N, 112, 112, 32]
net = tf.nn.relu(bacthnorm(net, "conv_1/bn", is_training=self.is_training))
net = self._depthwise_separable_conv2d(net, 64, self.width_multiplier,
"ds_conv_2") # ->[N, 112, 112, 64]
net = self._depthwise_separable_conv2d(net, 128, self.width_multiplier,
"ds_conv_3", downsample=True) # ->[N, 56, 56, 128]
net = self._depthwise_separable_conv2d(net, 128, self.width_multiplier,
"ds_conv_4") # ->[N, 56, 56, 128]
net = self._depthwise_separable_conv2d(net, 256, self.width_multiplier,
"ds_conv_5", downsample=True) # ->[N, 28, 28, 256]
net = self._depthwise_separable_conv2d(net, 256, self.width_multiplier,
"ds_conv_6") # ->[N, 28, 28, 256]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_7", downsample=True) # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_8") # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_9")  # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_10")  # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_11")  # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 512, self.width_multiplier,
"ds_conv_12")  # ->[N, 14, 14, 512]
net = self._depthwise_separable_conv2d(net, 1024, self.width_multiplier,
"ds_conv_13", downsample=True) # ->[N, 7, 7, 1024]
net = self._depthwise_separable_conv2d(net, 1024, self.width_multiplier,
"ds_conv_14") # ->[N, 7, 7, 1024]
net = avg_pool(net, 7, "avg_pool_15")
net = tf.squeeze(net, [1, 2], name="SpatialSqueeze")
self.logits = fc(net, self.num_classes, "fc_16")
self.predictions = tf.nn.softmax(self.logits)

def _depthwise_separable_conv2d(self, inputs, num_filters, width_multiplier,
scope, downsample=False):
"""depthwise separable convolution 2D function"""
num_filters = round(num_filters * width_multiplier)
strides = 2 if downsample else 1

with tf.variable_scope(scope):
# depthwise conv2d
dw_conv = depthwise_conv2d(inputs, "depthwise_conv", strides=strides)
# batchnorm
bn = bacthnorm(dw_conv, "dw_bn", is_training=self.is_training)
# relu
relu = tf.nn.relu(bn)
# pointwise conv2d (1x1)
pw_conv = conv2d(relu, "pointwise_conv", num_filters)
# bn
bn = bacthnorm(pw_conv, "pw_bn", is_training=self.is_training)
return tf.nn.relu(bn)```

# 本文简单介绍了Google提出的移动端模型MobileNet，其核心是采用了可分解的depthwise separable convolution，其不仅可以降低模型计算复杂度，而且可以大大降低模型大小。在真实的移动端应用场景，像MobileNet这样类似的网络将是持续研究的重点。后面我们会介绍其他的移动端CNN模型

1. MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Applications: https://arxiv.org/abs/1704.04861.

197 篇文章83 人订阅

0 条评论

## 相关文章

1373

5296

3668

6286

### 人群计数--Single-Image Crowd Counting via Multi-Column Convolutional Neural Network

Single-Image Crowd Counting via Multi-Column Convolutional Neural Network CVPR...

38210

17510

2805

35110

### 语义分割研究党福利来袭，谷歌宣布开源 DeepLabv3+

DeepLab 是一种用于图像语义分割的顶尖深度学习模型，其目标是将语义标签（如人、狗、猫等）分配给输入图像的每个像素。 经过三年左右的发展，目前 DeepLa...

4507

### 量化投资之机器学习应用——基于 SVM 模型的商品期货择时交易策略（提出质疑和讨论）

2016年在东证期货的量化报告里，读到一篇文章，关于量化投资策略之机器学习应用——基于 SVM 模型的期货择时交易策略 。就顺手算了一下，发现了一些问题，因此和...

4189