LeNet5—论文及源码阅读

article/2025/7/3 9:31:47
LeNet5实现图像分类

🐬 目录:

  • 一、概论
  • 二、论文选读
  • 三、源码精读
    1. 所用数据集介绍
    2. LeNet5模型网络构建
    3. LeNet5模型训练
    4. 测试代码
  • 四、参考资料

一、概论

LeNet-5是一种经典的卷积神经网络结构,于1998年投入实际使用中。该网络最早应用于手写体字符识别应用中。普遍认为,卷积神经网络的出现开始于LeCun等提出的LeNet网络,可以说LeCun等是CNN的缔造者,而LeNet则是LeCun等创造的CNN经典之作。

二、论文选读

论文: 《Gradient-Based Learning Applied to Document Recognition》


LeNet-5 comprises seven layers, not counting the input, all of which contain trainable parameters (weights). The input is a 32×32 pixel image.

理解: LeNet5共包含7层,输入为32×32像素的图片,如下图所示:

在这里插入图片描述



Layer C1 is a convolutional layer with six feature maps.Each unit in each feature map is connected to a 5✖5 neigh-borhood in the input.

理解: C1 层是卷积层,使用 6 个 5×5 大小的卷积核,padding=0,stride=1进行卷积,得到 6 个 28×28 大小的特征图:32-5+1=28



Layer S2 is a subsampling layer with six feature maps of size 14×14. Each unit in each feature map is connected to a 2×2 neighborhood in the corresponding feature map in C1.The four inputs to a unit in S2 are added, then multiplied by a trainable coefficient, and then added to a trainable bias.The result is passed through a sigmoidal function.

理解: S2 层是降采样层,使用 6 个 2×2 大小的卷积核进行池化,padding=0,stride=2,得到 6 个 14×14 大小的特征图:28/2=14。S2 层其实相当于降采样层+激活层。先是降采样,然后激活函数 sigmoid 非线性输出。先对 C1 层 2x2 的视野求和,然后进入激活函数。



Layer C3 is a convolutional layer with 16 feature maps.Each unit in each feature map is connected to several 5×5 neighborhoods at identical locations in a subset of S2’s feature maps.
The first six C3 feature maps take inputs from every contiguous subsets of three feature maps in S2. The next six take input from every contiguous subset of four. The next three take input from some discontinuous subsets of four. Finally, the last one takes input from all S2 feature maps.

理解: C3 层是卷积层,使用 16 个 5×5xn 大小的卷积核,padding=0,stride=1 进行卷积,得到 16 个 10×10 大小的特征图:14-5+1=10。
16 个卷积核并不是都与 S2 的 6 个通道层进行卷积操作,如下图所示,C3 的前六个特征图(0,1,2,3,4,5)由 S2 的相邻三个特征图作为输入,对应的卷积核尺寸为:5x5x3;接下来的 6 个特征图(6,7,8,9,10,11)由 S2 的相邻四个特征图作为输入对应的卷积核尺寸为:5x5x4;接下来的 3 个特征图(12,13,14)号特征图由 S2 间断的四个特征图作为输入对应的卷积核尺寸为:5x5x4;最后的 15 号特征图由 S2 全部(6 个)特征图作为输入,对应的卷积核尺寸为:5x5x6。

在这里插入图片描述



Layer S4 is a subsampling layer with 16 feature maps of size 5×5. Each unit in each feature map is connected to a 2×2 neighborhood in the corresponding feature map in C3.

理解: S4 层与 S2 一样也是降采样层,使用 16 个 2×2 大小的卷积核进行池化,padding=0,stride=2,得到 16 个 5×5 大小的特征图:10/2=5。



Layer C5 is a convolutional layer with 120 feature maps.Each unit is connected to a 5×5 neighborhood on all 16 of S4’s feature maps.

理解: C5 层是卷积层,使用 120 个 5×5x16 大小的卷积核,padding=0,stride=1进行卷积,得到 120 个 1×1 大小的特征图:5-5+1=1。即相当于 120 个神经元的全连接层。
值得注意的是,与C3层不同,这里120个卷积核都与S4的16个通道层进行卷积操作。



Layer F6 contains 84 units (the reason for this number comes from the design of the output layer, explained below) and is fully connected to C5.
units in layers up to F6 compute adot product between their input vector and their weigh tvector, to which a bias is added.
then passed through a sigmoid squashing function

理解: F6 是全连接层,共有 84 个神经元,与 C5 层进行全连接,即每个神经元都与 C5 层的 120 个特征图相连。计算输入向量和权重向量之间的点积,再加上一个偏置,结果通过 sigmoid 函数输出。



the output layer is composed of Euclidean RBFunits, one for each class, with 84 inputs each.

理解 :最后的 Output 层也是全连接层,是 Gaussian Connections,采用了 RBF 函数(即径向欧式距离函数),计算输入向量和参数向量之间的欧式距离(目前已经被Softmax 取代)。

三、源码精读

📡 实现效果
在这里插入图片描述

3.1 所用数据集介绍

CIFAR-10 是由 Hinton 的学生 Alex Krizhevsky 和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集。一共包含 10 个类别的 RGB 彩色图 片:飞机( airplane )、汽车( automobile )、鸟类( bird )、猫( cat )、鹿( deer )、狗( dog )、蛙类( frog )、马( horse )、船( ship )和卡车( truck )。图片的尺寸为 32×32 ,数据集中一共有 50000 张训练圄片和 10000 张测试图片。
请添加图片描述

数据集分为五个训练batches和一个测试batch,每个batch有10000张图像。测试batch包含从每个类中随机选择的1000个图像。训练batches以随机顺序包含剩余的图像,但有些训练batches可能包含一个类的图像多于另一个类的图像。在它们之间,训练batches包含来自每个类的5000张图像

3.2 LeNet5模型网络构建

class LeNet(nn.Module):def __init__(self):super(LeNet, self).__init__()	#调用父类nn.Module中的init方法对属性进行初始化self.conv1 = nn.Conv2d(3, 16, 5)	 self.pool1 = nn.MaxPool2d(2, 2)self.conv2 = nn.Conv2d(16, 32, 5)self.pool2 = nn.MaxPool2d(2, 2)self.fc1 = nn.Linear(32*5*5, 120)self.fc2 = nn.Linear(120, 84)self.fc3 = nn.Linear(84, 10)

根据论文描述,LeNet5共包含7层。上述代码作用为初始化属性,为后续forward函数的调用做准备,上述代码中所用基本函数定义,查阅pytorch官方文档如下表所示:

函数介绍
nn.Conv2d(3,16,5)torch.nn.Conv2d(in_channels = 3, out_channels = 16, kernel_size = 5 * 5, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=‘zeros’, device=None, dtype=None)
nn.MaxPool2d(2, 2)torch.nn.MaxPool2d(kernel_size = 2 * 2, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False
nn.Linear(32 * 5 * 5, 120)torch.nn.Linear(in_features = 32 * 5 * 120, out_features = 120, bias=True, device=None, dtype=None)

3.3 LeNet5模型训练

🐢 3.3.1 下载数据集

    #逐channel的对图像均值/方差进行标准化。可以加快模型的收敛transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])# 50000张训练图片# 第一次使用时要将download设置为True才会自动去下载数据集train_set = torchvision.datasets.CIFAR10(root='./data', train=True,download=True, transform=transform)train_loader = torch.utils.data.DataLoader(train_set, batch_size=36,shuffle=True, num_workers=0)# 10000张验证图片# 第一次使用时要将download设置为True才会自动去下载数据集val_set = torchvision.datasets.CIFAR10(root='./data', train=False,download=False, transform=transform)

🐢 3.3.2 加载数据集

Data loader. Combines a dataset and a sampler, and provides an iterable over the given dataset.

train_loader = torch.utils.data.DataLoader(train_set, batch_size=36,shuffle=True, num_workers=0)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=5000,shuffle=False, num_workers=0)

🐢 3.3.3 训练LeNet5网络,学习参数

    for epoch in range(5):  # loop over the dataset multiple timesrunning_loss = 0.0for step, data in enumerate(train_loader, start=0):# get the inputs; data is a list of [inputs, labels]inputs, labels = data# zero the parameter gradientsoptimizer.zero_grad()# forward + backward + optimizeoutputs = net(inputs)loss = loss_function(outputs, labels)loss.backward()optimizer.step()# print statisticsrunning_loss += loss.item()	#tensor to Intif step % 500 == 499:    # print every 500 mini-batcheswith torch.no_grad():outputs = net(val_image)predict_y = torch.max(outputs, dim=1)[1]accuracy = torch.eq(predict_y, val_label).sum().item() / val_label.size(0)print('[%d, %5d] train_loss: %.3f  test_accuracy: %.3f' %(epoch + 1, step + 1, running_loss / 500, accuracy))running_loss = 0.0print('Finished Training')

🐢 3.3.4 保存模型权重

    save_path = './Lenet.pth'torch.save(net.state_dict(), save_path)     #获取模型的参数,而不保存结构

3.4 测试代码

import torch
import torchvision.transforms as transforms
from PIL import Imagefrom model import LeNetdef main():transform = transforms.Compose([transforms.Resize((32, 32)),transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog', 'horse', 'ship', 'truck')net = LeNet()net.load_state_dict(torch.load('Lenet.pth'))im = Image.open('img.png')im = transform(im)  # [C, H, W]im = torch.unsqueeze(im, dim=0)  # [N, C, H, W]with torch.no_grad():outputs = net(im)predict = torch.max(outputs, dim=1)[1].numpy()print(classes[int(predict)])if __name__ == '__main__':main()

四、参考资料

这可能是神经网络 LeNet-5 最详细的解释了!


http://chatgpt.dhexx.cn/article/MNlw5NOu.shtml

相关文章

重学深度学习系列---LeNet5实现手写数字识别(TensorFlow2-mnist数据集)

重学深度学习系列—LeNet5实现手写数字识别(TensorFlow2-mnist数据集) 文章目录 重学深度学习系列---LeNet5实现手写数字识别(TensorFlow2-mnist数据集)我的环境:一、LeNet5简单介绍二、LeNet-5代码实现三、训练四、对图片进行预测五、训练过程截图:参考…

经典网络-LeNet-5

上图展示展示了 LeNet-5 模型的架构。 是Yann LeCun 教授于1998 年在论文Gradient-based learning applied to document recognition中提出的,它是第一个成功应用于数字识别问题的卷积神经网络。 LeNet-5 详细结构 在下面的篇幅中将详细介绍LeNet-5 模型每一层的…

深度学习---卷积神经网络之LeNet5(TensorFlow 代码实现)

一、前言 1.1 使用全连接神经网络对图像进行处理存在的问题 1、需要处理的数据量大,效率低 现在的图像都有着极高的像素,假设一张需要处理的图片像素是 1000 * 1000 * 3(彩色图片,具有 RGB 3 个通道),使用具有 100 个隐藏单元的…

浅谈LeNet-5

浅谈LeNet-5 基于Tensorflow的实现欢迎查看我下一篇博客Tensorflow实战 LeNet-5神经网络进行手写体数字识别 一、LetNet是什么? LetNet是一种入门级的神经网络模型,是一个简单的卷积神经网络,可以用来做手写体识别。 下面我将以上图为例简…

卷积神经网络CNN与LeNet5详解(可训练参数量、计算量、连接数的计算+项目实战)

文章目录 神经网络CNN卷积神经网络CNN的由来局部感受野共享权重池化CNN的结构光栅化 LeNet5详解LeNet5-C1层LeNet5-S2层LeNet5-C3层LeNet5-S4层LeNet5-C5层LeNet5-F6层LeNet5-OUTPUT层计算公式 LeNet5实战定义网络模型初始化模型参数训练测试准确率预测结果 神经网络 神经网络可…

lenet5实现

Pytorch环境下搭建lenet5网络实现手写数字识别 (文章采用cuda9.0cudnn7.4pytorch1.6环境) 数据集选用EMNIST dataset中的手写数据集,参考链接如下: 数据集下载地址 代码部分参考S.E作者的pytorch实现手写英文字母识别&#xff0…

从零开始的神经网络构建历程(三,LeNet5复现)

前两篇博文主要介绍了torch如何构建全连接前馈神经网络,本篇博客主要针对经典卷积神经网络LeNet5进行复现。 卷积神经网络的基本结构 相信不少人都看过不少博客,也都对卷积神经网络的大致结构了解一点,这里本人站在神经元的角度来描述卷积神…

卷积神经网络LeNet5结构

LeNet5可以说是最早的卷积神经网络了,它发表于1998年,论文原文Gradient-Based Learning Applied to Doucment Recognition作者是Yann Le Cun等。下面对LeNet5网络架构进行简单的说明,有兴趣的同学可以去参考原文,论文原文地址http…

Lenet5(1998)

先读 https://blog.csdn.net/zhangjunhit/article/details/53536915 https://blog.csdn.net/qianqing13579/article/details/71076261 第三个卷积层,S2 6个1010的特征映射,C3是16个1010的特征映射,怎么做的呢? 关注C3层 Why not …

LeNet5的论文及理解

LeNet5网络的来源:Lcun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proceedings of the IEEE, 1998, 86(11):2278-2324. 1. 卷积神经网络(Convolutional Neural Network,CNN)基…

Pytorch搭建LeNet5网络

本讲目标:   介绍Pytorch搭建LeNet5网络的流程。 Pytorch八股法搭建LeNet5网络 1.LeNet5网络介绍2.Pytorch搭建LeNet5网络2.1搭建LeNet网络2.2测试LeNet网络输出2.3下载数据集2.4加载并配置网络2.5训练并保存网络2.6测试图片 1.LeNet5网络介绍 借鉴点:…

HLS:卷积神经网络LeNet5的实现与测试

目录 一、引言二、LeNet5的学习三、数学知识补充四、HLS代码设计五、仿真综合与优化六、Zynq平台搭建测试七、一些注意点八、文献时间线与后续工作 一、引言 1、开发环境。 Windows10、Vivado2018.2、Vivado HLS与Xilinx SDK。 2、LeNet5概述。 1994年,CNN网络&…

卷积神经网络(一):LeNet5的基本结构

在机器视觉,图像处理领域,卷积神经网络取得了巨大的成功。本文将参考UFLDL和DEEPLEARNING.NET的教程,结合自己的理解,梳理一下卷积神经网络的构成以及其BP算法的求解。虽然利用theano可以方便的实现LeNet5,但是不利于学…

LeNet5网络结构详解

文章目录 1.论文地址:2.LeNet5网络结构:3.首先了解参数量和计算量之间的区别和计算:(1)参数量(Params):(2)计算量(FLOPS):&#xff08…

经典网络模型介绍系列——LeNet-5

从今天开始,带大家从LeNet5开始学习经典的网络模型。 一、LeNet-5 LeNet-5是LeNet系列的最终稳定版,它被美国银行用于手写数字识别,该网络有以下特点: 所有卷积核大小均为5*5,步长为1;所有池化方法为平均…

LeNet5的深入解析

论文:Gradient-based-learning-applied-to-document-recognition 参考:http://blog.csdn.net/strint/article/details/44163869 LeNet5 这个网络虽然很小,但是它包含了深度学习的基本模块:卷积层,池化层,…

LeNet5模型讲解

加粗样式LeNet5模型讲解 LeNet5模型总览 总共8层网络,分别为: 输入层(INPUT)、卷积层(Convolutions,C1)、池化层(Subsampling,S2)、卷积层(C3)、…

LeNet-5详解

一、前言 LeNet-5出自论文Gradient-Based Learning Applied to Document Recognition,是一种用于手写体字符识别的非常高效的卷积神经网络。 本文将从卷积神经网络结构的基础说起,详细地讲解每个网络层。 论文下载:请到文章结尾处下载。 …

卷积神经网络:LeNet-5

2021SCSDUSC LeNet-5卷积神经网络的整体框架: 特征映射:一幅图在经过卷积操作后得到结果称为特征图。 LeNet-5共有8层,包含输入层,每层都包含可训练参数;每个层有多个特征映射(,每个特征映射通过一种卷积…

LeNet-5网络详解

文章目录 1 模型介绍2 模型结构3 模型特性 1 模型介绍 LeNet-5出自论文《Gradient-Based Learning Applied to Document Recognition》,是由 L e C u n LeCun LeCun 于1998年提出的一种用于识别手写数字和机器印刷字符的卷积神经网络,其命名来源于作者 …