当前位置:网站首页>Convolutional neural network model -- lenet network structure and code implementation
Convolutional neural network model -- lenet network structure and code implementation
2022-07-25 13:07: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)

边栏推荐
- 485通讯( 详解 )
- Zero basic learning canoe panel (14) -- led control and LCD control
- Leetcode 1184. distance between bus stops
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- JS sorts according to the attributes of the elements in the array
- Zero basic learning canoe panel (12) -- progress bar
- Detailed explanation of flex box
- Moving Chinese figure liushenglan
- R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
- 工业互联网的内涵及其应用
猜你喜欢

mysql函数汇总之日期和时间函数

B tree and b+ tree

Shell common script: check whether a domain name and IP address are connected

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

Atcoder beginer contest 261 f / / tree array

7行代码让B站崩溃3小时,竟因“一个诡计多端的0”

Memory layout of program

Cyberspace Security penetration attack and defense 9 (PKI)

机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理

艰辛的旅程
随机推荐
零基础学习CANoe Panel(15)—— 文本输出(CAPL Output View )
2022.07.24 (lc_6124_the first letter that appears twice)
公安部:国际社会普遍认为中国是世界上最安全的国家之一
状态(State)模式
Word style and multi-level list setting skills (II)
Is the securities account opened by qiniu safe? How to open an account
Moving Chinese figure liushenglan
OAuth, JWT, oidc, you mess me up
[today in history] July 25: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
【历史上的今天】7 月 25 日:IBM 获得了第一项专利;Verizon 收购雅虎;亚马逊发布 Fire Phone
Requirements specification template
[CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
Handwriting a blog platform ~ first day
Docekr learning - MySQL 8 master-slave replication setup deployment
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
Deployment of Apache website services and implementation of access control
Simple understanding of flow
Shell Basics (exit control, input and output, etc.)
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
Shell常用脚本:检测某域名、IP地址是否通