tensorflow2.X搭建/调用VGG16及加载预训练参数

说明

最近在学习过程中,遇到了VGG16的搭建、调用和加载预训练参数的问题,下面做一个记录
下面是我在另一个平台写的可以直接运行的项目的链接
基于VGG16的好莱坞明星识别
摘录出关键代码如下:

一、直接调用VGG16模型

函数说明

tf.keras.applications.vgg16.VGG16(
include_top=True, #是否使用顶层
weights=‘imagenet’,#是否加载imgenet预训练参数
input_tensor=None,
input_shape=None,#输入向量形状
pooling=None,#max或avg,最大或平均池化层
classes=1000,#如果使用顶层且使用预训练参数,分类数必须为1000,否则可以自己改动
classifier_activation=‘softmax’#分类激活函数
)
其中顶层指的是非卷积层

使用

下面使用是要了顶层

from tensorflow.keras.applications.vgg16 import VGG16
model = VGG16(
   include_top=True,
   weights=None,
   input_tensor=None,
   input_shape=(img_height, img_width, 3),
   pooling=max,
   classes=len(class_names)
   classifier_activation='softmax'
model.summary()  # 打印网络结构

二、自己搭建VGG16模型

VGG16_model = models.Sequential([
#两次使用64个3*3的卷积核,采用l2正则化,池化后维度(112,112,64)
    layers.Conv2D(64, (3, 3),padding='same', activation='relu', input_shape=(img_height, img_width, 3)), 
    layers.Conv2D(64, (3, 3), padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)),  
#两次使用128个3*3的卷积核,池化后维度(56,56,128)   
    layers.Conv2D(128, (3, 3),padding='same',activation='relu'), 
    layers.Conv2D(128, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)),  
#三次使用256个3*3的卷积核,池化后维度(28,28,256)
    layers.Conv2D(256, (3, 3), padding='same',activation='relu'),     
    layers.Conv2D(256, (3, 3), padding='same',activation='relu'),   
    layers.Conv2D(256, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)), 
#三次使用512个3*3的卷积核,池化后维度(14,14,512)
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),   
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)), 
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),   
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)), 
    layers.Flatten(),                       
    layers.Dense(4096, activation='relu'), 
    layers.Dense(4096, activation='relu'),
    layers.Dense(len(class_names), activation="softmax")  
])

三、VGG16模型加载卷积层预训练参数

一般都会只加载前十三层的卷积参数

VGG16_model = models.Sequential([
#两次使用64个3*3的卷积核,采用l2正则化,池化后维度(112,112,64)
    layers.Conv2D(64, (3, 3),padding='same', activation='relu', input_shape=(img_height, img_width, 3)), 
    layers.Conv2D(64, (3, 3), padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)),  
#两次使用128个3*3的卷积核,池化后维度(56,56,128)   
    layers.Conv2D(128, (3, 3),padding='same',activation='relu'), 
    layers.Conv2D(128, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)),  
#三次使用256个3*3的卷积核,池化后维度(28,28,256)
    layers.Conv2D(256, (3, 3), padding='same',activation='relu'),     
    layers.Conv2D(256, (3, 3), padding='same',activation='relu'),   
    layers.Conv2D(256, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)), 
#三次使用512个3*3的卷积核,池化后维度(14,14,512)
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),   
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)), 
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),   
    layers.Conv2D(512, (3, 3),padding='same',activation='relu'),     
    layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)),  
])
# 加载模型参数
VGG16_model.load_weights('./weight/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
# 冻结前13层网络参数  保证加载的预训练参数不被改变
for layer in VGG16_model.layers[:13]:
    layer.trainable = False
model = models.Sequential([
    VGG16_model,
    layers.Flatten(),                       
    layers.Dense(4096, activation='relu'), 
    layers.Dense(4096, activation='relu'),
    layers.Dense(len(class_names), activation="softmax")               
])

四、其他说明

1.自己搭建的VGG16模型也可以增加BN层、Drop层、L2正则化进行优化。
2.预训练参数可以从GitHub上下载(最全),或者是进入我开头放的链接,fork后也可以在project中下载(只有vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5一种)

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
tensorflow.keras.applications.vgg16模块提供了VGG16模型的预训练版本。VGG16是一个经典的卷积神经网络模型,由Karen Simonyan和Andrew Zisserman于2014年提出。它在ImageNet数据集上取得了很好的性能,并成为了深度学习图像分类任务的重要基准模型之一。 使用tensorflow.keras.applications.vgg16可以加载VGG16模型的预训练权重,并进行图像分类任务。下面是一个加载VGG16模型并进行图像分类的示例: ```python from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions from tensorflow.keras.preprocessing import image import numpy as np # 加载VGG16模型(不包括顶部的全连接层) model = VGG16(weights='imagenet', include_top=False) # 载入并预处理图像 img_path = 'path/to/your/image.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) # 使用VGG16模型进行预测 features = model.predict(x) # 解码预测结果 decoded_predictions = decode_predictions(features, top=3)[0] for pred_class in decoded_predictions: print(pred_class[1], pred_class[2]) ``` 以上示例代码中,我们首先加载VGG16模型,并指定了使用ImageNet数据集上预训练的权重。然后,我们加载了待预测的图像,并进行了预处理,包括调整尺寸和归一化。接下来,使用VGG16模型对图像进行预测,并通过decode_predictions函数解码预测结果,输出前三个最可能的类别标签和对应的概率。 需要注意的是,VGG16模型的输入尺寸为224x224像素的RGB图像。在使用该模型进行预测时,需要将输入图像调整为相应的尺寸,并进行适当的预处理操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱挠静香的下巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值