VGG19的实现

环境配置:tensorflow2.1+python3.7

代码如下:

import tensorflow as tf
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Activation
from tensorflow.keras import Input 
from tensorflow.keras import Model

def conv2d_1(input_tensor,filters,kernel_size=3):
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(input_tensor)
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(x)
    x=MaxPooling2D(strides=2)(x)
    return x
def conv2d_2(input_tensor,filters,kernel_size=3):
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(input_tensor)
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(x)
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(x)
    x=Conv2D(filters,kernel_size,padding='same',activation='relu')(x)
    x=MaxPooling2D(strides=2)(x)
    return x
def vgg16(filters,input_tensor):
    filter1,filter2,filter3,filter4=filters
    x=conv2d_1(input_tensor,filter1)
    x=conv2d_1(x,filter2)
    x=conv2d_2(x,filter3)
    x=conv2d_2(x,filter4)
    x=conv2d_2(x,filter4)
    return x
inputs=Input([224,224,3])
x=vgg16([64,128,256,512],inputs)
x=Flatten()(x)
x=Dense(4096,activation='relu')(x)
x=Dense(4096)(x)
outputs=Activation('softmax')(x)
model=Model(inputs,outputs)
model.summary()

vgg19结构图如下:

"""
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 224, 224, 64)      1792      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 224, 224, 64)      36928     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 64)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 112, 112, 128)     73856     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 112, 112, 128)     147584    
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 56, 56, 256)       295168    
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 56, 56, 256)       590080    
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 56, 56, 256)       590080    
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 56, 56, 256)       590080    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 28, 28, 256)       0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 28, 28, 512)       1180160   
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 28, 28, 512)       2359808   
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 28, 28, 512)       2359808   
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 28, 28, 512)       2359808   
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 14, 14, 512)       0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
dense (Dense)                (None, 4096)              102764544 
_________________________________________________________________
dense_1 (Dense)              (None, 4096)              16781312  
_________________________________________________________________
activation (Activation)      (None, 4096)              0         
=================================================================
Total params: 139,570,240
Trainable params: 139,570,240
Non-trainable params: 0
"""

论文地址:https://arxiv.org/abs/1409.1556

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值