网络介绍请参看:博文
keras搭建深度学习模型的若干方法:博文
直接上网络结构
依旧看看标准答案
import tensorflow as tf
from tensorflow import keras
base_model = keras.applications.MobileNet(weights='imagenet')
base_model.summary()
一个是标准的3x3卷积block,另一个就是Depthwise Separable block
import tensorflow as tf
from tensorflow import keras
import tensorflow.keras.backend as K
from tensorflow.keras import layers, models, Sequential, backend
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, BatchNormalization, Activation, GlobalAveragePooling2D
from tensorflow.keras.layers import Concatenate, Lambda, Input, ZeroPadding2D, AveragePooling2D, DepthwiseConv2D, Reshape
def depthwise_separable_block(x, nb_filter, stride=(1, 1), name=None):
x = DepthwiseConv2D((3,3), padding='same', strides=stride, depth_multiplier=1, use_bias=False, name=name+'_dpconv')(x)
x = BatchNormalization(axis=3, name=name+'_bn1')(x)
x = Activation(relu6, name=name+'_relu1')(x)
x = Conv2D(nb_filter, (1,1), padding='same', use_bias=False, strides=(1,1), name=name+'conv_2')(x)
x = BatchNormalization(axis=3, name=name+'_bn2')(x)
x = Activation(relu6, name=name+'_relu2')(x)
return x
def conv_block (x, nb_filter, stride=(1,1), name=None):
x = Conv2D(nb_filter, (3,3), strides=stride, padding='same', use_bias=False, name=name+'_conv1')(x)
x = BatchNormalization(axis=3, name=name+'bn1')(x)
x = Activation(relu6, name=name+'relu')(x)
return x
def relu6(x):
return K.relu(x, max_value=6)
此处注意网络中的所有卷积网络use_bias=False,即神经元无偏差设置
def MovblieNet (nb_classes, dropout=0):
img_input = Input(shape=(224, 224, 3))
x = conv_block(img_input, 32, (2,2), name='conv_block')
x = depthwise_separable_block(x, 64, stride=(1,1), name='dep1')
x = depthwise_separable_block(x, 128, stride=(2,2), name='dep2')
x = depthwise_separable_block(x, 128, stride=(1,1), name='dep3')
x = depthwise_separable_block(x, 256, stride=(2,2), name='dep4')
x = depthwise_separable_block(x, 256, stride=(1,1), name='dep5')
x = depthwise_separable_block(x, 512, stride=(2,2), name='dep6')
x = depthwise_separable_block(x, 512, stride=(1,1), name='dep7')
x = depthwise_separable_block(x, 512, stride=(1,1), name='dep8')
x = depthwise_separable_block(x, 512, stride=(1,1), name='dep9')
x = depthwise_separable_block(x, 512, stride=(1,1), name='dep10')
x = depthwise_separable_block(x, 512, stride=(1,1), name='dep11')
x = depthwise_separable_block(x, 1024, stride=(2,2), name='dep12')
x = depthwise_separable_block(x, 1024, stride=(1,1), name='dep13')
x = GlobalAveragePooling2D()(x)
x = Reshape((1,1,1024), name='reshape_1')(x)
x = Dropout(dropout, name='dropout')(x)
x = Conv2D(nb_classes, (1,1), padding='same', name='conv_preds')(x)
x = Reshape((nb_classes,), name='reshape_2')(x)
x = Activation('softmax', name='act_softmax')(x)
model = models.Model(img_input, x, name='MobileNet')
return model
def main():
model = MovblieNet(1000, 0.2)
model.summary()
if __name__=='__main__':
main()
网络总参数与迁移学习的数目相同,说明重建成功
https://github.com/rcmalli/keras-mobilenet/blob/master/keras_mobilenet/mobilenet.py