活动地址:CSDN21天学习挑战赛
卷积神经网络与普通神经网络的区别在于,卷积神经网络包含了一个由卷积层和子采样层(池化层)构成的特征抽取器。在卷积神经网络的卷积层中,一个神经元只与部分邻层神经元连接。在CNN的一个卷积层中,通常包含若干个特征图(featureMap),每个特征图由一些矩形排列的的神经元组成,同一特征图的神经元共享权值,这里共享的权值就是卷积核。卷积核一般以随机小数矩阵的形式初始化,在网络的训练过程中卷积核将学习得到合理的权值。共享权值(卷积核)带来的直接好处是减少网络各层之间的连接,同时又降低了过拟合的风险。子采样也叫做池化(pooling),通常有均值子采样(mean pooling)和最大值子采样(max pooling)两种形式。子采样可以看作一种特殊的卷积过程。卷积和子采样大大简化了模型复杂度,减少了模型的参数。
- 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 pathlib
- data_dir = "./017_Eye_dataset"
-
- data_dir = pathlib.Path(data_dir)
- image_count = len(list(data_dir.glob('*/*')))
-
- print("图片总数为:",image_count)
图片总数为: 4308
- batch_size = 64
- img_height = 224
- img_width = 224
- """
- 关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
- """
- 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)
Found 4308 files belonging to 4 classes.Using 3447 files for training
- """
- 关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
- """
- 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)
Found 4308 files belonging to 4 classes. Using 861 files for validation.
- class_names = train_ds.class_names
- print(class_names)
['close_look', 'forward_look', 'left_look', 'right_look']
- 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
(64, 224, 224, 3) (64,)
- AUTOTUNE = 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-4
-
- lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- initial_learning_rate,
- decay_steps=20, # 敲黑板!!!这里是指 steps,不是指epochs
- decay_rate=0.96, # lr经过一次衰减就会变成 decay_rate*lr
- staircase=True)
-
- # 将指数衰减学习率送入优化器
- optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
在准备对模型进行训练之前,还需要再对其进行一些设置。以下内容是在模型的编译步骤中添加的:
- model.compile(optimizer=optimizer,
- loss ='sparse_categorical_crossentropy',
- metrics =['accuracy'])
- epochs = 10
-
- history = model.fit(
- train_ds,
- validation_data=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('model/17_model.h5')
- # 加载模型
- new_model = tf.keras.models.load_model('model/17_model.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")
- 本文为🔗365天深度学习训练营 中的学习记录博客
- 参考文章地址: 🔗深度学习100例-卷积神经网络(CNN)识别眼睛状态 | 第17天