当前位置: 首页 > news >正文

day39 pythonCNN网络

目录

从零开始构建CNN网络:以MNIST手写数字识别为例

一、CNN的基本原理

二、CNN的代码实现

1. 导入必要的库

2. 加载和预处理数据

3. 构建CNN模型

4. 编译模型

5. 训练模型

6. 评估模型

7. 可视化训练过程

三、MNIST-C数据集的处理

四、Fine-tuning方法训练

五、学习心得


一、CNN的基本原理

CNN的核心在于卷积操作,它通过卷积核在图像上滑动,提取局部特征。卷积核的大小和数量会影响特征提取的效果。例如,一个3x3的卷积核可以在图像上提取边缘信息,而多个卷积核可以提取不同的特征。卷积操作的结果是一个特征图(feature map),它反映了输入图像与卷积核之间的匹配程度。为了减少计算量,CNN还引入了池化(Pooling)操作。池化层通常使用最大池化(max-pooling),即取池化区域内的最大值作为输出。这样可以保留特征图中的重要信息,同时减小特征图的尺寸。此外,CNN还使用了激活函数ReLU(Rectified Linear Unit),它将输入值小于0的部分置为0,保留正值。ReLU的作用是引入非线性因素,使网络能够学习复杂的特征。CNN的结构通常包括卷积层、池化层、全连接层等。卷积层用于提取特征,池化层用于降采样,全连接层用于分类。通过堆叠多个卷积层和池化层,CNN可以提取图像的多层次特征。

二、CNN的代码实现

在学习CNN的过程中,我使用了TensorFlow和Keras框架来构建模型。以下是一个简单的CNN模型的代码实现,用于MNIST手写数字识别任务。

1. 导入必要的库

import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
import numpy as np

2. 加载和预处理数据

MNIST数据集是一个经典的手写数字数据集,包含60,000张训练图像和10,000张测试图像。每张图像的大小为28x28像素。

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()# 数据预处理
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255# 将标签转换为one-hot编码
train_labels = tf.keras.utils.to_categorical(train_labels)
test_labels = tf.keras.utils.to_categorical(test_labels)

3. 构建CNN模型

CNN模型由多个卷积层、池化层和全连接层组成。以下是一个简单的CNN模型结构:

model = models.Sequential([# 卷积层1layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),layers.MaxPooling2D((2, 2)),# 卷积层2layers.Conv2D(64, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),# 展平层layers.Flatten(),# 全连接层layers.Dense(64, activation='relu'),layers.Dense(10, activation='softmax')
])

4. 编译模型

在编译模型时,需要指定优化器、损失函数和评估指标。这里使用Adam优化器和交叉熵损失函数。

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])

5. 训练模型

训练模型时,需要指定训练数据、验证数据、训练轮数(epochs)和批次大小(batch size)。

history = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)

6. 评估模型

评估模型的性能,输出测试集的准确率。

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")

7. 可视化训练过程

通过绘制训练过程中的准确率曲线,可以直观地了解模型的训练效果。

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.show()

三、MNIST-C数据集的处理

MNIST-C是MNIST数据集的一个变体,加入了人工噪声,如模糊、扭曲、亮度变化等,用于测试模型的鲁棒性。以下是如何加载和处理MNIST-C数据集的代码:

import os
import numpy as np
import matplotlib.pyplot as plt# 加载数据集并打印每个子文件夹前10个数据集
data_root = './mnist_c'
dirlist = os.listdir(data_root)
fig, axs = plt.subplots(len(dirlist), 10, figsize=(10, 10))for i, folder_name in enumerate(dirlist):folder_path = os.path.join(data_root, folder_name)if os.path.isdir(folder_path):file_path = os.path.join(folder_path, 'train_images.npy')data = np.load(file_path)for j in range(0, 10):axs[i, j].imshow(data[j].reshape(28, 28), cmap='gray')axs[i, j].axis('off')
plt.tight_layout()
plt.show()

四、Fine-tuning方法训练

假设我们已经有一个在MNIST数据集上训练好的模型(位于./model.h5),我们可以使用Fine-tuning方法在MNIST-C数据集上继续训练该模型。以下是实现Fine-tuning的代码:

import tensorflow.keras as layers
import tensorflow as tf
import datetimeTARGET_MODEL_DIR = "./"
MODEL_NAME = "model.h5"
epochs_count = 5def againTrain(x_train, y_train, x_test, y_test):targetModel = os.path.join(TARGET_MODEL_DIR, MODEL_NAME)# 加载CNN模型model = tf.keras.models.load_model(targetModel)# 冻结模型的前几层model.layers[0].trainable = Falsemodel.layers[1].trainable = False# 对输入图像进行预处理x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)now = datetime.datetime.now()format_time = now.strftime("%Y-%m-%d%H-%M-%S")checkpoint = tf.keras.callbacks.ModelCheckpoint(targetModel, save_best_only=True, save_weights_only=False, monitor='val_loss')# 继续训练模型history = model.fit(x_train, y_train, batch_size=128, epochs=epochs_count, validation_data=(x_test, y_test), callbacks=[checkpoint])test_loss, test_acc = model.evaluate(x_test, y_test)print('Test accuracy:', test_acc)def loadDataMnistC(data_root, func):dirlist = os.listdir(data_root)for i, folder_name in enumerate(dirlist):folder_path = os.path.join(data_root, folder_name)if os.path.isdir(folder_path):print("开始读取:" + folder_path)train_images = np.load(os.path.join(folder_path, 'train_images.npy'))train_labels = np.load(os.path.join(folder_path, 'train_labels.npy'))test_images = np.load(os.path.join(folder_path, 'test_images.npy'))test_labels = np.load(os.path.join(folder_path, 'test_labels.npy'))print("开始训练:" + folder_path)func(train_images, train_labels, test_images, test_labels)print("训练完成:" + folder_path)# 加载 MNIST-C 数据集
data_root = './mnist_c'
loadDataMnistC(data_root, againTrain)
print("全部训练完成")

五、学习心得

通过这次学习,我对CNN有了更深入的理解。CNN通过卷积操作提取图像的局部特征,通过池化操作减少计算量,通过激活函数引入非线性因素。这些设计使得CNN能够自动学习图像中的特征,而无需人工设计特征提取器。

参考:深度学习03-卷积神经网络(CNN)_cnn卷积神经网络-CSDN博客

@浙大疏锦行

相关文章:

  • DeepSeek实战:打造智能数据分析与可视化系统
  • QT 5.15.2 程序中文乱码
  • 如何处理 Python 入门难以进步的现象
  • 小样本学习
  • VirtualBox怎样安装Win10
  • 《P5507 机关》
  • windows本地虚拟机上运行docker-compose案例
  • 多台电脑共用一个ip地址可以吗?会怎么样
  • 解决线程安全问题
  • C++补充基础小知识:什么是接口类 和 抽象类?为什么要继承?
  • 易学探索助手-个人记录(十二)
  • 运用集合知识做斗地主案例
  • DOM和BOM的区别
  • 国内外AI编程工具对比(Trae对比Cursor)
  • CloudCompare——点云统计滤波
  • 关于JavaScript、TypeScript Module的配置和用法
  • 解决 AntV G6 使用 SVG 渲染脑图时节点文字过多导致拖动卡顿的问题
  • Mac OS 使用说明
  • 从0到1:某智慧园区数字孪生项目的技术选型复盘
  • 【Golang入门】第一章:环境搭建与Hello World
  • 网站建设维护单位/seo关键词的优化技巧
  • 重庆网站建设电话/搜索引擎优化技术都有哪些
  • 做网站的的需求文档/百度知道提问
  • 建设一个电商网站的流程是什么/域名信息查询系统
  • 阜阳网站建设云平台/seo如何优化排名
  • 官方网站下载拼多多/广告推广赚钱在哪接