欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python神經(jīng)網(wǎng)絡(luò)MobileNetV3?large模型的復(fù)現(xiàn)詳解

 更新時間:2022年05月07日 10:49:44   作者:Bubbliiiing  
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)MobileNetV3?large模型的復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

什么是MobileNetV3

為了防止某位我的粉絲寒假沒有辦法正常工作,我趕緊看了看MobilenetV3

最新的MobileNetV3的被寫在了論文《Searching for MobileNetV3》中。

它是mobilnet的最新版,據(jù)說效果還是很好的。

作為一種輕量級網(wǎng)絡(luò),它的參數(shù)量還是一如既往的小。

它綜合了以下四個特點:

1、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。

2、MobileNetV2的具有線性瓶頸的逆殘差結(jié)構(gòu)(the inverted residual with linear bottleneck)。

3、輕量級的注意力模型。

4、利用h-swish代替swish函數(shù)。

代碼下載

MobileNetV3(large)的網(wǎng)絡(luò)結(jié)構(gòu)

1、MobileNetV3(large)的整體結(jié)構(gòu)

如何看懂這個表呢?我們從每一列出發(fā):

第一列Input代表mobilenetV3每個特征層的shape變化;

第二列Operator代表每次特征層即將經(jīng)歷的block結(jié)構(gòu),我們可以看到在MobileNetV3中,特征提取經(jīng)過了許多的bneck結(jié)構(gòu);

第三、四列分別代表了bneck內(nèi)逆殘差結(jié)構(gòu)上升后的通道數(shù)、輸入到bneck時特征層的通道數(shù)。

第五列SE代表了是否在這一層引入注意力機制。

第六列NL代表了激活函數(shù)的種類,HS代表h-swish,RE代表RELU。

第七列s代表了每一次block結(jié)構(gòu)所用的步長。

2、MobileNetV3特有的bneck結(jié)構(gòu)

bneck結(jié)構(gòu)如下圖所示:

它綜合了以下四個特點:

a、MobileNetV2的具有線性瓶頸的逆殘差結(jié)構(gòu)(the inverted residual with linear bottleneck)。

即先利用1x1卷積進行升維度,再進行下面的操作,并具有殘差邊。

b、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。

在輸入1x1卷積進行升維度后,進行3x3深度可分離卷積。

c、輕量級的注意力模型。

這個注意力機制的作用方式是調(diào)整每個通道的權(quán)重。

d、利用h-swish代替swish函數(shù)。

在結(jié)構(gòu)中使用了h-swishj激活函數(shù),代替swish函數(shù),減少運算量,提高性能。

網(wǎng)絡(luò)實現(xiàn)代碼

由于keras代碼沒有預(yù)訓(xùn)練權(quán)重,所以只是把網(wǎng)絡(luò)結(jié)構(gòu)po出來。

from keras.layers import Conv2D, DepthwiseConv2D, Dense, GlobalAveragePooling2D,Input
from keras.layers import Activation, BatchNormalization, Add, Multiply, Reshape
from keras.models import Model
from keras import backend as K
alpha = 1
def relu6(x):
    # relu函數(shù)
    return K.relu(x, max_value=6.0)
def hard_swish(x):
    # 利用relu函數(shù)乘上x模擬sigmoid
    return x * K.relu(x + 3.0, max_value=6.0) / 6.0
def return_activation(x, nl):
    # 用于判斷使用哪個激活函數(shù)
    if nl == 'HS':
        x = Activation(hard_swish)(x)
    if nl == 'RE':
        x = Activation(relu6)(x)
    return x
def conv_block(inputs, filters, kernel, strides, nl):
    # 一個卷積單元,也就是conv2d + batchnormalization + activation
    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 return_activation(x, nl)
def squeeze(inputs):
    # 注意力機制單元
    input_channels = int(inputs.shape[-1])
    x = GlobalAveragePooling2D()(inputs)
    x = Dense(int(input_channels/4))(x)
    x = Activation(relu6)(x)
    x = Dense(input_channels)(x)
    x = Activation(hard_swish)(x)
    x = Reshape((1, 1, input_channels))(x)
    x = Multiply()([inputs, x])
    return x
def bottleneck(inputs, filters, kernel, up_dim, stride, sq, nl):
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    input_shape = K.int_shape(inputs)
    tchannel = int(up_dim)
    cchannel = int(alpha * filters)
    r = stride == 1 and input_shape[3] == filters
    # 1x1卷積調(diào)整通道數(shù),通道數(shù)上升
    x = conv_block(inputs, tchannel, (1, 1), (1, 1), nl)
    # 進行3x3深度可分離卷積
    x = DepthwiseConv2D(kernel, strides=(stride, stride), depth_multiplier=1, padding='same')(x)
    x = BatchNormalization(axis=channel_axis)(x)
    x = return_activation(x, nl)
    # 引入注意力機制
    if sq:
        x = squeeze(x)
    # 下降通道數(shù)
    x = Conv2D(cchannel, (1, 1), strides=(1, 1), padding='same')(x)
    x = BatchNormalization(axis=channel_axis)(x)
    if r:
        x = Add()([x, inputs])
    return x
def MobileNetv3_large(shape = (224,224,3),n_class = 1000):
    inputs = Input(shape)
    # 224,224,3 -> 112,112,16
    x = conv_block(inputs, 16, (3, 3), strides=(2, 2), nl='HS')
    x = bottleneck(x, 16, (3, 3), up_dim=16, stride=1, sq=False, nl='RE')
    # 112,112,16 -> 56,56,24
    x = bottleneck(x, 24, (3, 3), up_dim=64, stride=2, sq=False, nl='RE')
    x = bottleneck(x, 24, (3, 3), up_dim=72, stride=1, sq=False, nl='RE')
    # 56,56,24 -> 28,28,40
    x = bottleneck(x, 40, (5, 5), up_dim=72, stride=2, sq=True, nl='RE')
    x = bottleneck(x, 40, (5, 5), up_dim=120, stride=1, sq=True, nl='RE')
    x = bottleneck(x, 40, (5, 5), up_dim=120, stride=1, sq=True, nl='RE')
    # 28,28,40 -> 14,14,80
    x = bottleneck(x, 80, (3, 3), up_dim=240, stride=2, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=200, stride=1, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=184, stride=1, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=184, stride=1, sq=False, nl='HS')
    # 14,14,80 -> 14,14,112
    x = bottleneck(x, 112, (3, 3), up_dim=480, stride=1, sq=True, nl='HS')
    x = bottleneck(x, 112, (3, 3), up_dim=672, stride=1, sq=True, nl='HS')
    # 14,14,112 -> 7,7,160
    x = bottleneck(x, 160, (5, 5), up_dim=672, stride=2, sq=True, nl='HS')
    x = bottleneck(x, 160, (5, 5), up_dim=960, stride=1, sq=True, nl='HS')
    x = bottleneck(x, 160, (5, 5), up_dim=960, stride=1, sq=True, nl='HS')
    # 7,7,160 -> 7,7,960
    x = 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 = return_activation(x, 'HS')
    x = Conv2D(n_class, (1, 1), padding='same', activation='softmax')(x)
    x = Reshape((n_class,))(x)
    model = Model(inputs, x)
    return model
if __name__ == "__main__":
    model = MobileNetv3_large()
    model.summary()

以上就是python神經(jīng)網(wǎng)絡(luò)MobileNetV3 large模型的復(fù)現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于MobileNetV3 large模型復(fù)現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論