keras复现MobileNet

论文地址链接:MobileNet:Efficient Convolutional Neural Networks for Mobile Vision Applications

MobileNet与其他网络不同的地方在于:使用ZeroPadding2D和DepthwiseConv2D

复现代码:

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 02:07:28 2020

@author: panansi
"""


from keras.layers import Input
from keras.layers import ZeroPadding2D
from keras.layers import Conv2D
from keras.layers import BatchNormalization
from keras.layers import DepthwiseConv2D
from keras.layers import Activation
from keras.models import Model

width = 125
input_shape = (width, width, 3)


def zeropadding2d(inputs, input_shape=None, padding = ((1,0),(0,1))):
    if input_shape==None:
        x = ZeroPadding2D(padding=padding)(inputs)
    else:
        x = ZeroPadding2D(input_shape = input_shape, padding=padding)(inputs)
    return x

def conv2d(inputs=None, filters=32, kernel_size=(1,1), strides=(1,1), padding='same', use_bias=False):
    x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, use_bias=use_bias)(inputs)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    return x

def depthwiseconv2d(inputs, strides=(1,1), kernel_size=(3,3), use_bias=False):
    if strides[0] == 1:
        padding = 'same'
    else:
        padding = 'valid'
    x = DepthwiseConv2D(kernel_size=kernel_size, strides=strides, padding=padding, use_bias = use_bias)(inputs)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    return x
INPUT = Input(shape=input_shape)

#32
x = zeropadding2d(INPUT, input_shape=input_shape)
x = conv2d(inputs=x, filters=32, kernel_size=(3,3), strides=(2,2), padding='valid')
x = depthwiseconv2d(x, strides=(1,1))
#64
x = conv2d(inputs=x, filters=64)
x = zeropadding2d(x)
x = depthwiseconv2d(x, strides=(2,2))
#128
x = conv2d(inputs=x, filters=128)
x = depthwiseconv2d(x, strides=(1,1))
x = conv2d(inputs=x, filters=128)
x = zeropadding2d(x)
x = depthwiseconv2d(x, strides=(2,2))
#256
x = conv2d(inputs=x, filters=256)
x = depthwiseconv2d(x, strides=(1,1))
x = conv2d(inputs=x, filters=256)
x = zeropadding2d(x)
x = depthwiseconv2d(x, strides=(2,2))
#512
x = conv2d(inputs=x, filters=512)
x = depthwiseconv2d(x, strides=(1,1))

x = conv2d(inputs=x, filters=512)
x = depthwiseconv2d(x, strides=(1,1))

x = conv2d(inputs=x, filters=512)
x = depthwiseconv2d(x, strides=(1,1))

x = conv2d(inputs=x, filters=512)
x = depthwiseconv2d(x, strides=(1,1))

x = conv2d(inputs=x, filters=512)
x = depthwiseconv2d(x, strides=(1,1))
x = conv2d(inputs=x, filters=512)

x = zeropadding2d(x)
x = depthwiseconv2d(x, strides=(2,2))
#1024
x = conv2d(inputs=x, filters=1024)
x = depthwiseconv2d(x, strides=(1,1))
x = conv2d(inputs=x, filters=1024)




model = Model(INPUT, x)

model.summary():

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 125, 125, 3)       0         
_________________________________________________________________
zero_padding2d_1 (ZeroPaddin (None, 126, 126, 3)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 62, 62, 32)        864       
_________________________________________________________________
batch_normalization_1 (Batch (None, 62, 62, 32)        128       
_________________________________________________________________
activation_1 (Activation)    (None, 62, 62, 32)        0         
_________________________________________________________________
depthwise_conv2d_1 (Depthwis (None, 62, 62, 32)        288       
_________________________________________________________________
batch_normalization_2 (Batch (None, 62, 62, 32)        128       
_________________________________________________________________
activation_2 (Activation)    (None, 62, 62, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 62, 62, 64)        2048      
_________________________________________________________________
batch_normalization_3 (Batch (None, 62, 62, 64)        256       
_________________________________________________________________
activation_3 (Activation)    (None, 62, 62, 64)        0         
_________________________________________________________________
zero_padding2d_2 (ZeroPaddin (None, 63, 63, 64)        0         
_________________________________________________________________
depthwise_conv2d_2 (Depthwis (None, 31, 31, 64)        576       
_________________________________________________________________
batch_normalization_4 (Batch (None, 31, 31, 64)        256       
_________________________________________________________________
activation_4 (Activation)    (None, 31, 31, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 31, 31, 128)       8192      
_________________________________________________________________
batch_normalization_5 (Batch (None, 31, 31, 128)       512       
_________________________________________________________________
activation_5 (Activation)    (None, 31, 31, 128)       0         
_________________________________________________________________
depthwise_conv2d_3 (Depthwis (None, 31, 31, 128)       1152      
_________________________________________________________________
batch_normalization_6 (Batch (None, 31, 31, 128)       512       
_________________________________________________________________
activation_6 (Activation)    (None, 31, 31, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 31, 31, 128)       16384     
_________________________________________________________________
batch_normalization_7 (Batch (None, 31, 31, 128)       512       
_________________________________________________________________
activation_7 (Activation)    (None, 31, 31, 128)       0         
_________________________________________________________________
zero_padding2d_3 (ZeroPaddin (None, 32, 32, 128)       0         
_________________________________________________________________
depthwise_conv2d_4 (Depthwis (None, 15, 15, 128)       1152      
_________________________________________________________________
batch_normalization_8 (Batch (None, 15, 15, 128)       512       
_________________________________________________________________
activation_8 (Activation)    (None, 15, 15, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 15, 15, 256)       32768     
_________________________________________________________________
batch_normalization_9 (Batch (None, 15, 15, 256)       1024      
_________________________________________________________________
activation_9 (Activation)    (None, 15, 15, 256)       0         
_________________________________________________________________
depthwise_conv2d_5 (Depthwis (None, 15, 15, 256)       2304      
_________________________________________________________________
batch_normalization_10 (Batc (None, 15, 15, 256)       1024      
_________________________________________________________________
activation_10 (Activation)   (None, 15, 15, 256)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 15, 15, 256)       65536     
_________________________________________________________________
batch_normalization_11 (Batc (None, 15, 15, 256)       1024      
_________________________________________________________________
activation_11 (Activation)   (None, 15, 15, 256)       0         
_________________________________________________________________
zero_padding2d_4 (ZeroPaddin (None, 16, 16, 256)       0         
_________________________________________________________________
depthwise_conv2d_6 (Depthwis (None, 7, 7, 256)         2304      
_________________________________________________________________
batch_normalization_12 (Batc (None, 7, 7, 256)         1024      
_________________________________________________________________
activation_12 (Activation)   (None, 7, 7, 256)         0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 7, 7, 512)         131072    
_________________________________________________________________
batch_normalization_13 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_13 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
depthwise_conv2d_7 (Depthwis (None, 7, 7, 512)         4608      
_________________________________________________________________
batch_normalization_14 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_14 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 7, 7, 512)         262144    
_________________________________________________________________
batch_normalization_15 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_15 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
depthwise_conv2d_8 (Depthwis (None, 7, 7, 512)         4608      
_________________________________________________________________
batch_normalization_16 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_16 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 7, 7, 512)         262144    
_________________________________________________________________
batch_normalization_17 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_17 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
depthwise_conv2d_9 (Depthwis (None, 7, 7, 512)         4608      
_________________________________________________________________
batch_normalization_18 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_18 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 7, 7, 512)         262144    
_________________________________________________________________
batch_normalization_19 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_19 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
depthwise_conv2d_10 (Depthwi (None, 7, 7, 512)         4608      
_________________________________________________________________
batch_normalization_20 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_20 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 7, 7, 512)         262144    
_________________________________________________________________
batch_normalization_21 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_21 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
depthwise_conv2d_11 (Depthwi (None, 7, 7, 512)         4608      
_________________________________________________________________
batch_normalization_22 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_22 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 7, 7, 512)         262144    
_________________________________________________________________
batch_normalization_23 (Batc (None, 7, 7, 512)         2048      
_________________________________________________________________
activation_23 (Activation)   (None, 7, 7, 512)         0         
_________________________________________________________________
zero_padding2d_5 (ZeroPaddin (None, 8, 8, 512)         0         
_________________________________________________________________
depthwise_conv2d_12 (Depthwi (None, 3, 3, 512)         4608      
_________________________________________________________________
batch_normalization_24 (Batc (None, 3, 3, 512)         2048      
_________________________________________________________________
activation_24 (Activation)   (None, 3, 3, 512)         0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 3, 3, 1024)        524288    
_________________________________________________________________
batch_normalization_25 (Batc (None, 3, 3, 1024)        4096      
_________________________________________________________________
activation_25 (Activation)   (None, 3, 3, 1024)        0         
_________________________________________________________________
depthwise_conv2d_13 (Depthwi (None, 3, 3, 1024)        9216      
_________________________________________________________________
batch_normalization_26 (Batc (None, 3, 3, 1024)        4096      
_________________________________________________________________
activation_26 (Activation)   (None, 3, 3, 1024)        0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 3, 3, 1024)        1048576   
_________________________________________________________________
batch_normalization_27 (Batc (None, 3, 3, 1024)        4096      
_________________________________________________________________
activation_27 (Activation)   (None, 3, 3, 1024)        0         
=================================================================
Total params: 3,228,864
Trainable params: 3,206,976
Non-trainable params: 21,888
_________________________________________________________________

在输入大小为125×125×3与通过from keras.applications.mobilenet import MobileNet导出来的模型参数数量,以及每一层的数据大小完全一致。证实复现的正确性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值