TensorFlow2.0之还原lenet-5模型

本文旨在详细讲解如何在TensorFlow2.0环境下,依据原始论文,精确还原经典的LeNet-5神经网络模型。通过实践,确保模型的构建与论文描述一致,适用于学习和教学用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近课程需要,将lenet-5模型进行还原,但是网上博客大都根据原论文进行了自己的改进,不能满足还原的这个要求,所以按照论文结合TensorFlow2.0的API,将其还原。

from tensorflow.python.client import device_lib 
device_lib.list_local_devices()

The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.
We recommend you upgrade now or ensure your notebook will continue to use TensorFlow 1.x via the %tensorflow_version 1.x magic: more info.

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 16313844761984461564, name: "/device:XLA_CPU:0"
 device_type: "XLA_CPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 17583002155353847962
 physical_device_desc: "device: XLA_CPU device", name: "/device:XLA_GPU:0"
 device_type: "XLA_GPU"
 memory_limit: 17179869184
 locality {
 }
 incarnation: 188667365564072257
 physical_device_desc: "device: XLA_GPU device", name: "/device:GPU:0"
 device_type: "GPU"
 memory_limit: 15956161332
 locality {
   bus_id: 1
   links {
   }
 }
 incarnation: 540513892452511862
 physical_device_desc: "device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0"]


import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
%matplotlib inline
print(tf.__version__)


1.15.0
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# np.newaxis增加一维数据
X_val, y_val = X_train[55000:, ..., np.newaxis], y_train[55000:]
X_train, y_train = X_train[:55000, ..., np.newaxis], y_train[:55000]
X_test =  X_test[..., np.newaxis]

print("图片形状:{}".format(X_train.shape), end = '\n\n')
print("训练集合:{}".format(len(X_train)))
print("验证集合:{}".format(len(X_val)))
print("测试集合:{}".format(len(X_test)))
图片形状:(55000, 28, 28, 1)

训练集合:55000
验证集合:5000
测试集合:10000
# 将图片大小reshape为32*32,第1维=数量前面扩充0列,最后面扩充0列;第2维==行,前面扩充2列,最后面扩充2列;依次第3维=列、第4维...constant_values=未指定,扩充全为0
X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
X_val = np
在学习如何使用TensorFlow 2.0和Keras API来实现手写数字识别的项目实战中,推荐参考这份资料《TensorFlow手写数字识别:MNIST实验与GUI应用》。这份资源详细介绍了在TensorFlow 2.0环境下,利用经典的LeNet-5卷积神经网络(CNN)模型,处理MNIST数据集,并通过Keras API构建和训练模型的过程。同时,还包括了如何将模型与图形用户界面(GUI)进行集成,使得用户可以通过直观的界面与模型进行交互。 参考资源链接:[TensorFlow手写数字识别:MNIST实验与GUI应用](https://wenku.csdn.net/doc/645ef0645928463033a69905?spm=1055.2569.3001.10343) 首先,确保你的开发环境已经安装了TensorFlow 2.0版本。接下来,根据Keras API,构建LeNet-5模型LeNet-5通常包含多个卷积层、池化层和全连接层。具体代码如下: ```python model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(6, (5, 5), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.AveragePooling2D(), tf.keras.layers.Conv2D(16, (5, 5), activation='relu'), tf.keras.layers.AveragePooling2D(), tf.keras.layers.Flatten(), tf.keras.layers.Dense(120, activation='relu'), tf.keras.layers.Dense(84, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) ``` 然后,配置模型的训练参数,包括损失函数、优化器和评价指标。接着,使用MNIST数据集对模型进行训练。在训练过程中,可以使用Keras提供的回调函数来监控模型训练状态。 完成模型训练后,需要设计一个GUI应用,可以使用Python的tkinter库来实现。在GUI中,添加画板功能,使用户可以在画板上手写数字,然后通过按钮触发模型的预测过程。预测结果可以显示在GUI的另一部分。示例代码框架如下: ```python import tkinter as tk from PIL import Image, ImageDraw def draw_digit(event): # 在这里编写绘图代码,将用户绘制的数字保存为模型需要的格式 def predict_digit(): # 在这里编写预测代码,将绘图结果转换为模型输入并进行预测 root = tk.Tk() canvas = tk.Canvas(root, width=200, height=200) canvas.pack() canvas.bind( 参考资源链接:[TensorFlow手写数字识别:MNIST实验与GUI应用](https://wenku.csdn.net/doc/645ef0645928463033a69905?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值