LeNet5—论文及源码阅读

文章详细介绍了LeNet5的网络结构,包括卷积层C1、S2、C3、S4、C5以及全连接层F6和Output层,并解析了每层的功能。接着,文章展示了如何使用PyTorch实现LeNet5,包括数据预处理、模型构建、训练过程和模型保存。最后,提供了测试代码以加载模型并预测图像类别。

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

LeNet5实现图像分类

🐬 目录:


一、概论

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 times

        running_loss = 0.0
        for step, data in enumerate(train_loader, start=0):
            # get the inputs; data is a list of [inputs, labels]
            inputs, labels = data

            # zero the parameter gradients
            optimizer.zero_grad()
            # forward + backward + optimize
            outputs = net(inputs)
            loss = loss_function(outputs, labels)
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()	#tensor to Int
            if step % 500 == 499:    # print every 500 mini-batches
                with 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.0

    print('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 Image

from model import LeNet


def 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 最详细的解释了!

### 回答1: ARMv8架构和指令集是ARM处理器的最新版本,以满足不断增长的需求和技术发展,ARMv8架构在能耗、性能、功能和安全性等方面都有了重大提升,并且支持更多高级编程语言。它主要分为两种模式:AArch64和AArch32。 AArch64模式比AArch32模式更加先进和复杂,它使用了更大的寄存器集合,并且支持更加复杂的指令集和更高级的编程语言。AArch64位模式还扩展了虚拟内存地址空间,能够支持更大的内存容量,并且提供更加安全的指令集。 AArch32模式是ARMv8 AArch64模式的兼容模式,它使用了传统ARM32位指令,可以执行现有的ARMv7和ARMv6应用程序,并且节省成本和功耗。AArch32还支持一个新的指令集,称为Thumb-2指令集,它是一种增强型32位指令集,它可以实现更高的性能和更低的功耗。 ARMv8架构采用的是多核心技术,可以实现多个CPU核心同时运行任务,处理数据。在可扩展性方面也比以前的ARMv7和ARMv6版本提高了很多,它可以适用于从手机和智能家居设备到数据中心和超级计算机的各种应用场景。 总之,ARMv8架构和指令集是ARM处理器的新一代技术,具有更高的性能、更强的功能和更加安全的指令集,这对于计算机行业的发展和智能设备的应用都有重要的推动作用。 ### 回答2: ARM架构是目前最流行的处理器架构之一,其支持不同类型的指令集,其中包括ARMv8指令集。 ARMv8架构是ARMv7架构的延伸,它的最大特点是支持64位处理器,而ARMv7架构只有32位处理器。ARMv8架构的设计主要就是为了提升处理器的性能,增加指令集的数量和优化处理器的代码执行效率。 除此之外,ARMv8还提供了一个新的特性,叫做AArch64模式。AArch64是一种全新的64位执行模式,可以运行具有64位指令和寄存器的代码,而与此同时,它还可以执行传统的32位代码和指令,这给架构带来了更好的兼容性。 在指令集方面,ARMv8架构引入了许多新的指令,并支持一些高效的操作,例如向量计算和加密/解密。这些指令有助于提高处理器的执行速度和功耗效率。 总的来说,ARMv8架构和指令集的引入使得ARM处理器在高端领域的性能迈上了新的台阶,也为人们提供了更多的开发选择和应用场景。 ### 回答3: ARMv8是英国ARM公司推出的第8代ARM处理器架构,它于2011年发布,旨在提供更高的性能、能效和安全性。ARMv8架构有两种模式:AArch64和AArch32,其中AArch64称为64位模式,支持64位操作;AArch32称为32位模式,支持32位操作。ARMv8架构被广泛应用于手机、平板电脑、服务器、工业控制等领域。 ARMv8指令集是指在ARMv8架构下使用的指令集。 ARMv8指令集与之前的ARMv7指令集相比,增加了许多新指令和操作方式,例如条件语句执行、原子操作、SIMD指令、异常处理等。此外,ARMv8指令集中还包括与安全有关的指令,例如加密、数字签名、授权等功能。这些指令和功能使得ARMv8架构更加适用于安全需求更高、要求更高性能和能效的应用领域。 总的来说,ARMv8架构和指令集是为了满足不断提升的计算需求而设计的,其主要目标是提供更高的性能、能效和安全性。与此相比,ARMv7架构和指令集的性能和安全性都有所限制,无法满足现代处理器的需求。随着ARMv8的广泛应用,它将继续推动计算领域的发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值