180705 利用Keras参看Vgg16模型参数

从keras看VGG16结构图
180705 利用Keras参看Vgg16模型参数_第1张图片
180705 利用Keras参看Vgg16模型参数_第2张图片

  • 模型代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul  5 14:50:29 2018

@author: brucelau
"""
from keras.models import Sequential
from keras.layers import Dense, Dropout,Flatten, ZeroPadding2D,Convolution2D,MaxPooling2D

from keras import backend as K
K.set_image_dim_ordering('th')

model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))#卷积输入层,指定了输入图像的大小
model.add(Convolution2D(64, 3, 3, activation='relu'))#64个3x3的卷积核,生成64*224*224的图像,激活函数为relu
model.add(ZeroPadding2D((1,1)))#补0,保证图像卷积后图像大小不变,其实用padding = 'valid'参数就可以了
model.add(Convolution2D(64, 3, 3, activation='relu'))#再来一次卷积 生成64*224*224
model.add(MaxPooling2D((2,2), strides=(2,2)))#pooling操作,相当于变成64*112*112

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))#128*56*56

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))#256*28*28

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))#512*14*14

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))  #到这里已经变成了512*7*7

model.add(Flatten())#压平上述向量,变成一维25088
model.add(Dense(4096, activation='relu'))#全连接层有4096个神经核,参数个数就是4096*25088
model.add(Dropout(0.5))#0.5的概率抛弃一些连接
model.add(Dense(4096, activation='relu'))#再来一个全连接
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
  • 模型参数
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
zero_padding2d_1 (ZeroPaddin (None, 3, 226, 226)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 64, 224, 224)      1792      
_________________________________________________________________
zero_padding2d_2 (ZeroPaddin (None, 64, 226, 226)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 64, 224, 224)      36928     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 64, 112, 112)      0         
_________________________________________________________________
zero_padding2d_3 (ZeroPaddin (None, 64, 114, 114)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 128, 112, 112)     73856     
_________________________________________________________________
zero_padding2d_4 (ZeroPaddin (None, 128, 114, 114)     0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 128, 112, 112)     147584    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 128, 56, 56)       0         
_________________________________________________________________
zero_padding2d_5 (ZeroPaddin (None, 128, 58, 58)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 256, 56, 56)       295168    
_________________________________________________________________
zero_padding2d_6 (ZeroPaddin (None, 256, 58, 58)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 256, 56, 56)       590080    
_________________________________________________________________
zero_padding2d_7 (ZeroPaddin (None, 256, 58, 58)       0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 256, 56, 56)       590080    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 256, 28, 28)       0         
_________________________________________________________________
zero_padding2d_8 (ZeroPaddin (None, 256, 30, 30)       0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 512, 28, 28)       1180160   
_________________________________________________________________
zero_padding2d_9 (ZeroPaddin (None, 512, 30, 30)       0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 512, 28, 28)       2359808   
_________________________________________________________________
zero_padding2d_10 (ZeroPaddi (None, 512, 30, 30)       0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 512, 28, 28)       2359808   
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 512, 14, 14)       0         
_________________________________________________________________
zero_padding2d_11 (ZeroPaddi (None, 512, 16, 16)       0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 512, 14, 14)       2359808   
_________________________________________________________________
zero_padding2d_12 (ZeroPaddi (None, 512, 16, 16)       0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 512, 14, 14)       2359808   
_________________________________________________________________
zero_padding2d_13 (ZeroPaddi (None, 512, 16, 16)       0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 512, 14, 14)       2359808   
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 512, 7, 7)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 25088)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 4096)              102764544 
_________________________________________________________________
dropout_1 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 4096)              16781312  
_________________________________________________________________
dropout_2 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_3 (Dense)              (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________

你可能感兴趣的:(keras)