首页 > 编程知识 正文

pytorch搭建卷积神经网络,pytorch搭建vggnet

时间:2023-05-05 12:58:22 阅读:187514 作者:1103

Pytorch搭建LeNet5

1. LeNet神经网络介绍

LeNet神经网络由深度学习三巨头之一的Yan LeCun提出,他同时也是卷积神经网络 (CNN,Convolutional Neural Networks)之父。LeNet主要用来进行手写字符的识别与分类。LeNet的实现确立了CNN的结构,现在神经网络中的许多内容在LeNet的网络结构中都能看到,例如卷积层,Pooling层,ReLU层。虽然LeNet早在20世纪90年代就已经提出了,但由于当时缺乏大规模的训练数据,计算机硬件的性能也较低,因此LeNet神经网络在处理复杂问题时效果并不理想。虽然LeNet网络结构比较简单,但是刚好适合神经网络的入门学习。

2. LeNet神经网络结构

卷积层块里的基本单位是卷积层后接最大池化层:卷积层用来识别图像里的空间模式,如线条和物体局部,之后的最大池化层则用来降低卷积层对位置的敏感性。

卷积层块由两个这样的基本单位重复堆叠构成。在卷积层块中,每个卷积层都使用5×5的窗口,并在输出上使用ReLU激活函数。

第一个卷积层输出通道数为6,第二个卷积层输出通道数则增加到16。这是因为第二个卷积层比第一个卷积层的输入的高和宽要小,所以增加输出通道使两个卷积层的参数尺寸类似。

卷积层块的两个最大池化层的窗口形状均为2×2,且步幅为2。由于池化窗口与步幅形状相同,池化窗口在输入上每次滑动所覆盖的区域互不重叠。

卷积层块的输出形状为(批量大小, 通道, 高, 宽)

in_put = torch.rand(1, 1, 32, 32) # 随机构建一zjdpy个通道的32*32的输入图片 当卷积层块的输出传入全连接层块时,全连接层块会将小批量中每个样本变平(flatten)。 - output = output.view(img.size(0), -1)

也就是说,全连接层的输入形状将变成二维,其中第一维是小批量中的样本,第二维是每个样本变平后的向量表示,且向量长度为通道、高和宽的乘积。

全连接层块含3个全连接层。它们的输出个数分别是120、84和10,其中10为输出的类别个数。

3. Pytorch搭建LeNet5

import torch.nn as nnfrom collections import OrderedDictimport torch# 利用torch.nn.Sequential和collections.OrderedDict()来构建每一个layerclass C1(nn.Module): def __init__(self): super(C1, self).__init__() self.c1 = nn.Sequential(OrderedDict([ # 1代表输入通道数,6代表输出通道数,5代表卷积核的大小5x5,默认stride=1 ('c1', nn.Conv2d(1, 6, 5)), # 6@28×28 ('relu1', nn.ReLU()), ('s2', nn.MaxPool2d(2, 2)) # 6@14x14,通道数不变,超参数2分别表示池化大小和步长 ])) def forward(self, img): output = self.c1(img) # 输入是1x32x32 return outputclass C3(nn.Module): def __init__(self): super(C3, self).__init__() self.c3 = nn.Sequential(OrderedDict([ ('c3', nn.Conv2d(6, 16, 5)), # 16@10×10 ('relu2', nn.ReLU()), ('s4', nn.MaxPool2d(2, 2)) # 16@5×5 ])) def forward(self, img): output = self.c3(img) return outputclass C5(nn.Module): def __init__(self): super(C5, self).__init__() self.c5 = nn.Sequential(OrderedDict([ ('c5', nn.Conv2d(16, 120, 5)), # 120@1x1 ('relu3', nn.ReLU()) ])) def forward(self, img): output = self.c5(img) return outputclass F6(nn.Module): def __init__(self): super(F6, self).__init__() self.f6 = nn.Sequential(OrderedDict([ ('f4', nn.Linear(120, 84)), ('relu4', nn.ReLU()) ])) def forward(self, img): output = self.f6(img) return outputclass F7(nn.Module): def __init__(self): super(F7, self).__init__() self.f7 = nn.Sequential(OrderedDict([ ('f7', nn.Linear(84, 10)), ('sig8', nn.LogSoftmax(dim=-1)) ])) def forward(self, img): output = self.f7(img) return outputclass LeNet5(nn.Module): """ Input - 1x32x32 Output - 10 """ def __init__(self): super(LeNet5, self).__init__() self.c1 = C1() self.c3_1 = C3() self.c3_2 = C3() self.c5 = C5() self.f6 = F6() self.f7 = F7() def forward(self, img): output = self.c1(img) x = self.c3_1(output) output = self.c3_2(output) output += x output = self.c5(output) output = output.view(img.size(0), -1) # 将卷积层的输出摊平为120维向量 output = self.f6(output) # 全连接层将120维变为84维 output = self.f7(output) # 全连接层将84维变为10维,用于预测输出的结果 return outputif __name__ == '__main__': model = LeNet5() # 网络实例化 print(model) in_put = torch.rand(1, 1, 32, 32) # 随机构建一zjdpy个通道的32*32的输入图片 out = model(in_put) print(out) # 哪一维的数字最大,输入就属于那一类别 LeNet5( (c1): C1( (c1): Sequential( (c1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1)) (relu1): ReLU() (s2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) ) (c3_1): C3( (c3): Sequential( (c3): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (relu2): ReLU() (s4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) ) (c3_2): C3( (c3): Sequential( (c3): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (relu2): ReLU() (s4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) ) (c5): C5( (c5): Sequential( (c5): Conv2d(16, 120, kernel_size=(5, 5), stride=(1, 1)) (relu3): ReLU() ) ) (f6): F6( (f6): Sequential( (f4): Linear(in_features=120, out_features=84, bias=True) (relu4): ReLU() ) ) (f7): F7( (f7): Sequential( (f7): Linear(in_features=84, out_features=10, bias=True) (sig8): LogSoftmax() ) ))tensor([[-2.2676, -2.2554, -2.3434, -2.3523, -2.4532, -2.2200, -2.1823, -2.3595, -2.3212, -2.2984]], grad_fn=<LogSoftmaxBackward>)Process finished with exit code 0

参考
https://www.jianshu.com/p/cd73bc979ba9
https://tangshusen.me/Dive-into-DL-PyTorch/#/chapter05_CNN/5.5_lenet

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。