当前位置:网站首页>Neural network implementation of handwritten numeral classification matlab
Neural network implementation of handwritten numeral classification matlab
2022-07-27 17:18:00 【Bald head whining devil】
1 experimental result
It's a bit mushy , Make do with it , Automatic recognition of a handwritten numeral , The recognition accuracy is about 94%
2、 Data sets Minist
Download address :http://yann.lecun.com/exdb/mnist/
The four files are training set data 、 Training set label 、 Test set data 、 Test set label . The official introduction , Training set data has 60000 Zhang , The test set data has 10000 Zhang .( explain : After downloading, the computer will automatically decompress it into .ubyte.gz Format ), These four files are not in standard image format , So we need to build one .m File to read data . Every picture is 2828, So every time you read 2828 The size is a picture .
2.1 Read data into vectors
Vectorize the picture into 7841, Save all image vectors in the training set x_train in , The size is 78460000, Labels are stored in y_train in , The size is 1*60000( The same goes for test sets , Respectively x_test,y_test)
In order to find the most suitable network parameters later , Therefore, the training set read directly 、 The test set data is stored in the file , Then directly load the file and call . Read training set 、 The test set is the same function , In order to distinguish the read data, they are stored in train and test In file , Define a description character in the function , Indicates whether the file is a training file or a test file .
function build_dataset(image_file,label_file,describe)
% Read the training set image file
images = fopen(image_file,'r');
% Read the file description , The first sixteen bytes of data are descriptive information
a = fread(images,16,'uint8');
% Explain that the information is in 32 Bit integer expression
%MagicNum = ((a(1)*256+a(2))*256+a(3))*256+a(4);
% The first four bytes are idx Document format description
ImageNum = ((a(5)*256+a(6))*256+a(7))*256+a(8);% graphics
rows = ((a(9)*256+a(10))*256+a(11))*256+a(12);% Row number :28
cols = ((a(13)*256+a(14))*256+a(15))*256+a(16);% Number of columns :28
% Read the training set label file
labels = fopen(label_file,'r');
% Read the file description , The first eight bytes
a1 = fread(labels,8,'uint8');
%MagicNum1 = ((a1(1)*256+a1(2))*256+a1(3))*256+a1(4);%idx Document format description
ImageNum1 = ((a1(5)*256+a1(6))*256+a1(7))*256+a1(8);% Number of images ( Four bytes , use 32 Bit integer expression )
% Read data information
Label = zeros(1,ImageNum1);
data = [];
for i=1:ImageNum
im = im2double(fread(images,rows*cols,'uint8'));
% Read data one picture size at a time ,28*28, Vector
label= fread(labels,1,'uint8');% Corresponding label , This value indicates that the number in the picture is 0~9 Which number of
Label(i) = label;
im_arr = im';% Convert pictures into row storage
im_vec = reshape(im_arr,rows*cols,1); % Convert to matrix
data = [data,im_vec];
end
% According to the descriptor , Store the data
if describe == "train"
x_train = data;
y_train = Label;
save("e:/minist/train_data","x_train","y_train");
end
if describe == "test"
x_test = data;
y_test = Label;
save("e:/minist/test_data","x_test","y_test");
end
end
2.2 Read some data and convert it into pictures
Load test set data directly ,x_test Each column in represents a picture , Put it reshape by 28*28, Turn into uint8 Format , Because pictures are stored in columns , So transpose it and write it into the picture .
function build()
load("e:/minist/test_data");
[m,n] = size(x_test);
for k = 1:n
x = x_test(:,k);
x = reshape(x,28,28);
x = uint8(x);
imwrite(x',"e:/minist/image/"+k+".bmp");
end
end
2、 Artificial neural network
Neural network choose the simplest three-layer network : Input layer 、 Hidden layer 、 Output layer . Neural network involves two parts : Forward and backward propagation .
The main function
% Get the training set 、 Test set
load("e:/minist/train_data");
load("e:/minist/test_data");
%load("e:/minist/data.mat");
% Normalize the data to 0-1 Between
x_train = mapminmax(x_train,0,1);
x_test = mapminmax(x_test,0,1);
a = 0.03;
step = 100;% The number of iterations
hid = 27;% Number of hidden layers
[w,b,w_h,b_h] = train(x_train,y_train,step,a,hid);
acc = test(x_test,y_test,w,b,w_h,b_h);
fprintf(" Hidden layer is :"+hid+" The accuracy of the test set is : "+acc+"\n");
Network training
% Training neural network
function [w,b,w_hid,b_hid] = train(x_train,y_train,step,a,hid)
% The neural network consists of three layers , They are input layers input、 Hidden layer hidden、 Output layer output
in = 784;% Number of input layers
out = 10;% Number of neurons in the output layer (0-9)
w = randn(hid,in);% Input layer -- Hide layer weights
b = randn(hid,1);% Input layer -- Hidden layer offset
w_hid = randn(out,hid);% Hidden layer -- Enter the layer weight
b_hid = randn(out,1);% Hidden layer -- Output layer offset
% Definition 10 Correct output of digits
Y = [1,0,0,0,0,0,0,0,0,0;
0,1,0,0,0,0,0,0,0,0;
0,0,1,0,0,0,0,0,0,0;
0,0,0,1,0,0,0,0,0,0;
0,0,0,0,1,0,0,0,0,0;
0,0,0,0,0,1,0,0,0,0;
0,0,0,0,0,0,1,0,0,0;
0,0,0,0,0,0,0,1,0,0;
0,0,0,0,0,0,0,0,1,0;
0,0,0,0,0,0,0,0,0,1;];
for i=1:step
% Out of order
r = randperm(60000);
for j = 1:60000
% Take the first place j A picture
x = x_train(:,j);
y = y_train(1,j);
% Forward propagation calculates the output of each layer
out_hid = Layerout(w,b,x);% Hidden layer output
out_y = Layerout(w_hid,b_hid,out_hid);% The output layer outputs
% Back propagation
out_update = (Y(:,y+1)-out_y).*out_y.*(1-out_y);% Output layer -- Hidden layer
hid_update = ((w_hid')*out_update).*out_hid.*(1-out_hid);% Hidden layer -- Input layer
% Update parameters
w_hid = w_hid + a*(out_update*(out_hid'));
b_hid = b_hid + a*out_update;
w = w + a*(hid_update*(x'));
b = b + a*hid_update;
end
end
save("e:/minist/data.mat","w","b","w_hid","b_hid");
end
Neuron activation function output
% Each layer activates the function output
function [y] = Layerout(w,b,x)
y = w*x+b;
n = length(y);
for i =1:n
y(i) = 1.0/(1+exp(-y(i)));
end
end
forecast
function [index] = predict(path)
%UNTITLED3 A summary of this function is shown here
% The probability of storing test pictures belonging to each number
load("e:/minist/data.mat");
im = im2double(imread(path));% obtain 28*28j matrix ss
im = im';
x = reshape(im,784,1);
hid = Layerout(w,b,x);
res = Layerout(w_hid,b_hid,hid);
[t,index] = max(res);
index = index-1;
end
3、GUI Design
Open the interface 
Press down start key , Start handwritten digit recognition . Define an editable text box img, Use it to display the path of each picture , At the same time, define a text box img_index Indicates the number of pictures ( Easy to read pictures ), And set it to invisible . Three buttons ,start、stop、continue,start Key to initialize , Set up img_index=1, And display the first picture in the coordinate axis . Define a timer Timer , Every second img_index Add 1, That is, read the next picture every second , For identification , Display the recognition results in result The text box .
If this article helps you , Thank you for your support !

边栏推荐
- 科目三: 直线行驶
- Getting started with unity
- Cryptography series: certificate format representation of PKI X.509
- Niuke topic - the minimum number of K
- Hegong sky team vision training Day8 - vision, target recognition
- ES6数组新增属性
- Kubernetes第七篇:使用kubernetes部署prometheus+grafana监控系统(Kubernetes工作实践类)
- Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
- Storage of data in C language
- SAP UI5 FileUploader 的本地文件上传技术实现分享
猜你喜欢

Natural sorting: comparable interface, customized sorting: the difference between comparator interface

SAP UI5 FileUploader 的本地文件上传技术实现分享

动作捕捉系统用于柔性机械臂的末端定位控制

Flex弹性盒布局

App Crash收集和分析

Sharing of local file upload technology of SAP ui5 fileuploader

Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)

Program environment and preprocessing of C language

三表联查3
![[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial](/img/7d/c372dba73531f4574ca3d379668b13.jpg)
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
随机推荐
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程
下棋机器人折断7岁男孩手指,网友:违反了机器人第一定律
Flex flex flex box layout 2
高精度定时器
三表联查3
Dynamic memory allocation in C language
Mobile page layout
Structure and bit segment of C language
信通院陈屹力:降本增效是云原生应用最大价值
(2)融合cbam的two-stream项目搭建----数据准备
密集光流提取dense_flow理解
MySQL: 函数
Reference of meta data placeholder
mysql视图及存储过程
从零开始Blazor Server(1)--项目搭建
深度学习能颠覆视频编解码吗?国家技术发明奖一等奖得主在小红书给你唠
这种精度高,消耗资源少的大模型稀疏训练方法被阿里云科学家找到了!已被收录到IJCAI
SVM+Surf+K-means花朵分类(Matlab)
Program environment and preprocessing of C language