当前位置:网站首页>Convolutional neural network model -- lenet network structure and code implementation
Convolutional neural network model -- lenet network structure and code implementation
2022-07-04 22:11:00 【1 + 1= Wang】
List of articles
LeNet brief introduction
LeNet Original address :https://ieeexplore.ieee.org/abstract/document/726791
LeNet It can be said to be convolutional neural network “HelloWorld”, It is through clever design , Using convolution 、 Pool and other operations to extract features , Then use the fully connected neural network to classify .
Lenet It's a 7 Layer of neural network ( Does not include the input layer ), contain 3 Convolution layers ,2 A pool layer ,2 All connection layers . Its network structure is shown below :
LeNet7 The layer structure
The first 0 layer : Input
The input original image size is 32×32 Pixel 3 Channel image .
C1: The first convolution layer
C1 It's a convolution , The convolution kernel size is 5, The input size is 3X32X32, The size of the output feature map is 16X28X28.
self.conv1 = nn.Conv2d(3, 16, 5)
torch.nn.Conv2d() The parameters of are explained as follows :
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
- in_channels (int) – Number of channels in the input image
- out_channels (int) – Number of channels produced by the convolution
- kernel_size (int or tuple) – Size of the convolving kernel
- stride (int or tuple, optional) – Stride of the convolution. Default: 1
- padding (int, tuple or str, optional) – Padding added to all four sides of the input. Default: 0
- padding_mode (string, optional) – ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’
- dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1
- groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1
- bias (bool, optional) – If True, adds a learnable bias to the output. Default: True
The output size after convolution is calculated as the following :
S2: First lower sampling layer
S2 It's a pool layer ,kernel_size by 2,stride by 2, The input size is 16X28X28, The size of the output feature map is 16X14X14.
self.pool1 = nn.MaxPool2d(2, 2)
torch.nn.MaxPool2d The parameters of are explained as follows :
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
- kernel_size – the size of the window to take a max over
- stride – the stride of the window. Default value is kernel_size
- padding – implicit zero padding to be added on both sides
- dilation – a parameter that controls the stride of elements in the window
- return_indices – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
- ceil_mode – when True, will use ceil instead of floor to compute the output shape
The output size after pooling is calculated as the following :
C3: The first 2 Convolution layers
C3 It's a convolution , The convolution kernel size is 5, The input size is 16X14X14, The size of the output feature map is 32X10X10.
self.conv2 = nn.Conv2d(16, 32, 5)
S4: The first 2 Next sampling layer
S4 It's a pool layer ,kernel_size by 2,stride by 2, The input size is 32X10X10, The size of the output feature map is 32X5X5.
self.pool2 = nn.MaxPool2d(2, 2)
C5: The first 3 Convolution layers
C5 It's a convolution , The convolution kernel size is 5, The input size is 32X5X5, The size of the output feature map is 120X1X1.
Here, the full connection layer is used instead
self.fc1 = nn.Linear(32*5*5, 120)
F6: The first 1 All connection layers
F6 Is a fully connected layer , The input size is 120, The size of the output feature map is 84.
self.fc2 = nn.Linear(120, 84)
F7: The first 2 All connection layers
F7 Is a fully connected layer , The input size is 84, The size of the output feature map is 10( Express 10 Species category ).
self.fc3 = nn.Linear(84, 10)
Use pytorch build LeNet
Build a network model , At least two steps are needed
1. Create a class and inherit nn.Module
import torch.nn as nn
# pytorch: Channel sorting :[N,Channel,Height,Width]
class LeNet(nn.Module):
2. Class to implement two methods
def __init__(self): Define the network layer structure that needs to be used in building the network
def forward(self, x): Define the forward propagation process
LeNet Network structure pytorch Realization :
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 5) # C1
self.pool1 = nn.MaxPool2d(2, 2) # S2
self.conv2 = nn.Conv2d(16, 32, 5) # C3
self.pool2 = nn.MaxPool2d(2, 2) # S4
self.fc1 = nn.Linear(32*5*5, 120) # C5( Replace with full connection )
self.fc2 = nn.Linear(120, 84) # F6
self.fc3 = nn.Linear(84, 10) # F7
def forward(self, x):
x = F.relu(self.conv1(x)) # input(3, 32, 32) output(16, 28, 28)
x = self.pool1(x) # output(16, 14, 14)
x = F.relu(self.conv2(x)) # output(32, 10, 10)
x = self.pool2(x) # output(32, 5, 5)
x = x.view(-1, 32*5*5) # output(32*5*5) Flattening
x = F.relu(self.fc1(x)) # output(120)
x = F.relu(self.fc2(x)) # output(84)
x = self.fc3(x) # output(10)
return x
Print LeNet The structure is as follows :
model = LeNet()
print(model)
边栏推荐
- VIM from dislike to dependence (23) -- the last gossip
- Operation of adding material schedule in SolidWorks drawing
- 电话加密,中间4为****代替
- GTEST from ignorance to proficiency (3) what are test suite and test case
- 如何借助自动化工具落地DevOps
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- gtest从一无所知到熟练使用(4)如何用gtest写单元测试
- Bookmark
猜你喜欢
Interpreting the development of various intelligent organizations in maker Education
【C语言进阶篇】数组&&指针&&数组笔试题
QT—双缓冲绘图
【Acwing】第58场周赛 题解
Redis has three methods for checking big keys, which are necessary for optimization
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
从RepVgg到MobileOne,含mobileone的代码
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
From repvgg to mobileone, including mobileone code
VS2019 C# release下断点调试
随机推荐
Solana链上应用Crema因黑客攻击停运
抖音实战~评论数量同步更新
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Analyzing the maker space contained in steam Education
QT—双缓冲绘图
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe to open an account online
PMO:比较25种分子优化方法的样本效率
ApacheCN 翻译、校对、笔记整理活动进度公告 2022.7
【C语言进阶篇】数组&&指针&&数组笔试题
HDU - 1078 fatmouse and cheese (memory search DP)
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
What is business intelligence (BI), just look at this article is enough
VS2019 C# release下断点调试
时空预测3-graph transformer
gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
# 2156. 查找给定哈希值的子串-后序遍历
Bookmark
gtest从一无所知到熟练运用(1)gtest安装
gtest从一无所知到熟练使用(3)什么是test suite和test case