?
活动地址:CSDN21天学习挑战赛
今天继续跟着k同学啊老师继续学习,之前在自己看书的时候,看到过VGG模型的介绍,VGG网络明确指出并实践证明了,浅而大的卷积核不如深而小的卷积核。今天有机会可以实际上手了,老规矩,先跟着案例做,然后再逐步消化。完整源码附后
- 本文为365天深度学习训练营 中的学习记录博客
- 参考文章地址: 深度学习100例-卷积神经网络(CNN)识别眼睛状态 | 第17天
1、VGG16模型简介 VGG16模型可以很好的适用于分类和定位任务,其名称来自牛津大学几何组(Visual Geometry Group)的缩写。
根据卷积核的大小、核卷积层数,VGG共有6种配置,分别为A、A-LRN、B、C、D、E,其中D和E两种是最为常用的VGG16和VGG19。
文章图片
VGG-16:
- 输入层224×224×3,经过两层相同的卷积
- 卷积filter为3*3,stride为1,filter数为64
- 一层pooling
- 按照相同的方式,让宽和高越来越小,而通道数逐倍增加,直到512
- 最后用两层相同全连接加一个softmax。
文章图片
2、加载、配置数据 数据集是老师提供的:
- 4308张图片
- 四类:close_look(闭眼)、forward_look(向前看)、left_look(向左看)、right_look(向右看)
文章图片
import numpy as np
np.random.seed(1)# 设置随机种子尽可能使结果可以重现
import tensorflow as tf
tf.random.set_seed(1)import pathlibdata_dir = "./017_Eye_dataset"data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*')))print("图片总数为:",image_count)# 加载
batch_size = 64
img_height = 224
img_width = 224
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)# 可视化
plt.figure(figsize=(10, 5))# 图形的宽为10高为5
plt.suptitle("数据展示")for images, labels in train_ds.take(1):
for i in range(8):ax = plt.subplot(2, 4, i + 1)ax.patch.set_facecolor('yellow')plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])plt.axis("off")for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
# 配置数据集
# 这里如果是 tf2.6 或者报错,使用 AUTOTUNE = tf.data.experimental.AUTOTUNEAUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds= val_ds.cache().prefetch(buffer_size=AUTOTUNE)
3、构建模型 这里加载官方的
VGG16
模型model = tf.keras.applications.VGG16()
# 打印模型信息
model.summary()
打印如下:
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5
553467904/553467096 [==============================] - 5531s 10us/step
Model: "vgg16"
_________________________________________________________________
Layer (type)Output ShapeParam #
=================================================================
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
_________________________________________________________________
4、设置动态学习率 动态学习率有很多种,这里设置的动态学习率为:指数衰减型(ExponentialDecay)
假设1个epoch有100个batch(相当于100step),20个epoch过后,step==2000,即step会随着epoch累加
指数衰减学习率方法说明
tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=初始学习率, decay_steps=衰减步数, decay_rate=衰减率, staircase= 如果等于true则学习率阶梯型下降
)#返回值为处理后的学习率
# 设置初始学习率
initial_learning_rate = 1e-4
# 【注意】这里的1e-4,是科学计数法,即1乘以10的-4次方。
# 所以,1e-4等同于0.0001lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps=20,
decay_rate=0.96,
staircase=True)# 将指数衰减学习率送入优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
学习率大小的优缺点
学习率大
优点:1、加快学习速率。 2、有助于跳出局部最优值。
缺点:1、导致模型训练不收敛。2、单单使用大学习率容易导致模型不精确。
学习率小
优点:1、有助于模型收敛、模型细化。2、提高模型精度。
缺点: 1、很难跳出局部最优值。2、收敛缓慢。
5、配置模型
model.compile(optimizer=optimizer,
loss ='sparse_categorical_crossentropy',
metrics =['accuracy'])
6、训练模型
epochs = 10history = model.fit(
train_ds,
validation_data=https://www.it610.com/article/val_ds,
epochs=epochs
)
文章图片
7、模型评估 7.1 Accuracy与Loss图
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']loss = history.history['loss']
val_loss = history.history['val_loss']epochs_range = range(epochs)plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
文章图片
7.2 混淆矩阵
Seaborn 是一个画图库,它基于 Matplotlib 核心库进行了更高阶的 API封装
from sklearn.metrics import confusion_matrix
import seaborn as sns
import pandas as pd# 定义一个绘制混淆矩阵图的函数
def plot_cm(labels, predictions):# 生成混淆矩阵
conf_numpy = confusion_matrix(labels, predictions)
# 将矩阵转化为 DataFrame
conf_df = pd.DataFrame(conf_numpy, index=class_names ,columns=class_names)plt.figure(figsize=(8,7))sns.heatmap(conf_df, annot=True, fmt="d", cmap="BuPu")plt.title('混淆矩阵',fontsize=15)
plt.ylabel('真实值',fontsize=14)
plt.xlabel('预测值',fontsize=14)
val_pre= []
val_label = []for images, labels in val_ds:#这里可以取部分验证数据(.take(1))生成混淆矩阵
for image, label in zip(images, labels):
# 需要给图片增加一个维度
img_array = tf.expand_dims(image, 0)
# 使用模型预测图片中的人物
prediction = model.predict(img_array)val_pre.append(class_names[np.argmax(prediction)])
val_label.append(class_names[label])
plot_cm(val_label, val_pre)
文章图片
8、保存和加载模型
# 保存模型
model.save('17.h5')
# 加载模型
new_model = tf.keras.models.load_model('17.h5')
9、预测 **使用刚刚保存的模型,加载后的的模型(new_model)预测结果
plt.figure(figsize=(10, 5))# 图形的宽为10高为5
plt.suptitle("预测结果展示")for images, labels in val_ds.take(1):
for i in range(8):
ax = plt.subplot(2, 4, i + 1)# 显示图片
plt.imshow(images[i].numpy().astype("uint8"))# 需要给图片增加一个维度
img_array = tf.expand_dims(images[i], 0) # 使用模型预测图片中的人物
predictions = new_model.predict(img_array)
plt.title(class_names[np.argmax(predictions)])plt.axis("off")
文章图片
10、完整源码
import matplotlib.pyplot as plt
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']# 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False# 用来正常显示负号import os,PIL# 设置随机种子尽可能使结果可以重现
import numpy as np
np.random.seed(1)# 设置随机种子尽可能使结果可以重现
import tensorflow as tf
tf.random.set_seed(1)import pathlibdata_dir = "./017_Eye_dataset"data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*')))print("图片总数为:",image_count)# 加载
batch_size = 64
img_height = 224
img_width = 224
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)# 可视化
plt.figure(figsize=(10, 5))# 图形的宽为10高为5
plt.suptitle("数据展示")for images, labels in train_ds.take(1):
for i in range(8):ax = plt.subplot(2, 4, i + 1)ax.patch.set_facecolor('yellow')plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])plt.axis("off")for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break# 配置数据集
# 这里如果是 tf2.6 或者报错,使用 AUTOTUNE = tf.data.experimental.AUTOTUNEAUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds= val_ds.cache().prefetch(buffer_size=AUTOTUNE)model = tf.keras.applications.VGG16()
model.summary()
# 设置初始学习率
initial_learning_rate = 1e-4lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps=20,
decay_rate=0.96,
staircase=True)# 将指数衰减学习率送入优化器
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])epochs = 10history = model.fit(
train_ds,
validation_data=https://www.it610.com/article/val_ds,
epochs=epochs
)acc = history.history['accuracy']
val_acc = history.history['val_accuracy']loss = history.history['loss']
val_loss = history.history['val_loss']epochs_range = range(epochs)plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()from sklearn.metrics import confusion_matrix
import seaborn as sns
import pandas as pd# 定义一个绘制混淆矩阵图的函数
def plot_cm(labels, predictions):# 生成混淆矩阵
conf_numpy = confusion_matrix(labels, predictions)
# 将矩阵转化为 DataFrame
conf_df = pd.DataFrame(conf_numpy, index=class_names ,columns=class_names)plt.figure(figsize=(8,7))sns.heatmap(conf_df, annot=True, fmt="d", cmap="BuPu")plt.title('混淆矩阵',fontsize=15)
plt.ylabel('真实值',fontsize=14)
plt.xlabel('预测值',fontsize=14)
val_pre= []
val_label = []for images, labels in val_ds:#这里可以取部分验证数据(.take(1))生成混淆矩阵
for image, label in zip(images, labels):
# 需要给图片增加一个维度
img_array = tf.expand_dims(image, 0)
# 使用模型预测图片中的人物
prediction = model.predict(img_array)val_pre.append(class_names[np.argmax(prediction)])
val_label.append(class_names[label])
plot_cm(val_label, val_pre)# 保存模型
model.save('17.h5')
# 加载模型
new_model = tf.keras.models.load_model('17.h5')# 采用加载的模型(new_model)来看预测结果plt.figure(figsize=(10, 5))# 图形的宽为10高为5
plt.suptitle("预测结果展示")for images, labels in val_ds.take(1):
for i in range(8):
ax = plt.subplot(2, 4, i + 1)# 显示图片
plt.imshow(images[i].numpy().astype("uint8"))# 需要给图片增加一个维度
img_array = tf.expand_dims(images[i], 0) # 使用模型预测图片中的人物
predictions = new_model.predict(img_array)
plt.title(class_names[np.argmax(predictions)])plt.axis("off")
**
学习日记 **
1,学习知识点
a、VGG16模型的实践2,学习遇到的问题
b、学习了混淆矩阵的用法
c、尝试了设置动态学习率的使用方法
还需要仔细消化各知识点,补齐基础【深度学习21天学习挑战赛|【深度学习21天学习挑战赛】6、使用VGG16模型实现卷积神经网——识别眼睛状态】?
推荐阅读
- 物联网|脉冲神经网络原理及应用,脉冲神经网络怎么训练
- 医疗NLP论文汇总|医学自然语言处理(NLP)相关论文汇总之 NAACL 2021
- 论文|SciFive: a text-to-text transformer model for biomedical literature
- 笔记|【深度学习】KNN算法学习
- 神经网络|深度学习算法3-BP神经网络
- 学习|【控制工程】自动控制原理和系统建模
- 神经网络|深度学习算法第三课——BP神经网络
- 鸿蒙开发系统的初步学习
- 笔记|【深度学习】BP神经网络