一、简介
vgg19是一种深度卷积神经网络,由牛津大学的Visual Geometry Group开发。它是vgg16模型的扩展,具有更深的网络层数。vgg19模型在图像分类、物体检测和语义分割等领域都有应用。
二、模型结构
vgg19模型共有19层,由卷积层、池化层和全连接层构成。下面是vgg19模型的结构:
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 (MaxPooli (None, 112, 112, 64) 0
________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128 73856
________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128 147584
________________________________________________________________
block2_pool (MaxPooli (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_conv4 (Conv2D) (None, 56, 56, 256) 590080
________________________________________________________________
block3_pool (MaxPooli (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_conv4 (Conv2D) (None, 28, 28, 512) 2359808
________________________________________________________________
block4_pool (MaxPooli (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_conv4 (Conv2D) (None, 14, 14, 512) 2359808
________________________________________________________________
block5_pool (MaxPooli (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: 139,574,984
Trainable params: 139,574,984
Non-trainable params: 0
________________________________________________________________
三、特点
1. 采用小卷积核
vgg19模型使用了小的3×3卷积核,较大的空间感受野由叠加这些小卷积核得到,这种方式比使用较大的卷积核更有效。在vgg19模型中,每个卷积层都使用了两个3×3的卷积核,因此每层都有两次卷积操作。
2. 使用多层卷积和池化
vgg19模型共有5个卷积块,每个卷积块中都包含多层卷积层和一个池化层,这些层可以大大增强模型的表达能力,提高分类精度。
3. 预处理方式是固定的
在训练vgg19模型时,需要将每个图像的像素值进行归一化处理。采用的处理方式是将每个像素减去ImageNet数据集的均值,然后除以标准差。
四、代码实现
下面是使用vgg19模型进行图像分类的代码实现:
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions
import numpy as np
model = VGG19(weights="imagenet")
img_path = "test_img.jpg"
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
以上代码首先导入VGG19模型和必要的库,然后加载测试图像,将图像输入到VGG19模型进行预测,并输出预测结果。