LeNet-5是卷积神经网络模型的早期代表,它由LeCun在1998年提出。是人们第一次将卷积神经网络应用到图像分类任务上,在手写数字任务上取得巨大成功。
LeNet-5共有7层,包含3个卷积层、2个汇聚层以及2个全连接层的简单卷积神经网络。
- 使用卷积解决了全连接层的不足之处
- 卷积和池化层组合使用,逐层级的提取图像特征
- 在网络的最后使用全两层连接作为输出。
构建代码:
import paddle
import paddle.nn as nn
from paddle.nn import Conv2D,AvgPool2D,MaxPool2D
import paddle.nn.functional as F
import numpy as np
#定义LeNet-5网络模型
class LeNet(nn.Layer):
def __init__(self,in_channels,num_classes=10):
super(LeNet,self).__init__()
#卷积层:输出通道数为6,卷积核大小为5*5
self.conv1=Conv2D(in_channels=in_channels,out_channels=6,kernel_size=5,weight_attr=paddle.ParamAttr())
#汇聚层:汇聚窗口为2*2,步长为2
self.pool1=MaxPool2D(kernel_size=2,stride=2)
#卷积层:输入通道数为6,输出通道数为16,卷积核大小为5*5
self.conv2=Conv2D(in_channels=6,out_channels=16,kernel_size=5,stride=1,weight_attr=paddle.ParamAttr())
#汇聚层:汇聚窗口为2*2,步长为2
self.pool2=AvgPool2D(kernel_size=2,stride=2)
#卷积层:输入通道数为16,输出通道数为120,卷积核大小为5*5
self.conv3=Conv2D(in_channels=16,out_channels=120,kernel_size=5,stride=1,weight_attr=paddle.ParamAttr())
#全连接层:输入神经元为120,输出神经元为84
self.linear1=nn.Linear(in_features=120,out_features=84)
#全连接层:输入神经元为84,输出神经元为类别数
self.linear2=nn.Linear(in_features=84,out_features=num_classes)
def forward(self,x):
#卷积层+激活函数
output = F.relu(self.conv1(x))
#汇聚层
output = self.pool1(output)
#卷积层+激活函数
output = F.relu(self.conv2(output))
#汇聚层
output = self.pool2(output)
output = self.conv3(output)
#输入层将数据拉平[B,C,H,W] -> [B,C*H*W]
output = paddle.reshape(output, [output.shape[0], -1])
#2个全连接层
output = F.relu(self.linear1(output))
output = self.linear2(output)
return output
#模型测试
#构造一个形状#为 [1,1,32,32]的输入数据送入网络,观察每一层特征图的形状变化。代码实现如下:
inputs = np.random.randn(*[1,1,32,32]).astype(np.float32)
#创建LeNet类的实例,指定模型名称和分类的类别数目
model = LeNet(in_channels=1, num_classes=10)
#通过调用LeNet从基类继承的sublayers()函数,查看LeNet中所包含的子层
print(model.sublayers())
x = paddle.to_tensor(inputs)
for item in model.sublayers():
#item是LeNet类中的一个子层
#查看经过子层之后的输出数据形状
try:
x = item(x)
except:
#如果是最后一个卷积层输出,需要展平后才可以送入全连接层
x = paddle.reshape(x, [x.shape[0], -1])
x = item(x)
if len(item.parameters())==2:
#查看卷积和全连接层的数据和参数的形状,
#其中item.parameters()[0]是权重参数w,item.parameters()[1]是偏置参数b
print(item.full_name(), x.shape, item.parameters()[0].shape, item.parameters()[1].shape)
else:
#汇聚层没有参数
print(item.full_name(), x.shape)
#输出结果:
[ Conv2D(1, 6, kernel_size=[5, 5], data_format=NCHW),
MaxPool2D(kernel_size=2, stride=2, padding=0),
Conv2D(6, 16, kernel_size=[5, 5], data_format=NCHW),
AvgPool2D(kernel_size=2, stride=2, padding=0),
Conv2D(16, 120, kernel_size=[5, 5], data_format=NCHW),
Linear(in_features=120, out_features=84, dtype=float32),
Linear(in_features=84, out_features=10, dtype=float32)]
conv2d_3 [1, 6, 28, 28] [6, 1, 5, 5] [6]
max_pool2d_1 [1, 6, 14, 14]
conv2d_4 [1, 16, 10, 10] [16, 6, 5, 5] [16]
avg_pool2d_1 [1, 16, 5, 5]
conv2d_5 [1, 120, 1, 1] [120, 16, 5, 5] [120]
linear_2 [1, 84] [120, 84] [84]
linear_3 [1, 10] [84, 10] [10]
从输出结果看:
- 对于大小为32×32的单通道图像,先用6个大小为5×5的卷积核对其进行卷积运算,输出为6个28×28大小的特征图;
- 6个28×28大小的特征图经过大小为2×2,步长为2的汇聚层后,输出特征图的大小变为14×14;
- 6个14×14大小的特征图再经过16个大小为5×5的卷积核对其进行卷积运算,得到16个10×10大小的输出特征图;
- 16个10×10大小的特征图经过大小为2×2,步长为2的汇聚层后,输出特征图的大小变为5×5;
- 16个5×5大小的特征图再经过120个大小为5×5的卷积核对其进行卷积运算,得到120个1×1大小的输出特征图;
- 此时,将特征图展平成1维,则有120个像素点,经过输入神经元个数为120,输出神经元个数为84的全连接层后,输出的长度变为84。
- 再经过一个全连接层的计算,最终得到了长度为类别数的输出结果。