VGGNet是在2014年由Karen Simonyan和Andrew Zisserman提出的,网络模型包括VGG-11、VGG-13、VGG-16以及VGG-19,其中VGG-16和VGG-19在分类和定位任务上效果最好,因此作者在2014年ImageNet Challenge上获得分类第二和定位第一的网络。
论文地址:VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNIT
VGG与AlexNet相比,它是将AlexNet模型中较大的卷积核(例如:11 x 11、7 x 7、5 x 5)换成连续几个3 x 3的卷积核。其目的是:减少网络参数量;由于参数量被大幅减小,于是可以用多个感受野小的卷积层替换掉之前一个感受野大的卷积层,从而增加网络的非线性表达能力。
例如:两个3x3的卷积层的感受野可以代替一个5x5的卷积层,三个3x3的卷积层可以代替一个7x7的卷积层,这样可以有效地减少参数计算成本。假设输入输出channel均为C,三个3x3参数个数为3x(3x3xCxC)=27xC²,一个7x7参数个数为7x7xCxC=49xC²,因此用三个3x3的卷积层代替一个7x7的卷积层可以节省近一半的参数计算量。
VGG-16和VGG-19如上图的D和E所示:
VGG-16:包括16个隐藏层(13个卷积层和3个全连接层)
VGG-19:包括19个隐藏层(16个卷积层和3个全连接层)
以VGG-16为例,如下图所示:
详细过程为:
block1:
block2:
block3:
block4:
block5:
最后三层全连接层和AlexNet最后三层相同,可以参考博客:AlexNet模型详解及代码实现
import matplotlib.pyplot as plt
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
def percent(value):
return '%.2f%%' % (value * 100)
# 下载VGG16模型,下载地址为 c:\user(用户)\.keras\models\vgg16_weights_tf_dim_ordering_tf_kernels.h5
model = VGG16(weights='imagenet', include_top=True)
# 显示模型结构
model.summary()
# Input:要预测的图片
img_path = '.\xx.png'
# img_path = '.\xx.png' 并转化为224*224的标准尺寸
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img) # 转化为浮点型
x = np.expand_dims(x, axis=0) # 转化为张量size为(1, 224, 224, 3)
x = preprocess_input(x)
# 预测,取得features,維度为 (1,1000)
features = model.predict(x)
# 取得前五个最可能的类别及概率
pred = decode_predictions(features, top=5)[0]
# 整理预测结果,value
values = []
bar_label = []
for element in pred:
values.append(element[2])
bar_label.append(element[1])
# 绘图并保存
fig = plt.figure("预测结果")
ax = fig.add_subplot(111)
ax.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc='g')
ax.set_ylabel('probability')
ax.set_title('Tree view')
for a, b in zip(range(len(values)), values):
ax.text(a, b + 0.0005, percent(b), ha='center', va='bottom', fontsize=7)
fig = plt.gcf()
plt.show()
name = img_path[0:-4] + '_pred'
fig.savefig(name, dpi=200)
model.summary()
显示模型结构
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
fc1 (Dense) (None, 4096) 102764544
_________________________________________________________________
fc2 (Dense) (None, 4096) 16781312
_________________________________________________________________
predictions (Dense) (None, 1000) 4097000
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
2021-12-27 16:59:25.846173: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
预测图片1:
VGG-论文解读
VGG16学习笔记