python神經網絡MobileNetV3?small模型的復現詳解
什么是MobileNetV3
不知道咋地,就是突然想把small也一起寫了。
最新的MobileNetV3的被寫在了論文《Searching for MobileNetV3》中。
它是mobilnet的最新版,據說效果還是很好的。
作為一種輕量級網絡,它的參數量還是一如既往的小。
它綜合了以下四個特點:
1、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。
2、MobileNetV2的具有線性瓶頸的逆殘差結構(the inverted residual with linear bottleneck)。
3、輕量級的注意力模型。
4、利用h-swish代替swish函數。
large與small的區(qū)別
其實MobileNetV3中的large與small模型沒有特別大的區(qū)別,主要的區(qū)別是通道數的變化與bneck的次數。
MobileNetV3(small)的網絡結構
1、MobileNetV3(small)的整體結構
如上為MobileNetV3(small)的表,與MobileNetV3(large)相比,bneck的次數與通道數都有一定的下降。
如何看懂這個表呢?我們從每一列出發(fā):
第一列Input代表mobilenetV3每個特征層的shape變化;
第二列Operator代表每次特征層即將經歷的block結構,我們可以看到在MobileNetV3中,特征提取經過了許多的bneck結構;
第三、四列分別代表了bneck內逆殘差結構上升后的通道數、輸入到bneck時特征層的通道數。
第五列SE代表了是否在這一層引入注意力機制。
第六列NL代表了激活函數的種類,HS代表h-swish,RE代表RELU。
第七列s代表了每一次block結構所用的步長。
2、MobileNetV3特有的bneck結構
bneck結構如下圖所示:
它綜合了以下四個特點:
a、MobileNetV2的具有線性瓶頸的逆殘差結構(the inverted residual with linear bottleneck)。
即先利用1x1卷積進行升維度,再進行下面的操作,并具有殘差邊。
b、MobileNetV1的深度可分離卷積(depthwise separable convolutions)。
在輸入1x1卷積進行升維度后,進行3x3深度可分離卷積。
c、輕量級的注意力模型。
這個注意力機制的作用方式是調整每個通道的權重。
d、利用h-swish代替swish函數。
在結構中使用了h-swishj激活函數,代替swish函數,減少運算量,提高性能。
網絡實現代碼
由于keras代碼沒有預訓練權重,所以只是把網絡結構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函數 return K.relu(x, max_value=6.0) def hard_swish(x): # 利用relu函數乘上x模擬sigmoid return x * K.relu(x + 3.0, max_value=6.0) / 6.0 def return_activation(x, nl): # 用于判斷使用哪個激活函數 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卷積調整通道數,通道數上升 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) # 下降通道數 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神經網絡MobileNetV3 small模型的復現詳解的詳細內容,更多關于MobileNetV3 small模型復現的資料請關注腳本之家其它相關文章!
- Python機器學習利用鳶尾花數據繪制ROC和AUC曲線
- 利用Python畫ROC曲線和AUC值計算
- 一文詳解Python灰色預測模型實現示例
- python回歸分析邏輯斯蒂模型之多分類任務詳解
- python深度學習tensorflow訓練好的模型進行圖像分類
- Python實現自動駕駛訓練模型
- python神經網絡Keras?GhostNet模型的實現
- python神經網絡ShuffleNetV2模型復現詳解
- python神經網絡Densenet模型復現詳解
- python神經網絡MobileNetV3?large模型的復現詳解
- python神經網絡Inception?ResnetV2模型復現詳解
- python模型性能ROC和AUC分析詳解
相關文章
python3環(huán)境搭建過程(利用Anaconda+pycharm)完整版
這篇文章主要介紹了python3環(huán)境搭建過程(利用Anaconda+pycharm)完整版,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08