一、從論文下載開始
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/zh-hant/n/187562.html