【深度学习】:《PyTorch入门到项目实战》(十五):三大经典卷积神经网络架构:LeNet、AlexNet、VGG
发布人:shili8
发布时间:2025-01-02 21:03
阅读次数:0
**深度学习**: 《PyTorch入门到项目实战》(十五): 三大经典卷积神经网络架构: LeNet、AlexNet、VGG在前几篇文章中,我们已经介绍了PyTorch的基本使用和一些常见的神经网络结构。今天,我们将重点讨论三大经典卷积神经网络架构:LeNet、AlexNet和VGG。
###1. LeNetLeNet是由Yann LeCun等人在1998年提出的,最初用于手写数字识别任务。它的结构非常简单,但却取得了很好的效果。
**LeNet结构**
markdown# LeNet结构- 卷积层(5x5,6个通道) - 池化层(2x2) - 全连接层(120个神经元) - 全连接层(84个神经元) - 输出层
**LeNet代码示例**
import torchimport torch.nn as nnclass LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.conv1 = nn.Conv2d(1,6, kernel_size=5) self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(120,84) self.fc2 = nn.Linear(84,10) def forward(self, x): out = self.pool(nn.functional.relu(self.conv1(x))) out = out.view(-1,120) out = self.fc1(out) out = self.fc2(out) return out
###2. AlexNetAlexNet是由Alex Krizhevsky等人在2012年提出的,最初用于图像识别任务。它的结构比LeNet复杂,但却取得了更好的效果。
**AlexNet结构**
markdown# AlexNet结构- 卷积层(11x11,96个通道) - 池化层(3x3) - 卷积层(5x5,256个通道) - 池化层(3x3) - 全连接层(4096个神经元) - 全连接层(4096个神经元) - 输出层
**AlexNet代码示例**
import torchimport torch.nn as nnclass AlexNet(nn.Module): def __init__(self): super(AlexNet, self).__init__() self.conv1 = nn.Conv2d(3,96, kernel_size=11) self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2) self.conv2 = nn.Conv2d(96,256, kernel_size=5) self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2) self.fc1 = nn.Linear(256 *6 *6,4096) self.fc2 = nn.Linear(4096,4096) self.fc3 = nn.Linear(4096,10) def forward(self, x): out = self.pool1(nn.functional.relu(self.conv1(x))) out = self.pool2(nn.functional.relu(self.conv2(out))) out = out.view(-1,256 *6 *6) out = self.fc1(out) out = nn.functional.relu(out) out = self.fc2(out) out = nn.functional.relu(out) out = self.fc3(out) return out
###3. VGGVGG是由Karen Simonyan和Andrew Zisserman在2014年提出的,最初用于图像识别任务。它的结构比AlexNet复杂,但却取得了更好的效果。
**VGG结构**
markdown# VGG结构- 卷积层(3x3,64个通道) - 池化层(2x2) - 卷积层(3x3,128个通道) - 池化层(2x2) - 卷积层(3x3,256个通道) - 池化层(2x2) - 卷积层(3x3,512个通道) - 池化层(2x2) - 卷积层(3x3,512个通道) - 池化层(2x2) - 全连接层(4096个神经元) - 全连接层(4096个神经元) - 输出层
**VGG代码示例**
import torchimport torch.nn as nnclass VGG(nn.Module): def __init__(self): super(VGG, self).__init__() self.conv1 = nn.Conv2d(3,64, kernel_size=3) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(64,128, kernel_size=3) self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv3 = nn.Conv2d(128,256, kernel_size=3) self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv4 = nn.Conv2d(256,512, kernel_size=3) self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv5 = nn.Conv2d(512,512, kernel_size=3) self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(7 *7 *512,4096) self.fc2 = nn.Linear(4096,4096) self.fc3 = nn.Linear(4096,10) def forward(self, x): out = self.pool1(nn.functional.relu(self.conv1(x))) out = self.pool2(nn.functional.relu(self.conv2(out))) out = self.pool3(nn.functional.relu(self.conv3(out))) out = self.pool4(nn.functional.relu(self.conv4(out))) out = self.pool5(nn.functional.relu(self.conv5(out))) out = out.view(-1,7 *7 *512) out = self.fc1(out) out = nn.functional.relu(out) out = self.fc2(out) out = nn.functional.relu(out) out = self.fc3(out) return out
以上就是三大经典卷积神经网络架构:LeNet、AlexNet和VGG的介绍。这些模型在图像识别任务中取得了很好的效果,并且被广泛应用于各种领域。