tensorflow2 实现MobileNetV1网络结构及可视化

网络结构

在这里插入图片描述

代码展示


from tensorflow.keras.layers import SeparableConv2D, Dense, Dropout, Conv2D, MaxPool2D, Flatten, BatchNormalization, Activation, Add, Input, ZeroPadding2D, GlobalAvgPool2D
from plot_model import plot_model
from tensorflow.keras.models import Model
from tensorflow.keras.layers import SeparableConv2D, Dense, Dropout, Conv2D, MaxPool2D, Flatten, BatchNormalization, Activation, Add, Input, ZeroPadding2D, GlobalAvgPool2D
def Depth_Con_block(x, filters=32, strides=1):
    # 深度卷积
    x = DepthwiseConv2D(kernel_size=3, strides=strides, padding='same')(x)
    x = BatchNormalization()(x)
    x = ReLU(max_value=6)(x)

    # 逐点卷积
    x = Conv2D(filters=filters, kernel_size=1, strides=1, padding='same')(x)
    x = BatchNormalization()(x)
    x = ReLU(6)(x)

    return x

def MobileNetV1(input_size, classes):
    x = Conv2D(filters=32, kernel_size=3, strides=2, padding='same')(input_size)
    x = BatchNormalization()(x)
    x = ReLU(6)(x)
    x = Depth_Con_block(x, filters=64)
    x = Depth_Con_block(x, filters=128, strides=2)
    x = Depth_Con_block(x, filters=128)
    x = Depth_Con_block(x, filters=256, strides=2)
    x = Depth_Con_block(x, filters=256)
    x = Depth_Con_block(x, filters=512, strides=2)
    for i in range(5):
        x = Depth_Con_block(x, filters=512)
    x = Depth_Con_block(x, filters=1024, strides=2)
    x = Depth_Con_block(x, filters=1024, strides=1)
    x = AveragePooling2D(pool_size=(7, 7), strides=1)(x)
    x = Dense(classes, activation='softmax')(x)

    return x

inputs = Input(shape=(224,224,3))
model = Model(inputs=inputs, outputs=MobileNetV1(inputs, num_classes))
model.summary()

输出:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 112, 112, 32)      896       
_________________________________________________________________
batch_normalization (BatchNo (None, 112, 112, 32)      128       
_________________________________________________________________
re_lu (ReLU)                 (None, 112, 112, 32)      0         
_________________________________________________________________
depthwise_conv2d (DepthwiseC (None, 112, 112, 32)      320       
_________________________________________________________________
batch_normalization_1 (Batch (None, 112, 112, 32)      128       
_________________________________________________________________
re_lu_1 (ReLU)               (None, 112, 112, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 112, 64)      2112      
_________________________________________________________________
batch_normalization_2 (Batch (None, 112, 112, 64)      256       
_________________________________________________________________
re_lu_2 (ReLU)               (None, 112, 112, 64)      0         
_________________________________________________________________
depthwise_conv2d_1 (Depthwis (None, 56, 56, 64)        640       
_________________________________________________________________
batch_normalization_3 (Batch (None, 56, 56, 64)        256       
_________________________________________________________________
re_lu_3 (ReLU)               (None, 56, 56, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 56, 56, 128)       8320      
_________________________________________________________________
batch_normalization_4 (Batch (None, 56, 56, 128)       512       
_________________________________________________________________
re_lu_4 (ReLU)               (None, 56, 56, 128)       0         
_________________________________________________________________
depthwise_conv2d_2 (Depthwis (None, 56, 56, 128)       1280      
_________________________________________________________________
batch_normalization_5 (Batch (None, 56, 56, 128)       512       
_________________________________________________________________
re_lu_5 (ReLU)               (None, 56, 56, 128)       0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 56, 56, 128)       16512     
_________________________________________________________________
batch_normalization_6 (Batch (None, 56, 56, 128)       512       
_________________________________________________________________
re_lu_6 (ReLU)               (None, 56, 56, 128)       0         
_________________________________________________________________
depthwise_conv2d_3 (Depthwis (None, 28, 28, 128)       1280      
_________________________________________________________________
batch_normalization_7 (Batch (None, 28, 28, 128)       512       
_________________________________________________________________
re_lu_7 (ReLU)               (None, 28, 28, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 28, 28, 256)       33024     
_________________________________________________________________
batch_normalization_8 (Batch (None, 28, 28, 256)       1024      
_________________________________________________________________
re_lu_8 (ReLU)               (None, 28, 28, 256)       0         
_________________________________________________________________
depthwise_conv2d_4 (Depthwis (None, 28, 28, 256)       2560      
_________________________________________________________________
batch_normalization_9 (Batch (None, 28, 28, 256)       1024      
_________________________________________________________________
re_lu_9 (ReLU)               (None, 28, 28, 256)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 28, 28, 256)       65792     
_________________________________________________________________
batch_normalization_10 (Batc (None, 28, 28, 256)       1024      
_________________________________________________________________
re_lu_10 (ReLU)              (None, 28, 28, 256)       0         
_________________________________________________________________
depthwise_conv2d_5 (Depthwis (None, 14, 14, 256)       2560      
_________________________________________________________________
batch_normalization_11 (Batc (None, 14, 14, 256)       1024      
_________________________________________________________________
re_lu_11 (ReLU)              (None, 14, 14, 256)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 14, 14, 512)       131584    
_________________________________________________________________
batch_normalization_12 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_12 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_6 (Depthwis (None, 14, 14, 512)       5120      
_________________________________________________________________
batch_normalization_13 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_13 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 14, 14, 512)       262656    
_________________________________________________________________
batch_normalization_14 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_14 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_7 (Depthwis (None, 14, 14, 512)       5120      
_________________________________________________________________
batch_normalization_15 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_15 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 14, 14, 512)       262656    
_________________________________________________________________
batch_normalization_16 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_16 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_8 (Depthwis (None, 14, 14, 512)       5120      
_________________________________________________________________
batch_normalization_17 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_17 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 14, 14, 512)       262656    
_________________________________________________________________
batch_normalization_18 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_18 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_9 (Depthwis (None, 14, 14, 512)       5120      
_________________________________________________________________
batch_normalization_19 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_19 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 14, 14, 512)       262656    
_________________________________________________________________
batch_normalization_20 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_20 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_10 (Depthwi (None, 14, 14, 512)       5120      
_________________________________________________________________
batch_normalization_21 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_21 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 14, 14, 512)       262656    
_________________________________________________________________
batch_normalization_22 (Batc (None, 14, 14, 512)       2048      
_________________________________________________________________
re_lu_22 (ReLU)              (None, 14, 14, 512)       0         
_________________________________________________________________
depthwise_conv2d_11 (Depthwi (None, 7, 7, 512)         5120      
_________________________________________________________________
batch_normalization_23 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
re_lu_23 (ReLU)              (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 7, 7, 1024)        525312    
_________________________________________________________________
batch_normalization_24 (Batc (None, 7, 7, 1024)        4096      
_________________________________________________________________
re_lu_24 (ReLU)              (None, 7, 7, 1024)        0         
_________________________________________________________________
depthwise_conv2d_12 (Depthwi (None, 7, 7, 1024)        10240     
_________________________________________________________________
batch_normalization_25 (Batc (None, 7, 7, 1024)        4096      
_________________________________________________________________
re_lu_25 (ReLU)              (None, 7, 7, 1024)        0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 7, 7, 1024)        1049600   
_________________________________________________________________
batch_normalization_26 (Batc (None, 7, 7, 1024)        4096      
_________________________________________________________________
re_lu_26 (ReLU)              (None, 7, 7, 1024)        0         
_________________________________________________________________
average_pooling2d (AveragePo (None, 1, 1, 1024)        0         
_________________________________________________________________
dense (Dense)                (None, 1, 1, 2)           2050      
=================================================================
Total params: 3,241,858
Trainable params: 3,219,970
Non-trainable params: 21,888
_________________________________________________________________

网络可视化

plot_model(model,
           to_file='MobileNetV1.png',
           show_shapes=True,
           show_layer_names=False,
           rankdir='TB',
           expand_nested=False,
           style = 0,
           color = True,
           dpi=200)

在这里插入图片描述

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值