一、从论文下载开始
MobileNet论文是由Google Brain团队提出,2017年发布的一篇基于深度学习的轻量级模型架构讲稿,主要针对嵌入式设备和移动端设备进行优化,实现高效的图像识别应用。
首先,在官方网站https://arxiv.org/abs/1704.04861 上下载阅读论文。
$ wget https://arxiv.org/pdf/1704.04861.pdf
$ open 1704.04861.pdf
二、Mobilenet网络结构图
Mobilenet的网络结构的核心思想是Depthwise Separable Convolution。深度可分离卷积包括两个步骤,第一步是Depthwise Convolution,第二步是Pointwise Convolution。首先通过Depthwise Convolution计算每一个输入通道的卷积层,得到的结果叫做Depthwise Features。接着将Depthwise Features送入Pointwise Convolution中,进行全局卷积操作,然后产生新的N个卷积特征,输出结果就是Depthwise Features与Pointwise Features的融合。
该网络结构图如图所示:
from IPython.display import Image
Image(filename='mobilenet_structure.png', width=400, height=400)
三、Mobilenet v2
Mobilenet v2是MobileNet v1的后续版本。随着深度学习的快速发展,人们越来越需要更深、更准确的模型,所以Google在v1的基础上,提出了一些新的设计思路,如Inverted Residuals、Linear Bottlenecks等。具体的网络结构与v1有很大的不同,v2的网络结构图如下:
from IPython.display import Image
Image(filename='mobilenet_v2_structure.png', width=400, height=400)
四、实现MobileNet
下面是使用Keras框架实现MobileNet的代码示例:
from keras.layers import Input, Conv2D, DepthwiseConv2D, Activation
from keras.models import Model
def conv_block(inputs, filters, kernel, strides):
x = Conv2D(filters, kernel_size=kernel, strides=strides, padding='same')(inputs)
x = Activation('relu')(x)
return x
def depthwise_block(inputs, depth_multiplier, kernel, strides):
x = DepthwiseConv2D(kernel_size=kernel, strides=strides, depth_multiplier=depth_multiplier, padding='same')(inputs)
x = Activation('relu')(x)
return x
def mobileNet(input_shape, num_classes):
inp = Input(shape=input_shape)
x = conv_block(inp, 32, 3, strides=(2, 2))
x = depthwise_block(x, 1, 3, strides=(1, 1))
x = conv_block(x, 64, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(2, 2))
x = conv_block(x, 128, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(1, 1))
x = conv_block(x, 128, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(2, 2))
x = conv_block(x, 256, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(1, 1))
x = conv_block(x, 256, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(2, 2))
x = conv_block(x, 512, 1, strides=(1, 1))
for i in range(5):
x = depthwise_block(x, 1, 3, strides=(1, 1))
x = conv_block(x, 512, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(2, 2))
x = conv_block(x, 1024, 1, strides=(1, 1))
x = depthwise_block(x, 1, 3, strides=(1, 1))
x = conv_block(x, 1024, 1, strides=(1, 1))
x = Conv2D(num_classes, kernel_size=1, strides=(1, 1), padding='same')(x)
x = Activation('softmax')(x)
model = Model(inp, x)
return model
input_shape = (224, 224, 3)
num_classes = 10
model = mobileNet(input_shape, num_classes)
model.summary()
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/187562.html