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)文章!
- Python機器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線
- 利用Python畫ROC曲線和AUC值計算
- 一文詳解Python灰色預(yù)測模型實現(xiàn)示例
- python回歸分析邏輯斯蒂模型之多分類任務(wù)詳解
- python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進行圖像分類
- Python實現(xiàn)自動駕駛訓(xùn)練模型
- python神經(jīng)網(wǎng)絡(luò)Keras?GhostNet模型的實現(xiàn)
- python神經(jīng)網(wǎng)絡(luò)ShuffleNetV2模型復(fù)現(xiàn)詳解
- python神經(jīng)網(wǎng)絡(luò)Densenet模型復(fù)現(xiàn)詳解
- python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解
- python神經(jīng)網(wǎng)絡(luò)Inception?ResnetV2模型復(fù)現(xiàn)詳解
- python模型性能ROC和AUC分析詳解
相關(guān)文章
Python計算雙重差分模型DID及其對應(yīng)P值使用詳解
這篇文章主要介紹了Python計算DID及其對應(yīng)P值的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2021-09-09python實現(xiàn)將英文單詞表示的數(shù)字轉(zhuǎn)換成阿拉伯?dāng)?shù)字的方法
這篇文章主要介紹了python實現(xiàn)將英文單詞表示的數(shù)字轉(zhuǎn)換成阿拉伯?dāng)?shù)字的方法,涉及Python字符串轉(zhuǎn)換操作的相關(guān)技巧,需要的朋友可以參考下2015-07-07如何在windows下安裝Pycham2020軟件(方法步驟詳解)
這篇文章主要介紹了在windows下安裝Pycham2020軟件方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05python pandas 組內(nèi)排序、單組排序、標號的實例
下面小編就為大家分享一篇python pandas 組內(nèi)排序、單組排序、標號的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04