python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解
什么是MobileNetV3
不知道咋地,就是突然想把small也一起寫(xiě)了。
最新的MobileNetV3的被寫(xiě)在了論文《Searching for MobileNetV3》中。
它是mobilnet的最新版,據(jù)說(shuō)效果還是很好的。
作為一種輕量級(jí)網(wǎng)絡(luò),它的參數(shù)量還是一如既往的小。
它綜合了以下四個(gè)特點(diǎn):
1、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。
2、MobileNetV2的具有線性瓶頸的逆殘差結(jié)構(gòu)(the inverted residual with linear bottleneck)。
3、輕量級(jí)的注意力模型。
4、利用h-swish代替swish函數(shù)。
large與small的區(qū)別
其實(shí)MobileNetV3中的large與small模型沒(méi)有特別大的區(qū)別,主要的區(qū)別是通道數(shù)的變化與bneck的次數(shù)。
MobileNetV3(small)的網(wǎng)絡(luò)結(jié)構(gòu)
1、MobileNetV3(small)的整體結(jié)構(gòu)
如上為MobileNetV3(small)的表,與MobileNetV3(large)相比,bneck的次數(shù)與通道數(shù)都有一定的下降。
如何看懂這個(gè)表呢?我們從每一列出發(fā):
第一列Input代表mobilenetV3每個(gè)特征層的shape變化;
第二列Operator代表每次特征層即將經(jīng)歷的block結(jié)構(gòu),我們可以看到在MobileNetV3中,特征提取經(jīng)過(guò)了許多的bneck結(jié)構(gòu);
第三、四列分別代表了bneck內(nèi)逆殘差結(jié)構(gòu)上升后的通道數(shù)、輸入到bneck時(shí)特征層的通道數(shù)。
第五列SE代表了是否在這一層引入注意力機(jī)制。
第六列NL代表了激活函數(shù)的種類,HS代表h-swish,RE代表RELU。
第七列s代表了每一次block結(jié)構(gòu)所用的步長(zhǎng)。
2、MobileNetV3特有的bneck結(jié)構(gòu)
bneck結(jié)構(gòu)如下圖所示:
它綜合了以下四個(gè)特點(diǎn):
a、MobileNetV2的具有線性瓶頸的逆殘差結(jié)構(gòu)(the inverted residual with linear bottleneck)。
即先利用1x1卷積進(jìn)行升維度,再進(jìn)行下面的操作,并具有殘差邊。
b、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。
在輸入1x1卷積進(jìn)行升維度后,進(jìn)行3x3深度可分離卷積。
c、輕量級(jí)的注意力模型。
這個(gè)注意力機(jī)制的作用方式是調(diào)整每個(gè)通道的權(quán)重。
d、利用h-swish代替swish函數(shù)。
在結(jié)構(gòu)中使用了h-swishj激活函數(shù),代替swish函數(shù),減少運(yùn)算量,提高性能。
網(wǎng)絡(luò)實(shí)現(xiàn)代碼
由于keras代碼沒(méi)有預(yù)訓(xùn)練權(quán)重,所以只是把網(wǎng)絡(luò)結(jié)構(gòu)po出來(lái)。
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(chóng)模擬sigmoid return x * K.relu(x + 3.0, max_value=6.0) / 6.0 def return_activation(x, nl): # 用于判斷使用哪個(gè)激活函數(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): # 一個(gè)卷積單元,也就是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): # 注意力機(jī)制單元 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) # 進(jìn)行3x3深度可分離卷積 x = DepthwiseConv2D(kernel, strides=(stride, stride), depth_multiplier=1, padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) x = return_activation(x, nl) # 引入注意力機(jī)制 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_small(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') # 112,112,16 -> 56,56,16 x = bottleneck(x, 16, (3, 3), up_dim=16, stride=2, sq=True, nl='RE') # 56,56,16 -> 28,28,24 x = bottleneck(x, 24, (3, 3), up_dim=72, stride=2, sq=False, nl='RE') x = bottleneck(x, 24, (3, 3), up_dim=88, stride=1, sq=False, nl='RE') # 28,28,24 -> 14,14,40 x = bottleneck(x, 40, (5, 5), up_dim=96, stride=2, sq=True, nl='HS') x = bottleneck(x, 40, (5, 5), up_dim=240, stride=1, sq=True, nl='HS') x = bottleneck(x, 40, (5, 5), up_dim=240, stride=1, sq=True, nl='HS') # 14,14,40 -> 14,14,48 x = bottleneck(x, 48, (5, 5), up_dim=120, stride=1, sq=True, nl='HS') x = bottleneck(x, 48, (5, 5), up_dim=144, stride=1, sq=True, nl='HS') # 14,14,48 -> 7,7,96 x = bottleneck(x, 96, (5, 5), up_dim=288, stride=2, sq=True, nl='HS') x = bottleneck(x, 96, (5, 5), up_dim=576, stride=1, sq=True, nl='HS') x = bottleneck(x, 96, (5, 5), up_dim=576, stride=1, sq=True, nl='HS') x = conv_block(x, 576, (1, 1), strides=(1, 1), nl='HS') x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 576))(x) x = Conv2D(1024, (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_small() model.summary()
以上就是python神經(jīng)網(wǎng)絡(luò)MobileNetV3 small模型的復(fù)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于MobileNetV3 small模型復(fù)現(xiàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python機(jī)器學(xué)習(xí)利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線
- 利用Python畫(huà)ROC曲線和AUC值計(jì)算
- 一文詳解Python灰色預(yù)測(cè)模型實(shí)現(xiàn)示例
- python回歸分析邏輯斯蒂模型之多分類任務(wù)詳解
- python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類
- Python實(shí)現(xiàn)自動(dòng)駕駛訓(xùn)練模型
- python神經(jīng)網(wǎng)絡(luò)Keras?GhostNet模型的實(shí)現(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?large模型的復(fù)現(xiàn)詳解
- python神經(jīng)網(wǎng)絡(luò)Inception?ResnetV2模型復(fù)現(xiàn)詳解
- python模型性能ROC和AUC分析詳解
相關(guān)文章
Python發(fā)送手機(jī)動(dòng)態(tài)驗(yàn)證碼代碼實(shí)例
這篇文章主要介紹了Python發(fā)送手機(jī)動(dòng)態(tài)驗(yàn)證碼代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02python 實(shí)現(xiàn)按對(duì)象傳值
今天小編就為大家分享一篇python 實(shí)現(xiàn)按對(duì)象傳值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12用Python實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)(附代碼)
這篇文章主要介紹了用Python實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)(附代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Xadmin+rules實(shí)現(xiàn)多選行權(quán)限方式(級(jí)聯(lián)效果)
這篇文章主要介紹了Xadmin+rules實(shí)現(xiàn)多選行權(quán)限方式(級(jí)聯(lián)效果),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python3環(huán)境搭建過(guò)程(利用Anaconda+pycharm)完整版
這篇文章主要介紹了python3環(huán)境搭建過(guò)程(利用Anaconda+pycharm)完整版,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Python中實(shí)現(xiàn)變量賦值傳遞時(shí)的引用和拷貝方法
下面小編就為大家分享一篇Python中實(shí)現(xiàn)變量賦值傳遞時(shí)的引用和拷貝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04