当前位置:网站首页>Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
2022-06-28 15:40:00 【Hua Weiyun】
Implementation is based on CNN Handwritten font recognition on the Internet
First download the data 
1、 build CNN A network model ;
class CNN(nn.Module): def __init__(self): super(CNN,self).__init__() ''' Generally speaking , The convolution network includes the following : 1. Convolution layer 2. neural network 3. Pooling layer ''' self.conv1=nn.Sequential( nn.Conv2d( #--> (1,28,28) in_channels=1, # The incoming image is several layers , Grey is 1 layer ,RGB It's three floors out_channels=16, # The output picture is several layers kernel_size=5, # The area points representing the scanning are 5*5 stride=1, # Just jump every few steps padding=2, # Border completion , Its calculation formula =(kernel_size-1)/2=(5-1)/2=2 ), # 2d Represents two-dimensional convolution --> (16,28,28) nn.ReLU(), # Nonlinear activation layer nn.MaxPool2d(kernel_size=2), # Set the scanning area here as 2*2, And remove the 2*2 Maximum of --> (16,14,14) ) self.conv2=nn.Sequential( nn.Conv2d( # --> (16,14,14) in_channels=16, # The input here is the output of the upper layer 16 layer out_channels=32, # Here we need to output it as 32 layer kernel_size=5, # The area points representing the scanning are 5*5 stride=1, # Just jump every few steps padding=2, # Border completion , Its calculation formula =(kernel_size-1)/2=(5-1)/2= ), # --> (32,14,14) nn.ReLU(), nn.MaxPool2d(kernel_size=2), # Set the scanning area here as 2*2, And remove the 2*2 Maximum of --> (32,7,7), Here is the 3D data ) self.out=nn.Linear(32*7*7,10) # Note that the data here is two-dimensional data def forward(self,x): x=self.conv1(x) x=self.conv2(x) #(batch,32,7,7) # Then proceed to expand and flatten , Convert 3D data to 2D data x=x.view(x.size(0),-1) #(batch ,32 * 7 * 7) output=self.out(x) return output2、 Design loss function , Select the optimization function ;
# Add optimization methods optimizer=torch.optim.Adam(cnn.parameters(),lr=LR)# Specifies that the loss function uses cross information entropy loss_fn=nn.CrossEntropyLoss()3、 Implement model training and testing .
step=0for epoch in range(EPOCH): # Load training data for step,data in enumerate(train_loader): x,y=data # Get the training data respectively x and y The value of b_x=Variable(x) b_y=Variable(y) output=cnn(b_x) # Call the model to predict loss=loss_fn(output,b_y)# Calculate the loss value optimizer.zero_grad() # Before each cycle , Clear the gradient to zero loss.backward() # Back propagation optimizer.step() # gradient descent # Every execution 50 Time , Output the current epoch、loss、accuracy if (step%50==0): # Calculate the accuracy of the model prediction test_output=cnn(test_x) y_pred=torch.max(test_output,1)[1].data.squeeze() accuracy=sum(y_pred==test_y).item()/test_y.size(0) print('now epoch : ', epoch, ' | loss : %.4f ' % loss.item(), ' | accuracy : ' , accuracy)
Code :
import torchimport torch.nn as nnfrom torch.autograd import Variableimport torch.utils.data as Dataimport torchvision#Hyper prametersEPOCH=1BATCH_SIZE=50LR=0.001DOWNLOAD_MNIST=Falsetrain_data = torchvision.datasets.MNIST( root='./mnist', train=True, transform=torchvision.transforms.ToTensor(), # Convert the downloaded file into pytorch cognitive tensor type , And change the value of the picture from (0-255) Normalize to (0-1) download=DOWNLOAD_MNIST)train_loader=Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)test_data=torchvision.datasets.MNIST( root='./mnist', train=False,)with torch.no_grad(): test_x=Variable(torch.unsqueeze(test_data.data, dim=1)).type(torch.FloatTensor)[:2000]/255 # Just take the first twothousand data , Almost enough , And then normalize it . test_y=test_data.targets[:2000]''' Start building CNN The Internet '''class CNN(nn.Module): def __init__(self): super(CNN,self).__init__() ''' Generally speaking , The convolution network includes the following : 1. Convolution layer 2. neural network 3. Pooling layer ''' self.conv1=nn.Sequential( nn.Conv2d( #--> (1,28,28) in_channels=1, # The incoming image is several layers , Grey is 1 layer ,RGB It's three floors out_channels=16, # The output picture is several layers kernel_size=5, # The area points representing the scanning are 5*5 stride=1, # Just jump every few steps padding=2, # Border completion , Its calculation formula =(kernel_size-1)/2=(5-1)/2=2 ), # 2d Represents two-dimensional convolution --> (16,28,28) nn.ReLU(), # Nonlinear activation layer nn.MaxPool2d(kernel_size=2), # Set the scanning area here as 2*2, And remove the 2*2 Maximum of --> (16,14,14) ) self.conv2=nn.Sequential( nn.Conv2d( # --> (16,14,14) in_channels=16, # The input here is the output of the upper layer 16 layer out_channels=32, # Here we need to output it as 32 layer kernel_size=5, # The area points representing the scanning are 5*5 stride=1, # Just jump every few steps padding=2, # Border completion , Its calculation formula =(kernel_size-1)/2=(5-1)/2= ), # --> (32,14,14) nn.ReLU(), nn.MaxPool2d(kernel_size=2), # Set the scanning area here as 2*2, And remove the 2*2 Maximum of --> (32,7,7), Here is the 3D data ) self.out=nn.Linear(32*7*7,10) # Note that the data here is two-dimensional data def forward(self,x): x=self.conv1(x) x=self.conv2(x) #(batch,32,7,7) # Then proceed to expand and flatten , Convert 3D data to 2D data x=x.view(x.size(0),-1) #(batch ,32 * 7 * 7) output=self.out(x) return output cnn=CNN()# print(cnn)# Add optimization methods optimizer=torch.optim.Adam(cnn.parameters(),lr=LR)# Specifies that the loss function uses cross information entropy loss_fn=nn.CrossEntropyLoss()''' Start training our model '''step=0for epoch in range(EPOCH): # Load training data for step,data in enumerate(train_loader): x,y=data # Get the training data respectively x and y The value of b_x=Variable(x) b_y=Variable(y) output=cnn(b_x) # Call the model to predict loss=loss_fn(output,b_y)# Calculate the loss value optimizer.zero_grad() # Before each cycle , Clear the gradient to zero loss.backward() # Back propagation optimizer.step() # gradient descent # Every execution 50 Time , Output the current epoch、loss、accuracy if (step%50==0): # Calculate the accuracy of the model prediction test_output=cnn(test_x) y_pred=torch.max(test_output,1)[1].data.squeeze() accuracy=sum(y_pred==test_y).item()/test_y.size(0) print('now epoch : ', epoch, ' | loss : %.4f ' % loss.item(), ' | accuracy : ' , accuracy)''' Print the results of ten test sets '''test_output=cnn(test_x[:10])y_pred=torch.max(test_output,1)[1].data.squeeze() # Select the position of the most possible value print(y_pred.tolist(),'predecton Result')print(test_y[:10].tolist(),'Real Result')边栏推荐
- Opengauss kernel: analysis of SQL parsing process
- ROS knowledge points - build an ROS development environment using vscode
- Fleet | background Discovery issue 3: Status Management
- C语言学习-19-全排列
- 化学制品制造业智慧供应商管理系统深度挖掘供应商管理领域,提升供应链协同
- 当下不做元宇宙,就像20年前没买房!
- 【MySQL】表连接为什么比子查询快
- VS2013 帮助文档中没有 win32/com
- 开源大咖说 - Linus 与 Jim 对话中国开源
- VC2010 编绎Qt5.6.3 提示 CVTRES : fatal error CVT1107:
猜你喜欢

字节跳动数据平台技术揭秘:基于ClickHouse的复杂查询实现与优化

C语言基础语法

QT create 5.0.3 configuring qt4.8.7

C#/VB.NET 将PDF转为Excel
一文教你快速生成MySQL数据库关系图

Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)

Fleet | "backstage exploration" issue 3: status management

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

go-zero 微服务实战系列(七、请求量这么高该如何优化)

Qt create 5.0.3 配置Qt4.8.7
随机推荐
Talking about open source - Linus and Jim talk about open source in China
What! One command to get the surveillance?
智慧园区数智化供应链管理平台如何优化流程管理,驱动园区发展提速增质?
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合起来形成单个可视化结果图
C#/VB. Net to convert PDF to excel
Fleet |「后台探秘」第 3 期:状态管理
The past and present life of distributed cap theorem
go-zero 微服务实战系列(七、请求量这么高该如何优化)
Innovation and upgrading of supply chain system driven management mode in petrochemical industry and strengthening internal management of enterprises
Visual Studio 2010 compilation qt5.6.3
Naacl 2022 | distillation of machinetranslation SOTA model
PostgreSQL实现按年、月、日、周、时、分、秒的分组统计
ROS knowledge points - ROS create workspace
sql语句 练习题
QT create 5.0.3 configuring qt4.8.7
Grand launch of qodana: your favorite CI code quality platform
Leetcode 48. Rotate image (yes, resolved)
不要使用短路逻辑编写 stl sorter 多条件比较
GBASE南大通用亮相第六届世界智能大会
How can the digital intelligent supply chain management platform of the smart Park optimize process management and drive the development of the park to increase speed and quality?