作為移動端輕量級網(wǎng)絡(luò)的代表,MobileNet一直是大家關(guān)注的焦點。最近,Google提出了新一代的MobileNetV3網(wǎng)絡(luò)。這一代MobileNet結(jié)合了AutoML和人工調(diào)整,帶來了更加高效的性能。
Paper
:Searching for MobileNetV3
Github
:https://github.com/xiaochus/MobileNetV3
改進
- MobileNetV3的網(wǎng)絡(luò)模塊結(jié)構(gòu)延續(xù)了MobileNetV1的深度可分離卷積和MobileNetV2的bottleneck with residual 結(jié)構(gòu)。在此基礎(chǔ)上,還加入了SENet中的基于squeeze and excitation結(jié)構(gòu)的輕量級注意力模型。
-
MobileNetV3的結(jié)構(gòu)是通過AutoML技術(shù)生成的。在網(wǎng)絡(luò)結(jié)構(gòu)搜索中,作者結(jié)合兩種技術(shù):資源受限的NAS與NetAdapt,前者用于在計算和參數(shù)量受限的前提下搜索網(wǎng)絡(luò)的各個模塊,所以稱之為模塊級的搜索(Block-wise Search) ,后者用于對各個模塊確定之后網(wǎng)絡(luò)層的微調(diào)。
-
MobileNetV2在預(yù)測部分使用了一個Bottleneck結(jié)構(gòu)來提取特征,這種結(jié)構(gòu)帶來了額外的計算開銷。MobileNetV3中使用兩個1*1的卷積來代替了這個操作。
- 使用swish激活函數(shù)代替ReLU能夠有效提高網(wǎng)絡(luò)的精度,但是swish的計算量太大了。因此作者對swish進行了數(shù)值近似,提出h-swish(hard version of swish)函數(shù)。
網(wǎng)絡(luò)結(jié)構(gòu)
Large MobileNetV3:
Small MobileNetV3:
效果
與前一代MobieNetV2比較,V3-Large取得了最高的精度,V3-Small 取得了V2近似的精度,速度卻快很多。
與其他移動端網(wǎng)絡(luò)比較,MobieNetV3也有著良好的性能。
Keras實現(xiàn)
首先實現(xiàn)一個基礎(chǔ)類,定義了MobileNetV3的激活函數(shù)和各種基本結(jié)構(gòu)。
"""MobileNet v3 models for Keras.
# Reference
[Searching for MobileNetV3](https://arxiv.org/abs/1905.02244?context=cs)
"""
from keras.layers import Conv2D, DepthwiseConv2D, Dense, GlobalAveragePooling2D
from keras.layers import Activation, BatchNormalization, Add, Lambda
from keras import backend as K
class MobileNetBase:
def __init__(self, shape, n_class):
self.shape = shape
self.n_class = n_class
def _relu6(self, x):
"""Relu 6
"""
return K.relu(x, max_value=6.0)
def _hard_swish(self, x):
"""Hard swish
"""
return x * K.relu(x + 3.0, max_value=6.0) / 6.0
def _return_activation(self, x, nl):
"""Convolution Block
This function defines a activation choice.
# Arguments
x: Tensor, input tensor of conv layer.
nl: String, nonlinearity activation type.
# Returns
Output tensor.
"""
if nl == 'HS':
x = Activation(self._hard_swish)(x)
if nl == 'RE':
x = Activation(self._relu6)(x)
return x
def _conv_block(self, inputs, filters, kernel, strides, nl):
"""Convolution Block
This function defines a 2D convolution operation with BN and activation.
# Arguments
inputs: Tensor, input tensor of conv layer.
filters: Integer, the dimensionality of the output space.
kernel: An integer or tuple/list of 2 integers, specifying the
width and height of the 2D convolution window.
strides: An integer or tuple/list of 2 integers,
specifying the strides of the convolution along the width and height.
Can be a single integer to specify the same value for
all spatial dimensions.
nl: String, nonlinearity activation type.
# Returns
Output tensor.
"""
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs)
x = BatchNormalization(axis=channel_axis)(x)
return self._return_activation(x, nl)
def _squeeze(self, inputs):
"""Squeeze and Excitation.
This function defines a squeeze structure.
# Arguments
inputs: Tensor, input tensor of conv layer.
"""
input_channels = int(inputs.shape[-1])
x = GlobalAveragePooling2D()(inputs)
x = Dense(input_channels, activation='relu')(x)
x = Dense(input_channels, activation='hard_sigmoid')(x)
return x
def _bottleneck(self, inputs, filters, kernel, e, s, squeeze, nl):
"""Bottleneck
This function defines a basic bottleneck structure.
# Arguments
inputs: Tensor, input tensor of conv layer.
filters: Integer, the dimensionality of the output space.
kernel: An integer or tuple/list of 2 integers, specifying the
width and height of the 2D convolution window.
e: Integer, expansion factor.
t is always applied to the input size.
s: An integer or tuple/list of 2 integers,specifying the strides
of the convolution along the width and height.Can be a single
integer to specify the same value for all spatial dimensions.
squeeze: Boolean, Whether to use the squeeze.
nl: String, nonlinearity activation type.
# Returns
Output tensor.
"""
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
input_shape = K.int_shape(inputs)
tchannel = input_shape[channel_axis] * e
r = s == 1 and input_shape[3] == filters
x = self._conv_block(inputs, tchannel, (1, 1), (1, 1), nl)
x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
if squeeze:
x = Lambda(lambda x: x * self._squeeze(x))(x)
x = self._return_activation(x, nl)
x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x)
x = BatchNormalization(axis=channel_axis)(x)
if r:
x = Add()([x, inputs])
return x
def build(self):
pass
MobileNet V3 Large:
"""MobileNet v3 Large models for Keras.
# Reference
[Searching for MobileNetV3](https://arxiv.org/abs/1905.02244?context=cs)
"""
from keras.models import Model
from keras.layers import Input, Conv2D, GlobalAveragePooling2D, Reshape
from keras.utils.vis_utils import plot_model
from model.mobilenet_base import MobileNetBase
class MobileNetV3_Large(MobileNetBase):
def __init__(self, shape, n_class):
"""Init.
# Arguments
input_shape: An integer or tuple/list of 3 integers, shape
of input tensor.
n_class: Integer, number of classes.
# Returns
MobileNetv2 model.
"""
super(MobileNetV3_Large, self).__init__(shape, n_class)
def build(self, plot=False):
"""build MobileNetV3 Large.
# Arguments
plot: Boolean, weather to plot model.
# Returns
model: Model, model.
"""
inputs = Input(shape=self.shape)
x = self._conv_block(inputs, 16, (3, 3), strides=(2, 2), nl='HS')
x = self._bottleneck(x, 16, (3, 3), e=16, s=1, squeeze=False, nl='RE')
x = self._bottleneck(x, 24, (3, 3), e=64, s=2, squeeze=False, nl='RE')
x = self._bottleneck(x, 24, (3, 3), e=72, s=1, squeeze=False, nl='RE')
x = self._bottleneck(x, 40, (5, 5), e=72, s=2, squeeze=True, nl='RE')
x = self._bottleneck(x, 40, (5, 5), e=120, s=1, squeeze=True, nl='RE')
x = self._bottleneck(x, 40, (5, 5), e=120, s=1, squeeze=True, nl='RE')
x = self._bottleneck(x, 80, (3, 3), e=240, s=2, squeeze=False, nl='HS')
x = self._bottleneck(x, 80, (3, 3), e=200, s=1, squeeze=False, nl='HS')
x = self._bottleneck(x, 80, (3, 3), e=184, s=1, squeeze=False, nl='HS')
x = self._bottleneck(x, 80, (3, 3), e=184, s=1, squeeze=False, nl='HS')
x = self._bottleneck(x, 112, (3, 3), e=480, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 112, (3, 3), e=672, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 160, (5, 5), e=672, s=2, squeeze=True, nl='HS')
x = self._bottleneck(x, 160, (5, 5), e=960, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 160, (5, 5), e=960, s=1, squeeze=True, nl='HS')
x = self._conv_block(x, 960, (1, 1), strides=(1, 1), nl='HS')
x = GlobalAveragePooling2D()(x)
x = Reshape((1, 1, 960))(x)
x = Conv2D(1280, (1, 1), padding='same')(x)
x = self._return_activation(x, 'HS')
x = Conv2D(self.n_class, (1, 1), padding='same', activation='softmax')(x)
output = Reshape((self.n_class,))(x)
model = Model(inputs, output)
if plot:
plot_model(model, to_file='images/MobileNetv3_large.png', show_shapes=True)
return model
MobileNet V3 Small:
"""MobileNet v3 small models for Keras.
# Reference
[Searching for MobileNetV3](https://arxiv.org/abs/1905.02244?context=cs)
"""
from keras.models import Model
from keras.layers import Input, Conv2D, GlobalAveragePooling2D, Reshape
from keras.utils.vis_utils import plot_model
from model.mobilenet_base import MobileNetBase
class MobileNetV3_Small(MobileNetBase):
def __init__(self, shape, n_class):
"""Init.
# Arguments
input_shape: An integer or tuple/list of 3 integers, shape
of input tensor.
n_class: Integer, number of classes.
# Returns
MobileNetv2 model.
"""
super(MobileNetV3_Small, self).__init__(shape, n_class)
def build(self, plot=False):
"""build MobileNetV3 Small.
# Arguments
plot: Boolean, weather to plot model.
# Returns
model: Model, model.
"""
inputs = Input(shape=self.shape)
x = self._conv_block(inputs, 16, (3, 3), strides=(2, 2), nl='HS')
x = self._bottleneck(x, 16, (3, 3), e=16, s=2, squeeze=True, nl='RE')
x = self._bottleneck(x, 24, (3, 3), e=72, s=2, squeeze=False, nl='RE')
x = self._bottleneck(x, 24, (3, 3), e=88, s=1, squeeze=False, nl='RE')
x = self._bottleneck(x, 40, (5, 5), e=96, s=2, squeeze=True, nl='HS')
x = self._bottleneck(x, 40, (5, 5), e=240, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 40, (5, 5), e=240, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 48, (5, 5), e=120, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 48, (5, 5), e=144, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 96, (5, 5), e=288, s=2, squeeze=True, nl='HS')
x = self._bottleneck(x, 96, (5, 5), e=576, s=1, squeeze=True, nl='HS')
x = self._bottleneck(x, 96, (5, 5), e=576, s=1, squeeze=True, nl='HS')
x = self._conv_block(x, 576, (1, 1), strides=(1, 1), nl='HS')
x = GlobalAveragePooling2D()(x)
x = Reshape((1, 1, 576))(x)
x = Conv2D(1280, (1, 1), padding='same')(x)
x = self._return_activation(x, 'HS')
x = Conv2D(self.n_class, (1, 1), padding='same', activation='softmax')(x)
output = Reshape((self.n_class,))(x)
model = Model(inputs, output)
if plot:
plot_model(model, to_file='images/MobileNetv3_small.png', show_shapes=True)
return model
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
