import warnings warnings.filterwarnings("ignore") import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' from tensorflow.keras.layers import (DepthwiseConv2D,BatchNormalization, Activation,Conv2D,Input,GlobalAveragePooling2D, Reshape,Dropout,) import tensorflow.keras.backend as K from tensorflow.keras.models import Model def relu6(x): return K.relu(x, max_value=6) def _depthwise_conv_block(inputs,pointwise_conv_filters,alpha, depth_multiplier=1,strides=(1,1),block_id=1): pointwise_conv_filters = int(pointwise_conv_filters * alpha) x = DepthwiseConv2D((3,3),strides=strides,padding='same', depth_multiplier=depth_multiplier,use_bias=False, name=f'conv_dw_{block_id}')(inputs) x = BatchNormalization(name=f'conv_dw_{block_id}_bn')(x) x = Activation(relu6,name=f'conv_dw_{block_id}_relu')(x) x = Conv2D(pointwise_conv_filters,(1,1),padding='same', use_bias=False,strides=(1,1),name=f'conv_pw_{block_id}')(x) x = BatchNormalization(name=f'conv_pw_{block_id}_bn')(x) return Activation(relu6,name=f'conv_pw_{block_id}_relu')(x) def _conv_block(inputs,filters,alpha,kernel=(3,3),strides=(1,1)): filters = int(filters * alpha) x = Conv2D(filters,kernel,padding='same',use_bias=False, strides=strides,name='conv1')(inputs) x = BatchNormalization(name='conv1_bn')(x) return Activation(relu6,name='conv1_relu')(x) def MobileNet(input_shape=None,alpha=1.0,depth_multiplier=1,dropout=1e-3, classes=1000): img_input = Input(shape=input_shape) # 224,224,3 -> 112,112,32 [224/2]向上取整 x = _conv_block(img_input,32,alpha,strides=(2,2)) # 112,112,32 -> 112,112,64 x = _depthwise_conv_block(x,64,alpha,depth_multiplier,block_id=1) # 112,112,64 -> 56,56,128 x = _depthwise_conv_block(x,128,alpha,depth_multiplier,strides=(2,2),block_id=2) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3) # 56,56,128 -> 28,28,256 x = _depthwise_conv_block(x,256,alpha,depth_multiplier,strides=(2,2), block_id=4) x = _depthwise_conv_block(x,256,alpha,depth_multiplier,block_id=5) # 28,28,256 -> 14,14,512 x = _depthwise_conv_block(x,512,alpha,depth_multiplier,strides=(2,2), block_id=6) # 14,14,512 -> 14,14,512 (5*) for i in range(1,6): x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=(6+i)) # 14,14,512 -> 7,7,1024 x = _depthwise_conv_block(x,1024,alpha,depth_multiplier,strides=(2,2), block_id=12) # 7,7,1024 -> 7,7,1024 x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13) # 7,7,1024 -> 1,1,1024 x = GlobalAveragePooling2D()(x) print('x.shape = ',x.shape[-1]) shape = (1,1,x.shape[-1]) x = Reshape(shape,name='reshape_1')(x) x = Dropout(dropout,name='dropout')(x) x = Conv2D(classes,(1,1),padding='same',name='conv_preds')(x) x = Activation('softmax',name='act_softmax')(x) x = Reshape((classes,),name='reshape_2')(x) model = Model(img_input,x,name=f'mobilenet_{alpha:.2f}') return model if __name__ == '__main__': model = MobileNet(input_shape=(224,224,3)) model.summary()
x.shape = 1024 Model: "mobilenet_1.00" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 224, 224, 3)] 0 _________________________________________________________________ conv1 (Conv2D) (None, 112, 112, 32) 864 _________________________________________________________________ conv1_bn (BatchNormalization (None, 112, 112, 32) 128 _________________________________________________________________ conv1_relu (Activation) (None, 112, 112, 32) 0 _________________________________________________________________ conv_dw_1 (DepthwiseConv2D) (None, 112, 112, 32) 288 _________________________________________________________________ conv_dw_1_bn (BatchNormaliza (None, 112, 112, 32) 128 _________________________________________________________________ conv_dw_1_relu (Activation) (None, 112, 112, 32) 0 _________________________________________________________________ conv_pw_1 (Conv2D) (None, 112, 112, 64) 2048 _________________________________________________________________ conv_pw_1_bn (BatchNormaliza (None, 112, 112, 64) 256 _________________________________________________________________ conv_pw_1_relu (Activation) (None, 112, 112, 64) 0 _________________________________________________________________ conv_dw_2 (DepthwiseConv2D) (None, 56, 56, 64) 576 _________________________________________________________________ conv_dw_2_bn (BatchNormaliza (None, 56, 56, 64) 256 _________________________________________________________________ conv_dw_2_relu (Activation) (None, 56, 56, 64) 0 _________________________________________________________________ conv_pw_2 (Conv2D) (None, 56, 56, 128) 8192 _________________________________________________________________ conv_pw_2_bn (BatchNormaliza (None, 56, 56, 128) 512 _________________________________________________________________ conv_pw_2_relu (Activation) (None, 56, 56, 128) 0 _________________________________________________________________ conv_dw_3 (DepthwiseConv2D) (None, 56, 56, 128) 1152 _________________________________________________________________ conv_dw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 _________________________________________________________________ conv_dw_3_relu (Activation) (None, 56, 56, 128) 0 _________________________________________________________________ conv_pw_3 (Conv2D) (None, 56, 56, 128) 16384 _________________________________________________________________ conv_pw_3_bn (BatchNormaliza (None, 56, 56, 128) 512 _________________________________________________________________ conv_pw_3_relu (Activation) (None, 56, 56, 128) 0 _________________________________________________________________ conv_dw_4 (DepthwiseConv2D) (None, 28, 28, 128) 1152 _________________________________________________________________ conv_dw_4_bn (BatchNormaliza (None, 28, 28, 128) 512 _________________________________________________________________ conv_dw_4_relu (Activation) (None, 28, 28, 128) 0 _________________________________________________________________ conv_pw_4 (Conv2D) (None, 28, 28, 256) 32768 _________________________________________________________________ conv_pw_4_bn (BatchNormaliza (None, 28, 28, 256) 1024 _________________________________________________________________ conv_pw_4_relu (Activation) (None, 28, 28, 256) 0 _________________________________________________________________ conv_dw_5 (DepthwiseConv2D) (None, 28, 28, 256) 2304 _________________________________________________________________ conv_dw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 _________________________________________________________________ conv_dw_5_relu (Activation) (None, 28, 28, 256) 0 _________________________________________________________________ conv_pw_5 (Conv2D) (None, 28, 28, 256) 65536 _________________________________________________________________ conv_pw_5_bn (BatchNormaliza (None, 28, 28, 256) 1024 _________________________________________________________________ conv_pw_5_relu (Activation) (None, 28, 28, 256) 0 _________________________________________________________________ conv_dw_6 (DepthwiseConv2D) (None, 14, 14, 256) 2304 _________________________________________________________________ conv_dw_6_bn (BatchNormaliza (None, 14, 14, 256) 1024 _________________________________________________________________ conv_dw_6_relu (Activation) (None, 14, 14, 256) 0 _________________________________________________________________ conv_pw_6 (Conv2D) (None, 14, 14, 512) 131072 _________________________________________________________________ conv_pw_6_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_6_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_7 (DepthwiseConv2D) (None, 14, 14, 512) 4608 _________________________________________________________________ conv_dw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_dw_7_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_pw_7 (Conv2D) (None, 14, 14, 512) 262144 _________________________________________________________________ conv_pw_7_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_7_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_8 (DepthwiseConv2D) (None, 14, 14, 512) 4608 _________________________________________________________________ conv_dw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_dw_8_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_pw_8 (Conv2D) (None, 14, 14, 512) 262144 _________________________________________________________________ conv_pw_8_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_8_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_9 (DepthwiseConv2D) (None, 14, 14, 512) 4608 _________________________________________________________________ conv_dw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_dw_9_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_pw_9 (Conv2D) (None, 14, 14, 512) 262144 _________________________________________________________________ conv_pw_9_bn (BatchNormaliza (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_9_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 512) 4608 _________________________________________________________________ conv_dw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 _________________________________________________________________ conv_dw_10_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_pw_10 (Conv2D) (None, 14, 14, 512) 262144 _________________________________________________________________ conv_pw_10_bn (BatchNormaliz (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_10_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 512) 4608 _________________________________________________________________ conv_dw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 _________________________________________________________________ conv_dw_11_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_pw_11 (Conv2D) (None, 14, 14, 512) 262144 _________________________________________________________________ conv_pw_11_bn (BatchNormaliz (None, 14, 14, 512) 2048 _________________________________________________________________ conv_pw_11_relu (Activation) (None, 14, 14, 512) 0 _________________________________________________________________ conv_dw_12 (DepthwiseConv2D) (None, 7, 7, 512) 4608 _________________________________________________________________ conv_dw_12_bn (BatchNormaliz (None, 7, 7, 512) 2048 _________________________________________________________________ conv_dw_12_relu (Activation) (None, 7, 7, 512) 0 _________________________________________________________________ conv_pw_12 (Conv2D) (None, 7, 7, 1024) 524288 _________________________________________________________________ conv_pw_12_bn (BatchNormaliz (None, 7, 7, 1024) 4096 _________________________________________________________________ conv_pw_12_relu (Activation) (None, 7, 7, 1024) 0 _________________________________________________________________ conv_dw_13 (DepthwiseConv2D) (None, 7, 7, 1024) 9216 _________________________________________________________________ conv_dw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 _________________________________________________________________ conv_dw_13_relu (Activation) (None, 7, 7, 1024) 0 _________________________________________________________________ conv_pw_13 (Conv2D) (None, 7, 7, 1024) 1048576 _________________________________________________________________ conv_pw_13_bn (BatchNormaliz (None, 7, 7, 1024) 4096 _________________________________________________________________ conv_pw_13_relu (Activation) (None, 7, 7, 1024) 0 _________________________________________________________________ global_average_pooling2d (Gl (None, 1024) 0 _________________________________________________________________ reshape_1 (Reshape) (None, 1, 1, 1024) 0 _________________________________________________________________ dropout (Dropout) (None, 1, 1, 1024) 0 _________________________________________________________________ conv_preds (Conv2D) (None, 1, 1, 1000) 1025000 _________________________________________________________________ act_softmax (Activation) (None, 1, 1, 1000) 0 _________________________________________________________________ reshape_2 (Reshape) (None, 1000) 0 ================================================================= Total params: 4,253,864 Trainable params: 4,231,976 Non-trainable params: 21,888 _________________________________________________________________ Process finished with exit code 0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)